Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/keys arbitrary order #7

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rspec-json_api (1.2.1)
rspec-json_api (1.2.2)
activesupport (>= 6.1.4.1)
rails (>= 6.1.4.1)
rspec-rails (>= 5.0.2)
Expand Down
8 changes: 8 additions & 0 deletions lib/extentions/array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class Array
def deep_sort
map { |element| element.is_a?(Array) ? element.deep_sort : element }
.sort_by { |el| el.is_a?(Array) ? el.first.to_s : el.to_s }
end
end
1 change: 1 addition & 0 deletions lib/rspec/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Load extentions
require "extentions/hash"
require "extentions/array"

# Load matchers
require "rspec/json_api/matchers"
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/json_api/matchers/match_json_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def matches?(actual)
RSpec::JsonApi::CompareArray.compare(actual, expected)
else
# Compare actual and expected schema
return false unless actual.deep_keys == expected.deep_keys
return false unless actual.deep_keys.deep_sort == expected.deep_keys.deep_sort

RSpec::JsonApi::CompareHash.compare(actual, expected)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/json_api/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module RSpec
module JsonApi
VERSION = "1.2.1"
VERSION = "1.2.2"
end
end
46 changes: 46 additions & 0 deletions spec/rspec/json_api/matchers/match_json_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,52 @@
end
end

context 'when keys are in arbitrary order' do
let(:expected) do
{
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4f",
name: "Caroline Mayer",
age: 25,
children: [
{
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4a",
name: "Webster Medina",
age: 2
},
{
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4b",
name: "Roy Mcdaniel",
age: 3
}
]
}
end

context "when correct match" do
let(:actual) do
{
name: "Caroline Mayer",
age: 25,
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4f",
children: [
{
name: "Webster Medina",
age: 2,
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4a",
},
{
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4b",
name: "Roy Mcdaniel",
age: 3
}
]
}.to_json
end

include_examples "correct-match"
end
end

context "when data typed given" do
let(:expected) do
{
Expand Down
Loading