-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_test.go
173 lines (162 loc) · 6.11 KB
/
mod_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
package dyntpl
import (
"bytes"
"testing"
)
type modArgs map[string]any
func TestMod(t *testing.T) {
t.Run("modDefault", testMod)
t.Run("modDefaultStatic", testMod)
t.Run("modDefault1", testMod)
t.Run("modJSONEscape", func(t *testing.T) { testModWA(t, modArgs{"userName": `Foo"bar`}) })
t.Run("modJSONEscapeShort", func(t *testing.T) { testModWA(t, modArgs{"userName": `Foo"bar`}) })
t.Run("modJSONEscapeDbl", func(t *testing.T) {
testModWA(t, modArgs{"valueWithQuotes": `He said: "welcome friend"`})
})
t.Run("modJSONQuoteShort", func(t *testing.T) { testModWA(t, modArgs{"userName": `Foo"bar`}) })
t.Run("modJSONQuoteNoesc", func(t *testing.T) { testModWA(t, modArgs{"userName": `Foo"bar`}) })
t.Run("modHtmlEscape", func(t *testing.T) {
testModWA(t, modArgs{
"title": `<h1>Go is an open source programming language that makes it easy to build <strong>simple<strong>, <strong>reliable</strong>, and <strong>efficient</strong> software.</h1>`,
"text": `Visit >`,
})
})
t.Run("modHtmlEscapeShort", func(t *testing.T) {
testModWA(t, modArgs{
"title": `<h1>Go is an open source programming language that makes it easy to build <strong>simple<strong>, <strong>reliable</strong>, and <strong>efficient</strong> software.</h1>`,
"text": `Visit >`,
})
})
t.Run("modAttrEscape", func(t *testing.T) { testModWA(t, modArgs{"var1": "foo&<>\"'`!@$%()=+{}[]#;bar"}) })
t.Run("modAttrEscapeMB", func(t *testing.T) { testModWA(t, modArgs{"var1": "Привет мир😀!"}) })
t.Run("modCSSEscape", func(t *testing.T) { testModWA(t, modArgs{"var1": "<>'\"&Ā,._aAzZ09 !😀"}) })
t.Run("modJSEscape", func(t *testing.T) { testModWA(t, modArgs{"var1": "<>'\"&/,._aAzZ09 Ā😀"}) })
t.Run("modJSEscapeMB", func(t *testing.T) { testModWA(t, modArgs{"var1": "Привет мир😀!"}) })
t.Run("modLinkEscape", func(t *testing.T) {
testModWA(t, modArgs{"link": `http://x.com/link-with-"-and space-symbol`})
})
t.Run("modURLEncode", func(t *testing.T) {
testModWA(t, modArgs{"url": `https://golang.org/src/net/url/url.go#L100`})
})
t.Run("modURLEncode2", func(t *testing.T) {
testModWA(t, modArgs{"url": `https://golang.org/src/net/url/url.go#L100`})
})
t.Run("modURLEncode3", func(t *testing.T) {
testModWA(t, modArgs{"url": `https://golang.org/src/net/url/url.go#L100`})
})
t.Run("modIfThen", func(t *testing.T) { testModWA(t, modArgs{"allow": true}) })
t.Run("modIfThenElse", func(t *testing.T) { testModWA(t, modArgs{"logged": true, "userName": "foobar"}) })
t.Run("modRound", func(t *testing.T) {
testModWA(t, modArgs{
"f0": 7.243242,
"f1": 3.1415,
"f2": 11.39,
"f3": 56.68734,
"f4": 67.999,
"f5": 20.214999,
})
})
}
func testMod(t *testing.T) {
testTpl(t)
}
func testModWA(t *testing.T, args modArgs) {
key := getTBName(t)
st := getStage(key)
if st == nil {
t.Error("stage not found")
return
}
ctx := NewCtx()
for k, v := range args {
ctx.SetStatic(k, v)
}
result, err := Render(key, ctx)
if err != nil {
t.Error(err)
}
if !bytes.Equal(result, st.expect) {
t.Errorf("%s mismatch", key)
}
}
func BenchmarkMod(b *testing.B) {
b.Run("modDefault", benchMod)
b.Run("modDefaultStatic", benchMod)
b.Run("modDefault1", benchMod)
b.Run("modJSONEscape", func(b *testing.B) { benchModWA(b, modArgs{"userName": `Foo"bar`}) })
b.Run("modJSONEscapeShort", func(b *testing.B) { benchModWA(b, modArgs{"userName": `Foo"bar`}) })
b.Run("modJSONEscapeDbl", func(b *testing.B) {
benchModWA(b, modArgs{"valueWithQuotes": `He said: "welcome friend"`})
})
b.Run("modJSONQuoteShort", func(b *testing.B) { benchModWA(b, modArgs{"userName": `Foo"bar`}) })
b.Run("modJSONQuoteNoesc", func(b *testing.B) { benchModWA(b, modArgs{"userName": `Foo"bar`}) })
b.Run("modHtmlEscape", func(b *testing.B) {
benchModWA(b, modArgs{
"title": `<h1>Go is an open source programming language that makes it easy to build <strong>simple<strong>, <strong>reliable</strong>, and <strong>efficient</strong> software.</h1>`,
"text": `Visit >`,
})
})
b.Run("modHtmlEscapeShort", func(b *testing.B) {
benchModWA(b, modArgs{
"title": `<h1>Go is an open source programming language that makes it easy to build <strong>simple<strong>, <strong>reliable</strong>, and <strong>efficient</strong> software.</h1>`,
"text": `Visit >`,
})
})
b.Run("modAttrEscape", func(b *testing.B) { benchModWA(b, modArgs{"var1": "foo&<>\"'`!@$%()=+{}[]#;bar"}) })
b.Run("modAttrEscapeMB", func(b *testing.B) { benchModWA(b, modArgs{"var1": "Привет мир😀!"}) })
b.Run("modCSSEscape", func(b *testing.B) { benchModWA(b, modArgs{"var1": "<>'\"&Ā,._aAzZ09 !😀"}) })
b.Run("modJSEscape", func(b *testing.B) { benchModWA(b, modArgs{"var1": "<>'\"&/,._aAzZ09 Ā😀"}) })
b.Run("modJSEscapeMB", func(b *testing.B) { benchModWA(b, modArgs{"var1": "Привет мир😀!"}) })
b.Run("modLinkEscape", func(b *testing.B) {
benchModWA(b, modArgs{"link": `http://x.com/link-with-"-and space-symbol`})
})
b.Run("modURLEncode", func(b *testing.B) {
benchModWA(b, modArgs{"url": `https://golang.org/src/net/url/url.go#L100`})
})
b.Run("modURLEncode2", func(b *testing.B) {
benchModWA(b, modArgs{"url": `https://golang.org/src/net/url/url.go#L100`})
})
b.Run("modURLEncode3", func(b *testing.B) {
benchModWA(b, modArgs{"url": `https://golang.org/src/net/url/url.go#L100`})
})
b.Run("modIfThen", func(b *testing.B) { benchModWA(b, modArgs{"allow": true}) })
b.Run("modIfThenElse", func(b *testing.B) { benchModWA(b, modArgs{"logged": true, "userName": "foobar"}) })
b.Run("modRound", func(b *testing.B) {
benchModWA(b, modArgs{
"f0": 7.243242,
"f1": 3.1415,
"f2": 11.39,
"f3": 56.68734,
"f4": 67.999,
"f5": 20.214999,
})
})
}
func benchMod(b *testing.B) {
benchTpl(b)
}
func benchModWA(b *testing.B, args modArgs) {
key := getTBName(b)
st := getStage(key)
if st == nil {
b.Error("stage not found")
return
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx := AcquireCtx()
for k, v := range args {
ctx.SetStatic(k, v)
}
buf.Reset()
err := Write(&buf, key, ctx)
if err != nil {
b.Error(err)
}
if !bytes.Equal(buf.Bytes(), st.expect) {
b.Errorf("%s mismatch", key)
}
ReleaseCtx(ctx)
}
}