Skip to content

Commit 237ec94

Browse files
authored
Merge pull request #7 from nomtek/feature/keys-arbitrary-order
Feature/keys arbitrary order
2 parents 52bf27b + 8139322 commit 237ec94

6 files changed

Lines changed: 58 additions & 3 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
rspec-json_api (1.2.1)
4+
rspec-json_api (1.2.2)
55
activesupport (>= 6.1.4.1)
66
rails (>= 6.1.4.1)
77
rspec-rails (>= 5.0.2)

lib/extentions/array.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
class Array
4+
def deep_sort
5+
map { |element| element.is_a?(Array) ? element.deep_sort : element }
6+
.sort_by { |el| el.is_a?(Array) ? el.first.to_s : el.to_s }
7+
end
8+
end

lib/rspec/json_api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
# Load extentions
1313
require "extentions/hash"
14+
require "extentions/array"
1415

1516
# Load matchers
1617
require "rspec/json_api/matchers"

lib/rspec/json_api/matchers/match_json_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def matches?(actual)
2121
RSpec::JsonApi::CompareArray.compare(actual, expected)
2222
else
2323
# Compare actual and expected schema
24-
return false unless actual.deep_keys == expected.deep_keys
24+
return false unless actual.deep_keys.deep_sort == expected.deep_keys.deep_sort
2525

2626
RSpec::JsonApi::CompareHash.compare(actual, expected)
2727
end

lib/rspec/json_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module RSpec
44
module JsonApi
5-
VERSION = "1.2.1"
5+
VERSION = "1.2.2"
66
end
77
end

spec/rspec/json_api/matchers/match_json_schema_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,52 @@
217217
end
218218
end
219219

220+
context 'when keys are in arbitrary order' do
221+
let(:expected) do
222+
{
223+
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4f",
224+
name: "Caroline Mayer",
225+
age: 25,
226+
children: [
227+
{
228+
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4a",
229+
name: "Webster Medina",
230+
age: 2
231+
},
232+
{
233+
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4b",
234+
name: "Roy Mcdaniel",
235+
age: 3
236+
}
237+
]
238+
}
239+
end
240+
241+
context "when correct match" do
242+
let(:actual) do
243+
{
244+
name: "Caroline Mayer",
245+
age: 25,
246+
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4f",
247+
children: [
248+
{
249+
name: "Webster Medina",
250+
age: 2,
251+
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4a",
252+
},
253+
{
254+
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4b",
255+
name: "Roy Mcdaniel",
256+
age: 3
257+
}
258+
]
259+
}.to_json
260+
end
261+
262+
include_examples "correct-match"
263+
end
264+
end
265+
220266
context "when data typed given" do
221267
let(:expected) do
222268
{

0 commit comments

Comments
 (0)