require 'test/unit' require 'htree/scan' class TestScan < Test::Unit::TestCase def scan(str) result = [] HTree.scan(str) {|e| result << e } result end def test_empty assert_equal([], scan('')) end def t_single(s) n = yield assert_equal([n], scan(s)) end def test_single s = '' assert_equal([[:xmldecl, s]], scan(s)) s = '' assert_equal([[:doctype, s]], scan(s)) s = '' assert_equal([[:procins, s]], scan(s)) s = '' assert_equal([[:stag, s]], scan(s)) s = '' assert_equal([[:etag, s]], scan(s)) s = '' assert_equal([[:emptytag, s]], scan(s)) s = '' assert_equal([[:comment, s]], scan(s)) s = '' assert_equal([[:text_cdata_section, s]], scan(s)) s = 'abc' assert_equal([[:text_pcdata, s]], scan(s)) end def test_xmldecl_seen s0 = '' s1 = '' assert_equal([[:stag, s1]], scan(s1)) assert_equal([[:xmldecl, s0], [:stag, s1]], scan(s0 + s1)) end def test_cdata_content s = '' assert_equal([ [:stag, ''], [:stag, ''], [:stag, ''], ], scan(s)) s = '\nd\n\ne" assert_equal([ [:stag, ""], [:text_pcdata, "a\n"], [:stag, ""], [:text_pcdata, "\nb\n"], [:stag, ""], [:text_pcdata, "\nd\n"], [:etag, ""], [:text_pcdata, "\ne"], ], scan(s)) end def test_eol_xml # In XML, line breaks are treated as part of content. # It's because KEEPRSRE is yes in XML. # http://www.satoshii.org/markup/websgml/valid-xml#keeprsre s = "a\n\nb\n\nc\n\nd\n\ne" assert_equal([ [:xmldecl, ""], [:text_pcdata, "a\n"], [:stag, ""], [:text_pcdata, "\nb\n"], [:stag, ""], [:text_pcdata, "\nc\n"], [:etag, ""], [:text_pcdata, "\nd\n"], [:etag, ""], [:text_pcdata, "\ne"], ], scan(s)) end def test_xml_html_detection assert_equal([false, true], HTree.scan("") {}) assert_equal([true, false], HTree.scan("") {}) assert_equal([true, true], HTree.scan('') {}) end def test_quoted_attr assert_equal([[:emptytag, '']], scan('')) end def test_bare_slash assert_equal([[:stag, '']], scan('')) assert_equal([[:stag, '']], scan('')) end end