class TestXMLFormatter

Public Instance Methods

ok(e, w) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-format-xml.rb, line 149
def ok(e, w)
  ok_eq(e, w.rb_format_xml)
  ok_eq(e, w.format_xml)    # assert twice
end
ok_rb(e, w) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-format-xml.rb, line 154
def ok_rb(e, w)
  ok_eq(e, w.rb_format_xml)
end
test_all() click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-format-xml.rb, line 167
    def test_all
      # test_basic
      ok("<a\n/>", [:a])
      ok("<a\n>t</a\n>", [:a, 't'])
      ok("<a\n><b\n></b\n></a\n>", [:a, [:b, '']])
      ok("<a href=\"t\"\n>s</a\n>", [:a, {:href=>'t'}, 's'])

      # test_escape
      ok("<a\n>&lt;</a\n>", [:a, '<'])
      ok("<a href=\"&lt;\"\n/>", [:a, {:href=>'<'}])
      ok("<a &gt;=\"&lt;\"\n/>", [:a, {'>'=>'<'}])
      ok("<a &lt;=\"&lt;\"\n/>", [:a, {'<'=>'<'}])
      ok("<&lt;\n/>", [:'<'])
      ok("<&lt; &amp;=\"&lt;\"\n/>", [:'<', {'&'=>'<'}])

      # test_doctype
      ok("<!DOCTYPE html PUBLIC \"-//W3C//DTD html 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">",
         [:"!DOCTYPE", 'html', 'PUBLIC',
           '-//W3C//DTD html 4.01 Transitional//EN',
           'http://www.w3.org/TR/html4/loose.dtd'])
      ok("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",
         [:"!DOCTYPE", 'html', 'PUBLIC',
           '-//W3C//DTD XHTML 1.0 Transitional//EN',
           'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'])

      # test_comment
      ok('<!--comment-->', [:'!--', 'comment'])
      ok("<!--<-->", [:"!--", "<"])

      # test_text
      ok('', [])
      ok('', [''])
      ok('a', ['a'])
      ok('ab', ['a', 'b'])
      ok('abc', ['a', ['b'], 'c'])

      # test_null
      ok("<a\n></a\n>", [:a, ''])
      ok("<a\n></a\n>", [:a, nil])
      ok("<a\n></a\n>", [:a, []])
      ok("<a\n>nil</a\n>", [:a, :nil])
      ok("<p id=\"\"\n/>", [:p, {:id=>''}])
      ok("<p id=\"\"\n/>", [:p, {:id=>nil}])
      ok("<p id=\"\"\n/>", [:p, {:id=>[]}])
      ok("<p\n>b</p\n>", [:p, [['b']]])
      ok("<p\n>bc</p\n>", [:p, ['b'], 'c'])
      ok("<p id=\"\"\n></p\n>", [:p, {:id=>''}, ''])

      # test_non_destructive
      w = [:a]
      ok("<a\n/>", w)
      ok("<a\n/>", w)

      # test_bug
      ok("<a\n/><b\n/>", [[:a], [:b]])

      # test_format_xml # copied from gonzui-0.3
      html = [:html]
      head = [:head, [:title, 'foo']]
      body = [:body, [:h1, [:a, {:href => 'foo<&">.html'}, 'foo<&">']]]
      body.push([:p, 'hello'])
      html.push(head)
      html.push(body)
      ok("<html\n><head\n><title\n>foo</title\n></head\n><body\n><h1\n><a href=\"foo&lt;&amp;&quot;&gt;.html\"\n>foo&lt;&amp;&quot;&gt;</a\n></h1\n><p\n>hello</p\n></body\n></html\n>", html)

      # test_general
      ok(
"<html
><head
><title
>hello</title
></head
><body
><h1
>hello, world!</h1
><p
>This is <a href=\"hello.html\"
>hello, world</a
>example.</p
></body
></html
>",
         [:html,
           [:head,
             [:title, 'hello']],
           [:body,
             [:h1, 'hello, world!'],
             [:p, 'This is ',
               [:a, {:href=>'hello.html'}, 'hello, world'],
               'example.']]]
         )

      # test_etc
      ok_rb("<html lang=\"ja\" xml:lang=\"ja\" xmlns=\"http://www.w3.org/1999/xhtml\"\n></html\n>",
         [:html, {:xmlns=>'http://www.w3.org/1999/xhtml',
             :'xml:lang'=>'ja', :lang=>'ja'}, ''])
      ok_rb("<meta content=\"text/html; charset=shift_jis\" http-equiv=\"Content-Type\"\n/>",
         [:meta, {:'http-equiv'=>'Content-Type',
             :content=>'text/html; charset=shift_jis'}])

      # test_cdata
      ok("<![CDATA[cdata]]>", [:'![CDATA[', 'cdata'])
      ok("<![CDATA[<]]>", [:'![CDATA[', "<"])

      # test_xmldecl
      ok("<?xml?>", [:'?xml'])
      ok("<?xml version=\"1.0\"?>", [:'?xml', '1.0'])
      ok("<?xml version=\"1.0\" encoding=\"utf-8\"?>",
         [:'?xml', '1.0', 'utf-8'])
      ok("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>",
         [:'?xml', '1.0', 'utf-8', 'yes'])
      ok("<?xml version=\"1.0\" encoding=\"utf-8\"?><a\n/>",
         [[:'?xml', '1.0', 'utf-8'], [:a]])

      # check_wabisabi_output
      html = [:html,
        [:head,
          [:title, 'hello']],
        [:body,
          [:h1, 'hello, world!'],
          [:p, 'This is a ',
            [:a, {:href=>'hello.html'}, 'hello, world'],
            ' example.']]]
      #puts html.format_xml
      ok("<html
><head
><title
>hello</title
></head
><body
><h1
>hello, world!</h1
><p
>This is a <a href=\"hello.html\"
>hello, world</a
> example.</p
></body
></html
>",
         html)
    end
test_rb_format_xml() click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-format-xml.rb, line 158
def test_rb_format_xml
  ok_eq("<a\n/>", [:a].rb_format_xml(0))
  ok_eq("<a></a\n>", [:a, ""].rb_format_xml(0, -1))
  ok_eq("<a\n></a\n>", [:a, ""].rb_format_xml)
  ok_eq("<a><b></b></a>", [:a, [:b, '']].rb_format_xml(-1, -1))
  ok_eq("<a><b></b\n></a\n>", [:a, [:b, ""]].rb_format_xml(0, -1))
  ok_eq("<a\n><b\n></b\n></a\n>", [:a, [:b, ""]].rb_format_xml)
end