-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag_test.go
90 lines (76 loc) · 1.59 KB
/
tag_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
package salix
import (
"strings"
"testing"
)
func TestIf(t *testing.T) {
const tmplStr = `#if(weather.Temp > 30):
<p>It's a hot day!</p>
#elif(weather.Temp < 0):
<p>It's freezing!</p>
#else:
<p>The temperature is #(weather.Temp)</p>
#!if`
type weather struct {
Temp int
}
res := execStr(t, tmplStr, map[string]any{
"weather": weather{Temp: 40},
})
res = strings.TrimSpace(res)
if res != "<p>It's a hot day!</p>" {
t.Errorf("Expected %q, got %q", "<p>It's a hot day!</p>", res)
}
res = execStr(t, tmplStr, map[string]any{
"weather": weather{Temp: -1},
})
res = strings.TrimSpace(res)
if res != "<p>It's freezing!</p>" {
t.Errorf("Expected %q, got %q", "<p>It's freezing!</p>", res)
}
res = execStr(t, tmplStr, map[string]any{
"weather": weather{Temp: 25},
})
res = strings.TrimSpace(res)
if res != "<p>The temperature is 25</p>" {
t.Errorf("Expected %q, got %q", "<p>The temperature is 25</p>", res)
}
}
func TestFor(t *testing.T) {
const tmplStr = `#for(item in items):
#(item)
#!for`
res := execStr(t, tmplStr, map[string]any{
"items": []any{1.2, 3, "Hello", 4 + 2i, "-2", false, nil},
})
expected := `1.2
3
Hello
(4+2i)
-2
false
<nil>
`
if res != expected {
t.Errorf("Expected %q, got %q", expected, res)
}
}
func TestForTwoArgs(t *testing.T) {
const tmplStr = `#for(i, item in items):
#(i) #(item)
#!for`
res := execStr(t, tmplStr, map[string]any{
"items": []any{1.2, 3, "Hello", 4 + 2i, "-2", false, nil},
})
expected := `0 1.2
1 3
2 Hello
3 (4+2i)
4 -2
5 false
6 <nil>
`
if res != expected {
t.Errorf("Expected %q, got %q", expected, res)
}
}