Created
October 21, 2010 07:57
-
-
Save chsh/638090 to your computer and use it in GitHub Desktop.
2つのXMLが同じかどうかだけを調べるチェッカー
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'nokogiri' | |
| module XMLEqualityChecker | |
| def equal_as_xml(left, right) | |
| left_x = Nokogiri::XML(left.to_s) | |
| right_x = Nokogiri::XML(right.to_s) | |
| compare_element(left_x.root, right_x.root) | |
| end | |
| def compare_element(left_elm, right_elm) | |
| raise "Name unmatch: left:#{left_elm.name}, right:#{right_elm.name}" unless left_elm.name == right_elm.name | |
| raise "Attributes unmatch: left:#{left_elm.attributes.inspect}, right:#{right_elm.attributes.inspect}" unless equal_attributes?(left_elm, right_elm) | |
| left_children = left_elm.children | |
| right_children = right_elm.children | |
| raise "Children size unmatch: left:#{left_children.size}, right:#{right_children.size}" unless left_children.size == right_children.size | |
| left_children.each_with_index do |left_child, index| | |
| right_child = right_children[index] | |
| compare_element(left_child, right_child) | |
| end | |
| true | |
| end | |
| def equal_attributes?(left, right) | |
| return false unless left.keys.sort == right.keys.sort | |
| left.keys.each do |k| | |
| return false unless left[k] == right[k] | |
| end | |
| true | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment