Sha256: 1779b8b9c45e66438bda632d8b6b5471efa6ba78906b45668955db868e600e4d

Contents?: true

Size: 1.89 KB

Versions: 30

Compression:

Stored size: 1.89 KB

Contents

require 'rspec/expectations'
require 'equivalent-xml'

RSpec::Matchers.define :be_xml do |expected|

  def to_nokogiri(xml)
    return nil unless xml
    case xml
    when Nokogiri::XML::Element
      xml
    when Nokogiri::XML::Document
      xml.root
    when String
      to_nokogiri(Nokogiri::XML(xml))
    when REXML::Element
      to_nokogiri(xml.to_s)
    else
      fail "be_xml() expected XML, got #{xml.class}"
    end
  end

  def to_pretty(nokogiri)
    return nil unless nokogiri
    out = StringIO.new
    save_options = Nokogiri::XML::Node::SaveOptions::FORMAT | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
    nokogiri.write_xml_to(out, encoding: 'UTF-8', indent: 2, save_with: save_options)
    out.string
  end

  match do |actual|
    expected_xml = to_nokogiri(expected) || fail("expected value #{expected} does not appear to be XML")
    actual_xml = to_nokogiri(actual)

    EquivalentXml.equivalent?(expected_xml, actual_xml, element_order: false, normalize_whitespace: true)
  end

  failure_message do |actual|
    expected_string = to_pretty(to_nokogiri(expected))
    actual_string = to_pretty(to_nokogiri(actual)) || actual
    "expected XML:\n#{expected_string}\n\nbut was:\n#{actual_string}"
  end

  failure_message_when_negated do |actual|
    actual_xml = to_element(actual) || actual
    "expected not to get XML:\n\t#{actual_xml}"
  end
end

RSpec::Matchers.define :be_time do |expected|

  def to_string(time)
    time.is_a?(Time) ? time.utc.round(2).iso8601(2) : time.to_s
  end

  match do |actual|
    if expected
      fail "Expected value #{expected} is not a Time" unless expected.is_a?(Time)
      actual.is_a?(Time) && (to_string(expected) == to_string(actual))
    else
      return actual.nil?
    end
  end

  failure_message do |actual|
    expected_str = to_string(expected)
    actual_str = to_string(actual)
    "expected time:\n#{expected_str}\n\nbut was:\n#{actual_str}"
  end
end

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
datacite-mapping-0.1.17.2 spec/rspec_custom_matchers.rb
datacite-mapping-0.1.17.1 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.11.1 spec/rspec_custom_matchers.rb
datacite-mapping-0.1.17 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.11 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.10 spec/rspec_custom_matchers.rb
datacite-mapping-0.1.16 spec/rspec_custom_matchers.rb
datacite-mapping-0.1.15 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.9 spec/rspec_custom_matchers.rb
datacite-mapping-0.1.14 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.8 spec/rspec_custom_matchers.rb
datacite-mapping-0.1.13 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.7 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.6 spec/rspec_custom_matchers.rb
datacite-mapping-0.1.12 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.5 spec/rspec_custom_matchers.rb
datacite-mapping-0.1.10 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.4 spec/rspec_custom_matchers.rb
datacite-mapping-0.1.9 spec/rspec_custom_matchers.rb
stash-wrapper-0.1.3 spec/rspec_custom_matchers.rb