From cbacd5488524a3d04804ac435cfe02e68cedc1d6 Mon Sep 17 00:00:00 2001 From: Elizabeth Lunsford Date: Tue, 30 Sep 2014 16:00:04 -0700 Subject: [PATCH 1/2] do not return 1 entry for nil values --- features/sizes.feature | 2 +- lib/json_spec/matchers/have_json_size.rb | 8 ++++++-- spec/json_spec/matchers/have_json_size_spec.rb | 12 ++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/features/sizes.feature b/features/sizes.feature index 3c7d942..64d85c1 100644 --- a/features/sizes.feature +++ b/features/sizes.feature @@ -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 \ No newline at end of file diff --git a/lib/json_spec/matchers/have_json_size.rb b/lib/json_spec/matchers/have_json_size.rb index 53e829b..b723b72 100644 --- a/lib/json_spec/matchers/have_json_size.rb +++ b/lib/json_spec/matchers/have_json_size.rb @@ -10,8 +10,12 @@ def initialize(size) def matches?(json) ruby = parse_json(json, @path) - @actual = Enumerable === ruby ? ruby.size : 1 - @actual == @expected + if ruby.nil? && !@expected.nil? + false + else + @actual = Enumerable === ruby ? ruby.size : 1 + @actual == @expected + end end def at_path(path) diff --git a/spec/json_spec/matchers/have_json_size_spec.rb b/spec/json_spec/matchers/have_json_size_spec.rb index 92ea547..6526f47 100644 --- a/spec/json_spec/matchers/have_json_size_spec.rb +++ b/spec/json_spec/matchers/have_json_size_spec.rb @@ -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 nil" 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 From acfa17966bbe15bb66d5bd92049e2a6457d7bc5a Mon Sep 17 00:00:00 2001 From: Elizabeth Lunsford Date: Sun, 5 Oct 2014 22:27:49 -0700 Subject: [PATCH 2/2] have_json_size raise error with non Enumerables --- features/sizes.feature | 2 +- lib/json_spec/errors.rb | 12 ++++++++++++ lib/json_spec/matchers/have_json_size.rb | 9 +++------ spec/json_spec/matchers/have_json_size_spec.rb | 10 +++++----- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/features/sizes.feature b/features/sizes.feature index 64d85c1..3c7d942 100644 --- a/features/sizes.feature +++ b/features/sizes.feature @@ -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 \ No newline at end of file + And the JSON at "three" should have 3 numbers diff --git a/lib/json_spec/errors.rb b/lib/json_spec/errors.rb index 6c78388..39d23ae 100644 --- a/lib/json_spec/errors.rb +++ b/lib/json_spec/errors.rb @@ -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 diff --git a/lib/json_spec/matchers/have_json_size.rb b/lib/json_spec/matchers/have_json_size.rb index b723b72..e9e11c8 100644 --- a/lib/json_spec/matchers/have_json_size.rb +++ b/lib/json_spec/matchers/have_json_size.rb @@ -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) diff --git a/spec/json_spec/matchers/have_json_size_spec.rb b/spec/json_spec/matchers/have_json_size_spec.rb index 6526f47..8b9f357 100644 --- a/spec/json_spec/matchers/have_json_size_spec.rb +++ b/spec/json_spec/matchers/have_json_size_spec.rb @@ -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