-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum.test.js
105 lines (98 loc) · 2.84 KB
/
sum.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
test('sum without parameters has to be 0',()=>{
function sum(firstParameter){
let total=0
function validator(arg)
{
return arg(total)
}
return validator(firstParameter)
}
expect(sum(result=>result)).toBe(0)
})
test('sum with just 1 as parameter has to be 1',()=>{
function sum(firstParameter){
let total=0
function validator(arg)
{
if(typeof arg==="function"){
return arg(total)
}
else{
total+=arg
return validator
}
}
return validator(firstParameter)
}
expect(sum(1)(result=>result)).toBe(1)
})
test('sum 1+2, must be 3',()=>{
function sum(firstParameter){
let total=0
function validator(arg)
{
if(typeof arg==="function"){
return arg(total)
}
else{
total+=arg
return validator
}
}
return validator(firstParameter)
}
expect(sum(1)(2)(result=>result)).toBe(3)
})
test('sum 1+2+4, must be 7',()=>{
function sum(firstParameter){
let total=0
function validator(arg)
{
if(typeof arg==="function"){
return arg(total)
}
else{
total+=arg
return validator
}
}
return validator(firstParameter)
}
expect(sum(1)(2)(4)(result=>result)).toBe(7)
})
test('infinite sum in this case 1+2+3+4+5+6, must be 21',()=>{
function sum(firstParameter){
let total=0
function validator(arg)
{
if(typeof arg==="function"){
return arg(total)
}
else{
total+=arg
return validator
}
}
return validator(firstParameter)
}
expect(sum(1)(2)(3)(4)(5)(6)(result=>result)).toBe(21)
})
//Just in case we need to take care of more validations, like numbers or nulls.
test('arguments that are no numbers throw error',()=>{
function sum(initialParameter) {
let total = 0;
function validator (arg) {
if (typeof arg === "function") {
return arg(total);
} else if (typeof arg === 'number') {
total += arg;
return validator;
} else {
throw new TypeError('Invalid argument. Must be a number or function');
}
}
return validator(initialParameter);
}
expect(()=>sum(1)(2)('Team international')(result=>result)).toThrow(TypeError)
expect(()=>sum(1)(2)()(result=>result)).toThrow(TypeError)
})