Repository navigation
Expand file tree
/
Copy pathmath_test.go
More file actions
217 lines (195 loc) · 5.34 KB
/
Copy pathmath_test.go
File metadata and controls
217 lines (195 loc) · 5.34 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package plush_test
import (
"fmt"
"math"
"testing"
"github.com/gobuffalo/plush/v5"
"github.com/stretchr/testify/require"
)
func Test_Render_Int_Math_Division_By_Zero(t *testing.T) {
r := require.New(t)
input := `<%= 10 / 0 %>`
s, err := plush.Render(input, plush.NewContext())
r.Error(err)
r.Empty(s)
r.Contains(err.Error(), "division by zero 10 / 0")
}
func Test_Render_Int_Float_Division_By_Zero(t *testing.T) {
r := require.New(t)
input := `<%= 10.5 / 0.0 %>`
s, err := plush.Render(input, plush.NewContext())
r.Error(err)
r.Empty(s)
r.Contains(err.Error(), "division by zero 10.5 / 0")
}
func Test_Render_Int_Math(t *testing.T) {
r := require.New(t)
tests := []struct {
a int
b int
op string
res string
}{
{1, 3, "+", "4"},
{3, 1, "-", "2"},
{10, 2, "/", "5"},
{10, 2, "*", "20"},
{10, 2, ">", "true"},
{10, 2, ">=", "true"},
{10, 10, ">=", "true"},
{2, 2, "<=", "true"},
{10, 2, "<", "false"},
{10, 2, "<=", "false"},
{2, 2, "==", "true"},
{1, 2, "!=", "true"},
}
for _, tt := range tests {
input := fmt.Sprintf("<%%= %d %s %d %%>", tt.a, tt.op, tt.b)
s, err := plush.Render(input, plush.NewContext())
r.NoError(err)
r.Equal(tt.res, s)
}
}
func Test_Render_Float_Math(t *testing.T) {
r := require.New(t)
tests := []struct {
a float64
b float64
op string
res string
}{
{1, 3, "+", "4"},
{3, 1, "-", "2"},
{10, 2, "/", "5"},
{10, 2, "*", "20"},
{10, 2, ">", "true"},
{10, 2, ">=", "true"},
{10, 10, ">=", "true"},
{2, 2, "<=", "true"},
{10, 2, "<", "false"},
{10, 2, "<=", "false"},
{2, 2, "==", "true"},
{1, 2, "!=", "true"},
}
for _, tt := range tests {
input := fmt.Sprintf("<%%= %f %s %f %%>", tt.a, tt.op, tt.b)
s, err := plush.Render(input, plush.NewContext())
r.NoError(err)
r.Equal(tt.res, s)
}
}
func Test_Render_String_Math(t *testing.T) {
r := require.New(t)
tests := []struct {
a string
b string
op string
res string
}{
{"a", "b", "+", "ab"},
{"a", "b", "!=", "true"},
{"a", "a", "==", "true"},
{"a", "b", "==", "false"},
{"a", "b", ">", "false"},
{"a", "b", ">=", "false"},
{"a", "b", "<=", "true"},
}
for _, tt := range tests {
input := fmt.Sprintf("<%%= %q %s %q %%>", tt.a, tt.op, tt.b)
s, err := plush.Render(input, plush.NewContext())
r.NoError(err)
r.Equal(tt.res, s)
}
}
func Test_Render_Operator_Undefined_Var(t *testing.T) {
tests := []struct {
operator string
result interface{}
errorExpected bool
}{
{"+", "", true},
{"-", "", true},
{"/", "", true},
{"*", "", true},
{">", "", true},
{">=", "", true},
{"<=", "", true},
{"<", "", true},
{"==", "false", false},
{"!=", "true", false},
}
for _, tc := range tests {
t.Run(tc.operator, func(t *testing.T) {
r := require.New(t)
input := fmt.Sprintf("<%%= undefined %s 3 %%>", tc.operator)
s, err := plush.Render(input, plush.NewContext())
if tc.errorExpected {
r.Error(err, "undefined %s 3 --> '%v'", tc.operator, tc.result)
} else {
r.NoError(err, "undefined %s 3 --> '%v'", tc.operator, tc.result)
}
r.Equal(tc.result, s, "undefined %s 3", tc.operator)
input = fmt.Sprintf("<%%= 3 %s unknown %%>", tc.operator)
s, err = plush.Render(input, plush.NewContext())
if tc.errorExpected {
r.Error(err, "3 %s undefined --> '%v'", tc.operator, tc.result)
} else {
r.NoError(err, "3 %s undefined --> '%v'", tc.operator, tc.result)
}
r.Equal(tc.result, s, "undefined %s 3", tc.operator)
})
}
}
func Test_Render_String_Concat_Multiple(t *testing.T) {
r := require.New(t)
input := `<%= "a" + "b" + "c" %>`
s, err := plush.Render(input, plush.NewContext())
r.NoError(err)
r.Equal("abc", s)
}
func Test_Render_String_Int_Concat(t *testing.T) {
r := require.New(t)
input := `<%= "a" + 1 %>`
s, err := plush.Render(input, plush.NewContext())
r.NoError(err)
r.Equal("a1", s)
}
func Test_Render_Bool_Concat(t *testing.T) {
r := require.New(t)
input := `<%= true + 1 %>`
s, err := plush.Render(input, plush.NewContext())
r.Equal("true", s)
r.NoError(err)
}
func Test_Render_Safe_Mixed_Numeric_Comparisons(t *testing.T) {
type stats struct {
Count uint64
}
type robot struct {
Count uint32
Stats stats
}
ctx := plush.NewContextWith(map[string]interface{}{
"i32": int32(0),
"i32v": int32(3),
"neg": int32(-1),
"u32": uint32(0),
"u32v": uint32(3),
"u64": uint64(0),
"u64one": uint64(1),
"u64max": uint64(math.MaxUint64),
"f32": float32(3.5),
"f32i": float32(3),
"f64": float64(3.5),
"f64i": float64(3),
"counts": map[string]uint32{"active": 0},
"values": []uint32{0},
"robot": robot{Count: 0, Stats: stats{Count: 0}},
"robots": []robot{{Count: 1, Stats: stats{Count: 0}}},
"counter": func() uint32 { return 0 },
})
input := `<%= i32 == 0 %>|<%= u32 == 0 %>|<%= u64 == 0 %>|<%= u64one > 0 %>|<%= u64max > 0 %>|<%= neg < 0 %>|<%= neg < u64 %>|<%= neg == u64 %>|<%= u32v == 3 %>|<%= f32 == 3.5 %>|<%= f64 > 3 %>|<%= f32i == 3 %>|<%= f64i == i32v %>|<%= u32v == 3.0 %>|<%= u64one == 1.0 %>|<%= robot.Count == 0 %>|<%= robots[0].Stats.Count == 0 %>|<%= counts["active"] == 0 %>|<%= values[0] == 0 %>|<%= counter() == 0 %>|<%= robot.Count + 1 %>`
s, err := plush.Render(input, ctx)
require.NoError(t, err)
require.Equal(t, "true|true|true|true|true|true|true|false|true|true|true|true|true|true|true|true|true|true|true|true|1", s)
}