Skip to content

Instantly share code, notes, and snippets.

@jcsky
Created March 2, 2016 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcsky/09641ed6b9075c84d298 to your computer and use it in GitHub Desktop.
Save jcsky/09641ed6b9075c84d298 to your computer and use it in GitHub Desktop.
Qunit TDD practice
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TDD Practice</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.20.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="http://code.jquery.com/qunit/qunit-1.20.0.js"></script>
<script src="main.js"></script>
<script src="tests.js"></script>
</body>
</html>
var App = {
max: function(){
var max = -Infinity;
for(var i = 0; i < arguments.length; i++){
if(arguments[i] > max){
max = arguments[i];
}
}
return max;
},
isOdd: function (number) {
if (typeof number == 'string') {
throw Error('can not be string');
}
if (typeof number !== 'number') {
throw Error('The given argument is not a number');
}
return number % 2 !== 0;
},
stringContainsDigit: function (string) {
if (typeof string !== 'string') {
throw Error('The given argument is not a string');
}
var regexNum = /\d/g; // Global check for numbers
return regexNum.test(string);
}
};
QUnit.test("hello test", function( assert ) {
assert.ok( 1 == "1", "Passed!" );
});
QUnit.test('max', function (assert) {
expect(4);
assert.strictEqual(App.max(), -Infinity, 'No parameters');
assert.strictEqual(App.max(3, 1, 2), 3, 'All postive numbers');
assert.strictEqual(App.max(-13, 1, 2), 2, 'postive and negative numbers');
assert.strictEqual(App.max(-13, -22, -32), -13, 'All negative numbers');
});
QUnit.test('isOdd', function (assert) {
expect(7);
assert.strictEqual(App.isOdd(5), true, '5 is odd');
assert.strictEqual(App.isOdd(2), false, '2 is not odd');
assert.strictEqual(App.isOdd(0), false, '0 is not odd');
assert.throws(function(){
App.isOdd(null);
}, new Error('The given argument is not a number'), 'Passing null raises an Error');
assert.throws(function(){
App.isOdd([]);
}, new Error('The given argument is not a number'), 'Passing [] raises an Error');
assert.throws(function(){
App.isOdd();
}, new Error('The given argument is not a number'), 'No parameters');
assert.throws(function(){
App.isOdd("1234");
}, new Error('can not be string'), 'Passing a string');
});
QUnit.test('stringContainsDigit', function (assert) {
assert.strictEqual(App.stringContainsDigit("5ab67"), true, '5ab67');
assert.strictEqual(App.stringContainsDigit("abc"), false, 'abc');
assert.throws(function(){
App.stringContainsDigit(null);
}, new Error('The given argument is not a string'), 'Passing null raises an Error');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment