Created
January 5, 2012 12:35
-
-
Save joshnesbitt/1565077 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #suite is a helper method to more concisely list | |
// #describe groups. Example usage: | |
// | |
// suite("Examples", { | |
// | |
// "before" : function(){ | |
// // ... setup | |
// }, | |
// | |
// "should one" : function(){ | |
// // ... top level spec | |
// }, | |
// | |
// "should two" : function(){ | |
// // ... top level spec | |
// } | |
// | |
// "describing Nested" : { | |
// | |
// "should one" : function(){ | |
// // ... nested spec | |
// }, | |
// | |
// "should two" : function(){ | |
// // ... nested spec | |
// } | |
// | |
// } | |
// | |
// }); | |
var SuiteWalker = function(desc, object){ | |
var description = desc, | |
blocks = object; | |
function run(){ | |
describe(description, function(){ | |
var key; | |
for(key in blocks){ | |
var parts = key.split(" "), | |
keyword = parts[0], | |
block = blocks[key]; | |
switch(keyword){ | |
case 'scenario': | |
case 'describing': | |
var desc = parts.slice(1, parts.length).join(" "); | |
var walker = new SuiteWalker(desc, block); | |
walker.run(); | |
break; | |
case 'before': | |
beforeEach(block); | |
break; | |
case 'after': | |
afterEach(block); | |
break; | |
case 'it': | |
case 'should': | |
it(key, block); | |
break; | |
case 'pending': | |
xit(key, block); | |
break; | |
default: | |
throw "Can't determine context to call the current keyword '" + keyword + "' in." | |
break; | |
} | |
} | |
}); | |
}; | |
return { | |
run : run | |
} | |
}; | |
var suite = function(desc, blocks){ | |
var walker = new SuiteWalker(desc, blocks); | |
walker.run(); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Calculator = function(){ | |
function add(one, two){ | |
return one + two; | |
}; | |
function subtract(one, two){ | |
return one - two; | |
}; | |
return { | |
add : add, | |
subtract : subtract | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("Calculator", function(){ | |
var result, | |
calculator = new Calculator(); | |
describe("add", function(){ | |
beforeEach(function(){ | |
result = 0; | |
}); | |
it("should perform addition", function(){ | |
result = calculator.add(2, 2); | |
expect(result).toEqual(4); | |
}) | |
}); | |
describe("subtract", function(){ | |
beforeEach(function(){ | |
result = 0; | |
}); | |
it("should perform subtraction", function(){ | |
result = calculator.subtract(4, 2); | |
expect(result).toEqual(2); | |
}) | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suite("Calculator", { | |
before : function(){ | |
this.result = null; | |
this.calculator = new Calculator(); | |
}, | |
"describing #add" : { | |
before : function(){ | |
this.result = 0; | |
}, | |
"should perform addition" : function(){ | |
this.result = this.calculator.add(2, 2); | |
expect(this.result).toEqual(4); | |
} | |
}, | |
"describing #subtract" : { | |
before : function(){ | |
this.result = 0; | |
}, | |
"should perform subtraction" : function(){ | |
this.result = this.calculator.subtract(4, 2); | |
expect(this.result).toEqual(2); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work mate, nicer to read