TDD kata
HTML
<script src="http://pivotal.github.com/jasmine/lib/jasmine.js"></script>
<script src="http://pivotal.github.com/jasmine/lib/jasmine-html.js"></script>
<link rel="stylesheet" href="http://pivotal.github.com/jasmine/lib/jasmine.css">
<script>
function add(x){
return 0;
}
</script>
<div id="HTMLReporter" class="jasmine_reporter version"></div>
CSS
/* Credit to: http://osherove.com/tdd-kata-1/
Part of TDD Bootsrap - https://github.com/aheld/code-kata
The Rules:
* Try not to code ahead.
* Do one task at a time. The trick is to learn to work incrementally.
* Make sure you only test for correct inputs. there is no need to test for invalid inputs for this kata
Step 1: Create a simple String calculator with a method int Add(string numbers)
The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example “” or “1” or “1,2”
Start with the simplest test case of an empty string and move to 1 and two numbers
* Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
*Remember to refactor after each passing test
Step 2: Allow the Add method to handle an unknown amount of numbers
Step 3: Allow the Add method to handle new lines between numbers (instead of commas).
the following input is ok: “1\n2,3” (will equal 6)
the following input is NOT ok: “1,\n” (do not need to validate input - just clarifying case)
*/
JavaScript
// Test cases for kata
describe('add', function() {
it('an empty string should return 0',function() {
expect(add('')).toEqual(0);
});
});
// Sample Jasmine Matchers
describe('built-in matchers', function() {
describe('toBeTruthy', function() {
it('passes if subject is true', function() {
expect(true).toBeTruthy();
expect(false).not.toBeTruthy();
});
});
describe('toBeFalsy', function() {
it('passes if subject is false', function() {
expect(false).toBeFalsy();
expect(true).not.toBeFalsy();
});
});
describe('toBeDefined', function() {
it('passes if subject is not undefined', function() {
expect({}).toBeDefined();
expect(undefined).not.toBeDefined();
});
});
describe('toBeNull', function() {
it('passes if subject is null', function() {
expect(null).toBeNull();
expect(undefined).not.toBeNull();
expect({}).not.toBeNull();
});
});
describe('toEqual', function() {
it('passes if subject and expectation are equivalent', function() {
expect('Hello World!').toEqual('Hello World!');
expect('Hello World!').not.toEqual('Goodbye!');
expect('Hello World!').toNotEqual('Hi!');
expect([1, 2, 3]).toEqual([1, 2, 3]);
expect(1).toEqual(1);
expect({
foo: 1
}).toEqual({
foo: 1
});
});
});
describe('toBeCloseTo', function() {
it('checks that the expected item is equal to the actual item up to a given level of decimal precision ', function() {
expect(1.223).toBeCloseTo(1.22);
expect(1.233).not.toBeCloseTo(1.22);
expect(1.23326).toBeCloseTo(1.23324, 3);
});
});
describe('toContain', function() {
it('passes if the expected item is...