-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_test.go
81 lines (76 loc) · 1.4 KB
/
sort_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
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
package main
import (
"encoding/json"
"testing"
)
const sampleJSON0 = `{
"b": [[2, 0], "c", 1, {"a": 0}, 0.23, true, null, false],
"a": 1,
"d": [
{
"a": 1
},
1
],
"c": {
"b": 2,
"a": false
}
}`
const sampleJSON1 = `{
"a": [
{ "a": 1},
false,
"b",
1,
null,
"a",
[1, false],
true,
0.5
]
}`
const sampleJSON2 = `{
"c": false,
"a": 1,
"b": null
}`
func TestSort(t *testing.T) {
tests := []struct {
value string
name string
expected string
}{
{
name: "sampleJSON0",
value: sampleJSON0,
expected: `{"a":1,"b":[false,true,0.23,1,"c",[0,2],{"a":0},null],"c":{"a":false,"b":2},"d":[1,{"a":1}]}`,
},
{
name: "sampleJSON1",
value: sampleJSON1,
expected: `{"a":[false,true,0.5,1,"a","b",[false,1],{"a":1},null]}`,
},
{
name: "sampleJSON2",
value: sampleJSON2,
expected: `{"a":1,"b":null,"c":false}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var data map[string]any
if err := json.Unmarshal([]byte(tt.value), &data); err != nil {
t.Fatalf("Unmarshal() error = %v", err)
}
sortedData, err := NewSortedJSON(data).MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON() error = %v", err)
}
res := string(sortedData)
if string(sortedData) != tt.expected {
t.Errorf("expected: %s, but: %s", tt.expected, res)
}
})
}
}