Sha256: 49b32f9fef5c07feecf02acc8256d725d56957611fa03a19cc785737f88728e0

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

module XmlHelpers
  extend RSpec::Matchers::DSL

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

    failure_message 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 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

6 entries across 6 versions & 2 rubygems

Version Path
middleman-sitemap-xml-generator-1.0.0 spec/support/xml_helpers.rb
middleman-search_engine_sitemap-1.4.0 spec/support/xml_helpers.rb
middleman-search_engine_sitemap-1.4.0.beta spec/support/xml_helpers.rb
middleman-search_engine_sitemap-1.4.0.alpha spec/support/xml_helpers.rb
middleman-search_engine_sitemap-1.3.0 spec/support/xml_helpers.rb
middleman-search_engine_sitemap-1.2.0 spec/support/xml_helpers.rb