Skip to content

Commit

Permalink
added helper document that can test a strict order of items produced …
Browse files Browse the repository at this point in the history
…by parsers
  • Loading branch information
andrew-aladev committed Sep 28, 2017
1 parent ac7060c commit 91d8e3e
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,86 @@ def processing_instruction name, content
@processing_instructions << [name, content]
end
end

# This document will help us to test the strict order of items.

class DocWithOrderedItems < XML::SAX::Document
attr_reader :items

def initialize
# [
# [ :method_1, argument_1, ... ],
# [ :method_2, argument_2, ... ],
# ...
# ]
@items = Items.new
end

%i[
xmldecl
start_document end_document
start_element end_element
start_element_namespace end_element_namespace
characters comment cdata_block
processing_instruction
error warning
]
.each do |name|
define_method name do |*arguments|
@items << [name, *arguments]
super *arguments
end
end

class Items < Array
def get_root_content root_name
items = clone
is_inside_root = false

items.select! do |item|
method_name = item[0]
element_name = item[1]

case method_name
when :start_element, :start_element_namespace
if element_name == root_name
is_inside_root = true
next false
end

when :end_element, :end_element_namespace
is_inside_root = false if element_name == root_name and is_inside_root
end

is_inside_root
end

items
end

def select_methods(names)
items = clone

items.select! do |item|
name = item[0]
names.include? name
end

items
end

def strip_text! method_names
each do |item|
method_name = item[0]
text = item[1]

text.strip! if method_names.include? method_name
end

nil
end
end
end
end
end
end

0 comments on commit 91d8e3e

Please sign in to comment.