-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
96 lines (82 loc) · 1.83 KB
/
common_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
package vector_inspector
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"github.com/koykov/dyntpl"
)
type stage struct {
key string
origin, source, expect []byte
}
var stages []stage
func init() {
registerTestStages("parser")
registerTestStages("mod")
}
func registerTestStages(dir string) {
_ = filepath.Walk("testdata/"+dir, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".tpl" {
st := stage{}
st.key = strings.Replace(filepath.Base(path), ".tpl", "", 1)
st.origin, _ = os.ReadFile(path)
tree, _ := dyntpl.Parse(st.origin, false)
st.source, _ = os.ReadFile(strings.Replace(path, ".tpl", ".source.txt", 1))
st.source = bytes.Trim(st.source, "\n")
st.expect, _ = os.ReadFile(strings.Replace(path, ".tpl", ".expect.txt", 1))
st.expect = bytes.Trim(st.expect, "\n")
stages = append(stages, st)
dyntpl.RegisterTplKey(st.key, tree)
}
return nil
})
}
func getStage(key string) (st *stage) {
for i := 0; i < len(stages); i++ {
st1 := &stages[i]
if st1.key == key {
st = st1
}
}
return st
}
func getTBName(tb testing.TB) string {
key := tb.Name()
return key[strings.Index(key, "/")+1:]
}
func testStage(t *testing.T) {
key := getTBName(t)
st := getStage(key)
if st == nil {
t.Error("stage not found")
return
}
ctx := dyntpl.NewCtx()
ctx.SetBytes("source", st.source)
r, err := dyntpl.Render(key, ctx)
if err != nil {
t.Error(err)
}
if !bytes.Equal(r, st.expect) {
t.FailNow()
}
}
func benchStage(b *testing.B) {
b.ReportAllocs()
key := getTBName(b)
st := getStage(key)
if st == nil {
b.Error("stage not found")
return
}
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
ctx := dyntpl.AcquireCtx()
ctx.SetBytes("source", st.source)
_ = dyntpl.Write(&buf, "json", ctx)
dyntpl.ReleaseCtx(ctx)
buf.Reset()
}
}