-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperator_test.go
149 lines (121 loc) · 2.86 KB
/
operator_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
package operator
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/dackon/jtool/jvalue"
)
type caseDataCfg struct {
Data json.RawMessage `json:"data"`
Expected json.RawMessage `json:"expected"` // Optional.
Error bool `json:"error"` // Optional.
}
type testCaseCfg struct {
Name string `json:"name"`
Enable bool `json:"enable"`
Cases []caseDataCfg `json:"cases"`
}
type caseData struct {
Data *jvalue.V
Expected *jvalue.V
Error bool
}
type testCase struct {
Name string
CaseArr []*caseData
}
func loadOneCategory(name string, t *testing.T) []*testCase {
fileData, err := ioutil.ReadFile(name)
if err != nil {
t.Errorf("Failed to read test case file '%s'.", name)
return nil
}
caseCfgArr := []*testCaseCfg{}
if err = json.Unmarshal(fileData, &caseCfgArr); err != nil {
t.Fatalf("Unmarshal file data filed. Err is %s", err)
}
caseArr := make([]*testCase, 0, len(caseCfgArr))
for _, cfg := range caseCfgArr {
if !cfg.Enable {
continue
}
tc := &testCase{
Name: cfg.Name,
}
for _, c := range cfg.Cases {
datav, err := jvalue.ParseJSON(c.Data)
if err != nil {
t.Fatalf("Data ParseJSON failed. Raw is %s", string(c.Data))
}
cd := &caseData{
Data: datav,
Error: c.Error,
}
if len(c.Expected) > 0 {
expectedV, err := jvalue.ParseJSON(c.Expected)
if err != nil {
t.Fatalf("Expected ParseJSON failed. Raw is %s",
string(c.Expected))
}
cd.Expected = expectedV
}
tc.CaseArr = append(tc.CaseArr, cd)
}
caseArr = append(caseArr, tc)
}
return caseArr
}
func loadCases(t *testing.T) []*testCase {
path := "./testCase"
files, err := ioutil.ReadDir(path)
if err != nil {
t.Fatalf("ReadDir failed. Err is %s", err)
}
fileNames := []string{}
for _, f := range files {
if f.IsDir() {
continue
}
if filepath.Ext(f.Name()) == ".json" {
fileNames = append(fileNames, f.Name())
}
}
var caseArr []*testCase
for _, f := range fileNames {
arr := loadOneCategory(
fmt.Sprintf("%s%c%s", path, os.PathSeparator, f), t)
if len(arr) == 0 {
continue
}
caseArr = append(caseArr, arr...)
}
return caseArr
}
func TestDo(t *testing.T) {
caseArr := loadCases(t)
for _, c := range caseArr {
for _, d := range c.CaseArr {
jv, err := Do(d.Data)
if err != nil && !d.Error {
t.Fatalf("Case '%s' failed. Expected no error but err is %s.",
c.Name, err)
}
if err == nil && d.Error {
t.Fatalf("Case '%s' failed. Expected error but err is nil.",
c.Name)
}
if d.Expected != nil {
if err = jv.IsEqual(d.Expected); err != nil {
r, _ := jv.Marshal()
e, _ := d.Expected.Marshal()
t.Fatalf("Case '%s' failed. Result is '%s'. "+
"Expected is '%s'.", c.Name, string(r), string(e))
}
}
}
t.Logf("Success. Case is '%s'.", c.Name)
}
}