xamplr-pp 1.0.0 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,197 @@
1
+ # xampl-pp : XML pull parser
2
+ # Copyright (C) 2002-2009 Bob Hutchison
3
+ #
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+ #
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # #Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+ #
18
+ require "Lapidary/TestCase"
19
+ require "xampl-pp"
20
+
21
+ class XppTestNamespace < Xampl_PP
22
+ def doPushText(c)
23
+ pushTextBuffer(c)
24
+ end
25
+
26
+ def doParseText(delimiter, resolve)
27
+ parseText(delimiter, resolve)
28
+ end
29
+
30
+ def getTextBuffer
31
+ @textBuffer
32
+ end
33
+
34
+ def clearTextBuffer
35
+ @textBuffer = ''
36
+ end
37
+
38
+ def makeStart
39
+ @type = START_ELEMENT
40
+ end
41
+
42
+ def deeper(prefix, name)
43
+ @elementPrefix.push prefix
44
+ @elementName.push name
45
+ @elementQName.push (nil == prefix) ? name : (prefix + ':' + name)
46
+ @elementNamespace.push prefix
47
+ end
48
+
49
+ def doPeekType
50
+ peekType
51
+ end
52
+ end
53
+
54
+ class TC_Namespace < Lapidary::TestCase
55
+ def setup
56
+ @xpp = XppTestParse.new
57
+
58
+ @xppReporting = XppTestParse.new
59
+ @xppReporting.reportNamespaceAttributes = true
60
+ end
61
+ #def tearDown
62
+ #end
63
+
64
+ def testNSDefault
65
+ pp = @xpp
66
+ pp.input = "<start attr1='value1' attr2='value2' xmlns='default-namespace'>"
67
+
68
+ eventType = pp.nextEvent
69
+ assert pp.startElement?
70
+ assert 'start' == pp.name
71
+ assert 1 == pp.elementName.length
72
+ assert 1 == pp.elementQName.length
73
+ assert 1 == pp.elementNamespace.length
74
+ assert 1 == pp.elementPrefix.length
75
+
76
+ assert 2 == pp.attributeName.length
77
+ assert 2 == pp.attributeQName.length
78
+ assert 2 == pp.attributeNamespace.length
79
+ assert 2 == pp.attributePrefix.length
80
+ assert 2 == pp.attributeValue.length
81
+
82
+ assert 'start' == pp.elementName[0]
83
+ assert 'start' == pp.elementQName[0]
84
+ assert 'default-namespace' == pp.elementNamespace[0]
85
+ assert nil == pp.elementPrefix[0]
86
+
87
+ assert 'attr1' == pp.attributeName[0]
88
+ assert 'attr1' == pp.attributeQName[0]
89
+ assert nil == pp.attributeNamespace[0]
90
+ assert nil == pp.attributePrefix[0]
91
+ assert 'value1' == pp.attributeValue[0]
92
+
93
+ assert 'attr2' == pp.attributeName[1]
94
+ assert 'attr2' == pp.attributeQName[1]
95
+ assert nil == pp.attributeNamespace[1]
96
+ assert nil == pp.attributePrefix[1]
97
+ assert 'value2' == pp.attributeValue[1]
98
+ end
99
+
100
+ def testNSDefaultNSReporting
101
+ pp = @xppReporting
102
+ pp.input = "<start attr1='value1' attr2='value2' xmlns='default-namespace'>"
103
+
104
+ eventType = pp.nextEvent
105
+ assert pp.startElement?
106
+ assert 'start' == pp.name
107
+ assert 1 == pp.elementName.length
108
+ assert 1 == pp.elementQName.length
109
+ assert 1 == pp.elementNamespace.length
110
+ assert 1 == pp.elementPrefix.length
111
+
112
+ assert 3 == pp.attributeName.length
113
+ assert 3 == pp.attributeQName.length
114
+ assert 3 == pp.attributeNamespace.length
115
+ assert 3 == pp.attributePrefix.length
116
+ assert 3 == pp.attributeValue.length
117
+
118
+ assert 'start' == pp.elementName[0]
119
+ assert 'start' == pp.elementQName[0]
120
+ assert 'default-namespace' == pp.elementNamespace[0]
121
+ assert nil == pp.elementPrefix[0]
122
+
123
+ assert 'attr1' == pp.attributeName[0]
124
+ assert 'attr1' == pp.attributeQName[0]
125
+ assert nil == pp.attributeNamespace[0]
126
+ assert nil == pp.attributePrefix[0]
127
+ assert 'value1' == pp.attributeValue[0]
128
+
129
+ assert 'attr2' == pp.attributeName[1]
130
+ assert 'attr2' == pp.attributeQName[1]
131
+ assert nil == pp.attributeNamespace[1]
132
+ assert nil == pp.attributePrefix[1]
133
+ assert 'value2' == pp.attributeValue[1]
134
+
135
+ assert nil == pp.attributeName[2]
136
+ assert 'xmlns' == pp.attributeQName[2]
137
+ assert 'http://www.w3.org/2000/xmlns/' == pp.attributeNamespace[2]
138
+ assert 'xmlns' == pp.attributePrefix[2]
139
+ assert 'default-namespace' == pp.attributeValue[2]
140
+ end
141
+
142
+ def testNSqualified000
143
+ pp = @xppReporting
144
+ pp.input = "<ns:start attr1='value1' attr2='value2' xmlns='default-namespace' xmlns:ns='ns-namespace'>"
145
+
146
+ eventType = pp.nextEvent
147
+ assert pp.startElement?
148
+ assert 'start' == pp.name
149
+ assert 1 == pp.elementName.length
150
+ assert 4 == pp.attributeName.length
151
+
152
+ assert 'start' == pp.name
153
+ assert 'ns:start' == pp.qname
154
+ assert 'ns' == pp.prefix
155
+ assert 'ns-namespace' == pp.namespace
156
+
157
+ assert 'ns' == pp.attributeName[3]
158
+ assert 'xmlns:ns' == pp.attributeQName[3]
159
+ assert 'ns-namespace' == pp.attributeNamespace[3]
160
+ assert 'xmlns' == pp.attributePrefix[3]
161
+ assert 'ns-namespace' == pp.attributeValue[3]
162
+ end
163
+
164
+ def testNSqualified001
165
+ pp = @xppReporting
166
+ pp.input = "<ns:start attr1='value1' ns:attr2='value2' xmlns='default-namespace' xmlns:ns='ns-namespace'>"
167
+
168
+ eventType = pp.nextEvent
169
+ assert pp.startElement?
170
+ assert 'start' == pp.name
171
+ assert 1 == pp.elementName.length
172
+ assert 4 == pp.attributeName.length
173
+
174
+ assert 'start' == pp.name
175
+ assert 'ns:start' == pp.qname
176
+ assert 'ns' == pp.prefix
177
+ assert 'ns-namespace' == pp.namespace
178
+
179
+ assert 'attr2' == pp.attributeName[1]
180
+ assert 'ns:attr2' == pp.attributeQName[1]
181
+ assert 'ns-namespace' == pp.attributeNamespace[1]
182
+ assert 'ns' == pp.attributePrefix[1]
183
+ assert 'value2' == pp.attributeValue[1]
184
+ end
185
+
186
+ def testNSqualifiedNonExistant
187
+ pp = @xppReporting
188
+ pp.input = "<ns:start attr1='value1' x:attr2='value2' xmlns='default-namespace' xmlns:ns='ns-namespace'>"
189
+ assertRaises( RuntimeError ) { eventType = pp.nextEvent }
190
+
191
+ pp = @xppReporting
192
+ pp.input = "<x:start attr1='value1' ns:attr2='value2' xmlns='default-namespace' xmlns:ns='ns-namespace'>"
193
+ assertRaises( RuntimeError ) { eventType = pp.nextEvent }
194
+ end
195
+
196
+ end
197
+