-
Notifications
You must be signed in to change notification settings - Fork 332
/
KissXMLTests.m
1714 lines (1200 loc) · 72.1 KB
/
KissXMLTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// KissXMLTests.m
// KissXMLTests
//
// Created by Christopher Ballinger on 9/23/15.
//
//
#import <XCTest/XCTest.h>
#if SWIFT_PACKAGE
@import KissXML;
#else
#import <KissXML/KissXML.h>
#endif
@interface KissXMLTests : XCTestCase
@end
@implementation KissXMLTests
- (void)setUp
{
[super setUp];
}
- (void)tearDown
{
[super tearDown];
}
- (void)testName { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSString *str = @"<body xmlns:food='http://example.com/' food:genre='italian'>"
@" <food:pizza>yumyum</food:pizza>"
@"</body>";
NSError *error = nil;
NSXMLElement *nsBody = [[NSXMLElement alloc] initWithXMLString:str error:&error];
DDXMLElement *ddBody = [[DDXMLElement alloc] initWithXMLString:str error:&error];
// Test 1 - elements
NSString *nsNodeName = [[nsBody childAtIndex:0] name];
NSString *ddNodeName = [[ddBody childAtIndex:0] name];
XCTAssert([nsNodeName isEqualToString:ddNodeName], @"Failed test 1 - ns(%@) dd(%@)", nsNodeName, ddNodeName);
// Test 2 - attributes
NSString *nsAttrName = [[nsBody attributeForName:@"food:genre"] name];
NSString *ddAttrName = [[ddBody attributeForName:@"food:genre"] name];
XCTAssert([nsAttrName isEqualToString:ddAttrName], @"Failed test 2 - ns(%@) dd(%@)", nsAttrName, ddAttrName);
}}
- (void)testLocalName { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSString *nsTest1 = [NSXMLNode localNameForName:@"a:quack"];
NSString *ddTest1 = [DDXMLNode localNameForName:@"a:quack"];
XCTAssert([nsTest1 isEqualToString:ddTest1], @"Failed test 1");
NSString *nsTest2 = [NSXMLNode localNameForName:@"a:a:quack"];
NSString *ddTest2 = [DDXMLNode localNameForName:@"a:a:quack"];
XCTAssert([nsTest2 isEqualToString:ddTest2], @"Failed test 2");
NSString *nsTest3 = [NSXMLNode localNameForName:@"quack"];
NSString *ddTest3 = [DDXMLNode localNameForName:@"quack"];
XCTAssert([nsTest3 isEqualToString:ddTest3], @"Failed test 3");
NSString *nsTest4 = [NSXMLNode localNameForName:@"a:"];
NSString *ddTest4 = [DDXMLNode localNameForName:@"a:"];
XCTAssert([nsTest4 isEqualToString:ddTest4], @"Failed test 4");
NSString *nsTest5 = [NSXMLNode localNameForName:@""];
NSString *ddTest5 = [DDXMLNode localNameForName:@""];
XCTAssert(!nsTest5.length && !ddTest5.length, @"Failed test 5");
NSXMLNode *nsNode = [NSXMLNode namespaceWithName:@"tucker" stringValue:@"dog"];
DDXMLNode *ddNode = [DDXMLNode namespaceWithName:@"tucker" stringValue:@"dog"];
NSString *nsTest6 = [nsNode localName];
NSString *ddTest6 = [ddNode localName];
XCTAssert([nsTest6 isEqualToString:ddTest6], @"Failed test 6");
}}
- (void)testPrefixName { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSString *nsTest1 = [NSXMLNode prefixForName:@"a:quack"];
NSString *ddTest1 = [DDXMLNode prefixForName:@"a:quack"];
XCTAssert([nsTest1 isEqualToString:ddTest1], @"Failed test 1");
NSString *nsTest2 = [NSXMLNode prefixForName:@"a:a:quack"];
NSString *ddTest2 = [DDXMLNode prefixForName:@"a:a:quack"];
XCTAssert([nsTest2 isEqualToString:ddTest2], @"Failed test 2");
NSString *nsTest3 = [NSXMLNode prefixForName:@"quack"];
NSString *ddTest3 = [DDXMLNode prefixForName:@"quack"];
XCTAssert([nsTest3 isEqualToString:ddTest3], @"Failed test 3");
NSString *nsTest4 = [NSXMLNode prefixForName:@"a:"];
NSString *ddTest4 = [DDXMLNode prefixForName:@"a:"];
XCTAssert([nsTest4 isEqualToString:ddTest4], @"Failed test 4");
NSString *nsTest5 = [NSXMLNode prefixForName:@""];
NSString *ddTest5 = [DDXMLNode prefixForName:@""];
XCTAssert([nsTest5 isEqualToString:ddTest5], @"Failed test 5");
NSXMLNode *nsNode = [NSXMLNode namespaceWithName:@"tucker" stringValue:@"dog"];
DDXMLNode *ddNode = [DDXMLNode namespaceWithName:@"tucker" stringValue:@"dog"];
NSString *nsTest6 = [nsNode prefix];
NSString *ddTest6 = [ddNode prefix];
XCTAssert([nsTest6 isEqualToString:ddTest6], @"Failed test 6");
}}
- (void)testNsGeneral { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSXMLNode *nsNs = [NSXMLNode namespaceWithName:@"a" stringValue:@"deusty.com"];
DDXMLNode *ddNs = [DDXMLNode namespaceWithName:@"a" stringValue:@"deusty.com"];
NSString *nsTest1 = [nsNs XMLString];
NSString *ddTest1 = [ddNs XMLString];
XCTAssert([nsTest1 isEqualToString:ddTest1], @"Failed test 1");
[nsNs setName:@"b"];
[ddNs setName:@"b"];
NSString *nsTest2 = [nsNs XMLString];
NSString *ddTest2 = [ddNs XMLString];
XCTAssert([nsTest2 isEqualToString:ddTest2], @"Failed test 2");
[nsNs setStringValue:@"robbiehanson.com"];
[ddNs setStringValue:@"robbiehanson.com"];
NSString *nsTest3 = [nsNs XMLString];
NSString *ddTest3 = [ddNs XMLString];
XCTAssert([nsTest3 isEqualToString:ddTest3], @"Failed test 3");
}}
- (void)testNsLevel { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
// <root xmlns:a="apple.com">
// <node xmlns:d="deusty.com" xmlns:rh="robbiehanson.com"/>
// </root>
NSXMLElement *nsRoot = [NSXMLElement elementWithName:@"root"];
NSXMLElement *nsNode = [NSXMLElement elementWithName:@"node"];
NSXMLNode *nsNs0 = [NSXMLNode namespaceWithName:@"a" stringValue:@"apple.com"];
NSXMLNode *nsNs1 = [NSXMLNode namespaceWithName:@"d" stringValue:@"deusty.com"];
NSXMLNode *nsNs2 = [NSXMLNode namespaceWithName:@"rh" stringValue:@"robbiehanson.com"];
[nsNode addNamespace:nsNs1];
[nsNode addNamespace:nsNs2];
[nsRoot addNamespace:nsNs0];
[nsRoot addChild:nsNode];
DDXMLElement *ddRoot = [DDXMLElement elementWithName:@"root"];
DDXMLElement *ddNode = [DDXMLElement elementWithName:@"node"];
DDXMLNode *ddNs0 = [DDXMLNode namespaceWithName:@"a" stringValue:@"apple.com"];
DDXMLNode *ddNs1 = [DDXMLNode namespaceWithName:@"d" stringValue:@"deusty.com"];
DDXMLNode *ddNs2 = [DDXMLNode namespaceWithName:@"rh" stringValue:@"robbiehanson.com"];
[ddNode addNamespace:ddNs1];
[ddNode addNamespace:ddNs2];
[ddRoot addNamespace:ddNs0];
[ddRoot addChild:ddNode];
XCTAssert([nsNs0 index] == [ddNs0 index], @"Failed test 1");
XCTAssert([nsNs1 index] == [ddNs1 index], @"Failed test 2");
XCTAssert([nsNs2 index] == [ddNs2 index], @"Failed test 3");
XCTAssert([nsNs0 level] == [ddNs0 level], @"Failed test 4");
XCTAssert([nsNs1 level] == [ddNs1 level], @"Failed test 5");
XCTAssert([nsNs2 level] == [ddNs2 level], @"Failed test 6");
}}
- (void)testNsURI { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSXMLElement *nsNode = [NSXMLElement elementWithName:@"duck" URI:@"quack.com"];
DDXMLElement *ddNode = [DDXMLElement elementWithName:@"duck" URI:@"quack.com"];
NSString *nsTest1 = [nsNode URI];
NSString *ddTest1 = [ddNode URI];
XCTAssert([nsTest1 isEqualToString:ddTest1], @"Failed test 1");
[nsNode setURI:@"food.com"];
[ddNode setURI:@"food.com"];
NSString *nsTest2 = [nsNode URI];
NSString *ddTest2 = [ddNode URI];
XCTAssert([nsTest2 isEqualToString:ddTest2], @"Failed test 2");
NSXMLNode *nsAttr = [NSXMLNode attributeWithName:@"duck" URI:@"quack.com" stringValue:@"quack"];
DDXMLNode *ddAttr = [DDXMLNode attributeWithName:@"duck" URI:@"quack.com" stringValue:@"quack"];
NSString *nsTest3 = [nsAttr URI];
NSString *ddTest3 = [ddAttr URI];
XCTAssert([nsTest3 isEqualToString:ddTest3], @"Failed test 3");
[nsAttr setURI:@"food.com"];
[ddAttr setURI:@"food.com"];
NSString *nsTest4 = [nsAttr URI];
NSString *ddTest4 = [ddAttr URI];
XCTAssert([nsTest4 isEqualToString:ddTest4], @"Failed test 4");
}}
- (void)testAddAttr { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSString *attrName = @"artist";
NSXMLElement *nsNode = [NSXMLElement elementWithName:@"song"];
DDXMLElement *ddNode = [DDXMLElement elementWithName:@"song"];
// Test adding an attribute
NSString *attrValue1 = @"John Mayer";
NSXMLNode *nsAttr1 = [NSXMLNode attributeWithName:attrName stringValue:attrValue1];
DDXMLNode *ddAttr1 = [DDXMLNode attributeWithName:attrName stringValue:attrValue1];
[nsNode addAttribute:nsAttr1];
[ddNode addAttribute:ddAttr1];
NSString *nsAttrValue1 = [[nsNode attributeForName:attrName] stringValue];
NSString *ddAttrValue1 = [[ddNode attributeForName:attrName] stringValue];
XCTAssert([nsAttrValue1 isEqualToString:attrValue1], @"Failed CHECK 1");
XCTAssert([ddAttrValue1 isEqualToString:attrValue1], @"Failed test 1");
// Test replacing an attribute
NSString *attrValue2 = @"Paramore";
NSXMLNode *nsAttr2 = [NSXMLNode attributeWithName:attrName stringValue:attrValue2];
DDXMLNode *ddAttr2 = [DDXMLNode attributeWithName:attrName stringValue:attrValue2];
[nsNode addAttribute:nsAttr2];
[ddNode addAttribute:ddAttr2];
// The documentation for NSXMLElement's addAttribute: method says this:
//
// "If the receiver already has an attribute with the same name, anAttribute is not added."
//
// However, this is NOT the case.
// If the receiver already has an attribute with the same name, the previous attribute is replaced.
//
// Considering the fact that the API does NOT contain a setAttribute method,
// I believe this should be the desired functionality.
//
// We match the functionality rather than the documentation.
NSString *nsAttrValue2 = [[nsNode attributeForName:attrName] stringValue];
NSString *ddAttrValue2 = [[ddNode attributeForName:attrName] stringValue];
XCTAssert([nsAttrValue2 isEqualToString:attrValue2], @"Failed CHECK 2");
XCTAssert([ddAttrValue2 isEqualToString:attrValue2], @"Failed test 2");
// Test removing an attribute
[nsNode removeAttributeForName:attrName];
[ddNode removeAttributeForName:attrName];
XCTAssert([nsNode attributeForName:attrName] == nil, @"Failed CHECK 3");
XCTAssert([ddNode attributeForName:attrName] == nil, @"Failed test 3");
// Test detaching an attribute
NSString *attrValue3 = @"Katy Perry";
NSXMLNode *nsAttr3 = [NSXMLNode attributeWithName:attrName stringValue:attrValue3];
DDXMLNode *ddAttr3 = [DDXMLNode attributeWithName:attrName stringValue:attrValue3];
[nsNode addAttribute:nsAttr3];
[ddNode addAttribute:ddAttr3];
NSString *nsAttrValue3 = [[nsNode attributeForName:attrName] stringValue];
NSString *ddAttrValue3 = [[ddNode attributeForName:attrName] stringValue];
XCTAssert([nsAttrValue3 isEqualToString:attrValue3], @"Failed CHECK 4");
XCTAssert([ddAttrValue3 isEqualToString:attrValue3], @"Failed test 4");
[nsAttr3 detach];
[ddAttr3 detach];
XCTAssert([nsNode attributeForName:attrName] == nil, @"Failed CHECK 5");
XCTAssert([ddNode attributeForName:attrName] == nil, @"Failed test 5");
// Test reattaching an attribute
[nsNode addAttribute:nsAttr3];
[ddNode addAttribute:ddAttr3];
nsAttrValue3 = [[nsNode attributeForName:attrName] stringValue];
ddAttrValue3 = [[ddNode attributeForName:attrName] stringValue];
XCTAssert([nsAttrValue3 isEqualToString:attrValue3], @"Failed CHECK 6");
XCTAssert([ddAttrValue3 isEqualToString:attrValue3], @"Failed test 6");
}}
- (void)testAttrGeneral { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSXMLNode *nsAttr = [NSXMLNode attributeWithName:@"apple" stringValue:@"inc"];
DDXMLNode *ddAttr = [DDXMLNode attributeWithName:@"apple" stringValue:@"inc"];
NSString *nsStr1 = [nsAttr XMLString];
NSString *ddStr1 = [ddAttr XMLString];
XCTAssert([nsStr1 isEqualToString:ddStr1], @"Failed test 1");
[nsAttr setName:@"deusty"];
[ddAttr setName:@"deusty"];
NSString *nsStr2 = [nsAttr XMLString];
NSString *ddStr2 = [ddAttr XMLString];
XCTAssert([nsStr2 isEqualToString:ddStr2], @"Failed test 2");
[nsAttr setStringValue:@"designs"];
[ddAttr setStringValue:@"designs"];
NSString *nsStr3 = [nsAttr XMLString];
NSString *ddStr3 = [ddAttr XMLString];
XCTAssert([nsStr3 isEqualToString:ddStr3], @"Failed test 3");
[nsAttr setStringValue:@"😎"];
[ddAttr setStringValue:@"😎"];
NSString *nsStr4 = [nsAttr XMLString];
NSString *ddStr4 = [ddAttr XMLString];
XCTAssert([nsStr4 isEqualToString:ddStr4], @"Failed test 4");
XCTAssert([ddStr4 containsString:@"😎"], @"Failed test 4");
}}
- (void)testAttrSiblings { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
// <duck sound="quack" swims="YES" flys="YES"/>
NSXMLElement *nsNode = [NSXMLElement elementWithName:@"duck"];
[nsNode addAttribute:[NSXMLNode attributeWithName:@"sound" stringValue:@"quack"]];
[nsNode addAttribute:[NSXMLNode attributeWithName:@"swims" stringValue:@"YES"]];
[nsNode addAttribute:[NSXMLNode attributeWithName:@"flys" stringValue:@"YES"]];
DDXMLElement *ddNode = [DDXMLElement elementWithName:@"duck"];
[ddNode addAttribute:[DDXMLNode attributeWithName:@"sound" stringValue:@"quack"]];
[ddNode addAttribute:[DDXMLNode attributeWithName:@"swims" stringValue:@"YES"]];
[ddNode addAttribute:[DDXMLNode attributeWithName:@"flys" stringValue:@"YES"]];
NSXMLNode *nsAttr = [nsNode attributeForName:@"swims"];
DDXMLNode *ddAttr = [ddNode attributeForName:@"swims"];
NSString *nsTest1 = [nsAttr XMLString];
NSString *ddTest1 = [ddAttr XMLString];
XCTAssert([nsTest1 isEqualToString:ddTest1], @"Failed test 1");
// NSLog(@"nsAttr prev: %@", [[nsAttr previousSibling] XMLString]); // nil
// NSLog(@"nsAttr next: %@", [[nsAttr nextSibling] XMLString]); // nil
// NSLog(@"ddAttr prev: %@", [[ddAttr previousSibling] XMLString]); // sound="quack"
// NSLog(@"ddAttr next: %@", [[ddAttr nextSibling] XMLString]); // flys="YES"
// Analysis: DDXML works and NSXML doesn't. I see no need to cripple DDXML because of that.
}}
- (void)testAttrDocOrder { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
// <duck sound="quack" swims="YES" flys="YES"/>
NSXMLElement *nsNode = [NSXMLElement elementWithName:@"duck"];
[nsNode addAttribute:[NSXMLNode attributeWithName:@"sound" stringValue:@"quack"]];
[nsNode addAttribute:[NSXMLNode attributeWithName:@"swims" stringValue:@"YES"]];
[nsNode addAttribute:[NSXMLNode attributeWithName:@"flys" stringValue:@"YES"]];
DDXMLElement *ddNode = [DDXMLElement elementWithName:@"duck"];
[ddNode addAttribute:[DDXMLNode attributeWithName:@"sound" stringValue:@"quack"]];
[ddNode addAttribute:[DDXMLNode attributeWithName:@"swims" stringValue:@"YES"]];
[ddNode addAttribute:[DDXMLNode attributeWithName:@"flys" stringValue:@"YES"]];
NSXMLNode *nsAttr = [nsNode attributeForName:@"swims"];
DDXMLNode *ddAttr = [ddNode attributeForName:@"swims"];
NSXMLNode *nsTest1 = [nsAttr previousNode];
DDXMLNode *ddTest1 = [ddAttr previousNode];
XCTAssert((!nsTest1 && !ddTest1), @"Failed test 1");
NSXMLNode *nsTest2 = [nsAttr nextNode];
DDXMLNode *ddTest2 = [ddAttr nextNode];
XCTAssert((!nsTest2 && !ddTest2), @"Failed test 2");
// Notes: Attributes play no part in the document order.
}}
- (void)testAttrChildren { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSXMLNode *nsAttr1 = [NSXMLNode attributeWithName:@"deusty" stringValue:@"designs"];
DDXMLNode *ddAttr1 = [DDXMLNode attributeWithName:@"deusty" stringValue:@"designs"];
NSXMLNode *nsTest1 = [nsAttr1 childAtIndex:0];
DDXMLNode *ddTest1 = [ddAttr1 childAtIndex:0];
XCTAssert((!nsTest1 && !ddTest1), @"Failed test 1");
NSUInteger nsTest2 = [nsAttr1 childCount];
NSUInteger ddTest2 = [ddAttr1 childCount];
XCTAssert((nsTest2 == ddTest2), @"Failed test 2");
NSArray *nsTest3 = [nsAttr1 children];
NSArray *ddTest3 = [ddAttr1 children];
XCTAssert((!nsTest3 && !ddTest3), @"Failed test 3");
// Notes: Attributes aren't supposed to have children, although in libxml they technically do.
// The child is simply a pointer to a text node, which contains the attribute value.
}}
- (void)testString { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
// <pizza>
// <toppings>
// <pepperoni/>
// <sausage>
// <mild/>
// <spicy/>
// </sausage>
// </toppings>
// <crust>
// <thin/>
// <thick/>
// </crust>
// </pizza>
NSXMLElement *nsNode0 = [NSXMLElement elementWithName:@"pizza"];
NSXMLElement *nsNode1 = [NSXMLElement elementWithName:@"toppings"];
NSXMLElement *nsNode2 = [NSXMLElement elementWithName:@"pepperoni"];
NSXMLElement *nsNode3 = [NSXMLElement elementWithName:@"sausage"];
NSXMLElement *nsNode4 = [NSXMLElement elementWithName:@"mild"];
NSXMLElement *nsNode5 = [NSXMLElement elementWithName:@"spicy"];
NSXMLElement *nsNode6 = [NSXMLElement elementWithName:@"crust"];
NSXMLElement *nsNode7 = [NSXMLElement elementWithName:@"thin"];
NSXMLElement *nsNode8 = [NSXMLElement elementWithName:@"thick"];
[nsNode0 addChild:nsNode1];
[nsNode0 addChild:nsNode6];
[nsNode1 addChild:nsNode2];
[nsNode1 addChild:nsNode3];
[nsNode3 addChild:nsNode4];
[nsNode3 addChild:nsNode5];
[nsNode6 addChild:nsNode7];
[nsNode6 addChild:nsNode8];
DDXMLElement *ddNode0 = [DDXMLElement elementWithName:@"pizza"];
DDXMLElement *ddNode1 = [DDXMLElement elementWithName:@"toppings"];
DDXMLElement *ddNode2 = [DDXMLElement elementWithName:@"pepperoni"];
DDXMLElement *ddNode3 = [DDXMLElement elementWithName:@"sausage"];
DDXMLElement *ddNode4 = [DDXMLElement elementWithName:@"mild"];
DDXMLElement *ddNode5 = [DDXMLElement elementWithName:@"spicy"];
DDXMLElement *ddNode6 = [DDXMLElement elementWithName:@"crust"];
DDXMLElement *ddNode7 = [DDXMLElement elementWithName:@"thin"];
DDXMLElement *ddNode8 = [DDXMLElement elementWithName:@"thick"];
[ddNode0 addChild:ddNode1];
[ddNode0 addChild:ddNode6];
[ddNode1 addChild:ddNode2];
[ddNode1 addChild:ddNode3];
[ddNode3 addChild:ddNode4];
[ddNode3 addChild:ddNode5];
[ddNode6 addChild:ddNode7];
[ddNode6 addChild:ddNode8];
NSXMLNode *nsAttr1 = [NSXMLNode attributeWithName:@"price" stringValue:@"1.00"];
DDXMLNode *ddAttr1 = [DDXMLNode attributeWithName:@"price" stringValue:@"1.00"];
[nsNode1 addAttribute:nsAttr1];
[ddNode1 addAttribute:ddAttr1];
[nsNode4 setStringValue:@"<just right>"];
[ddNode4 setStringValue:@"<just right>"];
[nsNode5 setStringValue:@"too hot"];
[ddNode5 setStringValue:@"too hot"];
NSString *nsTest1 = [nsNode0 stringValue]; // Returns "<just right>too hot"
NSString *ddTest1 = [ddNode0 stringValue];
XCTAssert([nsTest1 isEqualToString:ddTest1], @"Failed test 1");
NSString *nsTest2 = [nsAttr1 stringValue]; // Returns "1.00"
NSString *ddTest2 = [ddAttr1 stringValue];
XCTAssert([nsTest2 isEqualToString:ddTest2], @"Failed test 2");
[nsAttr1 setStringValue:@"1.25"];
[ddAttr1 setStringValue:@"1.25"];
NSString *nsTest3 = [nsAttr1 stringValue]; // Returns "1.25"
NSString *ddTest3 = [ddAttr1 stringValue];
XCTAssert([nsTest3 isEqualToString:ddTest3], @"Failed test 3");
[nsNode0 setStringValue:@"<wtf>ESCAPE</wtf>"];
[ddNode0 setStringValue:@"<wtf>ESCAPE</wtf>"];
NSString *nsTest4 = [nsNode0 stringValue]; // Returns "<wtf>ESCAPE</wtf>"
NSString *ddTest4 = [ddNode0 stringValue];
XCTAssert([nsTest4 isEqualToString:ddTest4], @"Failed test 4");
// NSString *nsTest5 = [nsNode0 XMLString]; // Returns "<pizza><wtf>ESCAPE</wtf></pizza>"
// NSString *ddTest5 = [ddNode0 XMLString]; // Returns "<pizza><wtf>ESCAPE</wtf></pizza>"
//
// NSAssert2([nsTest5 isEqualToString:ddTest5], @"Failed test 5 - ns(%@) dd(%@)", nsTest5, ddTest5);
//
// The DDXML version is actually more accurate, so we'll accept the difference.
}}
- (void)testChildren { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSMutableString *xmlStr = [NSMutableString stringWithCapacity:100];
[xmlStr appendString:@"<?xml version=\"1.0\"?>"];
[xmlStr appendString:@"<beers> "];
[xmlStr appendString:@" <sam_adams/> "];
[xmlStr appendString:@" <left_hand/> "];
[xmlStr appendString:@" <goose_island/> "];
[xmlStr appendString:@" <!-- budweiser -->"];
[xmlStr appendString:@"</beers> "];
NSXMLDocument *nsDoc = [[NSXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
DDXMLDocument *ddDoc = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
NSUInteger nsChildCount = [[nsDoc rootElement] childCount];
NSUInteger ddChildCount = [[ddDoc rootElement] childCount];
XCTAssert(nsChildCount == ddChildCount, @"Failed test 1");
NSArray *nsChildren = [[nsDoc rootElement] children];
NSArray *ddChildren = [[ddDoc rootElement] children];
XCTAssert([nsChildren count] == [ddChildren count], @"Failed test 2");
NSString *nsBeer = [[[nsDoc rootElement] childAtIndex:1] name];
NSString *ddBeer = [[[ddDoc rootElement] childAtIndex:1] name];
XCTAssert([nsBeer isEqualToString:ddBeer], @"Failed test 3");
}}
- (void)testPreviousNextNode1 { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
// <pizza>
// <toppings>
// <pepperoni/>
// <sausage>
// <mild/>
// <spicy/>
// </sausage>
// </toppings>
// <crust>
// <thin/>
// <thick/>
// </crust>
// </pizza>
NSXMLElement *nsNode0 = [NSXMLElement elementWithName:@"pizza"];
NSXMLElement *nsNode1 = [NSXMLElement elementWithName:@"toppings"];
NSXMLElement *nsNode2 = [NSXMLElement elementWithName:@"pepperoni"];
NSXMLElement *nsNode3 = [NSXMLElement elementWithName:@"sausage"];
NSXMLElement *nsNode4 = [NSXMLElement elementWithName:@"mild"];
NSXMLElement *nsNode5 = [NSXMLElement elementWithName:@"spicy"];
NSXMLElement *nsNode6 = [NSXMLElement elementWithName:@"crust"];
NSXMLElement *nsNode7 = [NSXMLElement elementWithName:@"thin"];
NSXMLElement *nsNode8 = [NSXMLElement elementWithName:@"thick"];
[nsNode0 addChild:nsNode1];
[nsNode0 addChild:nsNode6];
[nsNode1 addChild:nsNode2];
[nsNode1 addChild:nsNode3];
[nsNode3 addChild:nsNode4];
[nsNode3 addChild:nsNode5];
[nsNode6 addChild:nsNode7];
[nsNode6 addChild:nsNode8];
DDXMLElement *ddNode0 = [DDXMLElement elementWithName:@"pizza"];
DDXMLElement *ddNode1 = [DDXMLElement elementWithName:@"toppings"];
DDXMLElement *ddNode2 = [DDXMLElement elementWithName:@"pepperoni"];
DDXMLElement *ddNode3 = [DDXMLElement elementWithName:@"sausage"];
DDXMLElement *ddNode4 = [DDXMLElement elementWithName:@"mild"];
DDXMLElement *ddNode5 = [DDXMLElement elementWithName:@"spicy"];
DDXMLElement *ddNode6 = [DDXMLElement elementWithName:@"crust"];
DDXMLElement *ddNode7 = [DDXMLElement elementWithName:@"thin"];
DDXMLElement *ddNode8 = [DDXMLElement elementWithName:@"thick"];
[ddNode0 addChild:ddNode1];
[ddNode0 addChild:ddNode6];
[ddNode1 addChild:ddNode2];
[ddNode1 addChild:ddNode3];
[ddNode3 addChild:ddNode4];
[ddNode3 addChild:ddNode5];
[ddNode6 addChild:ddNode7];
[ddNode6 addChild:ddNode8];
NSString *nsTest1 = [[nsNode2 nextNode] name];
NSString *ddTest1 = [[ddNode2 nextNode] name];
NSAssert2([nsTest1 isEqualToString:ddTest1], @"Failed test 1: ns(%@) dd(%@)", nsTest1, ddTest1);
NSString *nsTest2 = [[nsNode3 nextNode] name];
NSString *ddTest2 = [[ddNode3 nextNode] name];
NSAssert2([nsTest2 isEqualToString:ddTest2], @"Failed test 2: ns(%@) dd(%@)", nsTest2, ddTest2);
NSString *nsTest3 = [[nsNode5 nextNode] name];
NSString *ddTest3 = [[ddNode5 nextNode] name];
NSAssert2([nsTest3 isEqualToString:ddTest3], @"Failed test 3: ns(%@) dd(%@)", nsTest3, ddTest3);
NSString *nsTest4 = [[nsNode5 previousNode] name];
NSString *ddTest4 = [[ddNode5 previousNode] name];
NSAssert2([nsTest4 isEqualToString:ddTest4], @"Failed test 4: ns(%@) dd(%@)", nsTest4, ddTest4);
NSString *nsTest5 = [[nsNode6 previousNode] name];
NSString *ddTest5 = [[ddNode6 previousNode] name];
NSAssert2([nsTest5 isEqualToString:ddTest5], @"Failed test 5: ns(%@) dd(%@)", nsTest5, ddTest5);
NSString *nsTest6 = [[nsNode8 nextNode] name];
NSString *ddTest6 = [[ddNode8 nextNode] name];
NSAssert2((!nsTest6 && !ddTest6), @"Failed test 6: ns(%@) dd(%@)", nsTest6, ddTest6);
NSString *nsTest7 = [[nsNode0 previousNode] name];
NSString *ddTest7 = [[ddNode0 previousNode] name];
NSAssert2((!nsTest7 && !ddTest7), @"Failed test 7: ns(%@) dd(%@)", nsTest7, ddTest7);
}}
- (void)testPreviousNextNode2 { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSMutableString *xmlStr = [NSMutableString stringWithCapacity:100];
[xmlStr appendString:@"<?xml version=\"1.0\"?>"];
[xmlStr appendString:@"<pizza> "];
[xmlStr appendString:@" <toppings> "];
[xmlStr appendString:@" <pepperoni/>"];
[xmlStr appendString:@" <sausage> "];
[xmlStr appendString:@" <mild/> "];
[xmlStr appendString:@" <spicy/> "];
[xmlStr appendString:@" </sausage> "];
[xmlStr appendString:@" </toppings> "];
[xmlStr appendString:@" <crust> "];
[xmlStr appendString:@" <thin/> "];
[xmlStr appendString:@" <thick/> "];
[xmlStr appendString:@" </crust> "];
[xmlStr appendString:@"</pizza> "];
NSXMLDocument *nsDoc = [[NSXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
DDXMLDocument *ddDoc = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
NSXMLNode *nsNode0 = [nsDoc rootElement]; // pizza
DDXMLNode *ddNode0 = [ddDoc rootElement]; // pizza
NSXMLNode *nsNode2 = [[[nsDoc rootElement] childAtIndex:0] childAtIndex:0]; // pepperoni
DDXMLNode *ddNode2 = [[[ddDoc rootElement] childAtIndex:0] childAtIndex:0]; // pepperoni
NSXMLNode *nsNode3 = [[[nsDoc rootElement] childAtIndex:0] childAtIndex:1]; // sausage
DDXMLNode *ddNode3 = [[[ddDoc rootElement] childAtIndex:0] childAtIndex:1]; // sausage
NSXMLNode *nsNode5 = [[[[nsDoc rootElement] childAtIndex:0] childAtIndex:1] childAtIndex:1]; // spicy
DDXMLNode *ddNode5 = [[[[ddDoc rootElement] childAtIndex:0] childAtIndex:1] childAtIndex:1]; // spicy
NSXMLNode *nsNode6 = [[nsDoc rootElement] childAtIndex:1]; // crust
DDXMLNode *ddNode6 = [[ddDoc rootElement] childAtIndex:1]; // crust
NSXMLNode *nsNode8 = [[[nsDoc rootElement] childAtIndex:1] childAtIndex:1]; // crust
DDXMLNode *ddNode8 = [[[ddDoc rootElement] childAtIndex:1] childAtIndex:1]; // crust
NSString *nsTest1 = [[nsNode2 nextNode] name];
NSString *ddTest1 = [[ddNode2 nextNode] name];
NSAssert2([nsTest1 isEqualToString:ddTest1], @"Failed test 1: ns(%@) dd(%@)", nsTest1, ddTest1);
NSString *nsTest2 = [[nsNode3 nextNode] name];
NSString *ddTest2 = [[ddNode3 nextNode] name];
NSAssert2([nsTest2 isEqualToString:ddTest2], @"Failed test 2: ns(%@) dd(%@)", nsTest2, ddTest2);
NSString *nsTest3 = [[nsNode5 nextNode] name];
NSString *ddTest3 = [[ddNode5 nextNode] name];
NSAssert2([nsTest3 isEqualToString:ddTest3], @"Failed test 3: ns(%@) dd(%@)", nsTest3, ddTest3);
NSString *nsTest4 = [[nsNode5 previousNode] name];
NSString *ddTest4 = [[ddNode5 previousNode] name];
NSAssert2([nsTest4 isEqualToString:ddTest4], @"Failed test 4: ns(%@) dd(%@)", nsTest4, ddTest4);
NSString *nsTest5 = [[nsNode6 previousNode] name];
NSString *ddTest5 = [[ddNode6 previousNode] name];
NSAssert2([nsTest5 isEqualToString:ddTest5], @"Failed test 5: ns(%@) dd(%@)", nsTest5, ddTest5);
NSString *nsTest6 = [[nsNode8 nextNode] name];
NSString *ddTest6 = [[ddNode8 nextNode] name];
NSAssert2((!nsTest6 && !ddTest6), @"Failed test 6: ns(%@) dd(%@)", nsTest6, ddTest6);
NSString *nsTest7 = [[nsNode0 previousNode] name];
NSString *ddTest7 = [[ddNode0 previousNode] name];
NSAssert2((!nsTest7 && !ddTest7), @"Failed test 7: ns(%@) dd(%@)", nsTest7, ddTest7);
}}
- (void)testPrefix { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
// <root xmlns:a="beagle" xmlns:b="lab">
// <dog/>
// <a:dog/>
// <a:b:dog/>
// <dog xmlns="beagle"/>
// </root>
NSXMLElement *nsNode1 = [NSXMLElement elementWithName:@"dog"];
NSXMLElement *nsNode2 = [NSXMLElement elementWithName:@"a:dog"];
NSXMLElement *nsNode3 = [NSXMLElement elementWithName:@"a:b:dog"];
NSXMLElement *nsNode4 = [NSXMLElement elementWithName:@"dog" URI:@"beagle"];
DDXMLElement *ddNode1 = [DDXMLElement elementWithName:@"dog"];
DDXMLElement *ddNode2 = [DDXMLElement elementWithName:@"a:dog"];
DDXMLElement *ddNode3 = [DDXMLElement elementWithName:@"a:b:dog"];
DDXMLElement *ddNode4 = [DDXMLElement elementWithName:@"dog" URI:@"beagle"];
NSString *nsTest1 = [nsNode1 prefix];
NSString *ddTest1 = [ddNode1 prefix];
XCTAssert([nsTest1 isEqualToString:ddTest1], @"Failed test 1");
NSString *nsTest2 = [nsNode2 prefix];
NSString *ddTest2 = [ddNode2 prefix];
XCTAssert([nsTest2 isEqualToString:ddTest2], @"Failed test 2");
NSString *nsTest3 = [nsNode3 prefix];
NSString *ddTest3 = [ddNode3 prefix];
XCTAssert([nsTest3 isEqualToString:ddTest3], @"Failed test 3");
NSString *nsTest4 = [nsNode4 prefix];
NSString *ddTest4 = [ddNode4 prefix];
XCTAssert([nsTest4 isEqualToString:ddTest4], @"Failed test 4");
}}
- (void)testURI { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
// <root xmlns:a="deusty.com" xmlns:b="robbiehanson.com">
// <test test="1"/>
// <a:test test="2"/>
// <b:test test="3"/>
// <test xmlns="deusty.com" test="4"/>
// <test xmlns="quack.com" test="5"/>
// </root>
NSXMLElement *nsRoot = [NSXMLElement elementWithName:@"root"];
[nsRoot addNamespace:[NSXMLNode namespaceWithName:@"a" stringValue:@"deusty.com"]];
[nsRoot addNamespace:[NSXMLNode namespaceWithName:@"b" stringValue:@"robbiehanson.com"]];
NSXMLElement *nsNode1 = [NSXMLElement elementWithName:@"test"];
[nsNode1 addAttribute:[NSXMLNode attributeWithName:@"test" stringValue:@"1"]];
NSXMLElement *nsNode2 = [NSXMLElement elementWithName:@"a:test"];
[nsNode2 addAttribute:[NSXMLNode attributeWithName:@"test" stringValue:@"2"]];
NSXMLElement *nsNode3 = [NSXMLElement elementWithName:@"b:test"];
[nsNode3 addAttribute:[NSXMLNode attributeWithName:@"test" stringValue:@"3"]];
NSXMLElement *nsNode4 = [NSXMLElement elementWithName:@"test" URI:@"deusty.com"];
[nsNode4 addAttribute:[NSXMLNode attributeWithName:@"test" stringValue:@"4"]];
NSXMLElement *nsNode5 = [NSXMLElement elementWithName:@"test" URI:@"quack.com"];
[nsNode5 addAttribute:[NSXMLNode attributeWithName:@"test" stringValue:@"5"]];
[nsRoot addChild:nsNode1];
[nsRoot addChild:nsNode2];
[nsRoot addChild:nsNode3];
[nsRoot addChild:nsNode4];
[nsRoot addChild:nsNode5];
DDXMLElement *ddRoot = [DDXMLElement elementWithName:@"root"];
[ddRoot addNamespace:[DDXMLNode namespaceWithName:@"a" stringValue:@"deusty.com"]];
[ddRoot addNamespace:[DDXMLNode namespaceWithName:@"b" stringValue:@"robbiehanson.com"]];
DDXMLElement *ddNode1 = [DDXMLElement elementWithName:@"test"];
[ddNode1 addAttribute:[DDXMLNode attributeWithName:@"test" stringValue:@"1"]];
DDXMLElement *ddNode2 = [DDXMLElement elementWithName:@"a:test"];
[ddNode2 addAttribute:[DDXMLNode attributeWithName:@"test" stringValue:@"2"]];
DDXMLElement *ddNode3 = [DDXMLElement elementWithName:@"b:test"];
[ddNode3 addAttribute:[DDXMLNode attributeWithName:@"test" stringValue:@"3"]];
DDXMLElement *ddNode4 = [DDXMLElement elementWithName:@"test" URI:@"deusty.com"];
[ddNode4 addAttribute:[DDXMLNode attributeWithName:@"test" stringValue:@"4"]];
DDXMLElement *ddNode5 = [DDXMLElement elementWithName:@"test" URI:@"quack.com"];
[ddNode5 addAttribute:[DDXMLNode attributeWithName:@"test" stringValue:@"5"]];
[ddRoot addChild:ddNode1];
[ddRoot addChild:ddNode2];
[ddRoot addChild:ddNode3];
[ddRoot addChild:ddNode4];
[ddRoot addChild:ddNode5];
NSString *nsTest1 = [[nsNode1 resolveNamespaceForName:[nsNode1 name]] stringValue];
NSString *ddTest1 = [[ddNode1 resolveNamespaceForName:[ddNode1 name]] stringValue];
XCTAssert(!nsTest1 && !ddTest1, @"Failed test 1");
NSString *nsTest2 = [[nsNode2 resolveNamespaceForName:[nsNode2 name]] stringValue];
NSString *ddTest2 = [[ddNode2 resolveNamespaceForName:[ddNode2 name]] stringValue];
NSAssert2([nsTest2 isEqualToString:ddTest2], @"Failed test 2: ns(%@) dd(%@)", nsTest2, ddTest2);
NSString *nsTest3 = [[nsNode3 resolveNamespaceForName:[nsNode3 name]] stringValue];
NSString *ddTest3 = [[ddNode3 resolveNamespaceForName:[ddNode3 name]] stringValue];
XCTAssert([nsTest3 isEqualToString:ddTest3], @"Failed test 3");
NSString *nsTest4 = [[nsNode4 resolveNamespaceForName:[nsNode4 name]] stringValue];
NSString *ddTest4 = [[ddNode4 resolveNamespaceForName:[ddNode4 name]] stringValue];
NSAssert2(!nsTest4 && !ddTest4, @"Failed test 4: ns(%@) dd(%@)", nsTest4, ddTest4);
NSString *nsTest5 = [nsNode4 resolvePrefixForNamespaceURI:@"deusty.com"];
NSString *ddTest5 = [ddNode4 resolvePrefixForNamespaceURI:@"deusty.com"];
NSAssert2([nsTest5 isEqualToString:ddTest5], @"Failed test 5: ns(%@) dd(%@)", nsTest5, ddTest5);
NSString *nsTest6 = [nsNode4 resolvePrefixForNamespaceURI:@"robbiehanson.com"];
NSString *ddTest6 = [ddNode4 resolvePrefixForNamespaceURI:@"robbiehanson.com"];
XCTAssert([nsTest6 isEqualToString:ddTest6], @"Failed test 6");
NSString *nsTest7 = [nsNode4 resolvePrefixForNamespaceURI:@"quack.com"];
NSString *ddTest7 = [ddNode4 resolvePrefixForNamespaceURI:@"quack.com"];
XCTAssert(!nsTest7 && !ddTest7, @"Failed test 7");
NSString *nsTest8 = [nsNode4 resolvePrefixForNamespaceURI:@""];
NSString *ddTest8 = [ddNode4 resolvePrefixForNamespaceURI:@""];
XCTAssert(!nsTest8 && !ddTest8, @"Failed test 8");
NSUInteger nsTest9 = [[nsRoot elementsForName:@"test"] count]; // Returns test1, test4, test5
NSUInteger ddTest9 = [[ddRoot elementsForName:@"test"] count]; // Returns test1, test4, test5
XCTAssert(nsTest9 == ddTest9, @"Failed test 9");
NSUInteger nsTest10 = [[nsRoot elementsForName:@"a:test"] count]; // Returns node2 and node4
NSUInteger ddTest10 = [[ddRoot elementsForName:@"a:test"] count]; // Returns node2 and node4
XCTAssert(nsTest10 == ddTest10, @"Failed test 10");
NSUInteger nsTest11 = [[nsRoot elementsForLocalName:@"test" URI:@"deusty.com"] count]; // Returns node2 and node4
NSUInteger ddTest11 = [[ddRoot elementsForLocalName:@"test" URI:@"deusty.com"] count]; // Returns node2 and node4
XCTAssert(nsTest11 == ddTest11, @"Failed test 11");
NSUInteger nsTest12 = [[nsRoot elementsForLocalName:@"a:test" URI:@"deusty.com"] count]; // Returns nothing
NSUInteger ddTest12 = [[ddRoot elementsForLocalName:@"a:test" URI:@"deusty.com"] count]; // Returns nothing
XCTAssert(nsTest12 == ddTest12, @"Failed test 12");
NSUInteger nsTest13 = [[nsRoot elementsForLocalName:@"test" URI:@"quack.com"] count]; // Returns node5
NSUInteger ddTest13 = [[ddRoot elementsForLocalName:@"test" URI:@"quack.com"] count]; // Returns node5
XCTAssert(nsTest13 == ddTest13, @"Failed test 13");
}}
- (void)testXmlns { @autoreleasepool
{
NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
NSString *parseMe = @"<query xmlns=\"jabber:iq:roster\"></query>";
NSData *data = [parseMe dataUsingEncoding:NSUTF8StringEncoding];
NSXMLDocument *nsDoc = [[NSXMLDocument alloc] initWithData:data options:0 error:nil];
NSXMLElement *nsRootElement = [nsDoc rootElement];
DDXMLDocument *ddDoc = [[DDXMLDocument alloc] initWithData:data options:0 error:nil];
DDXMLElement *ddRootElement = [ddDoc rootElement];
// Both URI and namespaceForPrefix:@"" should return "jabber:iq:roster"
NSString *nsTest1 = [nsRootElement URI];
NSString *ddTest1 = [ddRootElement URI];
XCTAssert([nsTest1 isEqualToString:ddTest1], @"Failed test 1");
NSString *nsTest2 = [[nsRootElement namespaceForPrefix:@""] stringValue];
NSString *ddTest2 = [[ddRootElement namespaceForPrefix:@""] stringValue];
XCTAssert([nsTest2 isEqualToString:ddTest2], @"Failed test 2");
// In NSXML namespaceForPrefix:nil returns nil
// In DDXML namespaceForPrefix:nil returns the same as namespaceForPrefix:@""
//
// This actually makes more sense, as many users would consider a prefix of nil or an empty string to be the same.
// Plus many XML documents state that a prefix of nil or "" should be treated equally.
//
// This difference comes into play in other areas.
//
// In NSXML creating a namespace with prefix:nil doesn't work.
// In DDXML creating a namespace with prefix:nil acts as if you had passed an empty string.
NSUInteger nsTest3 = [[nsRootElement namespaces] count];
NSUInteger ddTest3 = [[ddRootElement namespaces] count];
XCTAssert(nsTest3 == ddTest3, @"Failed test 3");
// An odd quirk of NSXML is that if the data is parsed, then the namespaces array contains the default namespace.
// However, if the XML tree is generated in code, and the default namespace was set via setting the URI,
// then the namespaces array doesn't contain that default namespace.
// If instead the addNamespace method is used to add the default namespace, then it is contained in the array,
// and the URI is also properly set.
//
// I consider this to be a bug in NSXML.
NSString *nsTest4 = [[nsRootElement resolveNamespaceForName:@""] stringValue];
NSString *ddTest4 = [[ddRootElement resolveNamespaceForName:@""] stringValue];
XCTAssert([nsTest4 isEqualToString:ddTest4], @"Failed test 4");
// Oddly enough, even though NSXML seems completely resistant to nil namespace prefixes, this works fine
NSString *nsTest5 = [[nsRootElement resolveNamespaceForName:@""] stringValue];
NSString *ddTest5 = [[ddRootElement resolveNamespaceForName:@""] stringValue];
XCTAssert([nsTest5 isEqualToString:ddTest5], @"Failed test 5");
NSXMLElement *nsNode = [NSXMLElement elementWithName:@"query"];
[nsNode addNamespace:[NSXMLNode namespaceWithName:@"" stringValue:@"jabber:iq:auth"]];