-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda_test.go
105 lines (101 loc) · 1.96 KB
/
lambda_test.go
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
package calc_test
import (
"testing"
"github.com/antklim/go-calc"
"github.com/stretchr/testify/assert"
)
func TestDo(t *testing.T) {
testCases := []struct {
desc string
lambda calc.Lambda
args []float64
expected float64
}{
{
desc: "applies Add to arguments",
lambda: calc.Add,
args: []float64{1, 2},
expected: 3,
},
{
desc: "applies Sub to arguments",
lambda: calc.Sub,
args: []float64{1, 2},
expected: -1,
},
{
desc: "applies Mul to arguments",
lambda: calc.Mul,
args: []float64{1, 2},
expected: 2,
},
{
desc: "applies Div to arguments",
lambda: calc.Div,
args: []float64{1, 2},
expected: 0.5,
},
{
desc: "applies Sqrt to arguments",
lambda: calc.Sqrt,
args: []float64{1},
expected: 1,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
actual := calc.Do(tC.lambda, tC.args)
assert.Equal(t, tC.expected, actual)
})
}
}
func TestLambdas(t *testing.T) {
testCases := []struct {
desc string
lambda calc.Lambda
a float64
b float64
expected float64
}{
{
desc: "sums arguments when handler is Add",
lambda: calc.Add,
a: 1,
b: 2,
expected: 3,
},
{
desc: "subtracts arguments when handler is Sub",
lambda: calc.Sub,
a: 1,
b: 2,
expected: -1,
},
{
desc: "multiplies arguments when handler is Mul",
lambda: calc.Mul,
a: 1,
b: 2,
expected: 2,
},
{
desc: "divides arguments when handler is Div",
lambda: calc.Div,
a: 1,
b: 2,
expected: 0.5,
},
{
desc: "returns square root of argument when handler is Sqrt",
lambda: calc.Sqrt,
a: 1,
expected: 1,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
actual := tC.lambda(tC.a)(tC.b)
assert.Equal(t, tC.expected, actual)
})
}
}