|
1 | | -/* |
2 | | - * Instead of hard-coding a bunch of separate test cases and their solutions, |
3 | | - * this code takes several argument lists, and compares the chain call answer |
4 | | - * you made for "sum()" to a more traditional way of adding the contents of an |
5 | | - * array. |
6 | | - */ |
7 | | - |
8 | 1 | describe("sum", function(){ |
| 2 | + |
| 3 | + it("sum(1)(2) == 3", function(){ |
| 4 | + assert.equal(3, sum(1)(2)); |
| 5 | + }); |
9 | 6 |
|
10 | | - let testArgumentLists = [ |
11 | | - [1, 2], |
12 | | - [5, -1, 2], |
13 | | - [6, -1, -2, -3], |
14 | | - [0, 1, 2, 3, 4, 5], |
15 | | - ]; |
| 7 | + it("sum(5)(-1)(2) == 6", function(){ |
| 8 | + assert.equal(6, sum(5)(-1)(2)); |
| 9 | + }); |
| 10 | + |
| 11 | + it("sum(6)(-1)(-2)(-3) == 0", function(){ |
| 12 | + assert.equal(0, sum(6)(-1)(-2)(-3)); |
| 13 | + }); |
16 | 14 |
|
17 | | - for (let argumentList of testArgumentLists){ |
18 | | - it(makeTestCaseName(argumentList), function(){ |
19 | | - assert.equal(traditionalSum(argumentList), chainCallSum(argumentList)); |
20 | | - }); |
21 | | - } |
| 15 | + it("sum(0)(1)(2)(3)(4)(5) == 15", function(){ |
| 16 | + assert.equal(15, sum(0)(1)(2)(3)(4)(5)); |
| 17 | + }); |
22 | 18 | }); |
23 | 19 |
|
24 | | -function traditionalSum(arr){ |
25 | | - return arr.reduce( |
26 | | - function(accumulator, item){ |
27 | | - return accumulator + item; |
28 | | - }, 0); |
29 | | -} |
30 | | - |
31 | | -function makeTestCaseName(arr){ |
32 | | - return `sum${makeChainCallString(arr)} == ${traditionalSum(arr)}`; |
33 | | -} |
34 | | - |
35 | | -/* Takes the elements of an array, and puts them in a string where each element |
36 | | - * is enclosed in parentheses. Example: |
37 | | - * |
38 | | - * (["a", "b", "c"]) => "(a)(b)(c)" |
39 | | - * |
40 | | - * Useful for making pretty test case names. |
41 | | - */ |
42 | | -function makeChainCallString(arr){ |
43 | | - return arr.reduce( |
44 | | - function(accumulator, item){ |
45 | | - return `${accumulator}(${item})`; |
46 | | - }, ""); |
47 | | -} |
48 | | - |
49 | | -function chainCallSum(arr){ |
50 | | - return arr.reduce( |
51 | | - function(accumulator, item){ |
52 | | - return accumulator(item); |
53 | | - }, sum); |
54 | | -} |
0 commit comments