Debian Codegraph @

formatter/delta_test.go

raw
1 package formatter_test
2
3 import (
4 . "github.com/yudai/gojsondiff/formatter"
5
6 . "github.com/onsi/ginkgo"
7 . "github.com/onsi/gomega"
8 . "github.com/yudai/gojsondiff/tests"
9
10 diff "github.com/yudai/gojsondiff"
11 )
12
13 var _ = Describe("Delta", func() {
14 Describe("Foramt", func() {
15 var (
16 a, b map[string]interface{}
17 )
18
19 Context("There are no difference between the two JSON strings", func() {
20 It("Returns empty JSON", func() {
21 a = LoadFixture("../FIXTURES/base.json")
22 b = LoadFixture("../FIXTURES/base.json")
23
24 diff := diff.New().CompareObjects(a, b)
25
26 f := NewDeltaFormatter()
27 deltaJson, err := f.FormatAsJson(diff)
28 Expect(err).To(BeNil())
29 Expect(deltaJson).To(Equal(map[string]interface{}{}))
30 })
31 })
32
33 Context("There are some values modified", func() {
34 It("Detects changes", func() {
35 a = LoadFixture("../FIXTURES/base.json")
36 b = LoadFixture("../FIXTURES/base_changed.json")
37
38 diff := diff.New().CompareObjects(a, b)
39
40 f := NewDeltaFormatter()
41 deltaJson, err := f.FormatAsJson(diff)
42 Expect(err).To(BeNil())
43 Expect(deltaJson).To(Equal(
44 map[string]interface{}{
45 "arr": map[string]interface{}{
46 "_t": "a",
47 "2": map[string]interface{}{
48 "str": []interface{}{
49 "pek3f", "changed",
50 },
51 },
52 "3": map[string]interface{}{
53 "_t": "a",
54 "1": []interface{}{
55 "1", "changed",
56 },
57 },
58 },
59 "null": []interface{}{nil, 0, 0},
60 "obj": map[string]interface{}{
61 "arr": map[string]interface{}{
62 "_t": "a",
63 "2": map[string]interface{}{
64 "str": []interface{}{
65 "eafeb", "changed",
66 },
67 },
68 },
69 "num": []interface{}{
70 float64(19),
71 0,
72 0,
73 },
74 "obj": map[string]interface{}{
75 "num": []interface{}{
76 float64(14),
77 float64(9999),
78 },
79 "str": []interface{}{
80 "efj3",
81 "changed",
82 },
83 },
84 "new": []interface{}{
85 "added",
86 },
87 },
88 },
89 ))
90 })
91 })
92
93 Context("There are long texts", func() {
94 It("Returns empty JSON", func() {
95 a = LoadFixture("../FIXTURES/long_text_from.json")
96 b = LoadFixture("../FIXTURES/long_text_to.json")
97
98 diff := diff.New().CompareObjects(a, b)
99
100 f := NewDeltaFormatter()
101 deltaJson, err := f.FormatAsJson(diff)
102 Expect(err).To(BeNil())
103 Expect(deltaJson).To(Equal(
104 map[string]interface{}{
105 "str": []interface{}{
106 "@@ -27,14 +27,15 @@\n 40fj\n-q048hf\n+nafefea\n bgvz\n@@ -55,25 +55,20 @@\n m48q\n+a3\n 9p8qfh\n-qn4gbqqq4qp\n+nafe\n 94hq\n",
107 0,
108 2,
109 },
110 },
111 ))
112 })
113 })
114
115 })
116 })