Debian Codegraph @

gojsondiff_test.go

raw
1 package gojsondiff_test
2
3 import (
4 . "github.com/yudai/gojsondiff"
5
6 . "github.com/onsi/ginkgo"
7 . "github.com/onsi/gomega"
8 . "github.com/yudai/gojsondiff/tests"
9
10 "io/ioutil"
11 )
12
13 var _ = Describe("Gojsondiff", func() {
14 Describe("Differ", func() {
15 Describe("CompareObjects", func() {
16
17 var (
18 a, b map[string]interface{}
19 differ *Differ
20 )
21
22 BeforeEach(func() {
23 differ = New()
24 })
25
26 Context("There are no difference between the two JSON strings", func() {
27 It("Detects nothing", func() {
28 a = LoadFixture("FIXTURES/base.json")
29 b = LoadFixture("FIXTURES/base.json")
30
31 diff := differ.CompareObjects(a, b)
32 Expect(diff.Modified()).To(BeFalse())
33 })
34 })
35
36 Context("There are some values modified", func() {
37 It("Detects changes", func() {
38 a = LoadFixture("FIXTURES/base.json")
39 b = LoadFixture("FIXTURES/base_changed.json")
40
41 diff := differ.CompareObjects(a, b)
42 Expect(diff.Modified()).To(BeTrue())
43 differ.ApplyPatch(a, diff)
44 Expect(a).To(Equal(LoadFixture("FIXTURES/base_changed.json")))
45 })
46 })
47
48 Context("There are values only types are changed", func() {
49 It("Detects changed types", func() {
50 a := LoadFixture("FIXTURES/changed_types_from.json")
51 b := LoadFixture("FIXTURES/changed_types_to.json")
52
53 diff := differ.CompareObjects(a, b)
54 differ.ApplyPatch(a, diff)
55 Expect(a).To(Equal(LoadFixture("FIXTURES/changed_types_to.json")))
56 })
57 })
58
59 Context("There is a moved item in an array", func() {
60 It("Detects changed types", func() {
61 a := LoadFixture("FIXTURES/move_from.json")
62 b := LoadFixture("FIXTURES/move_to.json")
63
64 diff := differ.CompareObjects(a, b)
65 Expect(diff.Modified()).To(BeTrue())
66 differ.ApplyPatch(a, diff)
67 Expect(a).To(Equal(LoadFixture("FIXTURES/move_to.json")))
68 })
69 })
70
71 Context("There are long text diff", func() {
72 It("Detects changes", func() {
73 a = LoadFixture("FIXTURES/long_text_from.json")
74 b = LoadFixture("FIXTURES/long_text_to.json")
75
76 diff := differ.CompareObjects(a, b)
77 Expect(diff.Modified()).To(BeTrue())
78 differ.ApplyPatch(a, diff)
79 Expect(a).To(Equal(LoadFixture("FIXTURES/long_text_to.json")))
80 })
81 })
82 })
83 Describe("CompareArrays", func() {
84
85 var (
86 a, b []interface{}
87 differ *Differ
88 )
89
90 BeforeEach(func() {
91 differ = New()
92 })
93
94 Context("There are no difference between the two JSON strings", func() {
95 It("Detects nothing", func() {
96 a = LoadFixtureAsArray("FIXTURES/array.json")
97 b = LoadFixtureAsArray("FIXTURES/array.json")
98
99 diff := differ.CompareArrays(a, b)
100 Expect(diff.Modified()).To(BeFalse())
101 })
102 })
103
104 Context("There are some values modified", func() {
105 It("Detects changes", func() {
106 a = LoadFixtureAsArray("FIXTURES/array.json")
107 b = LoadFixtureAsArray("FIXTURES/array_changed.json")
108
109 diff := differ.CompareArrays(a, b)
110 Expect(diff.Modified()).To(BeTrue())
111 Expect(len(diff.Deltas())).To(Equal(1))
112 })
113 })
114 })
115 Describe("Compare", func() {
116 Context("There are some values modified", func() {
117 It("Detects changes", func() {
118 aFile := "FIXTURES/base.json"
119 bFile := "FIXTURES/base_changed.json"
120 aObj := LoadFixture(aFile)
121 bObj := LoadFixture(bFile)
122
123 differ := New()
124
125 diffObj := differ.CompareObjects(aObj, bObj)
126 Expect(diffObj.Modified()).To(BeTrue())
127
128 aStr, err := ioutil.ReadFile(aFile)
129 Expect(err).To(BeNil())
130 bStr, err := ioutil.ReadFile(bFile)
131 Expect(err).To(BeNil())
132
133 diffStr, err := differ.Compare(aStr, bStr)
134 Expect(err).To(BeNil())
135 Expect(diffStr).To(Equal(diffObj))
136 })
137 })
138 })
139 })
140 })