-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_test.go
109 lines (80 loc) · 3.73 KB
/
object_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
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
109
package tox
import (
"github.com/davecgh/go-spew/spew"
"math"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
func TestReportSuite(t *testing.T) {
suite.Run(t, new(ReportSuite))
}
type ReportSuite struct {
suite.Suite
}
func (s *ReportSuite) SetupSuite() {
}
func (s *ReportSuite) TestSimpleFlatten() {
o := Object{"a": "abc", "b": 456, "c": Object{"d": 123, "e": Object{"f": 456}}}
s.Equal(Object{"a": "abc", "b": 456, "c.d": 123, "c.e.f": 456}, o.Flatten("."))
}
func (s *ReportSuite) TestArrayFlatten() {
o := Object{"a": "abc", "b": 456, "c": Object{"d": 123, "e": Object{"f": 456}, "g": []string{"h", "i", "j"}}}
s.Equal(Object{"a": "abc", "b": 456, "c.d": 123, "c.e.f": 456, "c.g[0]": "h", "c.g[1]": "i", "c.g[2]": "j"}, o.Flatten("."))
}
func (s *ReportSuite) TestDiffMod() {
old := Object{"a": "abc", "b": 456, "c": Object{"d": 123, "e": Object{"f": 456}}}
new2 := Object{"a": "abc", "b": 456, "c": Object{"d": 555, "e": Object{"f": 456}}}
s.Equal(ObjectDiff{Added: nil, Deleted: nil, Modified: map[string]FieldDiff{"c/d": {Old: 123, New: 555}}}, old.Diff(new2))
}
func (s *ReportSuite) TestDiff() {
old := Object{"a": "abc", "b": 456, "c": Object{"d": 123, "e": Object{"f": 456}}}
new2 := Object{"a": "abc", "c": Object{"d": 555, "e": Object{"f": 456, "g": 789}}}
s.Equal(ObjectDiff{Added: Object{"c/e/g": 789}, Deleted: Object{"b": 456}, Modified: map[string]FieldDiff{"c/d": {Old: 123, New: 555}}}, old.Diff(new2))
}
type Foo struct {
A string `json:"a,omitempty"`
B float64 `json:"b,omitempty"`
}
type FooUnexported struct {
A string `json:"a,omitempty"`
b float64
C Foo `json:"c,omitempty"`
}
func (s *ReportSuite) TestNaN() {
old := Object{"a": "abc", "b": math.NaN(), "c": Object{"d": 123, "e": math.NaN()}}
old.RemoveNaN()
s.Equal("{\"a\":\"abc\",\"c\":{\"d\":123}}", old.JsonString(false))
old = Object{"a": "abc", "b": math.NaN(), "c": &Foo{A: "abc", B: math.NaN()}}
old.RemoveNaN()
s.Equal("{\"a\":\"abc\",\"c\":{\"a\":\"abc\"}}", old.JsonString(false))
old = Object{"a": "abc", "b": math.NaN(), "c": Object{"d": &Foo{A: "abc", B: math.NaN()}}}
old.RemoveNaN()
s.Equal("{\"a\":\"abc\",\"c\":{\"d\":{\"a\":\"abc\"}}}", old.JsonString(false))
}
func (s *ReportSuite) TestBinary() {
old := Object{"a": "abc", "b": []byte{0x01, 0x02, 0x03, 0x04}}
n := NewObject(map[string]any{"a": "abc", "b": []byte{0x01, 0x02, 0x03, 0x04}})
n.Equals(old)
spew.Dump(old, n)
s.Equal(true, n.Equals(old))
}
func (s *ReportSuite) TestStructs() {
old := NewObject(map[string]interface{}{"a": "abc", "b": 123})
old.Set("c", Foo{A: "abc", B: 456})
s.Equal("{\"a\":\"abc\",\"b\":123,\"c\":{\"a\":\"abc\",\"b\":456}}", old.JsonString(false))
old = NewObject(map[string]interface{}{"a": "abc", "b": 123})
old.Set("c", FooUnexported{A: "abc", b: 456})
s.Equal("{\"a\":\"abc\",\"b\":123,\"c\":{\"a\":\"abc\",\"c\":{}}}", old.JsonString(false))
old = Object{"a": "abc", "b": 123, "c": FooUnexported{A: "abc", b: 456}}
s.Equal("{\"a\":\"abc\",\"b\":123,\"c\":{\"a\":\"abc\",\"c\":{}}}", old.JsonString(false))
old = Object{"a": "abc", "b": 123, "c": &FooUnexported{A: "abc", b: 456}}
s.Equal("{\"a\":\"abc\",\"b\":123,\"c\":{\"a\":\"abc\",\"c\":{}}}", old.JsonString(false))
old = Object{"a": "abc", "b": 123, "c": &FooUnexported{A: "abc", b: 456, C: Foo{}}}
s.Equal("{\"a\":\"abc\",\"b\":123,\"c\":{\"a\":\"abc\",\"c\":{}}}", old.JsonString(false))
old = Object{"a": "abc", "b": 123, "c": &FooUnexported{A: "abc", b: 456, C: Foo{A: "abc", B: 456}}}
s.Equal("{\"a\":\"abc\",\"b\":123,\"c\":{\"a\":\"abc\",\"c\":{\"a\":\"abc\",\"b\":456}}}", old.JsonString(false))
old = Object{"a": "abc", "b": time.Now()}
old.RemoveNaN()
s.Equal("{\"a\":\"abc\",\"b\":123,\"c\":{\"a\":\"abc\",\"c\":{\"a\":\"abc\",\"b\":456}}}", old.JsonString(false))
}