Skip to content
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
23 changes: 16 additions & 7 deletions lib/json_spec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,36 @@ def scrub(json, path = nil)
include JsonSpec::Helpers

match do |json|
parse_json(json, @path).is_a?(klass)
@json = json
actual.is_a?(klass)
end

chain :at_path do |path|
@path = path
end

failure_message_for_should do
message = "Expected JSON value type to be #{klass}"
message = "Expected JSON value type to be #{klass}, got #{actual.class}"
message << %( at path "#{@path}") if @path
message
end

failure_message_for_should_not do
message = "Expected JSON value type to not be #{klass}"
message = "Expected JSON value type to not be #{klass}, got #{actual.class}"
message << %( at path "#{@path}") if @path
message
end

def actual
parse_json(@json, @path)
end
end

RSpec::Matchers.define :have_json_size do |expected_size|
include JsonSpec::Helpers

match do |json|
ruby = parse_json(json, @path)
actual_size = ruby.is_a?(Enumerable) ? ruby.size : 1
@json = json
actual_size == expected_size
end

Expand All @@ -139,14 +143,19 @@ def scrub(json, path = nil)
end

failure_message_for_should do
message = "Expected JSON value size to be #{expected_size}"
message = "Expected JSON value size to be #{expected_size}, got #{actual_size}"
message << %( at path "#{@path}") if @path
message
end

failure_message_for_should_not do
message = "Expected JSON value size to not be #{expected_size}"
message = "Expected JSON value size to not be #{expected_size}, got #{actual_size}"
message << %( at path "#{@path}") if @path
message
end

def actual_size
ruby = parse_json(@json, @path)
ruby.is_a?(Enumerable) ? ruby.size : 1
end
end
24 changes: 24 additions & 0 deletions spec/json_spec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@
it "matches at a path" do
%({"one":[1,2,3]}).should have_json_size(3).at_path("one")
end

it "provides a failure message for should" do
matcher = have_json_size(3)
matcher.matches?(%([1,2]))
matcher.failure_message_for_should.should == "Expected JSON value size to be 3, got 2"
end

it "provides a failure message for should not" do
matcher = have_json_size(3)
matcher.matches?(%([1,2,3]))
matcher.failure_message_for_should_not.should == "Expected JSON value size to not be 3, got 3"
end
end

context "have_json_path" do
Expand Down Expand Up @@ -215,6 +227,18 @@
%(10.0).should have_json_type(Numeric)
end

it "provides a failure message for should" do
matcher = have_json_type(Numeric)
matcher.matches?(%("foo"))
matcher.failure_message_for_should.should == "Expected JSON value type to be Numeric, got String"
end

it "provides a failure message for should not" do
matcher = have_json_type(Numeric)
matcher.matches?(%(10))
matcher.failure_message_for_should_not.should == "Expected JSON value type to not be Numeric, got Fixnum"
end

context "somewhat uselessly" do
it "matches true" do
%(true).should have_json_type(TrueClass)
Expand Down