Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created Mar 23, 2009
Embed
What would you like to do?
# comparing xml is always a b-i-a-t-c-h in any testing environment. here is a
# little snippet for ruby that, i think, it a good first pass at making it
# easier. comment with your improvements please!
#
#
require 'rubygems'
require 'xmlsimple'
def xml_cmp a, b
eq_all_but_zero = Object.new.instance_eval do
def ==(other)
Integer(other) == 0 ? false : true
end
self
end
a = XmlSimple.xml_in(a.to_s, 'normalisespace' => eq_all_but_zero)
b = XmlSimple.xml_in(b.to_s, 'normalisespace' => eq_all_but_zero)
a == b
end
def random_attributes
attributes = ('a'..'z').to_a.inject({}){|h,k| h.update k => k.upcase}
pairs = attributes.to_a.map{|k,v| "#{ k }=#{ v.inspect }"}.sort_by{ rand }
string = ''
pairs.each{|pair| string << pair << (' '*(rand(4)+1))}
string.strip
end
a = <<-__
<root>
<foo >
<bar key='val' #{ random_attributes }> content</bar>
</foo>
<values>
<a>a</a>
<b>b</b>
</values>
</root>
__
b = <<-__
<root>
<foo> <bar
#{ random_attributes }
key
= 'val' >content \n</bar></foo>
<values> <a>a</a>
<b>b</b>
</values>
</root>
__
p xml_cmp(a, b) #=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment