File: gojsondiff_test.go

package info (click to toggle)
golang-github-yudai-gojsondiff 1.0.0%2Bgit20180504.0525c87-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 220 kB
  • sloc: makefile: 13
file content (140 lines) | stat: -rw-r--r-- 3,702 bytes parent folder | download | duplicates (2)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package gojsondiff_test

import (
	. "github.com/yudai/gojsondiff"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/yudai/gojsondiff/tests"

	"io/ioutil"
)

var _ = Describe("Gojsondiff", func() {
	Describe("Differ", func() {
		Describe("CompareObjects", func() {

			var (
				a, b   map[string]interface{}
				differ *Differ
			)

			BeforeEach(func() {
				differ = New()
			})

			Context("There are no difference between the two JSON strings", func() {
				It("Detects nothing", func() {
					a = LoadFixture("FIXTURES/base.json")
					b = LoadFixture("FIXTURES/base.json")

					diff := differ.CompareObjects(a, b)
					Expect(diff.Modified()).To(BeFalse())
				})
			})

			Context("There are some values modified", func() {
				It("Detects changes", func() {
					a = LoadFixture("FIXTURES/base.json")
					b = LoadFixture("FIXTURES/base_changed.json")

					diff := differ.CompareObjects(a, b)
					Expect(diff.Modified()).To(BeTrue())
					differ.ApplyPatch(a, diff)
					Expect(a).To(Equal(LoadFixture("FIXTURES/base_changed.json")))
				})
			})

			Context("There are values only types are changed", func() {
				It("Detects changed types", func() {
					a := LoadFixture("FIXTURES/changed_types_from.json")
					b := LoadFixture("FIXTURES/changed_types_to.json")

					diff := differ.CompareObjects(a, b)
					differ.ApplyPatch(a, diff)
					Expect(a).To(Equal(LoadFixture("FIXTURES/changed_types_to.json")))
				})
			})

			Context("There is a moved item in an array", func() {
				It("Detects changed types", func() {
					a := LoadFixture("FIXTURES/move_from.json")
					b := LoadFixture("FIXTURES/move_to.json")

					diff := differ.CompareObjects(a, b)
					Expect(diff.Modified()).To(BeTrue())
					differ.ApplyPatch(a, diff)
					Expect(a).To(Equal(LoadFixture("FIXTURES/move_to.json")))
				})
			})

			Context("There are long text diff", func() {
				It("Detects changes", func() {
					a = LoadFixture("FIXTURES/long_text_from.json")
					b = LoadFixture("FIXTURES/long_text_to.json")

					diff := differ.CompareObjects(a, b)
					Expect(diff.Modified()).To(BeTrue())
					differ.ApplyPatch(a, diff)
					Expect(a).To(Equal(LoadFixture("FIXTURES/long_text_to.json")))
				})
			})
		})
		Describe("CompareArrays", func() {

			var (
				a, b   []interface{}
				differ *Differ
			)

			BeforeEach(func() {
				differ = New()
			})

			Context("There are no difference between the two JSON strings", func() {
				It("Detects nothing", func() {
					a = LoadFixtureAsArray("FIXTURES/array.json")
					b = LoadFixtureAsArray("FIXTURES/array.json")

					diff := differ.CompareArrays(a, b)
					Expect(diff.Modified()).To(BeFalse())
				})
			})

			Context("There are some values modified", func() {
				It("Detects changes", func() {
					a = LoadFixtureAsArray("FIXTURES/array.json")
					b = LoadFixtureAsArray("FIXTURES/array_changed.json")

					diff := differ.CompareArrays(a, b)
					Expect(diff.Modified()).To(BeTrue())
					Expect(len(diff.Deltas())).To(Equal(1))
				})
			})
		})
		Describe("Compare", func() {
			Context("There are some values modified", func() {
				It("Detects changes", func() {
					aFile := "FIXTURES/base.json"
					bFile := "FIXTURES/base_changed.json"
					aObj := LoadFixture(aFile)
					bObj := LoadFixture(bFile)

					differ := New()

					diffObj := differ.CompareObjects(aObj, bObj)
					Expect(diffObj.Modified()).To(BeTrue())

					aStr, err := ioutil.ReadFile(aFile)
					Expect(err).To(BeNil())
					bStr, err := ioutil.ReadFile(bFile)
					Expect(err).To(BeNil())

					diffStr, err := differ.Compare(aStr, bStr)
					Expect(err).To(BeNil())
					Expect(diffStr).To(Equal(diffObj))
				})
			})
		})
	})
})