Permalink
Browse files

Merge pull request #22 from col/master

Add a description method to all the json_spec matchers
  • Loading branch information...
2 parents f7224b0 + da2faf0 commit c8c7ea8f9ed4436c2e75dda944f26776e9707434 @laserlemon laserlemon committed Feb 24, 2012
Showing with 83 additions and 0 deletions.
  1. +28 −0 lib/json_spec/matchers.rb
  2. +55 −0 spec/json_spec/matchers_spec.rb
View
@@ -49,6 +49,12 @@ def failure_message_for_should_not
message
end
+ def description
+ message = "equal JSON"
+ message << %( at path "#{@path}") if @path
+ message
+ end
+
private
def scrub(json, path = nil)
generate_normalized_json(exclude_keys(parse_json(json, path))).chomp + "\n"
@@ -99,6 +105,12 @@ def failure_message_for_should_not
message << %( at path "#{@path}") if @path
message
end
+
+ def description
+ message = "include JSON"
+ message << %( at path "#{@path}") if @path
+ message
+ end
end
class HaveJsonPath
@@ -124,6 +136,10 @@ def failure_message_for_should
def failure_message_for_should_not
%(Expected no JSON path "#{@path}")
end
+
+ def description
+ %(have JSON path "#{@path}")
+ end
end
class HaveJsonType
@@ -154,6 +170,12 @@ def failure_message_for_should_not
message << %( at path "#{@path}") if @path
message
end
+
+ def description
+ message = %(have JSON type "#{@klass.to_s}")
+ message << %( at path "#{@path}") if @path
+ message
+ end
end
class HaveJsonSize
@@ -185,6 +207,12 @@ def failure_message_for_should_not
message << %( at path "#{@path}") if @path
message
end
+
+ def description
+ message = %(have JSON size "#{@expected}")
+ message << %( at path "#{@path}") if @path
+ message
+ end
end
def be_json_eql(json)
@@ -86,6 +86,18 @@
JsonSpec.excluded_keys = %w(id json)
%({"id":1,"json":"spec"}).should_not be_json_eql(%({"id":2,"json":"different"})).including(:id, :json)
end
+
+ it "provide a description message" do
+ matcher = be_json_eql(%({"id":2,"json":"spec"}))
+ matcher.matches?(%({"id":1,"json":"spec"}))
+ matcher.description.should == "equal JSON"
+ end
+
+ it "provide a description message with path" do
+ matcher = be_json_eql(%({"id":1,"json":["spec"]})).at_path("json/0")
+ matcher.matches?(%({"id":1,"json":["spec"]}))
+ matcher.description.should == %(equal JSON at path "json/0")
+ end
end
context "include_json" do
@@ -140,6 +152,18 @@
it "ignores excluded keys" do
%([{"id":1,"two":3}]).should include_json(%({"two":3}))
end
+
+ it "provide a description message" do
+ matcher = include_json(%({"json":"spec"}))
+ matcher.matches?(%({"id":1,"json":"spec"}))
+ matcher.description.should == "include JSON"
+ end
+
+ it "provide a description message with path" do
+ matcher = include_json(%("spec")).at_path("json/0")
+ matcher.matches?(%({"id":1,"json":["spec"]}))
+ matcher.description.should == %(include JSON at path "json/0")
+ end
end
context "have_json_size" do
@@ -174,6 +198,18 @@
matcher.matches?(%([1,2,3]))
matcher.failure_message_for_should_not.should == "Expected JSON value size to not be 3, got 3"
end
+
+ it "provide a description message" do
+ matcher = have_json_size(1)
+ matcher.matches?(%({"id":1,"json":["spec"]}))
+ matcher.description.should == %(have JSON size "1")
+ end
+
+ it "provide a description message with path" do
+ matcher = have_json_size(1).at_path("json")
+ matcher.matches?(%({"id":1,"json":["spec"]}))
+ matcher.description.should == %(have JSON size "1" at path "json")
+ end
end
context "have_json_path" do
@@ -196,6 +232,13 @@
it "matches hash keys and array indexes" do
%({"one":[1,2,{"three":4}]}).should have_json_path("one/2/three")
end
+
+ it "provide a description message" do
+ matcher = have_json_path("json")
+ matcher.matches?(%({"id":1,"json":"spec"}))
+ matcher.description.should == %(have JSON path "json")
+ end
+
end
context "have_json_type" do
@@ -249,6 +292,18 @@
matcher.failure_message_for_should_not.should == "Expected JSON value type to not be Numeric, got Fixnum"
end
+ it "provide a description message" do
+ matcher = have_json_type(String)
+ matcher.matches?(%({"id":1,"json":"spec"}))
+ matcher.description.should == %(have JSON type "String")
+ end
+
+ it "provide a description message with path" do
+ matcher = have_json_type(String).at_path("json")
+ matcher.matches?(%({"id":1,"json":"spec"}))
+ matcher.description.should == %(have JSON type "String" at path "json")
+ end
+
context "somewhat uselessly" do
it "matches true" do
%(true).should have_json_type(TrueClass)

0 comments on commit c8c7ea8

Please sign in to comment.