@@ -20,6 +20,7 @@ class TestXMLDocument : LoopbackServerTest {
20
20
( " test_stringValue " , test_stringValue) ,
21
21
( " test_objectValue " , test_objectValue) ,
22
22
( " test_attributes " , test_attributes) ,
23
+ ( " test_attributesWithNamespace " , test_attributesWithNamespace) ,
23
24
( " test_comments " , test_comments) ,
24
25
( " test_processingInstruction " , test_processingInstruction) ,
25
26
( " test_parseXMLString " , test_parseXMLString) ,
@@ -264,6 +265,52 @@ class TestXMLDocument : LoopbackServerTest {
264
265
XCTAssertEqual ( element. attribute ( forName: " hello " ) ? . stringValue, " world " , " \( element. attribute ( forName: " hello " ) ? . stringValue as Optional ) " )
265
266
XCTAssertEqual ( element. attribute ( forName: " foobar " ) ? . stringValue, " buzbaz " , " \( element. attributes ?? [ ] ) " )
266
267
}
268
+
269
+ func test_attributesWithNamespace( ) {
270
+ let uriNs1 = " http://example.com/ns1 "
271
+ let uriNs2 = " http://example.com/ns2 "
272
+
273
+ let root = XMLNode . element ( withName: " root " ) as! XMLElement
274
+ root. addNamespace ( XMLNode . namespace ( withName: " ns1 " , stringValue: uriNs1) as! XMLNode )
275
+
276
+ let element = XMLNode . element ( withName: " element " ) as! XMLElement
277
+ element. addNamespace ( XMLNode . namespace ( withName: " ns2 " , stringValue: uriNs2) as! XMLNode )
278
+ root. addChild ( element)
279
+
280
+ // Add attributes without URI
281
+ element. addAttribute ( XMLNode . attribute ( withName: " name " , stringValue: " John " ) as! XMLNode )
282
+ element. addAttribute ( XMLNode . attribute ( withName: " ns1:name " , stringValue: " Tom " ) as! XMLNode )
283
+
284
+ // Add attributes with URI
285
+ element. addAttribute ( XMLNode . attribute ( withName: " ns1:age " , uri: uriNs1, stringValue: " 44 " ) as! XMLNode )
286
+ element. addAttribute ( XMLNode . attribute ( withName: " ns2:address " , uri: uriNs2, stringValue: " Foobar City " ) as! XMLNode )
287
+
288
+ // Retrieve attributes without URI
289
+ XCTAssertEqual ( element. attribute ( forName: " name " ) ? . stringValue, " John " , " name==John " )
290
+ XCTAssertEqual ( element. attribute ( forName: " ns1:name " ) ? . stringValue, " Tom " , " ns1:name==Tom " )
291
+ XCTAssertEqual ( element. attribute ( forName: " ns1:age " ) ? . stringValue, " 44 " , " ns1:age==44 " )
292
+ XCTAssertEqual ( element. attribute ( forName: " ns2:address " ) ? . stringValue, " Foobar City " , " ns2:addresss==Foobar City " )
293
+
294
+ // Retrieve attributes with URI
295
+ XCTAssertEqual ( element. attribute ( forLocalName: " name " , uri: nil ) ? . stringValue, " John " , " name==John " )
296
+ XCTAssertEqual ( element. attribute ( forLocalName: " name " , uri: uriNs1) ? . stringValue, " Tom " , " name==Tom " )
297
+ XCTAssertEqual ( element. attribute ( forLocalName: " age " , uri: uriNs1) ? . stringValue, " 44 " , " age==44 " )
298
+ XCTAssertNil ( element. attribute ( forLocalName: " address " , uri: uriNs1) , " address==nil " )
299
+ XCTAssertEqual ( element. attribute ( forLocalName: " address " , uri: uriNs2) ? . stringValue, " Foobar City " , " addresss==Foobar City " )
300
+
301
+ // Overwrite attributes
302
+ element. addAttribute ( XMLNode . attribute ( withName: " ns1:age " , stringValue: " 33 " ) as! XMLNode )
303
+ XCTAssertEqual ( element. attribute ( forName: " ns1:age " ) ? . stringValue, " 33 " , " ns1:age==33 " )
304
+ element. addAttribute ( XMLNode . attribute ( withName: " ns1:name " , uri: uriNs1, stringValue: " Tommy " ) as! XMLNode )
305
+ XCTAssertEqual ( element. attribute ( forLocalName: " name " , uri: uriNs1) ? . stringValue, " Tommy " , " ns1:name==Tommy " )
306
+
307
+ // Remove attributes
308
+ element. removeAttribute ( forName: " name " )
309
+ XCTAssertNil ( element. attribute ( forLocalName: " name " , uri: nil ) , " name removed " )
310
+ XCTAssertNotNil ( element. attribute ( forLocalName: " name " , uri: uriNs1) , " ns1:name not removed " )
311
+ element. removeAttribute ( forName: " ns1:name " )
312
+ XCTAssertNil ( element. attribute ( forLocalName: " name " , uri: uriNs1) , " ns1:name removed " )
313
+ }
267
314
268
315
func test_comments( ) {
269
316
let element = XMLElement ( name: " root " )
0 commit comments