-
Notifications
You must be signed in to change notification settings - Fork 12
/
context_test.go
124 lines (115 loc) · 2.19 KB
/
context_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
package jio
import (
"errors"
"reflect"
"testing"
)
func TestContext_Ref(t *testing.T) {
ctx := NewContext(map[string]interface{}{
"1": map[string]interface{}{
"2": "2",
},
"3": 3,
"4": []int{1, 2, 3, 4},
})
value, ok := ctx.Ref("1")
if !ok {
t.Error("not found refer 1")
}
valueMap, ok := value.(map[string]interface{})
if !ok {
t.Error("type assert failed")
}
if len(valueMap) != 1 {
t.Error("unknown map keys")
}
value, ok = ctx.Ref("1.2")
if !ok {
t.Error("not found refer 1.2")
}
if value != "2" {
t.Error("unknown value")
}
value, ok = ctx.Ref("3")
if !ok {
t.Error("not found refer 3")
}
if value != 3 {
t.Error("unknown value")
}
_, ok = ctx.Ref("4.1")
if ok {
t.Error("found refer 4")
}
_, ok = ctx.Ref("5")
if ok {
t.Error("found refer 5")
}
}
func TestContext_FieldPath(t *testing.T) {
ctx := NewContext(nil)
ctx.fields = []string{"1"}
if ctx.FieldPath() != "1" {
t.Error("error path")
}
ctx.fields = []string{"1", "2"}
if ctx.FieldPath() != "1.2" {
t.Error("error path")
}
}
func TestContext_Abort(t *testing.T) {
ctx := NewContext(nil)
ctx.Abort(errors.New("error"))
if ctx.Err == nil {
t.Error("should have error")
}
if !ctx.skip {
t.Error("should skip")
}
}
func TestContext_Skip(t *testing.T) {
ctx := NewContext(nil)
ctx.Skip()
if ctx.Err != nil {
t.Error("should no error")
}
if !ctx.skip {
t.Error("should skip")
}
}
func TestContext_SetAndGet(t *testing.T) {
ctx := NewContext(nil)
ctx.Set("name", "faceair")
name, ok := ctx.Get("name")
if !ok || name != "faceair" {
t.Error("get failed")
}
name, ok = ctx.Get("age")
if ok || name != nil {
t.Error("should be nil")
}
ctx = NewContext(nil)
name, ok = ctx.Get("name")
if ok || name != nil {
t.Error("should be nil")
}
}
func toInterface(value interface{}) *interface{} {
return &value
}
func TestContext_AssertKind(t *testing.T) {
name := "faceair"
ctx := NewContext(name)
if !ctx.AssertKind(reflect.String) {
t.Error("assert string faild")
}
if len(ctx.kindCache) != 1 {
t.Error("assert string faild")
}
if !ctx.AssertKind(reflect.String) {
t.Error("assert string faild")
}
if len(ctx.kindCache) != 1 {
t.Error("assert string faild")
}
}