Skip to content

Commit

Permalink
have_json_size raise error with non Enumerables
Browse files Browse the repository at this point in the history
  • Loading branch information
Elizabeth Lunsford authored and Elizabeth Lunsford committed Oct 6, 2014
1 parent cbacd54 commit acfa179
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion features/sizes.feature
Expand Up @@ -35,4 +35,4 @@ Feature: Sizes
When I get the JSON
Then the JSON at "one" should have 1 entry
And the JSON at "two" should have 2 values
And the JSON at "three" should have 3 numbers
And the JSON at "three" should have 3 numbers
12 changes: 12 additions & 0 deletions lib/json_spec/errors.rb
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
9 changes: 3 additions & 6 deletions lib/json_spec/matchers/have_json_size.rb
Expand Up @@ -10,12 +10,9 @@ def initialize(size)

def matches?(json)
ruby = parse_json(json, @path)
if ruby.nil? && !@expected.nil?
false
else
@actual = Enumerable === ruby ? ruby.size : 1
@actual == @expected
end
raise EnumerableExpected.new(ruby) unless Enumerable === ruby
@actual = ruby.size
@actual == @expected
end

def at_path(path)
Expand Down
10 changes: 5 additions & 5 deletions spec/json_spec/matchers/have_json_size_spec.rb
Expand Up @@ -49,13 +49,13 @@

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.")
expect { matcher.matches?(%({"id":1,"json":null})) }.to raise_error(JsonSpec::EnumerableExpected,
"Enumerable expected, got nil")
end

it "provides an error when parsing nil" do
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.")
expect { matcher.matches?(%({"id":1,"json":12345})) }.to raise_error(JsonSpec::EnumerableExpected,
"Enumerable expected, got 12345")
end
end

0 comments on commit acfa179

Please sign in to comment.