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
|
#import <Foundation/Foundation.h>
#import "Testing.h"
#if defined(GNUSTEP_BASE_LIBRARY) && (GS_USE_LIBXML == 1)
#import <Foundation/NSFileManager.h>
#import <GNUstepBase/GSXML.h>
#import "ObjectTesting.h"
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSFileManager *mgr;
GSXMLParser *parser;
GSXMLDocument *doc;
GSXMLNamespace *namespace;
NSMutableArray *iparams;
NSMutableArray *oparams;
GSXMLNode *node;
GSXMLRPC *rpc;
NSString *xml;
NSString *str;
NSString *testPath;
NSString *absolutePath;
NSData *dat;
TEST_FOR_CLASS(@"GSXMLDocument", AUTORELEASE([GSXMLDocument alloc]),
"GSXMLDocument +alloc returns a GSXMLDocument");
TEST_FOR_CLASS(@"GSXMLDocument", [GSXMLDocument documentWithVersion: @"1.0"],
"GSXMLDocument +documentWithVersion: returns a GSXMLDocument");
TEST_FOR_CLASS(@"GSXMLNode", AUTORELEASE([GSXMLNode alloc]),
"GSXMLNode +alloc returns a GSXMLNode");
TEST_FOR_CLASS(@"GSXMLRPC", AUTORELEASE([GSXMLRPC alloc]),
"GSXMLRPC +alloc returns a GSXMLRPC instance");
NS_DURING
node = [GSXMLNode new];
PASS(node == nil, "GSXMLNode +new returns nil");
NS_HANDLER
PASS(node == nil, "GSXMLNode +new returns nil");
NS_ENDHANDLER
TEST_FOR_CLASS(@"GSXMLNamespace", AUTORELEASE([GSXMLNamespace alloc]),
"GSXMLNamespace +alloc returns a GSXMLNamespace");
NS_DURING
namespace = [GSXMLNamespace new];
PASS(namespace == nil, "GSXMLNamespace +new returns nil");
NS_HANDLER
PASS(namespace == nil, "GSXMLNamespace +new returns nil");
NS_ENDHANDLER
doc = [GSXMLDocument documentWithVersion: @"1.0"];
node = [doc makeNodeWithNamespace: nil name: @"nicola" content: nil];
PASS (node != nil,"Can create a document node");
[doc setRoot: node];
PASS([[doc root] isEqual: node],"Can set document node as root node");
node = [doc makeNodeWithNamespace: nil name: @"nicola" content: nil];
[node makeChildWithNamespace: nil
name: @"paragraph"
content: @"Hi this is some text"];
[node makeChildWithNamespace: nil
name: @"paragraph"
content: @"Hi this is even some more text"];
[doc setRoot: node];
PASS([[doc root] isEqual: node],
"Can set a document node (with children) as root node");
namespace = [node makeNamespaceHref: @"http: //www.gnustep.org"
prefix: @"gnustep"];
PASS(namespace != nil,"Can create a node namespace");
node = [doc makeNodeWithNamespace: namespace name: @"nicola" content: nil];
PASS([[node namespace] isEqual: namespace],
"Can create a node with a namespace");
node = [doc makeNodeWithNamespace: namespace name: @"another" content: nil];
PASS([[node namespace] isEqual: namespace],
"Can create a node with same namespace as another node");
PASS([[namespace prefix] isEqual: @"gnustep"],
"A namespace remembers its prefix");
rpc = AUTORELEASE([(GSXMLRPC*)[GSXMLRPC alloc]
initWithURL: @"http://localhost/"]);
PASS(rpc != nil, "Can initialise an RPC instance");
iparams = [NSMutableArray array];
oparams = [NSMutableArray array];
dat = [rpc buildMethod: @"method" params: nil];
PASS(dat != nil, "Can build an empty method call (nil params)");
str = [rpc parseMethod: dat params: oparams];
PASS([str isEqual: @"method"] && [iparams isEqual: oparams],
"Can parse an empty method call (nil params)");
dat = [rpc buildMethod: @"method" params: iparams];
PASS(dat != nil, "Can build an empty method call");
str = [rpc parseMethod: dat params: oparams];
PASS([str isEqual: @"method"] && [iparams isEqual: oparams],
"Can parse an empty method call");
[iparams addObject: @"a string"];
dat = [rpc buildMethod: @"method" params: iparams];
PASS(dat != nil, "Can build a method call with a string");
str = [rpc parseMethod: dat params: oparams];
PASS([str isEqual: @"method"] && [iparams isEqual: oparams],
"Can parse a method call with a string");
[rpc setCompact: YES];
str = [rpc buildMethodCall: @"method" params: iparams];
[rpc setCompact: NO];
str = [str stringByReplacingString: @"<string>" withString: @""];
str = [str stringByReplacingString: @"</string>" withString: @""];
str = [rpc parseMethod: [str dataUsingEncoding: NSUTF8StringEncoding]
params: oparams];
PASS([str isEqual: @"method"] && [iparams isEqual: oparams],
"Can parse a method call with a string without the <string> element");
[iparams addObject: [NSNumber numberWithInt: 4]];
dat = [rpc buildMethod: @"method" params: iparams];
PASS(dat != nil, "Can build a method call with an integer");
str = [rpc parseMethod: dat params: oparams];
PASS([str isEqual: @"method"] && [iparams isEqual: oparams],
"Can parse a method call with an integer");
[iparams addObject: [NSNumber numberWithFloat: 4.5]];
dat = [rpc buildMethod: @"method" params: iparams];
PASS(dat != nil, "Can build a method call with a float");
str = [rpc parseMethod: dat params: oparams];
PASS([str isEqual: @"method"] && [iparams isEqual: oparams],
"Can parse a method call with a float");
[iparams addObject: [NSData dataWithBytes: "1234" length: 4]];
dat = [rpc buildMethod: @"method" params: iparams];
PASS(dat != nil, "Can build a method call with binary data");
str = [rpc parseMethod: dat params: oparams];
PASS([str isEqual: @"method"] && [iparams isEqual: oparams],
"Can parse a method call with binary data");
[rpc setTimeZone: [NSTimeZone systemTimeZone]];
[iparams addObject: [NSDate date]];
dat = [rpc buildMethod: @"method" params: iparams];
PASS(dat != nil, "Can build a method call with a date");
str = [rpc parseMethod: dat params: oparams];
PASS([str isEqual: @"method"]
&& [[iparams description] isEqual: [oparams description]],
"Can parse a method call with a date");
mgr = [NSFileManager defaultManager];
testPath = [[mgr currentDirectoryPath]
stringByAppendingPathComponent: @"GNUmakefile"];
absolutePath = [[NSURL fileURLWithPath: testPath] absoluteString];
xml = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
@"<!DOCTYPE foo [\n"
@"<!ENTITY foo SYSTEM \"%@\">\n"
@"]>\n"
@"<file>&&foo;A</file>", absolutePath];
dat = [xml dataUsingEncoding: NSUTF8StringEncoding];
parser = [GSXMLParser parserWithData: dat];
[parser substituteEntities: YES];
[parser parse];
str = [[[parser document] root] content];
PASS_EQUAL(str, @"&A", "external entity is ignored")
parser = [GSXMLParser parserWithData: dat];
[parser substituteEntities: YES];
[parser resolveEntities: YES];
[parser parse];
str = [[[parser document] root] content];
PASS(str != nil && [str rangeOfString: @"MAKEFILES"].length > 0,
"external entity is resolved")
testHopeful = YES;
xml = @"<!DOCTYPE plist PUBLIC \"-//GNUstep//DTD plist 0.9//EN\""
@" \"http://www.gnustep.org/plist-0_9.xml\">\n"
@"<plist></plist>";
dat = [xml dataUsingEncoding: NSUTF8StringEncoding];
parser = [GSXMLParser parserWithData: dat];
[parser substituteEntities: YES];
[parser resolveEntities: YES];
[parser doValidityChecking: YES];
PASS([parser parse] == NO, "empty plist is not valid")
xml = @"<!DOCTYPE plist PUBLIC \"-//GNUstep//DTD plist 0.9//EN\""
@" \"http://www.gnustep.org/plist-0_9.xml\">\n"
@"<plist><string>xxx</string></plist>";
dat = [xml dataUsingEncoding: NSUTF8StringEncoding];
parser = [GSXMLParser parserWithData: dat];
[parser substituteEntities: YES];
[parser resolveEntities: YES];
[parser doValidityChecking: YES];
PASS([parser parse] == YES, "plist containing string is valid")
testHopeful = NO;
PASS_EQUAL([[[[[parser document] root] firstChild] firstChild] content],
@"xxx", "root/plist/string is parsed")
[arp release]; arp = nil;
return 0;
}
#else
int main(int argc,char **argv)
{
START_SET("GSXML")
SKIP("GSXML support unavailable");
END_SET("GSXML")
return 0;
}
#endif
|