Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DRY up matcher messages

  • Loading branch information...
commit 047a176106d8af581e4ae1518062eba8101969eb 1 parent ac6d63f
@laserlemon laserlemon authored
View
1  lib/json_spec.rb
@@ -4,6 +4,7 @@
require "json_spec/configuration"
require "json_spec/exclusion"
require "json_spec/helpers"
+require "json_spec/messages"
require "json_spec/matchers"
require "json_spec/memory"
View
13 lib/json_spec/matchers/be_json_eql.rb
@@ -3,6 +3,7 @@ module Matchers
class BeJsonEql
include JsonSpec::Helpers
include JsonSpec::Exclusion
+ include JsonSpec::Messages
attr_reader :expected, :actual
@@ -42,21 +43,15 @@ def including(*keys)
end
def failure_message_for_should
- message = "Expected equivalent JSON"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("Expected equivalent JSON")
end
def failure_message_for_should_not
- message = "Expected inequivalent JSON"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("Expected inequivalent JSON")
end
def description
- message = "equal JSON"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("equal JSON")
end
private
View
13 lib/json_spec/matchers/have_json_size.rb
@@ -2,6 +2,7 @@ module JsonSpec
module Matchers
class HaveJsonSize
include JsonSpec::Helpers
+ include JsonSpec::Messages
def initialize(size)
@expected = size
@@ -19,21 +20,15 @@ def at_path(path)
end
def failure_message_for_should
- message = "Expected JSON value size to be #{@expected}, got #{@actual}"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("Expected JSON value size to be #{@expected}, got #{@actual}")
end
def failure_message_for_should_not
- message = "Expected JSON value size to not be #{@expected}, got #{@actual}"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("Expected JSON value size to not be #{@expected}, got #{@actual}")
end
def description
- message = %(have JSON size "#{@expected}")
- message << %( at path "#{@path}") if @path
- message
+ message_with_path(%(have JSON size "#{@expected}"))
end
end
end
View
13 lib/json_spec/matchers/have_json_type.rb
@@ -2,6 +2,7 @@ module JsonSpec
module Matchers
class HaveJsonType
include JsonSpec::Helpers
+ include JsonSpec::Messages
def initialize(type)
@classes = type_to_classes(type)
@@ -18,21 +19,15 @@ def at_path(path)
end
def failure_message_for_should
- message = "Expected JSON value type to be #{@classes.join(", ")}, got #{@ruby.class}"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("Expected JSON value type to be #{@classes.join(", ")}, got #{@ruby.class}")
end
def failure_message_for_should_not
- message = "Expected JSON value type to not be #{@classes.join(", ")}, got #{@ruby.class}"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("Expected JSON value type to not be #{@classes.join(", ")}, got #{@ruby.class}")
end
def description
- message = %(have JSON type "#{@classes.join(", ")}")
- message << %( at path "#{@path}") if @path
- message
+ message_with_path(%(have JSON type "#{@classes.join(", ")}"))
end
private
View
13 lib/json_spec/matchers/include_json.rb
@@ -3,6 +3,7 @@ module Matchers
class IncludeJson
include JsonSpec::Helpers
include JsonSpec::Exclusion
+ include JsonSpec::Messages
def initialize(expected_json = nil)
@expected_json = expected_json
@@ -41,21 +42,15 @@ def including(*keys)
end
def failure_message_for_should
- message = "Expected included JSON"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("Expected included JSON")
end
def failure_message_for_should_not
- message = "Expected excluded JSON"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("Expected excluded JSON")
end
def description
- message = "include JSON"
- message << %( at path "#{@path}") if @path
- message
+ message_with_path("include JSON")
end
end
end
View
8 lib/json_spec/messages.rb
@@ -0,0 +1,8 @@
+module JsonSpec
+ module Messages
+ def message_with_path(message)
+ message << %( at path "#{@path}") if @path
+ message
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.