# Features covered:  XML Namespaces
#
# This file tests the parser's performance on XML namespaces.
# Sourcing this file into Tcl runs the tests and generates output
# for errors.  No output means no errors were found.
#
# Copyright (c) 2000 Zveno Pty Ltd.
#
# $Id$

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

proc keysort args {
    array set keyvalue $args
    set result {}
    foreach key [lsort [array names keyvalue]] {
	lappend result $key $keyvalue($key)
    }
    return $result
}

catch {unset result}
catch {unset nsdecls}
proc EStart {tag attlist args} {
    global result nsdecls

    array set extra $args

    catch {eval lappend nsdecls $extra(-namespacedecls)}

    if {[info exists extra(-namespace)]} {
	lappend result $extra(-namespace)^$tag
    } else {
	lappend result $tag
    }
}

proc NSDeclStart {prefix uri} {
    global result

    lappend result $prefix
    lappend result $uri
}

test ns-1.1 {Namespace declaration} {
    set ::result {}
    set ::nsdecls {}

    catch {rename xml::ns-1.1 {}}
    set parser [xml::parser ns-1.1 \
            -namespace \
            -startnamespacedeclcommand NSDeclStart \
            -elementstartcommand EStart]
    $parser parse {<?xml version="1.0"?>
<Test xmlns:test="http://www.zveno.com/Schemas"></Test>
}
    list $::result $::nsdecls
} {{test http://www.zveno.com/Schemas Test} {}}

test ns-1.2 {Multiple namespace declarations} {
    set ::result {}
    set ::nsdecls {}

    catch {rename xml::ns-1.2 {}}
    set parser [xml::parser ns-1.2 \
            -namespace \
            -startnamespacedeclcommand NSDeclStart \
            -elementstartcommand EStart]
    $parser parse {<?xml version="1.0"?>
<Test xmlns:test="http://www.zveno.com/Schemas"
xmlns:x='urn:schema'></Test>
}
    list $::result [eval keysort $::nsdecls]
} {{test http://www.zveno.com/Schemas x urn:schema Test} {}}

test ns-1.3 {Default namespace declaration} {
    set ::result {}
    set ::nsdecls {}

    catch {rename xml::ns-1.3 {}}
    set parser [xml::parser ns-1.3 \
            -namespace \
            -startnamespacedeclcommand NSDeclStart \
            -elementstartcommand EStart]
    $parser parse {<?xml version="1.0"?>
<Test xmlns="http://www.zveno.com/Schemas"
xmlns:x='urn:schema'></Test>
}
    list $::result [eval keysort $::nsdecls]
} {{{} http://www.zveno.com/Schemas x urn:schema http://www.zveno.com/Schemas:Test} {}}

test ns-1.4 {Default namespace declaration w/- separate usage} {
    set ::result {}
    set ::nsdecls {}

    catch {rename xml::ns-1.4 {}}
    set parser [xml::parser ns-1.4 \
            -namespace \
            -startnamespacedeclcommand NSDeclStart \
            -elementstartcommand EStart]
    $parser parse {<?xml version="1.0"?>
<x:Test xmlns="http://www.zveno.com/Schemas"
xmlns:x='urn:schema'><Test/></x:Test>
}
    list $::result [eval keysort $::nsdecls]
} {{{} http://www.zveno.com/Schemas x urn:schema urn:schema:Test http://www.zveno.com/Schemas:Test} {}}

test ns-2.0 {Multiple namespace declarations, same prefix} {
    set ::result {}
    set ::nsdecls {}

    catch {rename xml::ns-2.0 {}}
    set parser [xml::parser ns-2.0 \
            -namespace \
            -startnamespacedeclcommand NSDeclStart \
            -elementstartcommand EStart]
    $parser parse {<?xml version="1.0"?>
<Test>
  <x:Test xmlns:x="http://www.zveno.com/Schemas">
    <x:y/>
  </x:Test>
  <x:Test xmlns:x='urn:schema'>
    <x:z/>
  </x:Test>
</Test>
}
    list $::result [eval keysort $::nsdecls]
} {{Test x http://www.zveno.com/Schemas http://www.zveno.com/Schemas:Test http://www.zveno.com/Schemas:y x urn:schema urn:schema:Test urn:schema:z} {}}

foreach parser [info commands ns-*] {
    $parser free
}

# cleanup
::tcltest::cleanupTests
return
