forked from timbray/quamina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern_test.go
114 lines (110 loc) · 2.68 KB
/
pattern_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
package quamina
import (
"testing"
)
func TestPatternFromJSON(t *testing.T) {
bads := []string{
`x`,
`{"foo": ]`,
`{"foo": 11 }`,
`{"foo": "x" }`,
`{"foo": true}`,
`{"foo": null}`,
`{"oof": [ ]`,
`[33,22]`,
`{"xxx": { }`,
`{"xxx": [ [ 22 ] }`,
`{"xxx": [ {"x": 1} ]`,
`{"xxx": [ { [`,
`{"xxx": [ { "exists": 23 } ] }`,
`{"xxx": [ { "exists": true }, 15 ] }`,
`{"xxx": [ { "exists": true, "a": 3 }] }`,
`{"xxx": [ { "exists": false, "x": ["a", 3 ] }] }`,
`{"abc": [ {"shellstyle":15} ] }`,
`{"abc": [ {"shellstyle":"a**b"}, "foo" ] }`,
}
for _, b := range bads {
_, _, err := patternFromJSON([]byte(b))
if err == nil {
t.Error("accepted bad pattern: " + b)
}
}
goods := []string{
`{"x": [ 2 ]}`,
`{"x": [ null, true, false, "hopp", 3.072e-11] }`,
`{"x": { "a": [27, 28], "b": { "m": [ "a", "b" ] } } }`,
`{"x": [ {"exists": true} ] }`,
`{"x": { "y": [ {"exists": false} ] } }`,
`{"abc": [ 3, {"shellstyle":"a*b"} ] }`,
`{"abc": [ {"shellstyle":"a*b"}, "foo" ] }`,
}
w1 := []*patternField{{path: "x", vals: []typedVal{{vType: numberType, val: "2"}}}}
w2 := []*patternField{{path: "x", vals: []typedVal{
{literalType, "null", nil},
{literalType, "true", nil},
{literalType, "false", nil},
{stringType, `"hopp"`, nil},
{numberType, "3.072e-11", nil},
}}}
w3 := []*patternField{
{path: "x\na", vals: []typedVal{
{numberType, "27", nil},
{numberType, "28", nil},
}},
{path: "x\nb\nm", vals: []typedVal{
{stringType, `"a"`, nil},
{stringType, `"b"`, nil},
}},
}
w4 := []*patternField{
{
path: "x", vals: []typedVal{
{vType: existsTrueType, val: ""},
},
},
}
w5 := []*patternField{
{
path: "x\ny", vals: []typedVal{
{vType: existsFalseType, val: ""},
},
},
}
w6 := []*patternField{
{
path: "abc", vals: []typedVal{
{vType: stringType, val: "3"},
{vType: shellStyleType, val: `"a*b"`},
},
},
}
w7 := []*patternField{
{
path: "abc", vals: []typedVal{
{vType: shellStyleType, val: `"a*b"`},
{vType: stringType, val: `"foo"`},
},
},
}
wanted := [][]*patternField{w1, w2, w3, w4, w5, w6, w7}
for i, good := range goods {
fields, _, err := patternFromJSON([]byte(good))
if err != nil {
t.Error("pattern:" + good + ": " + err.Error())
}
w := wanted[i]
if len(w) != len(fields) {
t.Errorf("at %d len(w)=%d, len(fields)=%d", i, len(w), len(fields))
}
for j, ww := range w {
if ww.path != fields[j].path {
t.Error("pathSegments mismatch: " + ww.path + "/" + fields[j].path)
}
for k, www := range ww.vals {
if www.val != fields[j].vals[k].val {
t.Errorf("At [%d][%d], val mismatch %s/%s", j, k, www.val, fields[j].vals[k].val)
}
}
}
}
}