-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnull_filter_test.go
48 lines (39 loc) · 1.42 KB
/
null_filter_test.go
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
package jsontools_test
import (
"testing"
"github.com/WqyJh/jsontools"
"github.com/stretchr/testify/require"
)
func TestJsonNullFilter(t *testing.T) {
cases := []struct {
input string
expected string
}{
{`{"a":null}`, `{}`},
{`{"a":null,"b":"null"}`, `{"b":"null"}`},
{`{"a":1 , "b":null}`, `{"a":1}`},
{`{"a":null , "b":2}`, `{"b":2}`},
{`{"a":1,"b":2,"c":null}`, `{"a":1,"b":2}`},
{`{"a":1, "b":null,"c":3}`, `{"a":1,"c":3}`},
{`{ "a":null,"b":2,"c":3}`, `{"b":2,"c":3}`},
{`{"a":1,"b":null,"c":null}`, `{"a":1}`},
{`{"a":null,"b":2, "c":null}`, `{"b":2}`},
{`{"a":null,"b":null,"c":3}`, `{"c":3}`},
{`{"a":null,"b":null,"c":null}`, `{}`},
{`{"a":null,"b":null,"c":[1,2,3]}`, `{"c":[1,2,3]}`},
{`{"a":null,"b":[1,2,3],"c":null}`, `{"b":[1,2,3]}`},
{`{"a":[1,2,3] , "b" :null,"c":null }`, `{"a":[1,2,3]}`},
{`{"a":null,"b":null,"c":[1,2,{ "a":1.23,"b":null ,"c" :"null"}]}`, `{"c":[1,2,{"a":1.23,"c":"null"}]}`},
}
for i, c := range cases {
dst, err := jsontools.NewJsonNullFilter(false).Filter([]byte(c.input))
require.NoError(t, err)
require.Equal(t, c.expected, string(dst), "case %d: %s", i, c.input)
// inplace
input := []byte(c.input) // cloned
dst, err = jsontools.NewJsonNullFilter(true).Filter(input)
require.NoError(t, err)
require.Equal(t, c.expected, string(dst), "case %d: dst:%s src:%s", i, dst, input)
t.Logf("inplace case %d: dst:%s src:%s", i, dst, input)
}
}