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

Raise error when checking a size of a json ruby value of nil #82

Merged
merged 2 commits into from
Oct 10, 2014
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
12 changes: 12 additions & 0 deletions lib/json_spec/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ def to_s
"No JSON file at #{path}"
end
end

class EnumerableExpected < Error
attr_reader :actual_value

def initialize(actual_value)
@actual_value = actual_value
end

def to_s
"Enumerable expected, got #{actual_value.inspect}"
end
end
end
3 changes: 2 additions & 1 deletion lib/json_spec/matchers/have_json_size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def initialize(size)

def matches?(json)
ruby = parse_json(json, @path)
@actual = Enumerable === ruby ? ruby.size : 1
raise EnumerableExpected.new(ruby) unless Enumerable === ruby
@actual = ruby.size
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good.

@actual == @expected
end

Expand Down
12 changes: 12 additions & 0 deletions spec/json_spec/matchers/have_json_size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@
matcher.matches?(%({"id":1,"json":["spec"]}))
matcher.description.should == %(have JSON size "1" at path "json")
end

it "provides an error when parsing nil" do
matcher = have_json_size(0).at_path("json")
expect { matcher.matches?(%({"id":1,"json":null})) }.to raise_error(JsonSpec::EnumerableExpected,
"Enumerable expected, got nil")
end

it "provides an error when parsing non-enumerables" do
matcher = have_json_size(0).at_path("json")
expect { matcher.matches?(%({"id":1,"json":12345})) }.to raise_error(JsonSpec::EnumerableExpected,
"Enumerable expected, got 12345")
end
end