diff options
| author | Phil Nash | 2011-04-26 19:00:25 +0100 |
|---|---|---|
| committer | Phil Nash | 2011-04-26 19:00:25 +0100 |
| commit | 95419e935cd57f0737ea2b30bb5793c5940a7abc (patch) | |
| tree | ee2bd418280b9ff806b12ca4af729cd2553536f8 /include/internal | |
| parent | 38edb9fa956aa1a62916bf19af5ed9ecb01b28bc (diff) | |
| download | catch3-95419e935cd57f0737ea2b30bb5793c5940a7abc.tar.gz | |
Merges objc top level headers into main headers (using #ifdef __OBJC__)
Diffstat (limited to 'include/internal')
| -rw-r--r-- | include/internal/catch_commandline.hpp | 1 | ||||
| -rw-r--r-- | include/internal/catch_objc.hpp | 184 |
2 files changed, 185 insertions, 0 deletions
diff --git a/include/internal/catch_commandline.hpp b/include/internal/catch_commandline.hpp index 0eedcb00..6a4d0368 100644 --- a/include/internal/catch_commandline.hpp +++ b/include/internal/catch_commandline.hpp @@ -183,6 +183,7 @@ namespace Catch if( m_args.size() != 0 ) return setErrorMode( m_command + " does not accept arguments" ); m_config.setShouldDebugBreak( true ); + break; case modeName: if( m_args.size() != 1 ) return setErrorMode( m_command + " requires exactly one argument (a name)" ); diff --git a/include/internal/catch_objc.hpp b/include/internal/catch_objc.hpp new file mode 100644 index 00000000..8b621695 --- /dev/null +++ b/include/internal/catch_objc.hpp @@ -0,0 +1,184 @@ +/* + * catch_objc.hpp + * Catch + * + * Created by Phil on 14/11/2010. + * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + +#ifndef TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED + +#import <objc/runtime.h> +#include <string> + +#include "internal/catch_test_case_info.hpp" + +/////////////////////////////////////////////////////////////////////////////// +// This protocol is really only here for (self) documenting purposes, since +// all its methods are optional. +@protocol OcFixture + +@optional + +-(void) setUp; +-(void) tearDown; + +@end + +namespace Catch +{ + class OcMethod : public ITestCase + { + public: + /////////////////////////////////////////////////////////////////////// + OcMethod + ( + Class cls, + SEL sel + ) + : m_cls( cls ), + m_sel( sel ) + { + } + + /////////////////////////////////////////////////////////////////////// + virtual void invoke + () + const + { + id obj = class_createInstance( m_cls, 0 ); + obj = [obj init]; + + if( [obj respondsToSelector: @selector(setUp) ] ) + [obj performSelector: @selector(setUp)]; + + if( [obj respondsToSelector: m_sel] ) + [obj performSelector: m_sel]; + + if( [obj respondsToSelector: @selector(tearDown) ] ) + [obj performSelector: @selector(tearDown)]; + + [obj release]; + } + + /////////////////////////////////////////////////////////////////////// + virtual ITestCase* clone + () + const + { + return new OcMethod( m_cls, m_sel ); + } + + /////////////////////////////////////////////////////////////////////// + virtual bool operator == + ( + const ITestCase& other + ) + const + { + const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); + return ocmOther && ocmOther->m_sel == m_sel; + } + + /////////////////////////////////////////////////////////////////////// + virtual bool operator < + ( + const ITestCase& other + ) + const + { + const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); + return ocmOther && ocmOther->m_sel < m_sel; + } + + private: + Class m_cls; + SEL m_sel; + }; + + namespace Detail + { + + /////////////////////////////////////////////////////////////////////// + inline bool startsWith + ( + const std::string& str, + const std::string& sub + ) + { + return str.length() > sub.length() && str.substr( 0, sub.length() ) == sub; + } + + /////////////////////////////////////////////////////////////////////// + inline const char* getAnnotation + ( + Class cls, + const std::string& annotationName, + const std::string& testCaseName + ) + { + NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; + SEL sel = NSSelectorFromString( selStr ); + [selStr release]; + if( [cls respondsToSelector: sel] ) + return (const char*)[cls performSelector: sel]; + return ""; + } + } + + /////////////////////////////////////////////////////////////////////////// + inline size_t registerTestMethods + () + { + size_t noTestMethods = 0; + int noClasses = objc_getClassList( NULL, 0 ); + + std::vector<Class> classes( noClasses ); + objc_getClassList( &classes[0], noClasses ); + + for( int c = 0; c < noClasses; c++ ) + { + Class cls = classes[c]; + { + u_int count; + Method* methods = class_copyMethodList( cls, &count ); + for( int m = 0; m < count ; m++ ) + { + SEL selector = method_getName(methods[m]); + std::string methodName = sel_getName(selector); + if( Detail::startsWith( methodName, "Catch_TestCase_" ) ) + { + std::string testCaseName = methodName.substr( 15 ); + const char* name = Detail::getAnnotation( cls, "Name", testCaseName ); + const char* desc = Detail::getAnnotation( cls, "Description", testCaseName ); + + Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new OcMethod( cls, selector ), name, desc, "", 0 ) ); + noTestMethods++; + + } + } + free(methods); + } + } + return noTestMethods; + } +} + +/////////////////////////////////////////////////////////////////////////////// +#define OC_TEST_CASE( name, desc )\ ++(const char*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Name_test ) \ +{\ +return name; \ +}\ ++(const char*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Description_test ) \ +{ \ +return desc; \ +} \ +-(void) INTERNAL_CATCH_UNIQUE_NAME( Catch_TestCase_test ) + +#endif // TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED
\ No newline at end of file |