From 9e33fdd46553d8319563b9c6f8021aa8deeca67e Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Tue, 8 Feb 2011 18:48:34 +0000 Subject: [PATCH] First draft of iOS test runner --- .../internal/iTchRunnerAppDelegate.h | 108 +++++++++++++++ .../iTchRunner/internal/iTchRunnerMainView.h | 127 ++++++++++++++++++ .../iTchRunner/internal/iTchRunnerReporter.h | 105 +++++++++++++++ runners/iTchRunner/itChRunnerMain.mm | 18 +++ runners/iTchRunner/readme | 6 + 5 files changed, 364 insertions(+) create mode 100644 runners/iTchRunner/internal/iTchRunnerAppDelegate.h create mode 100644 runners/iTchRunner/internal/iTchRunnerMainView.h create mode 100644 runners/iTchRunner/internal/iTchRunnerReporter.h create mode 100644 runners/iTchRunner/itChRunnerMain.mm create mode 100644 runners/iTchRunner/readme diff --git a/runners/iTchRunner/internal/iTchRunnerAppDelegate.h b/runners/iTchRunner/internal/iTchRunnerAppDelegate.h new file mode 100644 index 00000000..4fcf8044 --- /dev/null +++ b/runners/iTchRunner/internal/iTchRunnerAppDelegate.h @@ -0,0 +1,108 @@ +/* + * iTchRunnerAppDelegate.h + * iTchRunner + * + * Created by Phil on 07/02/2011. + * Copyright 2011 Two Blue Cubes Ltd. All rights reserved. + * + */ + +#import "iTchRunnerMainView.h" + + + +@interface iTchRunnerAppDelegate : NSObject +{ + UIWindow *window; +} + +@end + + +@implementation iTchRunnerAppDelegate + + +/////////////////////////////////////////////////////////////////////////////// +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions +{ + window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; + [window setUserInteractionEnabled:YES]; + [window setMultipleTouchEnabled:YES]; + + CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; + iTchRunnerMainView* view = [[iTchRunnerMainView alloc] initWithFrame:screenRect]; + + [window addSubview:view]; + [window makeKeyAndVisible]; + [view release]; + + return YES; +} + +/////////////////////////////////////////////////////////////////////////////// +- (void)dealloc +{ + [window release]; + [super dealloc]; +} + + +/////////////////////////////////////////////////////////////////////////////// +- (void)applicationWillResignActive:(UIApplication *)application +{ + /* + Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. + */ +} + + +/////////////////////////////////////////////////////////////////////////////// +- (void)applicationDidEnterBackground:(UIApplication *)application +{ + /* + Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + If your application supports background execution, called instead of applicationWillTerminate: when the user quits. + */ +} + + +/////////////////////////////////////////////////////////////////////////////// +- (void)applicationWillEnterForeground:(UIApplication *)application +{ + /* + Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background. + */ +} + + +/////////////////////////////////////////////////////////////////////////////// +- (void)applicationDidBecomeActive:(UIApplication *)application +{ + /* + Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. + */ +} + + +/////////////////////////////////////////////////////////////////////////////// +- (void)applicationWillTerminate:(UIApplication *)application +{ + /* + Called when the application is about to terminate. + See also applicationDidEnterBackground:. + */ +} + + +/////////////////////////////////////////////////////////////////////////////// +- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application +{ + /* + Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later. + */ +} + + + +@end \ No newline at end of file diff --git a/runners/iTchRunner/internal/iTchRunnerMainView.h b/runners/iTchRunner/internal/iTchRunnerMainView.h new file mode 100644 index 00000000..ac8c2805 --- /dev/null +++ b/runners/iTchRunner/internal/iTchRunnerMainView.h @@ -0,0 +1,127 @@ +/* + * iTchRunnerMainView.h + * iTchRunner + * + * Created by Phil on 07/02/2011. + * Copyright 2011 Two Blue Cubes Ltd. All rights reserved. + * + */ + +#include "internal/catch_config.hpp" +#include "internal/catch_runner_impl.hpp" +#include "internal/catch_hub_impl.hpp" +#include "catch.hpp" + +#include "iTchRunnerReporter.h" + +#import + +@interface iTchRunnerMainView : UIView +{ + UITextField* appName; +} + +@end + +@implementation iTchRunnerMainView + + +/////////////////////////////////////////////////////////////////////////////// +-(id) initWithFrame:(CGRect)frame +{ + if ((self = [super initWithFrame:frame])) + { + // Initialization code + self.backgroundColor = [UIColor blackColor]; + + appName = [[UITextField alloc] initWithFrame: CGRectMake( 0, 50, 320, 50 )]; + [self addSubview: appName]; + [appName release]; + appName.textColor = [[UIColor alloc] initWithRed:0.35 green:0.35 blue:1 alpha:1]; + [appName.textColor release]; + appName.textAlignment = UITextAlignmentCenter; + + NSString* appNameStr = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]; + if( [appNameStr isEqualToString:@"iTchRunner"] ) + appName.text = @"iTchRunner self-tests"; + else + appName.text = [NSString stringWithFormat:@"CATCH tests for %@", appNameStr]; + + + UIActionSheet* menu = [[UIActionSheet alloc] initWithTitle:@"Options" + delegate:self + cancelButtonTitle:nil + destructiveButtonTitle:nil + otherButtonTitles:@"Run all tests", nil]; + [menu showInView: self]; + [menu release]; + + } + return self; +} + +/////////////////////////////////////////////////////////////////////////////// +-(void) dealloc +{ + [appName removeFromSuperview]; + [super dealloc]; +} + +/////////////////////////////////////////////////////////////////////////////// +-(void) actionSheet: (UIActionSheet*) sheet clickedButtonAtIndex: (NSInteger) index +{ + Catch::Config config; + config.setReporter( new Catch::iTchRunnerReporter( self ) ); + Catch::Runner runner( config ); + + config.getReporter()->StartGroup( "" ); + runner.runAll( true ); + config.getReporter()->EndGroup( "", runner.getSuccessCount(), runner.getFailureCount() ); +} + +/////////////////////////////////////////////////////////////////////////////// +-(void) testWasRun: (const Catch::ResultInfo*) pResultInfo +{ + const Catch::ResultInfo& resultInfo = *pResultInfo; + std::ostringstream oss; + + if( resultInfo.hasExpression() ) + { + oss << resultInfo.getExpression(); + if( resultInfo.ok() ) + oss << " succeeded"; + else + oss << " failed"; + } + switch( resultInfo.getResultType() ) + { + case Catch::ResultWas::ThrewException: + if( resultInfo.hasExpression() ) + oss << " with unexpected"; + else + oss << "Unexpected"; + oss << " exception with message: '" << resultInfo.getMessage() << "'"; + break; + case Catch::ResultWas::Info: + oss << "info: '" << resultInfo.getMessage() << "'"; + break; + case Catch::ResultWas::Warning: + oss << "warning: '" << resultInfo.getMessage() << "'"; + break; + case Catch::ResultWas::ExplicitFailure: + oss << "failed with message: '" << resultInfo.getMessage() << "'"; + break; + default: + break; + } + + if( resultInfo.hasExpression() ) + { + oss << " for: " << resultInfo.getExpandedExpression(); + } + oss << std::endl; + NSLog( @"%s", oss.str().c_str() ); +} + + +@end diff --git a/runners/iTchRunner/internal/iTchRunnerReporter.h b/runners/iTchRunner/internal/iTchRunnerReporter.h new file mode 100644 index 00000000..35c1f0a1 --- /dev/null +++ b/runners/iTchRunner/internal/iTchRunnerReporter.h @@ -0,0 +1,105 @@ +/* + * iTchRunnerReporter.h + * iTchRunner + * + * Created by Phil on 07/02/2011. + * Copyright 2011 Two Blue Cubes Ltd. All rights reserved. + * + */ +#include "catch.hpp" + +@protocol iTchRunnerDelegate + +-(void) testWasRun: (const Catch::ResultInfo*) result; + +@end + +namespace Catch +{ + class iTchRunnerReporter : public IReporter + { + public: + /////////////////////////////////////////////////////////////////////////// + iTchRunnerReporter + ( + id delegate + ) + : m_succeeded( 0 ), + m_failed( 0 ), + m_delegate( delegate ) + { + } + + /////////////////////////////////////////////////////////////////////////// + static std::string getDescription + () + { + return "Captures results for iOS runner"; + } + + /////////////////////////////////////////////////////////////////////////// + size_t getSucceeded + () + const + { + return m_succeeded; + } + + /////////////////////////////////////////////////////////////////////////// + size_t getFailed + () + const + { + return m_failed; + } + + /////////////////////////////////////////////////////////////////////////// + void reset() + { + m_succeeded = 0; + m_failed = 0; + } + + private: // IReporter + + /////////////////////////////////////////////////////////////////////////// + virtual void StartTesting + () + {} + + /////////////////////////////////////////////////////////////////////////// + virtual void EndTesting + ( + std::size_t succeeded, + std::size_t failed + ) + { + m_succeeded = succeeded; + m_failed = failed; + } + + /////////////////////////////////////////////////////////////////////////// + virtual void Result + ( + const ResultInfo& result + ) + { + [m_delegate testWasRun: &result]; + } + + /////////////////////////////////////////////////////////////////////////// + // Deliberately unimplemented: + virtual void StartGroup( const std::string& ){} + virtual void EndGroup( const std::string&, std::size_t, std::size_t ){} + virtual void StartTestCase( const TestCaseInfo& ){} + virtual void StartSection( const std::string&, const std::string ){} + virtual void EndSection( const std::string&, std::size_t, std::size_t ){} + virtual void EndTestCase( const TestCaseInfo&, std::size_t, std::size_t, const std::string&, const std::string& ){} + + private: + size_t m_succeeded; + size_t m_failed; + + id m_delegate; + }; +} diff --git a/runners/iTchRunner/itChRunnerMain.mm b/runners/iTchRunner/itChRunnerMain.mm new file mode 100644 index 00000000..5a2b51f0 --- /dev/null +++ b/runners/iTchRunner/itChRunnerMain.mm @@ -0,0 +1,18 @@ +// +// iTchRunnerMain.mm +// iTchRunner +// +// Created by Phil on 04/02/2011. +// Copyright Two Blue Cubes Ltd 2011. All rights reserved. +// + +#import "internal/iTchRunnerAppDelegate.h" + +int main(int argc, char *argv[]) +{ + + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + int retVal = UIApplicationMain(argc, argv, nil, @"iTchRunnerAppDelegate"); + [pool release]; + return retVal; +} diff --git a/runners/iTchRunner/readme b/runners/iTchRunner/readme new file mode 100644 index 00000000..2b254917 --- /dev/null +++ b/runners/iTchRunner/readme @@ -0,0 +1,6 @@ +* Select Project -> New Target. Select Cocoa Touch -> Application. Click next and name it something like "Unit Tests" +* While the target info is displayed, find: 'User Header Search Paths' and add a path to the Catch folder +* Open the plist file for the target (Unit Test-Info.plist, if you used that name). Delete the entry for "Main nib file base name: MainWindow" +* From the overview drop-down select the new target. +* Add the file Catch/Runner/iTchRunnerMain.mm into your project - but only in the new target +* Write tests (adding files under test into the target as necessary) \ No newline at end of file