Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * catch_objc.hpp |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 3 | * Catch |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 4 | * |
| 5 | * Created by Phil on 14/11/2010. |
| 6 | * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. |
| 7 | * |
| 8 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 9 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #ifndef TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED |
| 14 | #define TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED |
| 15 | |
| 16 | #import <objc/runtime.h> |
| 17 | #include <string> |
Phil Nash | 823ea3e | 2011-04-26 08:32:40 +0100 | [diff] [blame] | 18 | |
Phil Nash | 89d1e6c | 2011-05-24 08:23:02 +0100 | [diff] [blame^] | 19 | // NB. Any general catch headers included here must be included |
| 20 | // in catch.hpp first to make sure they are included by the single |
| 21 | // header for non obj-usage |
Phil Nash | 0847a0f | 2011-02-01 16:09:18 +0000 | [diff] [blame] | 22 | #include "internal/catch_test_case_info.hpp" |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 23 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | // This protocol is really only here for (self) documenting purposes, since |
| 26 | // all its methods are optional. |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 27 | @protocol OcFixture |
| 28 | |
| 29 | @optional |
| 30 | |
| 31 | -(void) setUp; |
| 32 | -(void) tearDown; |
| 33 | |
| 34 | @end |
| 35 | |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 36 | namespace Catch |
| 37 | { |
Phil Nash | 0847a0f | 2011-02-01 16:09:18 +0000 | [diff] [blame] | 38 | class OcMethod : public ITestCase |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 39 | { |
| 40 | public: |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 41 | /////////////////////////////////////////////////////////////////////// |
| 42 | OcMethod |
| 43 | ( |
| 44 | Class cls, |
| 45 | SEL sel |
| 46 | ) |
| 47 | : m_cls( cls ), |
| 48 | m_sel( sel ) |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 52 | /////////////////////////////////////////////////////////////////////// |
| 53 | virtual void invoke |
| 54 | () |
| 55 | const |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 56 | { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 57 | id obj = class_createInstance( m_cls, 0 ); |
| 58 | obj = [obj init]; |
| 59 | |
| 60 | if( [obj respondsToSelector: @selector(setUp) ] ) |
| 61 | [obj performSelector: @selector(setUp)]; |
| 62 | |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 63 | if( [obj respondsToSelector: m_sel] ) |
| 64 | [obj performSelector: m_sel]; |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 65 | |
| 66 | if( [obj respondsToSelector: @selector(tearDown) ] ) |
| 67 | [obj performSelector: @selector(tearDown)]; |
| 68 | |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 69 | [obj release]; |
| 70 | } |
| 71 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 72 | /////////////////////////////////////////////////////////////////////// |
| 73 | virtual ITestCase* clone |
| 74 | () |
| 75 | const |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 76 | { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 77 | return new OcMethod( m_cls, m_sel ); |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 80 | /////////////////////////////////////////////////////////////////////// |
| 81 | virtual bool operator == |
| 82 | ( |
| 83 | const ITestCase& other |
| 84 | ) |
| 85 | const |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 86 | { |
| 87 | const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); |
| 88 | return ocmOther && ocmOther->m_sel == m_sel; |
| 89 | } |
| 90 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 91 | /////////////////////////////////////////////////////////////////////// |
| 92 | virtual bool operator < |
| 93 | ( |
| 94 | const ITestCase& other |
| 95 | ) |
| 96 | const |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 97 | { |
| 98 | const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); |
| 99 | return ocmOther && ocmOther->m_sel < m_sel; |
| 100 | } |
| 101 | |
| 102 | private: |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 103 | Class m_cls; |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 104 | SEL m_sel; |
| 105 | }; |
| 106 | |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 107 | namespace Detail |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 108 | { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 109 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 110 | /////////////////////////////////////////////////////////////////////// |
| 111 | inline bool startsWith |
| 112 | ( |
| 113 | const std::string& str, |
| 114 | const std::string& sub |
| 115 | ) |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 116 | { |
| 117 | return str.length() > sub.length() && str.substr( 0, sub.length() ) == sub; |
| 118 | } |
| 119 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 120 | /////////////////////////////////////////////////////////////////////// |
| 121 | inline const char* getAnnotation |
| 122 | ( |
| 123 | Class cls, |
| 124 | const std::string& annotationName, |
| 125 | const std::string& testCaseName |
| 126 | ) |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 127 | { |
| 128 | NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; |
| 129 | SEL sel = NSSelectorFromString( selStr ); |
| 130 | [selStr release]; |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 131 | if( [cls respondsToSelector: sel] ) |
| 132 | return (const char*)[cls performSelector: sel]; |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 133 | return ""; |
| 134 | } |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 137 | /////////////////////////////////////////////////////////////////////////// |
| 138 | inline size_t registerTestMethods |
| 139 | () |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 140 | { |
| 141 | size_t noTestMethods = 0; |
| 142 | int noClasses = objc_getClassList( NULL, 0 ); |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 143 | |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 144 | std::vector<Class> classes( noClasses ); |
| 145 | objc_getClassList( &classes[0], noClasses ); |
| 146 | |
| 147 | for( int c = 0; c < noClasses; c++ ) |
| 148 | { |
| 149 | Class cls = classes[c]; |
| 150 | { |
| 151 | u_int count; |
| 152 | Method* methods = class_copyMethodList( cls, &count ); |
| 153 | for( int m = 0; m < count ; m++ ) |
| 154 | { |
| 155 | SEL selector = method_getName(methods[m]); |
| 156 | std::string methodName = sel_getName(selector); |
| 157 | if( Detail::startsWith( methodName, "Catch_TestCase_" ) ) |
| 158 | { |
| 159 | std::string testCaseName = methodName.substr( 15 ); |
Phil Nash | 0847a0f | 2011-02-01 16:09:18 +0000 | [diff] [blame] | 160 | const char* name = Detail::getAnnotation( cls, "Name", testCaseName ); |
| 161 | const char* desc = Detail::getAnnotation( cls, "Description", testCaseName ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 162 | |
Phil Nash | 823ea3e | 2011-04-26 08:32:40 +0100 | [diff] [blame] | 163 | Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new OcMethod( cls, selector ), name, desc, "", 0 ) ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 164 | noTestMethods++; |
| 165 | |
| 166 | } |
| 167 | } |
| 168 | free(methods); |
| 169 | } |
| 170 | } |
| 171 | return noTestMethods; |
| 172 | } |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 175 | /////////////////////////////////////////////////////////////////////////////// |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 176 | #define OC_TEST_CASE( name, desc )\ |
| 177 | +(const char*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Name_test ) \ |
| 178 | {\ |
| 179 | return name; \ |
| 180 | }\ |
| 181 | +(const char*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Description_test ) \ |
| 182 | { \ |
| 183 | return desc; \ |
| 184 | } \ |
| 185 | -(void) INTERNAL_CATCH_UNIQUE_NAME( Catch_TestCase_test ) |
| 186 | |
| 187 | #endif // TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED |