#import "ObjectTesting.h" #import #import #import int main() { NSAutoreleasePool *arp = [NSAutoreleasePool new]; NSArray *nodes = nil; NSXMLDocument *node; NSXMLDocument *node2; NSXMLElement *elem; NSString *documentXML = @"" @"" @" " @" Everyday Italian" @" Giada De Laurentiis" @" 2005" @" 30.00" @" " @" " @" Harry Potter" @" J K. Rowling" @" 2005" @" 29.99" @" " @" " @" XQuery Kick Start" @" James McGovern" @" Per Bothner" @" Kurt Cagle" @" James Linn" @" Vaidyanathan Nagarajan" @" 2003" @" 49.99" @" " @" " @" Learning XML" @" Erik T. Ray" @" 2003" @" 39.95" @" " @""; node = [NSXMLDocument alloc]; PASS_EXCEPTION([node initWithData: nil options: 0 error: 0], NSInvalidArgumentException, "Cannot initialise an XML document with nil data"); node = [NSXMLDocument alloc]; PASS_EXCEPTION([node initWithData: (NSData*)@"bad" options: 0 error: 0], NSInvalidArgumentException, "Cannot initialise an XML document with bad data class"); node = [[NSXMLDocument alloc] init]; test_alloc(@"NSXMLDocument"); test_NSObject(@"NSXMLDocument", [NSArray arrayWithObject: node]); elem = [[NSXMLElement alloc] initWithName: @"elem1"]; [node addChild: elem]; PASS_EQUAL([[node children] lastObject], elem, "can add elem to doc"); [elem release]; elem = [[NSXMLElement alloc] initWithName: @"root"]; [node setRootElement: elem]; PASS_EQUAL([[node children] lastObject], elem, "can set elem as root"); PASS([[node children] count] == 1, "set root removes other children"); PASS_RUNS([node setRootElement: nil], "setting a nil root is ignored"); PASS_EQUAL([node rootElement], elem, "root element remains"); node = [[NSXMLDocument alloc] initWithXMLString:documentXML options:0 error:NULL]; elem = [node rootElement]; PASS(node != nil, "document was initialized from a string"); PASS_EQUAL([node rootElement], elem, "root element is correct"); PASS_EQUAL([elem name],@"bookstore", "root element is bookstore"); nodes = [node nodesForXPath:@"/bookstore/book" error:NULL]; PASS([nodes count] == 4, "Xpath function returns the correct number of elements (4)"); elem = [nodes objectAtIndex: 0]; PASS_EQUAL([elem class],[NSXMLElement class], "first node in Xpath result is an element"); PASS([[elem name] isEqualToString: @"book"], "Got the correct elements from XPath query"); node2 = [[NSXMLDocument alloc] initWithXMLString:documentXML options:0 error:NULL]; PASS([node isEqual: node2], "Equal documents are equivalent"); [arp release]; arp = nil; // [elem release]; // [node release]; return 0; }