-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathm_test.go
148 lines (121 loc) · 2.21 KB
/
m_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
package toolkit
import (
"encoding/json"
"fmt"
"testing"
)
func ExampleM_PathGet() {
input := []byte(`{
"student": {
"name": "John Doe",
"class": {
"name": "A5",
"year": 2006,
"major": {
"name": "Biomedic"
}
}
}
}`)
var obj M
err := json.Unmarshal(input, &obj)
if err != nil {
fmt.Println("error: ", err)
}
majorName, err := obj.PathGet("student.class.major.name")
if err != nil {
fmt.Println("error: ", err)
}
fmt.Printf("%v", majorName)
// Output:
// Biomedic
}
func TestMPathGetInvalidPath(t *testing.T) {
obj := M{
"student": M{
"name": "Charlie",
},
}
var result interface{}
var err error
result, err = obj.PathGet("student.class.major.name")
if err == nil {
t.Fail()
}
if result != nil {
t.Fail()
}
result, err = obj.PathGet("student.name.fullname")
if err == nil {
t.Fail()
}
if result != nil {
t.Fail()
}
}
func TestGetRef(t *testing.T) {
type model struct {
Key string
Value float32
}
var d3 model
d1 := model{"M1", 20.53}
m := M{}.Set("D1", &d1)
m.GetRef("D1", new(model), &d3)
if d3.Value != d1.Value {
t.Errorf("Expecting %v, got %v", d1.Value, d3.Value)
}
}
func TestGetRefNil(t *testing.T) {
type model struct {
Key string
Value float32
}
var d3 model
d1 := model{"M1", 20.53}
m := M{}.Set("D1", nil)
m.GetRef("D1", &d1, &d3)
if d3.Value != d1.Value {
t.Errorf("Expecting %v, got %v", d1.Value, d3.Value)
}
}
func TestGetRefNonPointer(t *testing.T) {
type model struct {
Key string
Value float32
}
var d3 model
d1 := model{"M1", 20.53}
m := M{}.Set("D1", nil)
m.GetRef("D1", &d1, &d3)
if d3.Value != d1.Value {
t.Errorf("Expecting %v, got %v", d1.Value, d3.Value)
}
}
type model struct {
Key string
Value float32
}
var result model
func BenchmarkGetRef(b *testing.B) {
var d3 model
d1 := model{"M1", 20.53}
m := M{}.Set("D1", d1)
for n := 0; n < b.N; n++ {
m.GetRef("D1", &d1, &d3)
}
}
func BenchmarkGet(b *testing.B) {
d1 := model{"M1", 20.53}
m := M{}.Set("D1", d1)
for n := 0; n < b.N; n++ {
_ = m.Get("D1", d1)
}
}
func BenchmarkGetCasted(b *testing.B) {
d1 := model{"M1", 20.53}
m := M{}.Set("D1", d1)
for n := 0; n < b.N; n++ {
result = m.Get("D1", d1).(model)
}
}