Sha256: 3d3de1e931e8fe359e9874250088f1a673683f2affc02a4e4fef3e99444ef050

Contents?: true

Size: 1.58 KB

Versions: 49

Compression:

Stored size: 1.58 KB

Contents

require 'rspec/matchers' # @see http://rubygems.org/gems/rspec
require 'json-schema'
require 'jsonpath'

RSpec::Matchers.define :match_json_schema do |schema|
  match do |actual|
    @error_message = JSON::Validator.fully_validate(schema, actual, validate_schema: true).join("")
    @error_message.empty?
  end

  failure_message do |actual|
    @error_message +
    "\nActual: #{actual.to_json(JSON::LD::JSON_STATE)}" +
    "\nSchema: #{schema.to_json(JSON::LD::JSON_STATE)}"
  end
end

RSpec::Matchers.define :match_json_path do |path|
  match do |actual|
    matched = JsonPath.new(path).on(actual)
    !matched.empty?
  end

  failure_message do |actual|
    "Path #{path} not found in data" +
    "\nActual: #{actual.to_json(JSON::LD::JSON_STATE)}"
  end
end

RSpec::Matchers.define :have_errors do |errors|
  match do |actual|
    return false unless actual.keys == errors.keys
    actual.each do |area_key, area_values|
      return false unless area_values.length == errors[area_key].length
      area_values.each do |term, values|
        return false unless values.length == errors[area_key][term].length
        values.each_with_index do |v, i|
          return false unless case m = errors[area_key][term][i]
          when Regexp then m.match v
          else  m == v
          end
        end
      end
    end
    true
  end

  failure_message do |actual|
    "expected errors to match #{errors.to_json(JSON::LD::JSON_STATE)}\nwas #{actual.to_json(JSON::LD::JSON_STATE)}"
  end

  failure_message_when_negated do |actual|
    "expected errors not to match #{errors.to_json(JSON::LD::JSON_STATE)}"
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
rdf-vocab-3.1.5 spec/matchers.rb
rdf-vocab-3.1.4 spec/matchers.rb
rdf-vocab-3.1.2 spec/matchers.rb
rdf-vocab-3.1.1 spec/matchers.rb
rdf-vocab-3.1.0 spec/matchers.rb
rdf-vocab-3.0.14 spec/matchers.rb
rdf-vocab-3.0.13 spec/matchers.rb
rdf-vocab-3.0.12 spec/matchers.rb
rdf-vocab-3.0.11 spec/matchers.rb
rdf-vocab-3.0.10 spec/matchers.rb
rdf-vocab-3.0.8 spec/matchers.rb
rdf-vocab-3.0.7 spec/matchers.rb
rdf-vocab-3.0.6 spec/matchers.rb
rdf-vocab-3.0.5 spec/matchers.rb
rdf-vocab-3.0.4 spec/matchers.rb
rdf-vocab-3.0.3 spec/matchers.rb
rdf-vocab-3.0.2 spec/matchers.rb
rdf-vocab-3.0.1 spec/matchers.rb
rdf-vocab-3.0.0 spec/matchers.rb
rdf-vocab-2.2.9 spec/matchers.rb