Skip to content

Commit acfa179

Browse files
Elizabeth LunsfordElizabeth Lunsford
authored andcommitted
have_json_size raise error with non Enumerables
1 parent cbacd54 commit acfa179

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

features/sizes.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ Feature: Sizes
3535
When I get the JSON
3636
Then the JSON at "one" should have 1 entry
3737
And the JSON at "two" should have 2 values
38-
And the JSON at "three" should have 3 numbers
38+
And the JSON at "three" should have 3 numbers

lib/json_spec/errors.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,16 @@ def to_s
3131
"No JSON file at #{path}"
3232
end
3333
end
34+
35+
class EnumerableExpected < Error
36+
attr_reader :actual_value
37+
38+
def initialize(actual_value)
39+
@actual_value = actual_value
40+
end
41+
42+
def to_s
43+
"Enumerable expected, got #{actual_value.inspect}"
44+
end
45+
end
3446
end

lib/json_spec/matchers/have_json_size.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ def initialize(size)
1010

1111
def matches?(json)
1212
ruby = parse_json(json, @path)
13-
if ruby.nil? && !@expected.nil?
14-
false
15-
else
16-
@actual = Enumerable === ruby ? ruby.size : 1
17-
@actual == @expected
18-
end
13+
raise EnumerableExpected.new(ruby) unless Enumerable === ruby
14+
@actual = ruby.size
15+
@actual == @expected
1916
end
2017

2118
def at_path(path)

spec/json_spec/matchers/have_json_size_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@
4949

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

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

0 commit comments

Comments
 (0)