Skip to content

Commit a8708bc

Browse files
committed
consolidate more tests
1 parent 658ff9e commit a8708bc

File tree

6 files changed

+54
-108
lines changed

6 files changed

+54
-108
lines changed

jsoniter_optional_test.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

jsoniter_raw_message_test.go renamed to misc_tests/jsoniter_raw_message_test.go

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,29 @@
1-
package jsoniter
1+
package misc_tests
22

33
import (
44
"encoding/json"
55
"github.com/stretchr/testify/require"
66
"strings"
77
"testing"
8+
"github.com/json-iterator/go"
89
)
910

10-
func Test_json_RawMessage(t *testing.T) {
11-
should := require.New(t)
12-
var data json.RawMessage
13-
should.Nil(Unmarshal([]byte(`[1,2,3]`), &data))
14-
should.Equal(`[1,2,3]`, string(data))
15-
str, err := MarshalToString(data)
16-
should.Nil(err)
17-
should.Equal(`[1,2,3]`, str)
18-
}
19-
2011
func Test_jsoniter_RawMessage(t *testing.T) {
2112
should := require.New(t)
22-
var data RawMessage
23-
should.Nil(Unmarshal([]byte(`[1,2,3]`), &data))
13+
var data jsoniter.RawMessage
14+
should.Nil(jsoniter.Unmarshal([]byte(`[1,2,3]`), &data))
2415
should.Equal(`[1,2,3]`, string(data))
25-
str, err := MarshalToString(data)
16+
str, err := jsoniter.MarshalToString(data)
2617
should.Nil(err)
2718
should.Equal(`[1,2,3]`, str)
2819
}
2920

30-
func Test_json_RawMessage_in_struct(t *testing.T) {
31-
type TestObject struct {
32-
Field1 string
33-
Field2 json.RawMessage
34-
}
35-
should := require.New(t)
36-
var data TestObject
37-
should.Nil(Unmarshal([]byte(`{"field1": "hello", "field2": [1,2,3]}`), &data))
38-
should.Equal(` [1,2,3]`, string(data.Field2))
39-
should.Equal(`hello`, data.Field1)
40-
}
41-
42-
func Test_decode_map_of_raw_message(t *testing.T) {
43-
should := require.New(t)
44-
type RawMap map[string]*json.RawMessage
45-
b := []byte("{\"test\":[{\"key\":\"value\"}]}")
46-
var rawMap RawMap
47-
should.Nil(Unmarshal(b, &rawMap))
48-
should.Equal(`[{"key":"value"}]`, string(*rawMap["test"]))
49-
type Inner struct {
50-
Key string `json:"key"`
51-
}
52-
var inner []Inner
53-
Unmarshal(*rawMap["test"], &inner)
54-
should.Equal("value", inner[0].Key)
55-
}
56-
57-
func Test_encode_map_of_raw_message(t *testing.T) {
58-
should := require.New(t)
59-
type RawMap map[string]*json.RawMessage
60-
value := json.RawMessage("[]")
61-
rawMap := RawMap{"hello": &value}
62-
output, err := MarshalToString(rawMap)
63-
should.Nil(err)
64-
should.Equal(`{"hello":[]}`, output)
65-
}
66-
6721
func Test_encode_map_of_jsoniter_raw_message(t *testing.T) {
6822
should := require.New(t)
69-
type RawMap map[string]*RawMessage
70-
value := RawMessage("[]")
23+
type RawMap map[string]*jsoniter.RawMessage
24+
value := jsoniter.RawMessage("[]")
7125
rawMap := RawMap{"hello": &value}
72-
output, err := MarshalToString(rawMap)
26+
output, err := jsoniter.MarshalToString(rawMap)
7327
should.Nil(err)
7428
should.Equal(`{"hello":[]}`, output)
7529
}
@@ -82,8 +36,8 @@ func Test_marshal_invalid_json_raw_message(t *testing.T) {
8236

8337
a := A{}
8438
should := require.New(t)
85-
should.Nil(ConfigCompatibleWithStandardLibrary.Unmarshal(message, &a))
86-
aout, aouterr := ConfigCompatibleWithStandardLibrary.Marshal(&a)
39+
should.Nil(jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(message, &a))
40+
aout, aouterr := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(&a)
8741
should.Equal(`{"raw":null}`, string(aout))
8842
should.Nil(aouterr)
8943
}
@@ -99,14 +53,14 @@ func Test_raw_message_memory_not_copied_issue(t *testing.T) {
9953
BiddingMax *float32 `json:"bidding_max"`
10054
BiddingMin *float32 `json:"bidding_min"`
10155
BiddingType *string `json:"bidding_type"`
102-
Freq *RawMessage `json:"freq"`
103-
Targeting *RawMessage `json:"targeting"`
104-
Url *RawMessage `json:"url"`
56+
Freq *jsoniter.RawMessage `json:"freq"`
57+
Targeting *jsoniter.RawMessage `json:"targeting"`
58+
Url *jsoniter.RawMessage `json:"url"`
10559
Speed *int `json:"speed" db:"speed"`
10660
}
10761

10862
obj := &IteratorObject{}
109-
decoder := NewDecoder(strings.NewReader(jsonStream))
63+
decoder := jsoniter.NewDecoder(strings.NewReader(jsonStream))
11064
err := decoder.Decode(obj)
11165
should := require.New(t)
11266
should.Nil(err)

value_tests/map_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package test
22

3-
import "math/big"
3+
import (
4+
"math/big"
5+
"encoding/json"
6+
)
47

58
func init() {
9+
var pRawMessage = func(val json.RawMessage) *json.RawMessage {
10+
return &val
11+
}
612
nilMap := map[string]string(nil)
713
marshalCases = append(marshalCases,
814
map[string]interface{}{"abc": 1},
@@ -20,13 +26,17 @@ func init() {
2026
},
2127
nilMap,
2228
&nilMap,
29+
map[string]*json.RawMessage{"hello":pRawMessage(json.RawMessage("[]"))},
2330
)
2431
unmarshalCases = append(unmarshalCases, unmarshalCase{
2532
ptr: (*map[string]string)(nil),
2633
input: `{"k\"ey": "val"}`,
2734
}, unmarshalCase{
2835
ptr: (*map[string]string)(nil),
2936
input: `null`,
37+
}, unmarshalCase{
38+
ptr: (*map[string]*json.RawMessage)(nil),
39+
input: "{\"test\":[{\"key\":\"value\"}]}",
3040
})
3141
}
3242

value_tests/ptr_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ func init() {
44
var pEFace = func(val interface{}) *interface{} {
55
return &val
66
}
7+
var pInt = func(val int) *int {
8+
return &val
9+
}
710
unmarshalCases = append(unmarshalCases, unmarshalCase{
811
ptr: (**interface{})(nil),
912
input: `"hello"`,
@@ -16,5 +19,7 @@ func init() {
1619
})
1720
marshalCases = append(marshalCases,
1821
pEFace("hello"),
22+
(*int)(nil),
23+
pInt(100),
1924
)
20-
}
25+
}

value_tests/raw_message_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ func init() {
66
marshalCases = append(marshalCases,
77
json.RawMessage("{}"),
88
)
9+
unmarshalCases = append(unmarshalCases, unmarshalCase{
10+
ptr: (*json.RawMessage)(nil),
11+
input: `[1,2,3]`,
12+
})
913
}

value_tests/struct_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
)
88

99
func init() {
10+
var pString = func(val string) *string {
11+
return &val
12+
}
1013
unmarshalCases = append(unmarshalCases, unmarshalCase{
1114
ptr: (*struct {
1215
Field interface{}
@@ -34,6 +37,18 @@ func init() {
3437
Field1 string
3538
})(nil),
3639
input: `{"\u0046ield1":"hello"}`,
40+
}, unmarshalCase{
41+
ptr: (*struct {
42+
Field1 *string
43+
Field2 *string
44+
})(nil),
45+
input: `{"field1": null, "field2": "world"}`,
46+
}, unmarshalCase{
47+
ptr: (*struct {
48+
Field1 string
49+
Field2 json.RawMessage
50+
})(nil),
51+
input: `{"field1": "hello", "field2":[1,2,3]}`,
3752
})
3853
marshalCases = append(marshalCases,
3954
struct {
@@ -116,6 +131,10 @@ func init() {
116131
MaxAge: 20,
117132
},
118133
structOrder{},
134+
struct {
135+
Field1 *string
136+
Field2 *string
137+
}{Field2: pString("world")},
119138
)
120139
}
121140

0 commit comments

Comments
 (0)