2022-12-27 02:37:02 +03:00
|
|
|
package json_test
|
2022-11-16 05:11:19 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2024-04-28 00:18:36 +03:00
|
|
|
"go.neonxp.ru/json"
|
|
|
|
"go.neonxp.ru/json/std"
|
2022-11-16 05:11:19 +03:00
|
|
|
)
|
|
|
|
|
2022-12-27 02:37:02 +03:00
|
|
|
func TestJSON_Unmarshal(t *testing.T) {
|
|
|
|
j := &json.JSON{
|
|
|
|
Factory: std.Factory,
|
|
|
|
}
|
2022-11-16 05:11:19 +03:00
|
|
|
type args struct {
|
2022-12-27 02:37:02 +03:00
|
|
|
input string
|
2022-11-16 05:11:19 +03:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2022-12-27 02:37:02 +03:00
|
|
|
want json.Node
|
2022-11-16 05:11:19 +03:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2022-12-27 02:37:02 +03:00
|
|
|
name: "object with strings",
|
2022-11-16 05:11:19 +03:00
|
|
|
args: args{
|
2022-12-27 02:37:02 +03:00
|
|
|
input: `{
|
|
|
|
"hello": "world",
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
want: std.ObjectNode{
|
|
|
|
"hello": &std.StringNode{Value: "world"},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "complex object",
|
|
|
|
args: args{
|
|
|
|
input: `{
|
|
|
|
"string key": "string value",
|
|
|
|
"number key": 123.321,
|
|
|
|
"bool key": true,
|
|
|
|
"object": {
|
|
|
|
"one": "two",
|
|
|
|
"object 2": {
|
|
|
|
"three": "four"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"array": [
|
|
|
|
"one",
|
|
|
|
2,
|
|
|
|
true,
|
|
|
|
null,
|
2022-11-16 05:11:19 +03:00
|
|
|
{
|
2022-12-27 02:37:02 +03:00
|
|
|
"five": "six"
|
2022-11-16 05:11:19 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}`,
|
|
|
|
},
|
2022-12-27 02:37:02 +03:00
|
|
|
want: std.ObjectNode{
|
|
|
|
"string key": j.MustNode("string value"),
|
|
|
|
"number key": j.MustNode(123.321),
|
|
|
|
"bool key": j.MustNode(true),
|
|
|
|
"object": std.ObjectNode{
|
|
|
|
"one": j.MustNode("two"),
|
|
|
|
"object 2": std.ObjectNode{
|
|
|
|
"three": j.MustNode("four"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"array": &std.ArrayNode{
|
|
|
|
j.MustNode("one"),
|
|
|
|
j.MustNode(2),
|
|
|
|
j.MustNode(true),
|
|
|
|
j.MustNode(nil),
|
|
|
|
std.ObjectNode{
|
|
|
|
"five": j.MustNode("six"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-16 05:11:19 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-12-27 02:37:02 +03:00
|
|
|
got, err := j.Unmarshal(tt.args.input)
|
2022-11-16 05:11:19 +03:00
|
|
|
if (err != nil) != tt.wantErr {
|
2022-12-27 02:37:02 +03:00
|
|
|
t.Errorf("JSON.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
|
2022-11-16 05:11:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2022-12-27 02:37:02 +03:00
|
|
|
t.Errorf("JSON.Unmarshal() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSON_Marshal(t *testing.T) {
|
|
|
|
j := &json.JSON{
|
|
|
|
Factory: std.Factory,
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
n json.Node
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "complex object",
|
|
|
|
args: args{
|
|
|
|
n: std.ObjectNode{
|
|
|
|
"string key": j.MustNode("string value"),
|
|
|
|
"number key": j.MustNode(123.321),
|
|
|
|
"bool key": j.MustNode(true),
|
|
|
|
"object": std.ObjectNode{
|
|
|
|
"one": j.MustNode("two"),
|
|
|
|
"object 2": std.ObjectNode{
|
|
|
|
"three": j.MustNode("four"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"array": &std.ArrayNode{
|
|
|
|
j.MustNode("one"),
|
|
|
|
j.MustNode(2),
|
|
|
|
j.MustNode(true),
|
|
|
|
j.MustNode(nil),
|
|
|
|
std.ObjectNode{
|
|
|
|
"five": j.MustNode("six"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: `{"string key":"string value","number key":123.321,"bool key":true,"object":{"one":"two","object 2":{"three":"four"}},"array":["one",2,true,null,{"five":"six"}]}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := j.Marshal(tt.args.n); len(got) != len(tt.want) {
|
|
|
|
t.Errorf("JSON.Marshal() = %v, want %v", got, tt.want)
|
2022-11-16 05:11:19 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|