-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpath_funcs_test.go
95 lines (69 loc) · 2.16 KB
/
path_funcs_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
package main
import "testing"
type hasPathFuncTest struct {
path string
expected bool
}
var hasPathFuncTests = []hasPathFuncTest{
{"size()", true},
{"my.path.string()", true},
{"some.path", false},
{"some.path", false},
{"items.1.path", false},
{"items.1.path.size()", true},
{"items.path.xyz()", false},
{"items.path.custom().string()", true},
{"items.path.sizeAsString()", true},
}
func TestHasPathFunc(t *testing.T) {
for _, tt := range hasPathFuncTests {
actual := HasPathFunc(tt.path)
if actual != tt.expected {
t.Errorf("HasPathFunc(%#v): expected %#v, actual %#v", tt.path, tt.expected, actual)
}
}
}
func TestCallPathFuncStr(t *testing.T) {
pathLine := "items.id.string()"
arg := 123.0
res, err := CallPathFunc(pathLine, arg)
expected := "123"
if res != expected || err != nil {
t.Errorf("Expected %s Got %#v, %#v = CallPathFunc(%#v, %#v)", expected, res, err, pathLine, arg)
}
}
func TestCallPathFuncStrArr(t *testing.T) {
pathLine := "items.string()"
arg := []string{"1", "2"}
res, err := CallPathFunc(pathLine, arg)
expected := "[1 2]"
if res != expected || err != nil {
t.Errorf("Expected %s Got %#v, %#v = CallPathFunc(%#v, %#v)", expected, res, err, pathLine, arg)
}
}
func TestCallPathFuncSizeNonArr(t *testing.T) {
pathLine := "items.id.size()"
arg := 99.0
res, err := CallPathFunc(pathLine, arg)
if res != nil || err == nil {
t.Errorf("Expected nil, error. Got %#v, %#v = CallPathFunc(%#v, %#v)", res, err, pathLine, arg)
}
}
func TestCallPathFuncSizeArr(t *testing.T) {
pathLine := "items.size()"
arg := []interface{}{5, 8}
res, err := CallPathFunc(pathLine, arg)
var expected float64 = 2 // float (not int) for json parser always return it for numbers
if res != expected || err != nil {
t.Errorf("Expected %#v Got %#v, %#v = CallPathFunc(%#v, %#v)", expected, res, err, pathLine, arg)
}
}
func TestCallPathFuncSizeAsString(t *testing.T) {
pathLine := "items.sizeAsString()"
arg := []interface{}{5, 2, 1}
res, err := CallPathFunc(pathLine, arg)
var expected = "3"
if res != expected || err != nil {
t.Errorf("Expected %#v Got %#v, %#v = CallPathFunc(%#v, %#v)", expected, res, err, pathLine, arg)
}
}