Repository navigation
Expand file tree
/
Copy pathstdlibmodule_test.go
More file actions
151 lines (136 loc) · 3.39 KB
/
Copy pathstdlibmodule_test.go
File metadata and controls
151 lines (136 loc) · 3.39 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package gel_test
import (
"testing"
"github.com/Stromberg/gel"
"github.com/stretchr/testify/assert"
)
func TestStdLibModuleStrings(t *testing.T) {
test := func(expr string, expected interface{}) {
g, err := gel.New(expr)
assert.NoError(t, err)
assert.NotNil(t, g)
s, err := g.Eval(gel.NewEnv())
assert.NoError(t, err)
assert.Equal(t, expected, s)
}
test("(strings.Title \"world\")", "World")
test("(strings.ToUpper \"world\")", "WORLD")
test("(strings.ToLower \"worLd\")", "world")
test("(strings.TrimSpace \" worLd \")", "worLd")
test("(printf \"Grr\\n\")", nil)
test("(printf \"Grr: %v\\n\" 3.14)", nil)
test("(sprintf \"Grr\\n\")", "Grr\n")
test("(sprintf \"Grr: %v\\n\" 3.14)", "Grr: 3.14\n")
test("(str 3.14)", "3.14")
}
func TestStdLibModuleMath(t *testing.T) {
test := func(expr string, expected interface{}) {
g, err := gel.New(expr)
assert.NoError(t, err)
assert.NotNil(t, g)
s, err := g.Eval(gel.NewEnv())
assert.NoError(t, err)
assert.Equal(t, expected, s)
}
test("((cap 3 4) 3.5)", 3.5)
test("((cap 3 4) 10.5)", float64(4))
test("((cap 3 4) 1)", int64(3))
test("(math.Pow 3 2)", float64(9))
test("(math.Pow 2 3)", float64(8))
test("(math.Sqrt 4)", float64(2))
test("(math.Log 10)", float64(2.302585092994046))
test("(math.Exp 10)", float64(22026.465794806718))
test("((pow 3) 2)", float64(8))
test("((pow 2) 3)", float64(9))
test("(nan? 3)", false)
test("(nan? nan)", true)
test("(pos-inf? 3)", false)
test("((with-default 0.0) 3.0)", 3.0)
test("((with-default 0.0) nan)", 0.0)
test("((positive 1.0) 3.0)", 3.0)
test("((positive 1.0) -1.0)", 1.0)
test("((positive 1.0) nan)", 1.0)
test("((in-range? 0 4) 3)", true)
test("((in-range? 0 4) 5)", false)
}
func TestStdLibModuleCombinations(t *testing.T) {
test := func(expr string, expected interface{}) {
g, err := gel.New(expr)
assert.NoError(t, err)
assert.NotNil(t, g)
s, err := g.Eval(gel.NewEnv())
assert.NoError(t, err)
assert.Equal(t, expected, s)
}
test(
"(combinations (list))",
[]interface{}{},
)
test(
"(combinations (list 1.0))",
[]interface{}{
[]interface{}{1.0},
},
)
test(
"(combinations (list 1.0 2.0))",
[]interface{}{
[]interface{}{1.0},
[]interface{}{2.0},
},
)
test(
"(combinations (list 1.0 2.0) (list 3.0))",
[]interface{}{
[]interface{}{1.0, 3.0},
[]interface{}{2.0, 3.0},
},
)
test(
"(combinations (list 1.0 2.0) (list 3.0 4.0) (list 5.0 6.0))",
[]interface{}{
[]interface{}{1.0, 3.0, 5.0},
[]interface{}{1.0, 3.0, 6.0},
[]interface{}{1.0, 4.0, 5.0},
[]interface{}{1.0, 4.0, 6.0},
[]interface{}{2.0, 3.0, 5.0},
[]interface{}{2.0, 3.0, 6.0},
[]interface{}{2.0, 4.0, 5.0},
[]interface{}{2.0, 4.0, 6.0},
},
)
}
func TestStdLibModuleTranspose(t *testing.T) {
test := func(expr string, expected interface{}) {
g, err := gel.New(expr)
assert.NoError(t, err)
assert.NotNil(t, g)
s, err := g.Eval(gel.NewEnv())
assert.NoError(t, err)
assert.Equal(t, expected, s)
}
test(
"(transpose (list))",
[]interface{}{},
)
test(
"(transpose (list (list 1.0)))",
[]interface{}{
[]interface{}{1.0},
},
)
test(
"(transpose (list (list 1.0 2.0) (list 3.0 4.0)))",
[]interface{}{
[]interface{}{1.0, 3.0},
[]interface{}{2.0, 4.0},
},
)
test(
"(transpose (list (list 1.0 2.0) (list 3.0 4.0) (list 5.0 6.0)))",
[]interface{}{
[]interface{}{1.0, 3.0, 5.0},
[]interface{}{2.0, 4.0, 6.0},
},
)
}