-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_test.go
93 lines (74 loc) · 2.29 KB
/
diff_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
// Tideland Go Dynamic JSON - Unit Tests
//
// Copyright (C) 2019-2023 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package dynaj_test
//--------------------
// IMPORTS
//--------------------
import (
"testing"
"tideland.dev/go/audit/asserts"
"tideland.dev/go/dynaj"
)
//--------------------
// TESTS
//--------------------
// TestCompare tests comparing two documents.
func TestCompare(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
first, _ := createDocument(assert)
second := createCompareDocument(assert)
firstDoc, err := dynaj.Unmarshal(first)
assert.NoError(err)
secondDoc, err := dynaj.Unmarshal(second)
assert.NoError(err)
diff, err := dynaj.Compare(first, first)
assert.NoError(err)
assert.Length(diff.Differences(), 0)
diff, err = dynaj.Compare(first, second)
assert.NoError(err)
assert.Length(diff.Differences(), 13)
diff, err = dynaj.CompareDocuments(firstDoc, secondDoc)
assert.NoError(err)
assert.Length(diff.Differences(), 13)
for _, path := range diff.Differences() {
fv, sv := diff.DifferenceAt(path)
fvs := fv.AsString("<first undefined>")
svs := sv.AsString("<second undefined>")
assert.Different(fvs, svs, path)
}
first, err = diff.FirstDocument().MarshalJSON()
assert.NoError(err)
second, err = diff.SecondDocument().MarshalJSON()
assert.NoError(err)
diff, err = dynaj.Compare(first, second)
assert.NoError(err)
assert.Length(diff.Differences(), 13)
// Special case of empty arrays, objects, and null.
first = []byte(`{}`)
second = []byte(`{"a":[],"b":{},"c":null}`)
sdocParsed, err := dynaj.Unmarshal(second)
assert.NoError(err)
sdocMarshalled, err := sdocParsed.MarshalJSON()
assert.NoError(err)
assert.Equal(string(sdocMarshalled), string(second))
diff, err = dynaj.Compare(first, second)
assert.NoError(err)
assert.Length(diff.Differences(), 4)
first = []byte(`[]`)
diff, err = dynaj.Compare(first, second)
assert.NoError(err)
assert.Length(diff.Differences(), 4)
first = []byte(`["A", "B", "C"]`)
diff, err = dynaj.Compare(first, second)
assert.NoError(err)
assert.Length(diff.Differences(), 6)
first = []byte(`"foo"`)
diff, err = dynaj.Compare(first, second)
assert.NoError(err)
assert.Length(diff.Differences(), 4)
}
// EOF