-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
123 lines (106 loc) · 2.67 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
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
package dyntpl
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"github.com/koykov/bytealg"
"github.com/koykov/byteconv"
"github.com/koykov/inspector/testobj"
"github.com/koykov/inspector/testobj_ins"
)
type stage struct {
key, err string
origin, expect, raw []byte
}
var (
stages []stage
user = &testobj.TestObject{
Id: "115",
Name: []byte("John"),
Status: 78,
Flags: testobj.TestFlag{
"export": 17,
"ro": 4,
"rw": 7,
"Valid": 1,
},
Finance: &testobj.TestFinance{
Balance: 9000.015,
AllowBuy: false,
History: []testobj.TestHistory{
{
DateUnix: 152354345634,
Cost: 14.345241,
Comment: []byte("pay for domain"),
},
{
DateUnix: 153465345246,
Cost: -3.0000342543,
Comment: []byte("got refund"),
},
{
DateUnix: 156436535640,
Cost: 2325242534.35324523,
Comment: []byte("maintenance"),
},
},
},
}
ins testobj_ins.TestObjectInspector
buf bytes.Buffer
)
func init() {
registerTestStages("tpl")
registerTestStages("mod")
registerTestStages("datetime")
registerTestStages("math")
_ = filepath.Walk("testdata/parser", 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.key = "parser/" + st.key
st.origin, _ = os.ReadFile(path)
tree, _ := Parse(st.origin, false)
if raw, err := os.ReadFile(strings.Replace(path, ".tpl", ".xml", 1)); err == nil {
st.expect = raw
} else if raw, err := os.ReadFile(strings.Replace(path, ".tpl", ".raw", 1)); err == nil {
st.raw = bytealg.Trim(raw, []byte("\n"))
} else if raw, err := os.ReadFile(strings.Replace(path, ".tpl", ".err", 1)); err == nil {
st.err = bytealg.Trim(byteconv.B2S(raw), "\n")
}
stages = append(stages, st)
RegisterTplKey(st.key, tree)
}
return nil
})
}
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, _ := Parse(st.origin, false)
st.expect, _ = os.ReadFile(strings.Replace(path, ".tpl", ".txt", 1))
st.expect = bytealg.Trim(st.expect, []byte("\n"))
stages = append(stages, st)
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:]
}