## Features covered: simple parser
#
# This file contains a collection of tests for the simple parser.
# Tested functionalities:
#    simple-1.*: Basic function, parsing
#    simple-2.*: Character encoding
#
# Copyright (c) 2002 Rolf Ade.
#
# RCS: @(#) $Id$

source [file join [file dir [info script]] loadtdom.tcl]

test simple-1.1 {document as only root node} {
    set doc [dom parse -simple {<doc/>}]
    set root [$doc documentElement]
    set result [$root asXML -indent none]
    $doc delete
    set result
} {<doc/>}

test simple-1.2 {parsing} {
    set doc [dom parse -simple {<doc><e/><e/><e/></doc>}]
    set root [$doc documentElement]
    set result [llength [$root childNodes]]
    $doc delete
    set result
} {3}

test simple-1.3 {simple parser - parsing} {knownBug} {
    set doc [dom parse -simple {<doc/> }]
    set root [$doc documentElement]
    set result [$root asXML -indent none]
    $doc delete
    set result
} {<doc/>}

test simple-1.4 {simple parser - parsing} {
    set doc [dom parse -simple {<doc><e/><e/><e/></doc> }]
    set root [$doc documentElement]
    set result [$root asXML -indent none]
    $doc delete
    set result
} {<doc><e/><e/><e/></doc>}

test simple-1.5 {simple parser - -keepEmpties} {
    set doc [dom parse -simple -keepEmpties {<doc></doc>}]
    set root [$doc documentElement]
    set result [$root hasChildNodes]
    $doc delete
    set result
} {0}

test simple-1.6 {simple parser - empty CDATA section} {
    set doc [dom parse -simple -keepEmpties {<doc><![CDATA[]]></doc>}]
    set root [$doc documentElement]
    set result [$root hasChildNodes]
    $doc delete
    set result
} {0}

test simple-1.7 {simple parser - -keepEmpties} {
    set doc [dom parse -simple -keepEmpties {<doc> </doc>}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<doc> </doc>}

test simple-1.8 {simple parser - empty comment} {
    set doc [dom parse -simple {<doc><!----></doc>}]
    set root [$doc documentElement]
    set result [$root hasChildNodes]
    lappend result [[$root firstChild] nodeValue]
    $doc delete
    set result
} {1 {}}

test simple-1.9 {simple parser - not wellformed input} {
    catch {[dom parse -simple {<doc><child/>}]}
} 1

test simple-1.10 {simple parser - not wellformed input} {
    catch {[dom parse -simple {<doc><chi}]}
} 1

test simple-1.11 {simple doesn't catch all not wellformed input} {
     catch {dom parse -simple {<xsl:transform 
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform
                    <http://www.w3.org/1999/XSL/Transform> "/>}}
} 0

test simple-1.12 {CDATA section} {
    set doc [dom parse -simple {<doc>foo <![CDATA[test of & <bad> format]]> bar </doc>}]
    set result [$doc selectNodes count(doc/node())]
    $doc delete
    set result
} 1

test simple-1.13 {CDATA section} {
    set doc [dom parse -simple {<doc>foo <![CDATA[test of & <bad> format]]> bar </doc>}]
    set root [$doc documentElement]
    set result [list]
    foreach child [$root childNodes] {
        lappend result [$child nodeType]
    }
    $doc delete
    set result
} {TEXT_NODE}

test simple-1.14 {CDATA section} {
    set doc [dom parse -simple {<doc>&lt;foo &amp; &gt;<![CDATA[test of & <bad> format]]>&apos; bar &quot;</doc>}]
    set result [$doc selectNodes string(doc)]
    $doc delete
    set result
} {<foo & >test of & <bad> format' bar "}
# emacs: "

test simple-1.15 {-keepCDATA} {
    set doc [dom parse -simple -keepCDATA {<doc>foo <![CDATA[test of & <bad> format]]> bar </doc>}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<doc>foo <![CDATA[test of & <bad> format]]> bar </doc>}

test simple-1.16 {-keepCDATA} {
    set doc [dom parse -simple -keepCDATA {<doc>foo <![CDATA[test of & <bad> format]]> bar </doc>}]
    set root [$doc documentElement]
    set result [list]
    foreach child [$root childNodes] {
        lappend result [$child nodeType]
    }
    $doc delete
    set result
} {TEXT_NODE CDATA_SECTION_NODE TEXT_NODE}

test simple-1.17 {-keepCDATA} {
    set doc [dom parse -simple -keepCDATA {<doc><e><![CDATA[one]]></e></doc>}]
    set result [list]
    foreach child [$doc selectNodes doc/e/node()] {
        lappend result [$child nodeType]
    }
    $doc delete
    set result
} {CDATA_SECTION_NODE}

test simple-1.18 {-keepCDATA} {
    set doc [dom parse -simple -keepCDATA {<doc><e><![CDATA[one]]><![CDATA[two]]></e></doc>}]
    set result [list]
    foreach child [$doc selectNodes doc/e/node()] {
        lappend result [$child nodeType]
    }
    $doc delete
    set result
} {CDATA_SECTION_NODE CDATA_SECTION_NODE}

test simple-1.19 {-keepCDATA} {
    set doc [dom parse -simple -keepCDATA {<doc><e><![CDATA[]]></e></doc>}]
    set result [$doc selectNodes count(doc/e/node())]
    $doc delete
    set result
} 0

test simple-1.20 {-keepCDATA white space only CDATA section} {
    set doc [dom parse -simple -keepCDATA {<doc><e><![CDATA[
    ]]></e></doc>}]
    set result [$doc selectNodes count(doc/e/node())]
    $doc delete
    set result
} 0

test simple-1.21 {-keepCDATA and -keepEmpties} {
    set doc [dom parse -simple -keepCDATA -keepEmpties {<doc><e><![CDATA[]]></e></doc>}]
    set result [$doc selectNodes count(doc/e/node())]
    $doc delete
    set result
} 1

test simple-1.22 {namespaces} {
    set doc [dom parse -simple {
        <help><br xmlns:xsi="a"/><em xmlns:xsi="a">notes</em></help>
    }]
    $doc delete
} {}

test simple-1.23 {-forest} {
    set doc [dom parse -simple -forest {<one>1</one><two>2</two>}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<one>1</one><two>2</two>}

test simple-1.24 {-forest} {
    catch {dom parse -simple -forest {<one>1</one><two>2}}
} {1}

test simple-1.25 {-forest} {
    set doc [dom parse -simple -forest {<one>1</one>some text<two>2</two>other text}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<one>1</one>some text<two>2</two>other text}

test simple-1.26 {-forest} {
    set xml {<one><two></one>}
    catch {dom parse -simple $xml} errMsg1
    catch {dom parse -simple -forest $xml} errMsg2
    expr {$errMsg1 eq $errMsg2}
} {1}

test simple-1.27 {-forest} {
    set xml {<one><two/></one>}
    set doc [dom parse -simple -forest $xml]
    $doc documentElement root
    set result [llength [$root selectNodes //two]]
    $doc delete
    set result
} 1

test simple-1.28 {-forest} {
    set xml {<div>something</div><div>something else</div>}
    set doc [dom parse -simple -forest $xml]
    set result ""
    foreach child [$doc selectNodes /*] {
        lappend result [$child parentNode]
    }
    $doc delete
    set result
} {{} {}}

test simple-1.29 {parse long string} {64bit Tcl9 longRunning largedata groklargedata} {
    set doc [dom parse -simple "<doc>[string repeat [string repeat [string repeat abc 1000] 1000] 1000]</doc>"]
    set result [string length [$doc selectNodes string(/doc)]]
    lappend result [string length [$doc selectNodes substring(doc,2999999999)]]
    lappend result [$doc selectNodes substring(doc,2999999995,3)]
    $doc delete
    set result
} {3000000000 2 abc}

test simple-2.1 {XML build in entities} {
    set doc [dom parse -simple {<doc>&lt;&gt;&amp;&apos;&quot;</doc>}]
    set root [$doc documentElement]
    set result [$root text]
    $doc delete
    set result
} {<>&'"}
# emacs: "

test simple-2.2 {character entities} {
    set doc [dom parse -simple {<doc>&#65;&#x42;</doc>}]
    set root [$doc documentElement]
    set result [$root text]
    $doc delete
    set result
} {AB}

test simple-2.3 {character entities} {
    set doc [dom parse -simple {<doc>&#214;&#xC4;&#xc4;</doc>}]
    set root [$doc documentElement]
    set result [$root text]
    $doc delete
    set result
} "\u00d6\u00c4\u00c4"

test simple-2.4 {invalid entity reference in attribute} {
    catch {dom parse -simple {<doc><e att="a&ampc"/></doc>}} errMsg
    set errMsg
} {error "Entity parsing error" at position 20
"<doc><e att="a&ampc"/ <--Error-- ></doc>"}

test simple-2.5 {invalid entity reference in element content} {
    catch {dom parse -simple {<doc>foo&ampl;bar"/></doc>}} errMsg
    set errMsg
} {error "Entity parsing error" at position 13
"<doc>foo&ampl; <--Error-- bar"/></doc>"}

test simple-2.6 {invalid character entity reference in element content} {
    catch {dom parse -simple {<doc>foo&#l;bar"/></doc>}} errMsg
    set errMsg
} {error "Entity parsing error" at position 10
"<doc>foo&#l <--Error-- ;bar"/></doc>"}

# cleanup
::tcltest::cleanupTests
return
