class RSpec::XmlHelpers::XmlFormatter

@api private

Public Instance Methods

format(xml) click to toggle source
# File lib/rspec/xml_helpers/xml_formatter.rb, line 12
def format(xml)
  xml = NawsXml::Parser.new.parse(xml)
  xml = stable_sort(xml)
  xml.to_s(indent: '  ')
end

Private Instance Methods

sort_attrs(attrs) click to toggle source
# File lib/rspec/xml_helpers/xml_formatter.rb, line 30
def sort_attrs(attrs)
  attrs.keys.sort.each_with_object({}) do |key, sorted|
    sorted[key] = attrs[key]
  end
end
sort_nodes(nodes) click to toggle source
# File lib/rspec/xml_helpers/xml_formatter.rb, line 36
def sort_nodes(nodes)
  nodes.sort_by.with_index { |node, idx| [node.name, idx] }
end
stable_sort(xml) click to toggle source
# File lib/rspec/xml_helpers/xml_formatter.rb, line 20
def stable_sort(xml)
  node = NawsXml::Node.new(xml.name)
  node.attributes.update(sort_attrs(xml.attributes))
  node << xml.text if xml.text
  sort_nodes(xml.children).each do |child|
    node << child
  end
  node
end