Hi Richard Le samedi 15 janvier 2011 à 06:45 +0000, Richard Frith-Macdonald a écrit :
> I have an OSX system I could run tests for you on, and I'd be very > pleased to do that if you could add tests to the testsuite at > http://svn.gna.org/viewcvs/gnustep/tests/testsuite/trunk/ > My problem with writing tests for a class is that I need to be > familiar with how the class is used (in order to write tests), and > getting that familiarity means I need to be working on code which uses > the class, or spend a lot of time reading the documentation ... > running testcases for someone else who *is* working with the classes, > is very quick/easy by comparison. Here's a first round of tests for NSURLRequest, NSURLProtocol and NSURLConnection. I'll try to create good client/server tests for NSURLConnection next but it will take some time. Philippe
Index: base/NSURLConnection/basic.m =================================================================== --- base/NSURLConnection/basic.m (révision 0) +++ base/NSURLConnection/basic.m (révision 0) @@ -0,0 +1,48 @@ +#import <Foundation/Foundation.h> +#import "Testing.h" +#import "ObjectTesting.h" + +int main() +{ + NSAutoreleasePool *arp = [NSAutoreleasePool new]; + NSMutableURLRequest *mutable; + NSURLConnection *connection; + NSURLResponse *response; + NSError *error; + NSData *data; + NSURL *httpURL; + NSString *path; + + httpURL = [NSURL URLWithString: @"http://www.gnustep.org"]; + + TEST_FOR_CLASS(@"NSURLConnection", [NSURLConnection alloc], + "NSURLConnection +alloc returns an NSURLConnection"); + + mutable = [NSMutableURLRequest requestWithURL:httpURL]; + pass([NSURLConnection canHandleRequest:mutable], + "NSURLConnection can handle an valid HTTP request (GET)"); + [mutable setHTTPMethod:@"WRONGMETHOD"]; + pass([NSURLConnection canHandleRequest:mutable], + "NSURLConnection can handle an invalid HTTP request (WRONGMETHOD)"); + + [mutable setHTTPMethod:@"GET"]; + connection = [NSURLConnection connectionWithRequest:mutable delegate:nil]; + pass(connection != nil, + "NSURLConnection +connectionWithRequest:delegate: with nil as delegate returns a instance"); + + data = [NSURLConnection sendSynchronousRequest:mutable returningResponse:&response error:&error]; + pass(data != nil && [data length] > 0, + "NSURLConnection synchronously load data from an http URL"); + [data release]; + + path = [[NSFileManager defaultManager] currentDirectoryPath]; + path = [path stringByAppendingPathComponent:@"basic.m"]; + [mutable setURL:[NSURL fileURLWithPath:path]]; + data = [NSURLConnection sendSynchronousRequest:mutable returningResponse:&response error:&error]; + pass(data != nil && [data length] > 0, + "NSURLConnection synchronously load data from a local file"); + [data release]; + + [arp release]; arp = nil; + return 0; +} Index: base/NSURLProtocol/basic.m =================================================================== --- base/NSURLProtocol/basic.m (révision 0) +++ base/NSURLProtocol/basic.m (révision 0) @@ -0,0 +1,36 @@ +#import <Foundation/Foundation.h> +#import "Testing.h" +#import "ObjectTesting.h" + +int main() +{ + NSAutoreleasePool *arp = [NSAutoreleasePool new]; + NSMutableURLRequest *mutable, *copy; + NSURLProtocol *protocol; + NSURL *httpURL; + + + httpURL = [NSURL URLWithString: @"http://www.gnustep.org"]; + + TEST_FOR_CLASS(@"NSURLProtocol", [NSURLProtocol alloc], + "NSURLProtocol +alloc returns an NSURLProtocol"); + + mutable = [[NSMutableURLRequest requestWithURL:httpURL] retain]; + TEST_EXCEPTION([NSURLProtocol canInitWithRequest:mutable], nil, YES, + "NSURLProtocol +canInitWithRequest throws an exeception (subclasses should be used)"); + + pass(mutable == [NSURLProtocol canonicalRequestForRequest:mutable], + "NSURLProtocol +canonicalRequestForRequest: return it argument"); + + copy = [mutable copy]; + pass([NSURLProtocol requestIsCacheEquivalent:mutable toRequest:copy], + "NSURLProtocol +requestIsCacheEquivalent:toRequest returns YES with a request and its copy"); + [copy setHTTPMethod:@"POST"]; + pass([NSURLProtocol requestIsCacheEquivalent:mutable toRequest:copy] == NO, + "NSURLProtocol +requestIsCacheEquivalent:toRequest returns NO after a method change"); + [copy release]; + [mutable release]; + + [arp release]; arp = nil; + return 0; +} Index: base/NSURLRequest/basic.m =================================================================== --- base/NSURLRequest/basic.m (révision 0) +++ base/NSURLRequest/basic.m (révision 0) @@ -0,0 +1,52 @@ +#import <Foundation/Foundation.h> +#import "Testing.h" +#import "ObjectTesting.h" + +int main() +{ + NSAutoreleasePool *arp = [NSAutoreleasePool new]; + NSURLRequest *request; + NSMutableURLRequest *mutable; + NSURL *httpURL, *foobarURL; + + + httpURL = [NSURL URLWithString: @"http://www.gnustep.org"]; + foobarURL = [NSURL URLWithString: @"foobar://localhost/madeupscheme"]; + + TEST_FOR_CLASS(@"NSURLRequest", [NSURLRequest alloc], + "NSURLRequest +alloc returns an NSURLRequest"); + + request = [[NSURLRequest requestWithURL:httpURL] retain]; + pass(request != nil, + "NSURLRequest +requestWithURL returns a request from a valid URL"); + pass([[[request URL] absoluteString] isEqualToString:[httpURL absoluteString]], + "Request URL is equal to the URL used for creation"); + pass([@"GET" isEqualToString:[request HTTPMethod]], + "Request is initialized with a GET method"); + + request = [NSURLRequest requestWithURL:foobarURL]; + pass(request != nil, + "NSURLRequest +requestWithURL returns a request from an invalid URL (unknown scheme)"); + + mutable = [request mutableCopy]; + pass(mutable != nil && [mutable isKindOfClass:[NSMutableURLRequest class]], + "NSURLRequest -mutableCopy returns a mutable request"); + [mutable setHTTPMethod:@"POST"]; + pass([@"POST" isEqualToString:[mutable HTTPMethod]], + "Can setHTTPMethod of a mutable request (POST)"); + [mutable setHTTPMethod:@"NONHTTPMETHOD"]; + pass([@"NONHTTPMETHOD" isEqualToString:[mutable HTTPMethod]], + "Can setHTTPMethod of a mutable request (non existant NONHTTPMETHOD)"); + + [mutable addValue:@"value1" forHTTPHeaderField:@"gnustep"]; + pass([@"value1" isEqualToString:[mutable valueForHTTPHeaderField:@"gnustep"]], + "Can set and get a value for an HTTP header field"); + [mutable addValue:@"value2" forHTTPHeaderField:@"gnustep"]; + pass([@"value1,value2" isEqualToString:[mutable valueForHTTPHeaderField:@"gnustep"]], + "Handle multiple values for an HTTP header field"); + [mutable release]; + [request release]; + + [arp release]; arp = nil; + return 0; +}
_______________________________________________ Gnustep-dev mailing list Gnustep-dev@gnu.org http://lists.gnu.org/mailman/listinfo/gnustep-dev