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
|
// Copyright 2021 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package config_test
import (
"bytes"
"testing"
"github.com/google/syzkaller/pkg/config"
)
func TestMergeJSONs(t *testing.T) {
tests := []struct {
left string
right string
result string
}{
{
`{"a":1,"b":2}`,
`{"b":3,"c":4}`,
`{"a":1,"b":3,"c":4}`,
},
{
`{"a":1,"b":{"c":{"d":"nested string","e":"another string"}}}`,
`{"b":{"c":{"d":12345}}}`,
`{"a":1,"b":{"c":{"d":12345,"e":"another string"}}}`,
},
{
`{}`,
`{"a":{"b":{"c":0}}}`,
`{"a":{"b":{"c":0}}}`,
},
{
`{"a":{"b":{"c":0}}}`,
``,
`{"a":{"b":{"c":0}}}`,
},
}
for _, test := range tests {
res, err := config.MergeJSONs([]byte(test.left), []byte(test.right))
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !bytes.Equal(res, []byte(test.result)) {
t.Errorf("expected %s, got %s", test.result, res)
}
}
}
func TestPatchJSON(t *testing.T) {
tests := []struct {
left string
patch map[string]any
result string
}{
{
`{"a":1,"b":2}`,
map[string]any{"b": "string val"},
`{"a":1,"b":"string val"}`,
},
{
`{"a":1,"b":2}`,
map[string]any{
"a": map[string]any{
"b": map[string]any{
"c": 5,
},
},
},
`{"a":{"b":{"c":5}},"b":2}`,
},
{
`{}`,
map[string]any{
"a": map[string]any{
"b": map[string]any{
"c": 0,
},
},
},
`{"a":{"b":{"c":0}}}`,
},
}
for _, test := range tests {
res, err := config.PatchJSON([]byte(test.left), test.patch)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !bytes.Equal(res, []byte(test.result)) {
t.Errorf("expected %s, got %s", test.result, res)
}
}
}
|