diff --git a/FIXTURES/array.json b/FIXTURES/array.json new file mode 100644 index 0000000..050c682 --- /dev/null +++ b/FIXTURES/array.json @@ -0,0 +1,10 @@ +[ + 1, + 2, + 6, + { + "str": "bcded", + "arr": [ "a", "b", 37.8 ] + }, + "string" +] diff --git a/FIXTURES/array_changed.json b/FIXTURES/array_changed.json new file mode 100644 index 0000000..83d1d74 --- /dev/null +++ b/FIXTURES/array_changed.json @@ -0,0 +1,10 @@ +[ + 1, + 2, + 6, + { + "str": "bcdef", + "arr": [ "a", "b", 37.8 ] + }, + "string" +] diff --git a/gojsondiff_test.go b/gojsondiff_test.go index aa178f6..cc62ff6 100644 --- a/gojsondiff_test.go +++ b/gojsondiff_test.go @@ -80,6 +80,38 @@ var _ = Describe("Gojsondiff", func() { }) }) }) + 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() { diff --git a/tests/helper.go b/tests/helper.go index 5dcd726..dd3474a 100644 --- a/tests/helper.go +++ b/tests/helper.go @@ -19,3 +19,16 @@ func LoadFixture(file string) map[string]interface{} { } return result } + +func LoadFixtureAsArray(file string) []interface{} { + content, err := ioutil.ReadFile(file) + if err != nil { + Fail("Fixture file '" + file + "' not found.") + } + var result []interface{} + err = json.Unmarshal(content, &result) + if err != nil { + Fail("Unmarshaling JSON of '" + file + "' failed: " + err.Error()) + } + return result +}