Made the test cases easier to read.
This commit is contained in:
parent
8e861596d7
commit
267e825560
1 changed files with 14 additions and 49 deletions
|
@ -1,54 +1,19 @@
|
|||
/*
|
||||
* Instead of hard-coding a bunch of separate test cases and their solutions,
|
||||
* this code takes several argument lists, and compares the chain call answer
|
||||
* you made for "sum()" to a more traditional way of adding the contents of an
|
||||
* array.
|
||||
*/
|
||||
|
||||
describe("sum", function(){
|
||||
|
||||
it("sum(1)(2) == 3", function(){
|
||||
assert.equal(3, sum(1)(2));
|
||||
});
|
||||
|
||||
let testArgumentLists = [
|
||||
[1, 2],
|
||||
[5, -1, 2],
|
||||
[6, -1, -2, -3],
|
||||
[0, 1, 2, 3, 4, 5],
|
||||
];
|
||||
it("sum(5)(-1)(2) == 6", function(){
|
||||
assert.equal(6, sum(5)(-1)(2));
|
||||
});
|
||||
|
||||
it("sum(6)(-1)(-2)(-3) == 0", function(){
|
||||
assert.equal(0, sum(6)(-1)(-2)(-3));
|
||||
});
|
||||
|
||||
for (let argumentList of testArgumentLists){
|
||||
it(makeTestCaseName(argumentList), function(){
|
||||
assert.equal(traditionalSum(argumentList), chainCallSum(argumentList));
|
||||
});
|
||||
}
|
||||
it("sum(0)(1)(2)(3)(4)(5) == 15", function(){
|
||||
assert.equal(15, sum(0)(1)(2)(3)(4)(5));
|
||||
});
|
||||
});
|
||||
|
||||
function traditionalSum(arr){
|
||||
return arr.reduce(
|
||||
function(accumulator, item){
|
||||
return accumulator + item;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
function makeTestCaseName(arr){
|
||||
return `sum${makeChainCallString(arr)} == ${traditionalSum(arr)}`;
|
||||
}
|
||||
|
||||
/* Takes the elements of an array, and puts them in a string where each element
|
||||
* is enclosed in parentheses. Example:
|
||||
*
|
||||
* (["a", "b", "c"]) => "(a)(b)(c)"
|
||||
*
|
||||
* Useful for making pretty test case names.
|
||||
*/
|
||||
function makeChainCallString(arr){
|
||||
return arr.reduce(
|
||||
function(accumulator, item){
|
||||
return `${accumulator}(${item})`;
|
||||
}, "");
|
||||
}
|
||||
|
||||
function chainCallSum(arr){
|
||||
return arr.reduce(
|
||||
function(accumulator, item){
|
||||
return accumulator(item);
|
||||
}, sum);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue