Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding another test case - found a discrepancy in Apple's documentation.

  • Loading branch information...
commit 46084996c1b4df34baa0c43b74d10ba3aa16ce5f 1 parent 12c142c
@robbiehanson authored
View
22 DDXMLElement.m
@@ -217,19 +217,37 @@ - (NSArray *)elementsWithName:(NSString *)name uri:(NSString *)uri
#pragma mark Attributes
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+- (BOOL)hasAttributeWithName:(NSString *)name
+{
+ // If we use xmlUnsetProp, then the attribute will be automatically freed.
+ // We don't want this unless no other wrapper objects have a reference to the property.
+
+ xmlAttrPtr attr = ((xmlNodePtr)genericPtr)->properties;
+ while(attr != NULL)
+ {
+ if(xmlStrEqual(attr->name, [name xmlChar]))
+ {
+ return YES;
+ }
+ attr = attr->next;
+ }
+
+ return NO;
+}
+
- (void)addAttribute:(DDXMLNode *)attribute
{
// NSXML version uses this same assertion
DDCheck([attribute hasParent] == NO, @"Cannot add an attribute with a parent; detach or copy first");
DDCheck([attribute isXmlAttrPtr], @"Not an attribute");
+ [self removeAttributeForName:[attribute name]];
+
// xmlNodePtr xmlAddChild(xmlNodePtr parent, xmlNodePtr cur)
// Add a new node to @parent, at the end of the child (or property) list merging
// adjacent TEXT nodes (in which case @cur is freed). If the new node is ATTRIBUTE, it is added
// into properties instead of children. If there is an attribute with equal name, it is first destroyed.
- [self removeAttributeForName:[attribute name]];
-
xmlAddChild((xmlNodePtr)genericPtr, (xmlNodePtr)attribute->genericPtr);
}
View
104 DDXMLTesting.m
@@ -10,6 +10,7 @@ + (void)testDoubleAdd;
+ (void)testNsGeneral;
+ (void)testNsLevel;
+ (void)testNsURI;
++ (void)testAddAttr;
+ (void)testAttrGeneral;
+ (void)testAttrSiblings;
+ (void)testAttrDocOrder;
@@ -46,6 +47,7 @@ + (void)performTests
[self testNsGeneral];
[self testNsLevel];
[self testNsURI];
+ [self testAddAttr];
[self testAttrGeneral];
[self testAttrSiblings];
[self testAttrDocOrder];
@@ -121,7 +123,7 @@ + (void)testLocalName
NSAssert([nsTest6 isEqualToString:ddTest6], @"Failed test 6");
- [pool release];
+ [pool drain];
}
+ (void)testPrefixName
@@ -162,7 +164,7 @@ + (void)testPrefixName
NSAssert([nsTest6 isEqualToString:ddTest6], @"Failed test 6");
- [pool release];
+ [pool drain];
}
+ (void)testDoubleAdd
@@ -210,7 +212,7 @@ + (void)testDoubleAdd
// [ddRoot1 addNamespace:ddNs];
// [ddRoot2 addNamespace:ddNs]; // Cannot add a namespace with a parent; detach or copy first
- [pool release];
+ [pool drain];
}
+ (void)testNsGeneral
@@ -242,7 +244,7 @@ + (void)testNsGeneral
NSAssert([nsTest3 isEqualToString:ddTest3], @"Failed test 3");
- [pool release];
+ [pool drain];
}
+ (void)testNsLevel
@@ -282,7 +284,7 @@ + (void)testNsLevel
NSAssert([nsNs1 level] == [ddNs1 level], @"Failed test 5");
NSAssert([nsNs2 level] == [ddNs2 level], @"Failed test 6");
- [pool release];
+ [pool drain];
}
+ (void)testNsURI
@@ -322,7 +324,57 @@ + (void)testNsURI
NSAssert([nsTest4 isEqualToString:ddTest4], @"Failed test 4");
- [pool release];
+ [pool drain];
+}
+
++ (void)testAddAttr
+{
+ NSLog(@"Starting %@...", NSStringFromSelector(_cmd));
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ NSXMLElement *nsNode = [NSXMLElement elementWithName:@"song"];
+ DDXMLElement *ddNode = [DDXMLElement elementWithName:@"song"];
+
+ NSXMLNode *nsAttr1 = [NSXMLNode attributeWithName:@"artist" stringValue:@"John Mayer"];
+ DDXMLNode *ddAttr1 = [DDXMLNode attributeWithName:@"artist" stringValue:@"John Mayer"];
+
+ [nsNode addAttribute:nsAttr1];
+ [ddNode addAttribute:ddAttr1];
+
+ NSAssert([nsNode attributeForName:@"artist"] == nsAttr1, @"Failed CHECK 1");
+ NSAssert([ddNode attributeForName:@"artist"] == ddAttr1, @"Failed test 1");
+
+ NSXMLNode *nsAttr2 = [NSXMLNode attributeWithName:@"artist" stringValue:@"Paramore"];
+ DDXMLNode *ddAttr2 = [DDXMLNode attributeWithName:@"artist" stringValue:@"Paramore"];
+
+ [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.
+ //
+ // We match the functionality rather than the documentation.
+
+ NSAssert([nsNode attributeForName:@"artist"] == nsAttr2, @"Failed CHECK 2");
+ NSAssert([ddNode attributeForName:@"artist"] == ddAttr2, @"Failed test 2");
+
+ [nsNode removeAttributeForName:@"artist"];
+ [ddNode removeAttributeForName:@"artist"];
+
+ NSAssert([nsNode attributeForName:@"artist"] == nil, @"Failed CHECK 3");
+ NSAssert([ddNode attributeForName:@"artist"] == nil, @"Failed test 3");
+
+ [nsNode addAttribute:nsAttr2];
+ [ddNode addAttribute:ddAttr2];
+
+ NSAssert([nsNode attributeForName:@"artist"] == nsAttr2, @"Failed CHECK 4");
+ NSAssert([ddNode attributeForName:@"artist"] == ddAttr2, @"Failed test 4");
+
+ [pool drain];
}
+ (void)testAttrGeneral
@@ -354,7 +406,7 @@ + (void)testAttrGeneral
NSAssert([nsStr3 isEqualToString:ddStr3], @"Failed test 3");
- [pool release];
+ [pool drain];
}
+ (void)testAttrSiblings
@@ -390,7 +442,7 @@ + (void)testAttrSiblings
// Analysis: DDXML works and NSXML doesn't. I see no need to cripple DDXML because of that.
- [pool release];
+ [pool drain];
}
+ (void)testAttrDocOrder
@@ -425,7 +477,7 @@ + (void)testAttrDocOrder
// Notes: Attributes play no part in the document order.
- [pool release];
+ [pool drain];
}
+ (void)testAttrChildren
@@ -454,7 +506,7 @@ + (void)testAttrChildren
// 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.
- [pool release];
+ [pool drain];
}
+ (void)testString
@@ -559,7 +611,7 @@ + (void)testString
//
// The DDXML version is actually more accurate, so we'll accept the difference.
- [pool release];
+ [pool drain];
}
+ (void)testChildren
@@ -594,7 +646,7 @@ + (void)testChildren
NSAssert([nsBeer isEqualToString:ddBeer], @"Failed test 3");
- [pool release];
+ [pool drain];
}
+ (void)testPreviousNextNode1
@@ -689,7 +741,7 @@ + (void)testPreviousNextNode1
NSAssert2((!nsTest7 && !ddTest7), @"Failed test 7: ns(%@) dd(%@)", nsTest7, ddTest7);
- [pool release];
+ [pool drain];
}
+ (void)testPreviousNextNode2
@@ -769,7 +821,7 @@ + (void)testPreviousNextNode2
NSAssert2((!nsTest7 && !ddTest7), @"Failed test 7: ns(%@) dd(%@)", nsTest7, ddTest7);
- [pool release];
+ [pool drain];
}
+ (void)testPrefix
@@ -814,7 +866,7 @@ + (void)testPrefix
NSAssert([nsTest4 isEqualToString:ddTest4], @"Failed test 4");
- [pool release];
+ [pool drain];
}
+ (void)testURI
@@ -945,7 +997,7 @@ + (void)testURI
NSAssert(nsTest13 == ddTest13, @"Failed test 13");
- [pool release];
+ [pool drain];
}
+ (void)testXmlns
@@ -1035,7 +1087,7 @@ + (void)testXmlns
NSAssert(nsTest9 == ddTest9, @"Failed test 9");
- [pool release];
+ [pool drain];
}
+ (void)testCopy
@@ -1132,7 +1184,7 @@ + (void)testCopy
NSAssert([ddAttrValue isEqualToString:@"4"], @"Failed test 9");
NSAssert([ddAttrCopyValue isEqualToString:@"5"], @"Failed test 10");
- [pool release];
+ [pool drain];
}
+ (void)testCData
@@ -1161,7 +1213,7 @@ + (void)testCData
[ddDoc release];
- [pool release];
+ [pool drain];
}
+ (void)testElements
@@ -1209,7 +1261,7 @@ + (void)testElements
}
[ddDoc release];
- [pool release];
+ [pool drain];
}
+ (void)testXPath
@@ -1324,7 +1376,7 @@ + (void)testXPath
NSAssert([[nsAttr XPath] isEqualToString:[ddAttr XPath]], @"Failed test 8");
- [pool release];
+ [pool drain];
}
+ (void)testNodesForXPath
@@ -1406,7 +1458,7 @@ + (void)testNodesForXPath
NSAssert([nsTest4 count] == [ddTest4 count], @"Failed test 8");
- [pool release];
+ [pool drain];
}
+ (void)testNSXMLBugs
@@ -1440,7 +1492,7 @@ + (void)testNSXMLBugs
[nsDoc release];
[ddDoc release];
- [pool release];
+ [pool drain];
}
+ (void)testInsertChild
@@ -1489,7 +1541,7 @@ + (void)testInsertChild
// [nsParent insertChild:nsChild5 atIndex:5]; // Exception - index (5) beyond bounds (5)
// [ddParent insertChild:ddChild5 atIndex:5]; // Exception - index (5) beyond bounds (5)
- [pool release];
+ [pool drain];
}
+ (void)testElementSerialization
@@ -1512,7 +1564,7 @@ + (void)testElementSerialization
NSAssert([[nse XMLString] isEqualToString:[dde XMLString]], @"Failed test 2");
- [pool release];
+ [pool drain];
}
+ (void)testAttributeWithColonInName
@@ -1531,7 +1583,7 @@ + (void)testAttributeWithColonInName
NSAssert(nsa != nil, @"Failed CHECK 1");
NSAssert(dda != nil, @"Failed test 1");
- [pool release];
+ [pool drain];
}
@end
View
12 KissXML.xcodeproj/project.pbxproj
@@ -210,7 +210,11 @@
isa = PBXProject;
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "KissXML" */;
compatibilityVersion = "Xcode 3.1";
+ developmentRegion = English;
hasScannedForEncodings = 1;
+ knownRegions = (
+ en,
+ );
mainGroup = 29B97314FDCFA39411CA2CEA /* KissXML */;
projectDirPath = "";
projectRoot = "";
@@ -304,30 +308,30 @@
C01FCF4F08A954540054247B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
- ARCHS = "$(ARCHS_STANDARD_32_BIT)";
+ ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
GCC_C_LANGUAGE_STANDARD = c99;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = /usr/include/libxml2;
+ MACOSX_DEPLOYMENT_TARGET = 10.6;
ONLY_ACTIVE_ARCH = YES;
OTHER_LDFLAGS = "-lxml2";
PREBINDING = NO;
- SDKROOT = macosx10.5;
};
name = Debug;
};
C01FCF5008A954540054247B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
- ARCHS = "$(ARCHS_STANDARD_32_BIT)";
+ ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = /usr/include/libxml2;
+ MACOSX_DEPLOYMENT_TARGET = 10.6;
OTHER_LDFLAGS = "-lxml2";
PREBINDING = NO;
- SDKROOT = macosx10.5;
};
name = Release;
};
View
7 KissXML.xcodeproj/project.xcworkspace/contents.xcworkspacedata
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Workspace
+ version = "1.0">
+ <FileRef
+ location = "self:KissXML.xcodeproj">
+ </FileRef>
+</Workspace>
Please sign in to comment.
Something went wrong with that request. Please try again.