Sha256: 9803ee403b5ee4e96bcbfbfe13088dec013d4b839fcf3dfe203129a4b64b8fb5

Contents?: true

Size: 1.35 KB

Versions: 33

Compression:

Stored size: 1.35 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)

    expect(@schema).to be_valid(@doc)
  end

  failure_message 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)
    expect(nodes).not_to be_empty
    if text
      nodes.each do |node|
        if text.is_a?(Regexp)
          expect(node.content).to match(text)
        else
          expect(node.content).to eq(text)
        end
      end
    end
    true
  end
end

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

  attributes.each do |attribute|
    match do |actual|
      values.all? { |value|
        expect(
          actual.new(attribute => value).errors_on(attribute).size
        ).to eq 0
      }
    end
  end

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

Version data entries

33 entries across 33 versions & 4 rubygems

Version Path
sps_king-0.5.0 spec/support/custom_matcher.rb
sps_king-0.4.0 spec/support/custom_matcher.rb
sps_king-0.3.1 spec/support/custom_matcher.rb
sps_king-0.3.0 spec/support/custom_matcher.rb
sps_king-0.2.0 spec/support/custom_matcher.rb
sepa_king-0.14.0 spec/support/custom_matcher.rb
sepa_king-0.13.0 spec/support/custom_matcher.rb
sps_king-0.1.1 spec/support/custom_matcher.rb
sepa_king_codeur-0.12.1 spec/support/custom_matcher.rb
sepa_king-0.12.0 spec/support/custom_matcher.rb
sps_king-0.1.0 spec/support/custom_matcher.rb
sepa_king-0.11.2 spec/support/custom_matcher.rb
sepa_king-0.11.1 spec/support/custom_matcher.rb
sepa_king_extended-0.12.2 spec/support/custom_matcher.rb
sepa_king_extended-0.12.1 spec/support/custom_matcher.rb
sepa_king_extended-0.12.0 spec/support/custom_matcher.rb
sepa_king_extended-0.11.6 spec/support/custom_matcher.rb
sepa_king_extended-0.11.5 spec/support/custom_matcher.rb
sepa_king_extended-0.11.4 spec/support/custom_matcher.rb
sepa_king_extended-0.11.3 spec/support/custom_matcher.rb