#!/usr/bin/env ruby -wW1
# encoding: UTF-8
$: << '../lib'
$: << '../ext'
if __FILE__ == $0
while (i = ARGV.index('-I'))
x,path = ARGV.slice!(i, 2)
$: << path
end
end
require 'stringio'
require 'test/unit'
require 'optparse'
require 'ox'
opts = OptionParser.new
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
files = opts.parse(ARGV)
class StartSax < ::Ox::Sax
attr_accessor :calls
def initialize()
@calls = []
end
def start_element(name)
@calls << [:start_element, name]
end
def attr(name, value)
@calls << [:attr, name, value]
end
end
class AllSax < StartSax
def initialize()
super
end
def instruct(target)
@calls << [:instruct, target]
end
def doctype(value)
@calls << [:doctype, value]
end
def comment(value)
@calls << [:comment, value]
end
def cdata(value)
@calls << [:cdata, value]
end
def text(value)
@calls << [:text, value]
end
def end_element(name)
@calls << [:end_element, name]
end
def error(message, line, column)
@calls << [:error, message, line, column]
end
end
class Func < ::Test::Unit::TestCase
def test_sax_io_pipe
handler = AllSax.new()
input,w = IO.pipe
w << %{}
w.close
Ox.sax_parse(handler, input)
assert_equal(handler.calls,
[[:start_element, :top],
[:end_element, :top]])
end
def test_sax_io_file
handler = AllSax.new()
input = IO.open(IO.sysopen('basic.xml'))
Ox.sax_parse(handler, input)
assert_equal(handler.calls,
[[:start_element, :top],
[:end_element, :top]])
end
def parse_compare(xml, expected, handler_class=AllSax)
handler = handler_class.new()
input = StringIO.new(xml)
Ox.sax_parse(handler, input)
assert_equal(expected, handler.calls)
end
def test_sax_instruct_simple
parse_compare(%{}, [[:instruct, 'xml']])
end
def test_sax_instruct_blank
parse_compare(%{}, [], StartSax)
end
def test_sax_instruct_attrs
parse_compare(%{},
[[:instruct, 'xml'],
[:attr, :version, "1.0"],
[:attr, :encoding, "UTF-8"]])
end
def test_sax_instruct_loose
parse_compare(%{ xml
version = "1.0"
encoding = "UTF-8" ?>},
[[:instruct, 'xml'],
[:attr, :version, "1.0"],
[:attr, :encoding, "UTF-8"]])
end
def test_sax_element_simple
parse_compare(%{},
[[:start_element, :top],
[:end_element, :top]])
end
def test_sax_element_attrs
parse_compare(%{},
[[:start_element, :top],
[:attr, :x, "57"],
[:attr, :y, "42"],
[:end_element, :top]])
end
def test_sax_two_top
parse_compare(%{},
[[:start_element, :top],
[:end_element, :top],
[:error, "invalid format, multiple top level elements", 1, 9],
[:start_element, :top],
[:end_element, :top]])
end
def test_sax_nested1
parse_compare(%{
},
[[:instruct, 'xml'],
[:attr, :version, "1.0"],
[:start_element, :top],
[:start_element, :child],
[:start_element, :grandchild],
[:end_element, :grandchild],
[:end_element, :child],
[:end_element, :top],
])
end
def test_sax_nested1_tight
parse_compare(%{},
[[:instruct, 'xml'],
[:attr, :version, "1.0"],
[:start_element, :top],
[:start_element, :child],
[:start_element, :grandchild],
[:end_element, :grandchild],
[:end_element, :child],
[:end_element, :top],
])
end
def test_sax_element_name_mismatch
parse_compare(%{
},
[[:instruct, 'xml'],
[:attr, :version, "1.0"],
[:start_element, :top],
[:start_element, :child],
[:start_element, :grandchild],
[:end_element, :grandchild],
[:error, "invalid format, element start and end names do not match", 5, 12]
])
end
def test_sax_nested
parse_compare(%{
},
[[:instruct, 'xml'],
[:attr, :version, "1.0"],
[:start_element, :top],
[:start_element, :child],
[:start_element, :grandchild],
[:end_element, :grandchild],
[:end_element, :child],
[:start_element, :child],
[:start_element, :grandchild],
[:end_element, :grandchild],
[:start_element, :grandchild],
[:end_element, :grandchild],
[:end_element, :child],
[:end_element, :top],
])
end
def test_sax_element_no_term
parse_compare(%{
},
[[:start_element, :top],
[:start_element, :child],
[:end_element, :child],
[:error, "invalid format, element not terminated", 4, 1]
])
end
def test_sax_text
parse_compare(%{This is some text.},
[[:start_element, :top],
[:text, "This is some text."],
[:end_element, :top]
])
end
def test_sax_text_no_term
parse_compare(%{This is some text.},
[[:start_element, :top],
[:error, "invalid format, text terminated unexpectedly", 1, 24],
])
end
# TBD invalid chacters in text
def test_sax_doctype
parse_compare(%{
},
[[:instruct, 'xml'],
[:attr, :version, "1.0"],
[:doctype, ' top PUBLIC "top.dtd"'],
[:start_element, :top],
[:end_element, :top]])
end
def test_sax_doctype_bad_order
parse_compare(%{
},
[[:instruct, 'xml'],
[:attr, :version, "1.0"],
[:start_element, :top],
[:end_element, :top],
[:error, "invalid format, DOCTYPE can not come after an element", 3, 11],
[:doctype, ' top PUBLIC "top.dtd"']])
end
def test_sax_instruct_bad_order
parse_compare(%{
},
[[:doctype, " top PUBLIC \"top.dtd\""],
[:error, "invalid format, instruction must come before elements", 3, 3],
[:instruct, "xml"],
[:attr, :version, "1.0"],
[:start_element, :top],
[:end_element, :top]])
end
def test_sax_comment
parse_compare(%{
BeforeAfter
},
[[:instruct, 'xml'],
[:attr, :version, "1.0"],
[:comment, 'First comment.'],
[:start_element, :top],
[:text, 'Before'],
[:comment, 'Nested comment.'],
[:text, 'After'],
[:end_element, :top]])
end
def test_sax_comment_no_term
parse_compare(%{