Browse files

Use RSpec 2.8's new preferred class matchers

  • Loading branch information...
1 parent 72f72ac commit d8b440dac08771741582bc969474b6db3770dfa2 @laserlemon laserlemon committed Jan 22, 2012
Showing with 196 additions and 143 deletions.
  1. +196 −143 lib/json_spec/matchers.rb
View
339 lib/json_spec/matchers.rb
@@ -1,161 +1,214 @@
require "json"
require "rspec"
-RSpec::Matchers.define :be_json_eql do |expected_json|
- include JsonSpec::Helpers
- include JsonSpec::Exclusion
-
- diffable
-
- match do |actual_json|
- @actual, @expected = scrub(actual_json, @path), [scrub(expected_json)]
- @actual == @expected.first
- end
-
- chain :at_path do |path|
- @path = path
- end
-
- chain :excluding do |*keys|
- excluded_keys.merge(keys.map{|k| k.to_s })
- end
-
- chain :including do |*keys|
- excluded_keys.subtract(keys.map{|k| k.to_s })
- end
-
- failure_message_for_should do
- message = "Expected equivalent JSON"
- message << %( at path "#{@path}") if @path
- message
- end
-
- failure_message_for_should_not do
- message = "Expected inequivalent JSON"
- message << %( at path "#{@path}") if @path
- message
- end
-
- def scrub(json, path = nil)
- generate_normalized_json(exclude_keys(parse_json(json, path))).chomp + "\n"
- end
-end
-
-RSpec::Matchers.define :include_json do |expected_json|
- include JsonSpec::Helpers
- include JsonSpec::Exclusion
-
- match do |actual_json|
- actual = parse_json(actual_json, @path)
- expected = exclude_keys(parse_json(expected_json))
- case actual
- when Hash then actual.values.map{|v| exclude_keys(v) }.include?(expected)
- when Array then actual.map{|e| exclude_keys(e) }.include?(expected)
- else false
+module JsonSpec
+ module Matchers
+ class BeJsonEql
+ include JsonSpec::Helpers
+ include JsonSpec::Exclusion
+
+ attr_reader :expected, :actual
+
+ def diffable?
+ true
+ end
+
+ def initialize(expected_json)
+ @expected_json = expected_json
+ end
+
+ def matches?(actual_json)
+ @actual, @expected = scrub(actual_json, @path), scrub(@expected_json)
+ @actual == @expected
+ end
+
+ def at_path(path)
+ @path = path
+ self
+ end
+
+ def excluding(*keys)
+ excluded_keys.merge(keys.map{|k| k.to_s })
+ self
+ end
+
+ def including(*keys)
+ excluded_keys.subtract(keys.map{|k| k.to_s })
+ self
+ end
+
+ def failure_message_for_should
+ message = "Expected equivalent JSON"
+ message << %( at path "#{@path}") if @path
+ message
+ end
+
+ def failure_message_for_should_not
+ message = "Expected inequivalent 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"
+ end
end
- end
-
- chain :at_path do |path|
- @path = path
- end
-
- chain :excluding do |*keys|
- excluded_keys.merge(*keys.map{|k| k.to_s })
- end
-
- chain :including do |*keys|
- excluded_keys.subtract(keys.map{|k| k.to_s })
- end
-
- failure_message_for_should do
- message = "Expected included JSON"
- message << %( at path "#{@path}") if @path
- message
- end
-
- failure_message_for_should_not do
- message = "Expected excluded JSON"
- message << %( at path "#{@path}") if @path
- message
- end
-end
-
-RSpec::Matchers.define :have_json_path do |path|
- include JsonSpec::Helpers
- match do |json|
- begin
- parse_json(json, path)
- true
- rescue JsonSpec::MissingPathError
- false
+ class IncludeJson
+ include JsonSpec::Helpers
+ include JsonSpec::Exclusion
+
+ def initialize(expected_json)
+ @expected_json = expected_json
+ end
+
+ def matches?(actual_json)
+ actual = parse_json(actual_json, @path)
+ expected = exclude_keys(parse_json(@expected_json))
+ case actual
+ when Hash then actual.values.map{|v| exclude_keys(v) }.include?(expected)
+ when Array then actual.map{|e| exclude_keys(e) }.include?(expected)
+ else false
+ end
+ end
+
+ def at_path(path)
+ @path = path
+ self
+ end
+
+ def excluding(*keys)
+ excluded_keys.merge(keys.map{|k| k.to_s })
+ self
+ end
+
+ def including(*keys)
+ excluded_keys.subtract(keys.map{|k| k.to_s })
+ self
+ end
+
+ def failure_message_for_should
+ message = "Expected included JSON"
+ message << %( at path "#{@path}") if @path
+ message
+ end
+
+ def failure_message_for_should_not
+ message = "Expected excluded JSON"
+ message << %( at path "#{@path}") if @path
+ message
+ end
end
- end
- failure_message_for_should do
- %(Expected JSON path "#{path}")
- end
+ class HaveJsonPath
+ include JsonSpec::Helpers
+
+ def initialize(path)
+ @path = path
+ end
+
+ def matches?(json)
+ begin
+ parse_json(json, @path)
+ true
+ rescue JsonSpec::MissingPathError
+ false
+ end
+ end
+
+ def failure_message_for_should
+ %(Expected JSON path "#{@path}")
+ end
+
+ def failure_message_for_should_not
+ %(Expected no JSON path "#{@path}")
+ end
+ end
- failure_message_for_should_not do
- %(Expected no JSON path "#{path}")
- end
-end
+ class HaveJsonType
+ include JsonSpec::Helpers
+
+ def initialize(klass)
+ @klass = klass
+ end
+
+ def matches?(json)
+ @ruby = parse_json(json, @path)
+ @ruby.is_a?(@klass)
+ end
+
+ def at_path(path)
+ @path = path
+ self
+ end
+
+ def failure_message_for_should
+ message = "Expected JSON value type to be #{@klass}, got #{@ruby.class}"
+ message << %( at path "#{@path}") if @path
+ message
+ end
+
+ def failure_message_for_should_not
+ message = "Expected JSON value type to not be #{@klass}, got #{@ruby.class}"
+ message << %( at path "#{@path}") if @path
+ message
+ end
+ end
-RSpec::Matchers.define :have_json_type do |klass|
- include JsonSpec::Helpers
+ class HaveJsonSize
+ include JsonSpec::Helpers
+
+ def initialize(size)
+ @expected = size
+ end
+
+ def matches?(json)
+ ruby = parse_json(json, @path)
+ @actual = ruby.is_a?(Enumerable) ? ruby.size : 1
+ @actual == @expected
+ end
+
+ def at_path(path)
+ @path = path
+ self
+ end
+
+ def failure_message_for_should
+ message = "Expected JSON value size to be #{@expected}, got #{@actual}"
+ message << %( at path "#{@path}") if @path
+ message
+ 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
+ end
+ end
- match do |json|
- @json = json
- actual.is_a?(klass)
- end
+ def be_json_eql(json)
+ JsonSpec::Matchers::BeJsonEql.new(json)
+ end
- chain :at_path do |path|
- @path = path
- end
+ def include_json(json)
+ JsonSpec::Matchers::IncludeJson.new(json)
+ end
- failure_message_for_should do
- message = "Expected JSON value type to be #{klass}, got #{actual.class}"
- message << %( at path "#{@path}") if @path
- message
- end
+ def have_json_path(path)
+ JsonSpec::Matchers::HaveJsonPath.new(path)
+ end
- failure_message_for_should_not do
- message = "Expected JSON value type to not be #{klass}, got #{actual.class}"
- message << %( at path "#{@path}") if @path
- message
- end
+ def have_json_type(klass)
+ JsonSpec::Matchers::HaveJsonType.new(klass)
+ end
- def actual
- parse_json(@json, @path)
+ def have_json_size(size)
+ JsonSpec::Matchers::HaveJsonSize.new(size)
+ end
end
end
-RSpec::Matchers.define :have_json_size do |expected_size|
- include JsonSpec::Helpers
-
- match do |json|
- @json = json
- actual_size == expected_size
- end
-
- chain :at_path do |path|
- @path = path
- end
-
- failure_message_for_should do
- message = "Expected JSON value size to be #{expected_size}, got #{actual_size}"
- message << %( at path "#{@path}") if @path
- message
- end
-
- failure_message_for_should_not do
- message = "Expected JSON value size to not be #{expected_size}, got #{actual_size}"
- message << %( at path "#{@path}") if @path
- message
- end
-
- def actual_size
- ruby = parse_json(@json, @path)
- ruby.is_a?(Enumerable) ? ruby.size : 1
- end
+RSpec.configure do |config|
+ config.include JsonSpec::Matchers
end

0 comments on commit d8b440d

Please sign in to comment.