Sha256: a6db8b171cbd7e28a53122b5dbcd54a5a4fa5e9a945dc452a367f6aa2ea640c3

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require 'rspec/expectations'
require 'nokogiri'

RSpec::Matchers.define :validate_against do |xsd|
  match do |actual|
    @schema = Nokogiri::XML::Schema(File.read("lib/schema/#{xsd}"))
    @doc = Nokogiri::XML(actual)

    @schema.should be_valid(@doc)
  end

  failure_message_for_should do |actual|
    # Return the validation errors as string
    @schema.validate(@doc).join("\n")
  end
end

RSpec::Matchers.define :have_xml do |xpath, text|
  match do |actual|
    doc = Nokogiri::XML(actual)
    doc.remove_namespaces! # so we can use shorter xpath's without any namespace

    nodes = doc.xpath(xpath)
    nodes.should_not be_empty
    if text
      nodes.each do |node|
        if text.is_a?(Regexp)
          node.content.should =~ text
        else
          node.content.should == text
        end
      end
    end
    true
  end
end

RSpec::Matchers.define :accept do |*values, options|
  attributes = Array(options[:for])

  attributes.each do |attribute|
    match_for_should do |actual|
      values.all? { |value|
        expect(
          actual.new(attribute => value)
        ).to have(:no).errors_on(attribute)
      }
    end
  end

  attributes.each do |attribute|
    match_for_should_not do |actual|
      values.all? { |value|
        expect(
          actual.new(attribute => value)
        ).to have_at_least(1).errors_on(attribute)
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sepa_king-0.3.0 spec/support/custom_matcher.rb
sepa_king-0.2.0 spec/support/custom_matcher.rb