-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsongo_test.go
94 lines (80 loc) · 1.61 KB
/
jsongo_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
// For finding allocations
// go test -c
// GODEBUG=allocfreetrace=1 ./jsongo.test -test.run=none -test.bench=BenchmarkJsonGo -test.benchtime=10ms 2>trace.log
package jsongo_test
import (
"testing"
"adrianlungu.com/jsongo"
"encoding/json"
)
var jsonByte []byte = []byte(`
{
"users": [
{
"id": 0,
"name": "Adam Carter",
"work": "Unilogic",
"email": "[email protected]",
"dob": "24/11/1978",
"address": "83 Warner Street",
"city": "Boston",
"optedin": true
},
{
"id": 1,
"name": "Leanne Brier",
"work": "Connic",
"email": "[email protected]",
"dob": "13/05/1987",
"address": "9 Coleman Avenue",
"city": "Toronto",
"optedin": false
}
],
"images": [
"img0.png",
"img1.png",
"img2.png"
],
"coordinates": {
"x": 35.12,
"y": -21.49
},
"price": "$59,395"
}
`)
var tests = []struct {
input string
output string
} {
{"users[0].id", "0"},
{"users[1].work", "Connic"},
{"coordinates.x", "35.12"},
{"images[0]", "img0.png"},
{"price", "$59,395"},
}
func TestJsonGo(t *testing.T) {
var j map[string]interface{}
err := json.Unmarshal(jsonByte, &j)
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
output := jsongo.Get(test.input, j)
if output != test.output {
t.Errorf("Expected %s, got %s", test.output, output)
}
}
}
func BenchmarkJsonGo(b *testing.B) {
var j map[string]interface{}
err := json.Unmarshal(jsonByte, &j)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i:=0; i < b.N; i++ {
jsongo.Get("users[0].id", j)
}
}