1# -*- tcl -*-
2# xsxp.test:  tests for the xsxp package.
3
4# This file contains a collection of tests for the xsxp
5# package. Sourcing this file into Tcl runs the tests and generates
6# output for errors.  No output means no errors were found.
7
8# Copyright (c) 2006,2008 Darren New. All Rights Reserved.
9# Copyright (c) 2008 Andreas Kupries <andreas_kupries@users.sourceforge.net>
10#               (Boilerplate stuff (header, footer))
11# All rights reserved.
12#
13# RCS: @(#) $Id: xsxp.test,v 1.3 2008/09/04 02:11:13 andreas_kupries Exp $
14
15# -------------------------------------------------------------------------
16
17source [file join \
18	[file dirname [file dirname [file join [pwd] [info script]]]] \
19	devtools testutilities.tcl]
20
21testsNeedTcl     8.5
22testsNeedTcltest 2.0
23
24if {[catch {package require xml}]} {
25    puts "    Aborting the tests found in \"[file tail [info script]]\""
26    puts "    Requiring xml package, not found."
27    return
28}
29
30support {
31    # Requires xml (TclXML)
32}
33testing {
34    useLocal xsxp.tcl xsxp
35}
36
37# -------------------------------------------------------------------------
38package require -exact xsxp 1.0
39
40tcltest::configure -verbose {body error pass}
41tcltest::configure -debug 1
42
43set setup_one {
44    set xml {<?xml version="1.0" encoding="UTF-8"?>
45<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Owner><ID>9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd</ID><DisplayName>dnew@san.rr.com</DisplayName></Owner><AccessControlList><Grant><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser"><ID>9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd</ID><DisplayName>dnew@san.rr.com</DisplayName></Grantee><Permission>FULL_CONTROL</Permission></Grant></AccessControlList></AccessControlPolicy>}
46}
47
48tcltest::test xsxp-1.10 {Basic parsing} -setup $setup_one -body {
49    set pxml [::xsxp::parse $xml]
50    return [lindex $pxml 0]
51} -result {AccessControlPolicy}
52
53tcltest::test xsxp-1.20 {Precision parsing} -setup $setup_one -body {
54    return [::xsxp::parse $xml]
55} -result {AccessControlPolicy {} {Owner {} {ID {} {%PCDATA {} 9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd}} {DisplayName {} {%PCDATA {} dnew@san.rr.com}}} {AccessControlList {} {Grant {} {Grantee {xsi:type CanonicalUser} {ID {} {%PCDATA {} 9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd}} {DisplayName {} {%PCDATA {} dnew@san.rr.com}}} {Permission {} {%PCDATA {} FULL_CONTROL}}}}}
56
57tcltest::test xsxp-1.30 {Test pretty printing} -setup $setup_one -body {
58    ::xsxp::prettyprint [::xsxp::parse $xml]
59} -output {AccessControlPolicy
60  Owner
61    ID
62      %PCDATA: 9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd
63    DisplayName
64      %PCDATA: dnew@san.rr.com
65  AccessControlList
66    Grant
67      Grantee xsi:type='CanonicalUser'
68        ID
69          %PCDATA: 9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd
70        DisplayName
71          %PCDATA: dnew@san.rr.com
72      Permission
73        %PCDATA: FULL_CONTROL
74}
75
76tcltest::test xsxp-1.40 {Access via path string} -setup $setup_one -body {
77    set pxml [::xsxp::parse $xml]
78    return [::xsxp::fetch $pxml "Owner/DisplayName" %PCDATA]
79} -result {dnew@san.rr.com}
80
81tcltest::test xsxp-1.50 {Access via path list} -setup $setup_one -body {
82    set pxml [::xsxp::parse $xml]
83    return [::xsxp::fetch $pxml "Owner DisplayName" %PCDATA]
84} -result {dnew@san.rr.com}
85
86set setup_two {
87set xml {<?xml version="1.0" encoding="UTF-8"?>
88<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Name>darren</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>t1.jpg</Key><LastModified>2006-10-27T23:19:07.000Z</LastModified><ETag>&quot;a251eabc2e69e9716878924b6ec291c7&quot;</ETag><Size>1512545</Size><Owner><ID>9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd</ID><DisplayName>dnew@san.rr.com</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>t2.jpg</Key><LastModified>2006-10-27T23:19:44.000Z</LastModified><ETag>&quot;ebc9b242811239ada85f202346353f31&quot;</ETag><Size>1826062</Size><Owner><ID>9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd</ID><DisplayName>dnew@san.rr.com</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>}
89set pxml [::xsxp::parse $xml]
90}
91
92tcltest::test xsxp-2.10 {Fetch top-level item} -setup $setup_two -body {
93    ::xsxp::fetch $pxml MaxKeys
94} -result {MaxKeys {} {%PCDATA {} 1000}}
95
96set c0 {Contents {} {Key {} {%PCDATA {} t1.jpg}} {LastModified {} {%PCDATA {} 2006-10-27T23:19:07.000Z}} {ETag {} {%PCDATA {} {"a251eabc2e69e9716878924b6ec291c7"}}} {Size {} {%PCDATA {} 1512545}} {Owner {} {ID {} {%PCDATA {} 9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd}} {DisplayName {} {%PCDATA {} dnew@san.rr.com}}} {StorageClass {} {%PCDATA {} STANDARD}}}
97
98tcltest::test xsxp-2.20 {Fetch another top-level item} -setup $setup_two -body {
99    ::xsxp::fetch $pxml Contents
100} -result $c0
101
102tcltest::test xsxp-2.30 {Fetch #0 item} -setup $setup_two -body {
103    ::xsxp::fetch $pxml Contents
104} -result $c0
105
106set c1 {Contents {} {Key {} {%PCDATA {} t2.jpg}} {LastModified {} {%PCDATA {} 2006-10-27T23:19:44.000Z}} {ETag {} {%PCDATA {} {"ebc9b242811239ada85f202346353f31"}}} {Size {} {%PCDATA {} 1826062}} {Owner {} {ID {} {%PCDATA {} 9fb13c24488e3d7556693247d5a463c1837c3c8ede28f4094228e6c4eb5d70bd}} {DisplayName {} {%PCDATA {} dnew@san.rr.com}}} {StorageClass {} {%PCDATA {} STANDARD}}}
107
108tcltest::test xsxp-2.40 {Fetch #1 item} -setup $setup_two -body {
109    ::xsxp::fetch $pxml Contents#1
110} -result $c1
111
112tcltest::test xsxp-2.50 {Fetch item past end} -setup $setup_two -body {
113    ::xsxp::fetch $pxml Contents#2
114} -result {}
115
116tcltest::test xsxp-2.60 {Check %TAGNAME} -setup $setup_two -body {
117    ::xsxp::fetch $pxml #4 %TAGNAME
118} -result {IsTruncated}
119
120tcltest::test xsxp-2.70 {check merge of PCDATA} -setup $setup_two -body {
121    ::xsxp::fetch $pxml Contents/ETag %PCDATA
122} -result {"a251eabc2e69e9716878924b6ec291c7"}
123
124tcltest::test xsxp-2.80 {Check lack of PCDATA} -setup $setup_two -body {
125    ::xsxp::fetch $pxml Prefix %PCDATA
126} -returnCodes 1 -result "xsxp::fetch did not find requested PCDATA"
127
128tcltest::test xsxp-2.90 {Check lack of PCDATA?} -setup $setup_two -body {
129    ::xsxp::fetch $pxml Prefix %PCDATA?
130} -result ""
131
132
133tcltest::test xsxp-3.10 {only} -setup $setup_two -body {
134    set only [::xsxp::only $pxml Contents]
135    return [list [llength $only] [lindex $only 0 0] [lindex $only 1 0]]
136} -result {2 Contents Contents}
137
138tcltest::test xsxp-4.10 {fetchall basic} -setup $setup_two -body {
139    set only [::xsxp::only $pxml Contents]
140    ::xsxp::fetchall $only Key %PCDATA
141} -result {t1.jpg t2.jpg}
142
143tcltest::test xsxp-5.10 {only} -setup $setup_two -body {
144    set only [::xsxp::only $pxml Contents]
145    ::xsxp::fetch $pxml Contents#1/Key/%PCDATA %CHILDREN
146} -result {t2.jpg}
147
148
149
150if {0} {
151    foreach file [glob -directory xml *] {
152	puts $file
153	if {".xml" != [string range $file end-3 end]} continue
154	set in [open $file r]
155	set xml [read $in]
156	close $in
157	set pxml [::xsxp::parse $xml]
158	set out [open [string range $file 0 end-4].txt w] ; #lazy
159	::xsxp::prettyprint $pxml $out
160	close $out
161    }
162}
163
164#----------------------------------------------------------------------
165testsuiteCleanup
166puts "Done!" ; after 5000
167