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
|
package model
import (
stdJSON "encoding/json"
"reflect"
"testing"
)
func TestNode_MarshalJSON(t *testing.T) {
type fields struct {
node Node
}
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{
{
name: "empty",
fields: fields{
node: NewNode(nil),
},
want: []byte(`null`),
},
{
name: "string",
fields: fields{
node: NewNode("this is a string"),
},
want: []byte(`"this is a string"`),
},
{
name: "int",
fields: fields{
node: NewNode(123),
},
want: []byte(`123`),
},
{
name: "float",
fields: fields{
node: NewNode(123.321),
},
want: []byte(`123.321`),
},
{
name: "booleant",
fields: fields{
node: NewNode(true),
},
want: []byte(`true`),
},
{
name: "booleanf",
fields: fields{
node: NewNode(false),
},
want: []byte(`false`),
},
{
name: "complex",
fields: fields{
node: NewNode(
NodeObjectValue{
"string key": NewNode("string value"),
"number key": NewNode(1337),
"float key": NewNode(123.3),
"object key": NewNode(NodeObjectValue{
"ab": NewNode("cd"),
}),
"array key": NewNode(NodeArrayValue{
NewNode(1), NewNode(2), NewNode("three"),
}),
"boolean key": NewNode(true),
"null key": NewNode(nil),
},
),
},
want: []byte(
`{"string key": "string value", "number key": 1337, "float key": 123.3, "object key": {"ab": "cd"}, "array key": [1, 2, "three"], "boolean key": true, "null key": null}`,
),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var (
gotObj any
wantObj any
)
got, err := tt.fields.node.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("Node.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
err = stdJSON.Unmarshal(got, &gotObj) // TODO use own unmarshaller
if err != nil {
t.Errorf("Generated invalid json = %s, error = %v", got, err)
}
_ = stdJSON.Unmarshal(tt.want, &wantObj) // I belive, test is correct
if !reflect.DeepEqual(gotObj, wantObj) {
t.Errorf("Node.MarshalJSON() = %s, want %s", got, tt.want)
}
})
}
}
|