Sha256: df770b227b18848307d814d2121b0b500297066dafc2b119683b6115b62cdcd4

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

module XmlHelpers
  extend RSpec::Matchers::DSL

  matcher :validate_against_schema do |schema|
    @schema = schema

    match do |actual|
      xsd = Nokogiri::XML::Schema(File.read(@schema))
      @validation_errors = xsd.validate(actual)
      @validation_errors.empty?
    end

    failure_message_for_should do |actual|
      num_errors = @validation_errors.length
      error_count = "#{num_errors} error#{'s' if num_errors > 1}"

      "expected XML to validate against #{@schema}, got #{error_count}:\n" +
        @validation_errors.each_with_index.map do |error, i|
          "  #{i + 1}. #{error}"
        end.join("\n")
    end
  end

  matcher :contain_node do |name|
    match do |doc|
      if @attributes
        doc.css(name).detect do |node|
          @attributes.all? { |k, v| node.css(k).text == v }
        end
      else
        doc.at_css(name)
      end
    end

    chain :with_children do |attributes|
      @attributes = attributes
    end

    failure_message_for_should do |doc|
      expected_xml = "<#{name}>\n"

      if @attributes
        @attributes.each { |k,v| expected_xml << "  <#{k}>#{v}</#{k}>\n" }
      end

      expected_xml << "</#{name}>"

      "expected XML to contain:\n\n#{expected_xml}\n\ninstead got:\n\n#{doc.to_s}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
middleman-search_engine_sitemap-1.1.0 spec/support/xml_helpers.rb
middleman-search_engine_sitemap-1.1.0.pre spec/support/xml_helpers.rb
middleman-search_engine_sitemap-1.0.1 spec/support/xml_helpers.rb
middleman-search_engine_sitemap-1.0.0 spec/support/xml_helpers.rb
middleman-search_engine_sitemap-1.0.0.rc spec/support/xml_helpers.rb