Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'nokogiri'
# This test runs an infinite loop which throws from random Nokogiri SAX handler
# callbacks.
#
# It uses the sample XML files in Nokogiri's test suite, and expects to run from
# the Nokogiri folder, e.g. via:
# $ cd ~/workspace/nokogiri
# $ ruby -Ilib smokesax.rb
class RandomJumpHandler < Nokogiri::XML::SAX::Document
def initialize(jumptag)
@jumptag = jumptag
super()
end
def xmldecl version, encoding, standalone
random_jump
end
def start_document
random_jump
end
def end_document
random_jump
end
def start_element name, attrs = []
random_jump
end
def end_element name
random_jump
end
def start_element_namespace name, attrs = [], prefix = nil, uri = nil, ns = []
random_jump
end
def end_element_namespace name, prefix = nil, uri = nil
random_jump
end
def characters string
random_jump
end
def comment string
random_jump
end
def warning string
random_jump
end
def error string
random_jump
end
def cdata_block string
random_jump
end
private
def random_jump
if (rand > 0.9)
throw @jumptag
end
end
end
files = Dir.glob('test/files/*.xml')
doc = RandomJumpHandler.new(:foo)
loop do
parser = Nokogiri::HTML::SAX::Parser.new(doc)
files.each do |file|
catch(:foo) do
parser.parse(File.open(file, 'r'))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment