Skip to content

Commit

Permalink
Merge pull request #82 from eluns/master
Browse files Browse the repository at this point in the history
Raise error when checking a size of a json ruby value of nil
  • Loading branch information
laserlemon committed Oct 10, 2014
2 parents 104b248 + acfa179 commit 794b747
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
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
@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

0 comments on commit 794b747

Please sign in to comment.