Skip to content
This repository has been archived by the owner on Jun 5, 2018. It is now read-only.

Commit

Permalink
Updating example iPhone project
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Apr 14, 2009
1 parent 34b770f commit 530189a
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Examples/MyTestable-IPhone/Libraries/GHUnit/GHAsyncTestCase.h
Expand Up @@ -102,6 +102,13 @@ enum {
*/
- (void)waitFor:(NSInteger)status timeout:(NSTimeInterval)timeout;

/*!
Wait for timeout to occur.
Fails if we did _NOT_ timeout.
@param timeout
*/
- (void)waitForTimeout:(NSTimeInterval)timeout;

/*!
Notify of status for test selector.
@param status For example, kGHUnitWaitStatusSuccess
Expand Down
Expand Up @@ -29,17 +29,18 @@

#import <Foundation/Foundation.h>


// The NSHTTPURLResponse doesn't have a way to set the status code or headers,
// so we'll subclass and add setters for the statusCode and allHeaderFields properties.
/*!
NSHTTPURLResponse for use with mocking.
Allows us to manually set the status code and headers in the response.
*/
@interface GHMockNSHTTPURLResponse : NSHTTPURLResponse {
NSInteger statusCode_;
NSDictionary *headers_;
}

- (id)initWithStatusCode:(NSInteger)statusCode headers:(NSDictionary *)headers;

- (void)setStatusCode:(int)code;
- (void)setStatusCode:(NSInteger)code;
- (void)setHeaders:(NSDictionary *)headers;

@end
Expand Up @@ -31,29 +31,98 @@

extern NSString *const GHMockNSURLConnectionException;

/*!
NSURLConnection for mocking.
Use with GHAsyncTestCase to mock out connections.
@code
@interface GHNSURLConnectionMockTest : GHAsyncTestCase {}
@end
@implementation GHNSURLConnectionMockTest
- (void)testMock {
[self prepare];
GHMockNSURLConnection *connection = [[GHMockNSURLConnection alloc] initWithRequest:nil delegate:self];
[connection receiveHTTPResponseWithStatusCode:204 headers:testHeaders_ afterDelay:0.1];
[connection receiveData:testData_ afterDelay:0.2];
[connection finishAfterDelay:0.3];
[self waitFor:kGHUnitWaitStatusSuccess timeout:1.0];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
GHAssertEquals([(NSHTTPURLResponse *)response statusCode], 204, nil);
GHAssertEqualObjects([(NSHTTPURLResponse *)response allHeaderFields], testHeaders_, nil);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
GHAssertEqualObjects(data, testData_, nil);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testMock)];
}
@end
*/
@interface GHMockNSURLConnection : NSObject {
NSURLRequest *request_;
id delegate_; // weak

BOOL cancelled_;
BOOL cancelled_;
}

@property (readonly, nonatomic, getter=isCancelled) BOOL cancelled;

// Mocked version of NSURLConnection#initWithRequest:delegate:
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate;

/*!
Send generic response to delegate after delay.
(For asynchronous requests)
@param delay Delay in seconds (if < 0, there is no delay)
*/
- (void)receiveResponse:(NSURLResponse *)response afterDelay:(NSTimeInterval)delay;

/*!
Send HTTP response to delegate with status code, headers, after delay.
This is only the HTTP response (and not data or finished).
(For asynchronous requests)
@param statusCode HTTP status code
@param headers Headers
@param delay Delay in seconds (if < 0, there is no delay)
*/
- (void)receiveHTTPResponseWithStatusCode:(int)statusCode headers:(NSDictionary *)headers afterDelay:(NSTimeInterval)delay;

/*!
Send data to connection delegate after delay.
@param data Data to send
@param delay Delay in seconds
*/
- (void)receiveData:(NSData *)data afterDelay:(NSTimeInterval)delay;

/*!
Send data (from file in bundle resource) to connection delegate after delay.
(For asynchronous requests)
@param path Path to file
@param delay Delay in seconds
*/
- (void)receiveDataFromPath:(NSString *)path afterDelay:(NSTimeInterval)delay;

/*!
Calls connectionDidFinish: delegate after delay.
(For asynchronous requests)
@param delay Delay in seconds (if < 0, there is no delay)
*/
- (void)finishAfterDelay:(NSTimeInterval)delay;

/*!
Sends mock response, sends data, and then calls finish.
(For asynchronous requests)
@param path Data to load path from. File should be available in Test target (bundle)
@param statusCode Status code for response
@param MIMEType Content type for response header
@param afterDelay Delay before responding
@param afterDelay Delay before responding (if < 0, there is no delay)
*/
- (void)receiveFromPath:(NSString *)path statusCode:(NSInteger)statusCode MIMEType:(NSString *)MIMEType afterDelay:(NSTimeInterval)delay;

Expand Down
47 changes: 47 additions & 0 deletions Examples/MyTestable-IPhone/Libraries/GHUnit/GHNSLocale+Mock.h
@@ -0,0 +1,47 @@
//
// GHMockNSLocale.h
// GHUnitIPhone
//
// Created by Gabriel Handford on 4/13/09.
// Copyright 2009. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

/*!
Category for overriding the current locale at runtime.
@code
#import "GHNSLocale+Mock.h"
// This aliases the currentLocale method and with the specified locale identifier
[NSLocale gh_setLocaleIdentifier:@"en_GB"];
[[NSLocale currentLocale] localeIdentifier] == "en_GB"
@endcode
*/
@interface NSLocale (GHMock)

+ (void)gh_setLocaleIdentifier:(NSString *)localeIdentifier;

+ (NSLocale *)gh_currentLocale;

@end
9 changes: 8 additions & 1 deletion Examples/MyTestable-IPhone/Libraries/GHUnit/GHTestCase.h
Expand Up @@ -51,7 +51,7 @@

// Log to your test case logger.
// For example, GHTestLog(@"Some debug info, %@", obj)
#define GHTestLog(...) [self _log:[NSString stringWithFormat:__VA_ARGS__, nil]]
#define GHTestLog(...) [self log:[NSString stringWithFormat:__VA_ARGS__, nil]]

/*!
@brief The base class for a test case.
Expand Down Expand Up @@ -123,4 +123,11 @@
//! Any special handling of exceptions after they are thrown; By default logs stack trace to standard out.
- (void)handleException:(NSException *)exception;

/*!
Log a message, which notifies the log delegate.
This is not meant to be used directly, see GHTestLog(...) macro.
@param message
*/
- (void)log:(NSString *)message;

@end
8 changes: 8 additions & 0 deletions Examples/MyTestable-IPhone/Libraries/GHUnit/GHTestGroup.h
Expand Up @@ -124,6 +124,14 @@
*/
- (id)initWithTestCase:(id)testCase delegate:(id<GHTestDelegate>)delegate;

/*!
Create test group from a single test.
@param testCase
@param selector Test to run
@param delegate
*/
- (id)initWithTestCase:(id)testCase selector:(SEL)selector delegate:(id<GHTestDelegate>)delegate;

/*!
Create test group from a test case.
@param testCase Test case, could be a subclass of SenTestCase or GHTestCase
Expand Down
36 changes: 36 additions & 0 deletions Examples/MyTestable-IPhone/Libraries/GHUnit/GHUNSObject+Swizzle.h
@@ -0,0 +1,36 @@
//
// GHNSObject+Swizzle.h
// GHUnit
//
// Created by Gabriel Handford on 4/13/09.
// Copyright 2009. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

@interface NSObject (GHUSwizzle)

+ (void)ghu_swizzleMethod:(SEL)original withMethod:(SEL)alternate;
+ (void)ghu_swizzleClassMethod:(SEL)original withClassMethod:(SEL)alternate;

@end

5 changes: 5 additions & 0 deletions Examples/MyTestable-IPhone/Libraries/GHUnit/GHUnit.h
Expand Up @@ -33,5 +33,10 @@
#import "GHTestMacros.h"
#import "GHTestRunner.h"

// Mocking
#import "GHMockNSURLConnection.h"
#import "GHMockNSHTTPURLResponse.h"

#import "GHUNSObject+Swizzle.h"
#import "GHNSLocale+Mock.h"

Binary file modified Examples/MyTestable-IPhone/Libraries/GHUnit/libGHUnitIPhone.a
Binary file not shown.
Expand Up @@ -23,6 +23,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
002059510F94754700FC1898 /* GHNSLocale+Mock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GHNSLocale+Mock.h"; sourceTree = "<group>"; };
002059520F94754700FC1898 /* GHUNSObject+Swizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GHUNSObject+Swizzle.h"; sourceTree = "<group>"; };
00264B180F916538007E9425 /* GHUnitIPhoneTestMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GHUnitIPhoneTestMain.m; sourceTree = "<group>"; };
00264B190F916538007E9425 /* GHMockNSURLConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GHMockNSURLConnection.h; sourceTree = "<group>"; };
00264B1A0F916538007E9425 /* GHMockNSHTTPURLResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GHMockNSHTTPURLResponse.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -96,6 +98,8 @@
00DA15A40F897CDD001FE08E /* GHUnit */ = {
isa = PBXGroup;
children = (
002059510F94754700FC1898 /* GHNSLocale+Mock.h */,
002059520F94754700FC1898 /* GHUNSObject+Swizzle.h */,
00DA16080F899905001FE08E /* GHTesting.h */,
00DA16090F899905001FE08E /* GHTestGroup.h */,
00DA160A0F899905001FE08E /* GHTest.h */,
Expand Down

0 comments on commit 530189a

Please sign in to comment.