# -*- coding: utf-8 -*- $top_srcdir = File.join(File.dirname(__FILE__), "..") $LOAD_PATH << File.join($top_srcdir, "lib") $KCODE = "u" if RUBY_VERSION < "1.9.0" require "rd2odt" require "tempfile" module XmlMatchers class BeSameAsThisXml def initialize(expected) @expected = expected if String === expected || Symbol === expected @expected_dom = to_dom(expected.to_s) end end def matches?(actual) @actual = actual if String === actual || Symbol === actual @actual_dom = to_dom(actual.to_s) end return expected_dom.to_s == actual_dom.to_s end def failure_message return "difference: #{diff}" end alias negative_failure_message failure_message private DOM_PREFIX = < EOF DOM_SUFFIX = < EOF def expected_dom return (@expected_dom ||= to_dom(RD2ODT.ah_to_xml(@expected))) end def actual_dom return (@actual_dom ||= to_dom(RD2ODT.ah_to_xml(@expected))) end def to_dom(s) root = REXML::Document.new(DOM_PREFIX + s + DOM_SUFFIX) result = root[2][1] # `result' is only XML from `s'. return result end def diff Tempfile.open("rd2odt-test") do |expected_file| Tempfile.open("rd2odt-test") do |actual_file| expected_file.puts(expected_dom.to_s) expected_file.close actual_file.puts(actual_dom.to_s) actual_file.close result = `docdiff --utf8 --char --tty #{expected_file.path} #{actual_file.path}` return result end end end end def be_same_as_this_xml(expected) return BeSameAsThisXml.new(expected) end end # for test on Ruby 1.8.6. if RUBY_VERSION < "1.8.7" class Hash def each keys.sort.each do |k| yield(k, self[k]) end end end end