Skip to content

Commit

Permalink
Moved project; Added iPhone project
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Jun 16, 2009
1 parent 816fad7 commit 2628306
Show file tree
Hide file tree
Showing 65 changed files with 5,358 additions and 5,142 deletions.
132 changes: 132 additions & 0 deletions Project-IPhone/Libraries/GHUnitIPhone/GHAsyncTestCase.h
@@ -0,0 +1,132 @@
//
// GHAsyncTestCase.h
// GHUnit
//
// Created by Gabriel Handford on 4/8/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.
//

#import "GHTestCase.h"

// Some default statuses to use; Or define and use your own
enum {
kGHUnitWaitStatusUnknown = 0,
kGHUnitWaitStatusSuccess,
kGHUnitWaitStatusFailure,
kGHUnitWaitStatusCancelled
};

/*!
Asynchronous test case with wait and notify.
Handles the case of notify occuring before wait has started (if it was a synchronous call).
Be sure to call prepare before the asynchronous method (otherwise an exception will raise).
@code
- (void)testSuccess {
[self prepare];
// Do asynchronous task here
[self performSelector:@selector(_succeed) withObject:nil afterDelay:0.1];
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
}
- (void)_succeed {
// Notice the forSelector points to the test above. This is so that
// stray notifies don't error or falsely succeed other tests.
[self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testSuccess)];
}
@endcode
*/
@interface GHAsyncTestCase : GHTestCase {

NSInteger waitForStatus_;
NSInteger notifiedStatus_;

BOOL prepared_; // Whether prepared was called before waitForStatus:timeout:
NSRecursiveLock *lock_; // Lock to synchronize on
SEL waitSelector_; // The selector we are waiting on

NSArray *_runLoopModes; // Run loop modes to run while waiting; Defaults to NSDefaultRunLoopMode, NSRunLoopCommonModes, NSConnectionReplyMode
}

@property (retain, nonatomic) NSArray *runLoopModes;

/*!
Prepare before calling the asynchronous method.
*/
- (void)prepare;

/*!
Prepare and specify the selector we will use in notify.
@param selector
*/
- (void)prepare:(SEL)selector;

/*!
Wait for notification of status or timeout.
Be sure to prepare before calling your asynchronous method.
For example,
@code
- (void)testFoo {
[self prepare];
// Do asynchronous task here
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
}
@endcode
@param status kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status
@param timeout Timeout in seconds
*/
- (void)waitForStatus:(NSInteger)status timeout:(NSTimeInterval)timeout;

// Deprecated
- (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 waiting of status for test selector.
@param status Status, for example, kGHUnitWaitStatusSuccess
@param selector If not NULL, then will verify this selector is where we are waiting.
This prevents stray asynchronous callbacks to fail a later test
*/
- (void)notify:(NSInteger)status forSelector:(SEL)selector;

/*!
Notify waiting of status for any selector.
@param status Status, for example, kGHUnitWaitStatusSuccess
*/
- (void)notify:(NSInteger)status;

@end
43 changes: 43 additions & 0 deletions Project-IPhone/Libraries/GHUnitIPhone/GHMockCLLocationManager.h
@@ -0,0 +1,43 @@
//
// GHMockCLLocationManager.h
// GHUnitIPhone
//
// Created by Gabriel Handford on 4/19/09.
// Copyright 2009. All rights reserved.
//

#import <CoreLocation/CoreLocation.h>

@interface GHMockCLLocationManager : CLLocationManager {
id<CLLocationManagerDelegate> _delegate; // weak

CLLocation *_previousLocation;

BOOL _locationServicesEnabled;
BOOL _updatingLocation;
}

- (void)setLocationServicesEnabled:(BOOL)enabled;

- (void)notifyDeniedAfterDelay:(NSTimeInterval)delay;
- (void)notifyUnknownAfterDelay:(NSTimeInterval)delay;
- (void)notifyLocationCoordinate:(CLLocationCoordinate2D)coordinate afterDelay:(NSTimeInterval)delay;

- (void)move:(CLLocationDistance)distance bearingInDegrees:(double)bearingInDegrees afterDelay:(NSTimeInterval)delay;

@end


/*!
Calculate an endpoint given a startpoint, bearing and distance
Vincenty 'Direct' formula based on the formula as described at http://www.movable-type.co.uk/scripts/latlong-vincenty-direct.html
Original JavaScript implementation © 2002-2006 Chris Veness
Obj-C code derived from http://www.thismuchiknow.co.uk/?p=120
@param source Starting lat/lng coordinates
@param distance Distance in meters to move
@param bearingInRadians Bearing in radians (bearing is 0 north clockwise compass direction; 0 degrees is north, 90 degrees is east)
@result New lat/lng coordinate
*/
CLLocationCoordinate2D GHULocationAtDistance(CLLocationCoordinate2D source, CLLocationDistance distance, double bearingInRadians);

double GHUDegreesToRadians(double val);
143 changes: 143 additions & 0 deletions Project-IPhone/Libraries/GHUnitIPhone/GHMockNSURLConnection.h
@@ -0,0 +1,143 @@
//
// GHMockNSURLConnection.h
// GHUnit
//
// Created by Gabriel Handford on 4/9/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.
//

#import <Foundation/Foundation.h>

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 waitForStatus: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
@endcode
*/
@interface GHMockNSURLConnection : NSObject {
NSURLRequest *request_;
id delegate_; // weak

BOOL cancelled_;
BOOL started_;
}

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

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

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

// Mocked version of NSURLConnection#scheduleInRunLoop:forMode: (NOOP)
- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;

// Mocked version of NSURLConnection#start (NOOP)
- (void)start;

/*!
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 (if < 0, there is no delay)
*/
- (void)receiveFromPath:(NSString *)path statusCode:(NSInteger)statusCode MIMEType:(NSString *)MIMEType afterDelay:(NSTimeInterval)delay;

@end

0 comments on commit 2628306

Please sign in to comment.