Fuzz testing iterators and extensions for Nokogiri
Add this to your Gemfile (in the test group) and run bundle install
:
gem 'nokofuzzi'
I have larger plans for methods to attach to the Nokogiri document, but for now there are two:
each_missing
sets up an "each" iterator that sends you the string xml that was removed, and the new xml string (both from Nokogiri::XML)
existing_xml = Nokogiri::XML('/path/to/xml')
existing_xml.each_missing do |removed_xml, new_xml|
puts "String representation of the xml removed: #{removed_xml}"
puts "String representation of the modified xml #{new_xml}"
end
without_xpath
creates a duplicate of the receiver with all matches for the given xpath removed.
modified_xml = Nokogiri::XML('<a><b></b><c><b></b></c></a>').without_xpath('//b')
puts modified_xml.to_xml # <?xml version="1.0"?><a><c/></a>
set_text_at_xpath
replaces the text for a node (as well as contents below that node)
modified_xml = Nokogiri::XML('<a><b></b><c><b></b></c></a>').set_text_at_xpath('//c', "HI")
puts modified_xml.to_xml # <?xml version="1.0"?><a><b/><c>HI</c></a>