-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
123 lines (94 loc) · 2.9 KB
/
Copy pathjson_test.go
File metadata and controls
123 lines (94 loc) · 2.9 KB
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
package json_test
import (
"testing"
"github.com/stretchr/testify/assert"
j "jsonparser"
"fmt"
)
const InvalidJson = `invalidJSon`
const SimpleJson = `{"name":"json"}`
func TestBuildJsonFailsForInvalidJson(t *testing.T) {
jsonStr := []byte(InvalidJson)
_, err := j.NewJSON(jsonStr)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to unmarshal json :")
}
func TestBuildJsonReturnsJSONForValidJson(t *testing.T) {
jsonBytes := []byte(SimpleJson)
expectedType := j.JSON{}
json, err := j.NewJSON(jsonBytes)
assert.NoError(t, err)
assert.NotNil(t, json)
assert.IsType(t, expectedType, json)
}
func TestKeyReturnsNilIfKeyDoesNotExist(t *testing.T) {
jsonBytes := []byte(SimpleJson)
json, _ := j.NewJSON(jsonBytes)
result, jsonType, _ := json.Key("non_existent_key")
assert.Nil(t, result)
assert.Equal(t, j.Type(0), jsonType)
}
func TestKeyReturnsValueWithTypeJSONObject(t *testing.T) {
jsonObject := `{"firstlevel":"jsonObjectValue"}`
jsonWithJsonObject := fmt.Sprintf(`{"key":%s}`, jsonObject)
expectedResult := map[string]interface{}{
"firstlevel": "jsonObjectValue",
}
jsonBytes := []byte(jsonWithJsonObject)
json, _ := j.NewJSON(jsonBytes)
result, jsonType, _ := json.Key("key")
assert.NotNil(t, result)
assert.Equal(t, expectedResult, result)
assert.Equal(t, j.Type(1), jsonType)
}
func TestKeyReturnsValueWithTypeJSONArray(t *testing.T) {
jsonArray := `["1","2","3","4"]`
jsonWithJsonArray := fmt.Sprintf(`{"key":%s}`, jsonArray)
expectedResult := []interface{}{
"1", "2", "3", "4",
}
jsonBytes := []byte(jsonWithJsonArray)
json, _ := j.NewJSON(jsonBytes)
result, jsonType, _ := json.Key("key")
assert.NotNil(t, result)
assert.Equal(t, expectedResult, result)
assert.Equal(t, j.Type(2), jsonType)
}
func TestKeyReturnsValueWithTypeString(t *testing.T) {
value := `json value`
jsonWithStringValue := fmt.Sprintf(`{"key":"%s"}`, value)
jsonBytes := []byte(jsonWithStringValue)
json, _ := j.NewJSON(jsonBytes)
result, jsonType, _ := json.Key("key")
assert.NotNil(t, result)
assert.Equal(t, value, result)
assert.Equal(t, j.Type(3), jsonType)
}
func TestKeyReturnsValueWithTypeInteger(t *testing.T) {
jsonWithIntegerValue := `{"key":123}`
expectedResult := float64(123)
jsonBytes := []byte(jsonWithIntegerValue)
json, _ := j.NewJSON(jsonBytes)
result, jsonType, _ := json.Key("key")
assert.NotNil(t, result)
assert.Equal(t, expectedResult, result)
assert.Equal(t, j.Type(4), jsonType)
}
func TestKeyReturnsErrorWhenJsonNotObject(t *testing.T) {
jsonArray := `["1","2","3","4"]`
jsonBytes := []byte(jsonArray)
json, _ := j.NewJSON(jsonBytes)
result, jsonType, err := json.Key("key")
assert.Error(t, err)
assert.Nil(t, result)
assert.Equal(t, j.Type(0), jsonType)
}
//func TestNextReturnsNextItemInJson(t *testing.T) {
//
// str := `{"key1":"value1","key2":"value2"}`
// jsonBytes := []byte(str)
//
// json, _ := j.NewJSON(jsonBytes)
// nextItem := json.Next()
//
//}