Skip to content
Closed
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
10 changes: 9 additions & 1 deletion lib/json_spec/exclusion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ def exclude_keys(ruby)
end

def exclude_key?(key)
excluded_keys.include?(key)
if only_keys.empty?
excluded_keys.include?(key)
else
!only_keys.include?(key)
end
end

def excluded_keys
@excluded_keys ||= Set.new(JsonSpec.excluded_keys)
end

def only_keys
@only_keys ||= Set.new
end
end
end
5 changes: 5 additions & 0 deletions lib/json_spec/matchers/be_json_eql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def including(*keys)
self
end

def only(*keys)
only_keys.merge(keys.map(&:to_s))
self
end

def failure_message_for_should
message_with_path("Expected equivalent JSON")
end
Expand Down
5 changes: 5 additions & 0 deletions lib/json_spec/matchers/include_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def including(*keys)
self
end

def only(*keys)
only_keys.merge(keys.map(&:to_s))
self
end

def failure_message_for_should
message_with_path("Expected included JSON")
end
Expand Down
5 changes: 5 additions & 0 deletions spec/json_spec/matchers/be_json_eql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
%({"id":1,"json":"spec"}).should_not be_json_eql(%({"id":2,"json":"different"})).including(:id, :json)
end

it 'matching only the specified keys' do
JsonSpec.excluded_keys = []
%({"id":1,"json":"spec"}).should be_json_eql(%({"id":1})).only(:id)
end

it "provides a description message" do
matcher = be_json_eql(%({"id":2,"json":"spec"}))
matcher.matches?(%({"id":1,"json":"spec"}))
Expand Down
4 changes: 4 additions & 0 deletions spec/json_spec/matchers/include_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
%([{"id":1,"two":3}]).should include_json(%({"two":3}))
end

it 'matching only the specified keys' do
%([{"id":1,"json":"spec"}]).should include_json(%({"id":1})).only(:id)
end

it "provides a description message" do
matcher = include_json(%({"json":"spec"}))
matcher.matches?(%({"id":1,"json":"spec"}))
Expand Down