lib/json_matchers/rspec.rb in json_matchers-0.7.3 vs lib/json_matchers/rspec.rb in json_matchers-0.8.0

- old
+ new

@@ -1,94 +1,34 @@ -require "delegate" require "json_matchers" -require "json_matchers/payload" +require "json_matchers/assertion" module JsonMatchers - class RSpec < SimpleDelegator - attr_reader :schema_name + self.schema_root = File.join("spec", "support", "api", "schemas") +end - def initialize(schema_name, **options) - @schema_name = schema_name +RSpec::Matchers.define :match_json_schema do |schema_name, **options| + assertion = JsonMatchers::Assertion.new(schema_name.to_s, options) - super(JsonMatchers::Matcher.new(schema_path, options)) - end + match do |json| + assertion.valid?(json) + end - def failure_message(json) - <<-FAIL -#{validation_failure_message} - ---- - -expected - -#{pretty_json(json)} - -to match schema "#{schema_name}": - -#{pretty_json(schema_body)} - - FAIL + if respond_to?(:failure_message) + failure_message do + assertion.valid_failure_message end - def failure_message_when_negated(json) - <<-FAIL -#{validation_failure_message} - ---- - -expected - -#{pretty_json(json)} - -not to match schema "#{schema_name}": - -#{pretty_json(schema_body)} - - FAIL + failure_message_when_negated do + assertion.invalid_failure_message end - - private - - def pretty_json(json) - payload = Payload.new(json).to_s - - JSON.pretty_generate(JSON.parse(payload)) + else + failure_message_for_should do + assertion.valid_failure_message end - def schema_path - JsonMatchers.path_to_schema(schema_name) + failure_message_for_should_not do + assertion.invalid_failure_message end - - def schema_body - File.read(schema_path) - end end end -if RSpec.respond_to?(:configure) - RSpec::Matchers.define :match_json_schema do |schema_name, **options| - matcher = JsonMatchers::RSpec.new(schema_name, options) - - match do |json| - matcher.matches?(json) - end - - if respond_to?(:failure_message) - failure_message do |json| - matcher.failure_message(json) - end - - failure_message_when_negated do |json| - matcher.failure_message_when_negated(json) - end - else - failure_message_for_should do |json| - matcher.failure_message(json) - end - - failure_message_for_should_not do |json| - matcher.failure_message_when_negated(json) - end - end - end - RSpec::Matchers.alias_matcher :match_response_schema, :match_json_schema -end +RSpec::Matchers.alias_matcher :match_response_schema, :match_json_schema