Skip to content

Commit

Permalink
Added CompareArrays func for comparing JSON arrays
Browse files Browse the repository at this point in the history
As well as CompareSlices for unmarshaling slices and LoadFixtureAsArray for unmarshaling JSON array from file into []interface{}

Signed-off-by: Alexey Knyshev <alexey.knyshev@gmail.com>
  • Loading branch information
alexeyknyshev authored and Iwasaki Yudai committed Nov 29, 2016
1 parent 4d06d22 commit 4e533b0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
10 changes: 10 additions & 0 deletions FIXTURES/array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
1,
2,
6,
{
"str": "bcded",
"arr": [ "a", "b", 37.8 ]
},
"string"
]
10 changes: 10 additions & 0 deletions FIXTURES/array_changed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
1,
2,
6,
{
"str": "bcdef",
"arr": [ "a", "b", 37.8 ]
},
"string"
]
32 changes: 32 additions & 0 deletions gojsondiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 13 additions & 0 deletions tests/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4e533b0

Please sign in to comment.