-
Notifications
You must be signed in to change notification settings - Fork 8
/
proxy_test.go
110 lines (95 loc) · 1.92 KB
/
proxy_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
package dproxy
import (
"encoding/json"
"testing"
)
func parseJSON(s string) interface{} {
var v interface{}
if err := json.Unmarshal([]byte(s), &v); err != nil {
panic(err)
}
return v
}
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, s := range a {
if s != b[i] {
return false
}
}
return true
}
func equalInts(a, b []int64) bool {
if len(a) != len(b) {
return false
}
for i, s := range a {
if s != b[i] {
return false
}
}
return true
}
func TestReadme(t *testing.T) {
v := parseJSON(`{
"cities": [ "tokyo", 100, "osaka", 200, "hakata", 300 ],
"data": {
"custom": [ "male", 21, "female", 22 ]
}
}`)
s, err := New(v).M("cities").A(0).String()
if s != "tokyo" {
t.Error("cities[0] must be \"tokyo\":", err)
}
_, err = New(v).M("cities").A(0).Float64()
if err == nil {
t.Error("cities[0] (float64) must be failed:", err)
}
n, err := New(v).M("cities").A(1).Float64()
if n != 100 {
t.Error("cities[1] must be 100:", err)
}
s2, err := New(v).M("data").M("custom").A(2).String()
if s2 != "female" {
t.Error("data.custom[2] must be \"female\":", err)
}
_, err = New(v).M("data").M("kustom").String()
if err == nil || err.Error() != "not found: data.kustom" {
t.Error("err is not \"not found: data.kustom\":", err)
}
}
func TestMapBool(t *testing.T) {
v := parseJSON(`{
"foo": true,
"bar": false
}`)
// check "foo"
foo, err := New(v).M("foo").Bool()
if err != nil {
t.Error(err)
} else if foo != true {
t.Errorf("foo must be true")
}
// check "bar"
bar, err := New(v).M("bar").Bool()
if err != nil {
t.Error(err)
} else if bar != false {
t.Errorf("bar must be false")
}
}
type wrappedMap map[string]interface{}
func TestWrappedMap(t *testing.T) {
v := wrappedMap{
"foo": 123,
}
n, err := New(v).M("foo").Int64()
if err != nil {
t.Fatalf("failed: %s", err)
}
if n != 123 {
t.Fatalf("unexpected value: %d", n)
}
}