generated from wesen/wesen-go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
if_test.go
184 lines (180 loc) · 3.75 KB
/
if_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
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
package emrichen
import (
"testing"
)
func TestEmrichenIfAndFilterTags(t *testing.T) {
tests := []testCase{
{
name: "Basic boolean condition - true",
inputYAML: `!If
test: true
then: 'Yes'
else: 'No'`,
expected: "'Yes'",
},
{
name: "Basic boolean condition - false",
inputYAML: `!If
test: false
then: 'Yes'
else: 'No'`,
expected: "'No'",
},
{
name: "Nested !If statements",
inputYAML: `!If
test: true
then: !If {test: false, then: "Inner Yes", else: "Inner No"}
else: 'Outer No'`,
expected: "'Inner No'",
},
{
name: "Variable substitution in conditions",
inputYAML: `!If
test: !Var condition
then: 'Variable Yes'
else: 'Variable No'`,
expected: "'Variable Yes'",
initVars: map[string]interface{}{
"condition": true,
},
},
{
name: "If tag true condition",
inputYAML: `!If
test: !Op
a: 10
op: ">"
b: 5
then: "True Condition"
else: "False Condition"`,
expected: "\"True Condition\"",
},
{
name: "If tag false condition",
inputYAML: `!If
test: !Op
a: 3
op: ">"
b: 5
then: "True Condition"
else: "False Condition"`,
expected: "\"False Condition\"",
},
{
name: "Type-checking with string in condition",
inputYAML: `!If
test: 'true' # String 'true', not boolean
then: 'String Yes'
else: 'String No'`,
expected: "'String Yes'",
},
{
name: "Type-checking with number in condition",
inputYAML: `!If
test: 1 # Number 1
then: 'Number Yes'
else: 'Number No'`,
expected: "'Number Yes'",
},
// Test 9: Conditional Evaluation with `null` and Empty Values
{
name: "Evaluation with null condition",
inputYAML: `!If
test: null
then: 'Null Yes'
else: 'Null No'`,
expected: "'Null No'",
},
{
name: "Evaluation with empty string condition",
inputYAML: `!If
test: ''
then: 'Empty Yes'
else: 'Empty No'`,
expected: "'Empty No'",
},
{
name: "Evaluation with empty list condition",
inputYAML: `!If
test: []
then: 'Empty List Yes'
else: 'Empty List No'`,
expected: "'Empty List No'",
},
{
name: "Omitting 'then' branch - true condition",
inputYAML: `!If
test: true
else: 'No'`,
expected: "null", // Expecting null or equivalent when 'then' is missing and condition is true
},
{
name: "Omitting 'else' branch - false condition",
inputYAML: `!If
test: false
then: 'Yes'`,
expected: "null", // Expecting null or equivalent when 'else' is missing and condition is false
},
{
name: "Omitting both 'then' and 'else' - true condition",
inputYAML: `!If
test: true`,
expected: "null", // Expecting null or equivalent when both branches are missing
},
{
name: "Omitting both 'then' and 'else' - false condition",
inputYAML: `!If
test: false`,
expected: "null", // Expecting null or equivalent when both branches are missing
},
{
name: "Variable substitution in condition with omitted 'then'",
inputYAML: `!If
test: !Var condition
else: 'Variable No'`,
expected: "null",
initVars: map[string]interface{}{
"condition": true,
},
},
{
name: "Variable substitution in condition with omitted 'else'",
inputYAML: `!If
test: !Var condition
then: 'Variable Yes'`,
expected: "null",
initVars: map[string]interface{}{
"condition": false,
},
},
{
name: "Nested Op test",
inputYAML: `!If
test: !Op
a: 10
op: ">"
b: 5
then: "True Condition"
else: "False Condition"`,
expected: "\"True Condition\"",
},
{
name: "Nested All in test",
inputYAML: `!If
test: !All
- !Op
a: 10
op: ">"
b: 5
- !Op
a: [ ]
op: '!='
b: [ ]
then: "True Condition"
else: "False Condition"`,
expected: "\"False Condition\"",
},
}
runTests(t, tests)
}