Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 1 | /* |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 2 | * Created by Phil on 14/11/2010. |
| 3 | * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. |
| 4 | * |
| 5 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 7 | */ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 8 | #ifndef TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED |
| 9 | #define TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED |
| 10 | |
Phil Nash | 6abf702 | 2012-02-10 08:28:37 +0000 | [diff] [blame] | 11 | #import <Foundation/Foundation.h> |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 12 | #import <objc/runtime.h> |
Phil Nash | 6abf702 | 2012-02-10 08:28:37 +0000 | [diff] [blame] | 13 | |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 14 | #include <string> |
Phil Nash | 823ea3e | 2011-04-26 08:32:40 +0100 | [diff] [blame] | 15 | |
Phil Nash | 89d1e6c | 2011-05-24 08:23:02 +0100 | [diff] [blame] | 16 | // NB. Any general catch headers included here must be included |
| 17 | // in catch.hpp first to make sure they are included by the single |
| 18 | // header for non obj-usage |
Phil Nash | 0847a0f | 2011-02-01 16:09:18 +0000 | [diff] [blame] | 19 | #include "internal/catch_test_case_info.hpp" |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 20 | |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 21 | #ifdef __has_feature |
| 22 | #define CATCH_ARC_ENABLED __has_feature(objc_arc) |
| 23 | #else |
| 24 | #define CATCH_ARC_ENABLED 0 |
| 25 | #endif |
| 26 | |
| 27 | void arcSafeRelease( NSObject* obj ); |
| 28 | id performOptionalSelector( id obj, SEL sel ); |
| 29 | |
| 30 | #if !CATCH_ARC_ENABLED |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 31 | inline void arcSafeRelease( NSObject* obj ) { |
Phil Nash | 861a1e7 | 2012-04-28 12:29:52 +0100 | [diff] [blame] | 32 | [obj release]; |
| 33 | } |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 34 | inline id performOptionalSelector( id obj, SEL sel ) { |
Phil Nash | 861a1e7 | 2012-04-28 12:29:52 +0100 | [diff] [blame] | 35 | if( [obj respondsToSelector: sel] ) |
| 36 | return [obj performSelector: sel]; |
| 37 | return nil; |
| 38 | } |
| 39 | #define CATCH_UNSAFE_UNRETAINED |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 40 | #else |
Phil Nash | 861a1e7 | 2012-04-28 12:29:52 +0100 | [diff] [blame] | 41 | inline void arcSafeRelease( NSObject* ){} |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 42 | inline id performOptionalSelector( id obj, SEL sel ) { |
Phil Nash | 861a1e7 | 2012-04-28 12:29:52 +0100 | [diff] [blame] | 43 | #pragma clang diagnostic push |
| 44 | #pragma clang diagnostic ignored "-Warc-performSelector-leaks" |
| 45 | if( [obj respondsToSelector: sel] ) |
| 46 | return [obj performSelector: sel]; |
| 47 | #pragma clang diagnostic pop |
| 48 | return nil; |
| 49 | } |
| 50 | #define CATCH_UNSAFE_UNRETAINED __unsafe_unretained |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 53 | /////////////////////////////////////////////////////////////////////////////// |
| 54 | // This protocol is really only here for (self) documenting purposes, since |
| 55 | // all its methods are optional. |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 56 | @protocol OcFixture |
| 57 | |
| 58 | @optional |
| 59 | |
| 60 | -(void) setUp; |
| 61 | -(void) tearDown; |
| 62 | |
| 63 | @end |
| 64 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 65 | namespace Catch { |
| 66 | |
| 67 | class OcMethod : public ITestCase { |
| 68 | |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 69 | public: |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 70 | OcMethod( Class cls, SEL sel ) : m_cls( cls ), m_sel( sel ) {} |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 71 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 72 | virtual void invoke() const { |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 73 | id obj = [[m_cls alloc] init]; |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 74 | |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 75 | performOptionalSelector( obj, @selector(setUp) ); |
| 76 | performOptionalSelector( obj, m_sel ); |
| 77 | performOptionalSelector( obj, @selector(tearDown) ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 78 | |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 79 | arcSafeRelease( obj ); |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 82 | virtual ITestCase* clone() const { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 83 | return new OcMethod( m_cls, m_sel ); |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 86 | virtual bool operator == ( const ITestCase& other ) const { |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 87 | const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); |
| 88 | return ocmOther && ocmOther->m_sel == m_sel; |
| 89 | } |
| 90 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 91 | virtual bool operator < ( const ITestCase& other ) const { |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 92 | const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); |
| 93 | return ocmOther && ocmOther->m_sel < m_sel; |
| 94 | } |
| 95 | |
| 96 | private: |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 97 | Class m_cls; |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 98 | SEL m_sel; |
| 99 | }; |
| 100 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 101 | namespace Detail{ |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 102 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 103 | inline bool startsWith( const std::string& str, const std::string& sub ) { |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 104 | return str.length() > sub.length() && str.substr( 0, sub.length() ) == sub; |
| 105 | } |
| 106 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 107 | inline std::string getAnnotation( Class cls, |
| 108 | const std::string& annotationName, |
| 109 | const std::string& testCaseName ) { |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 110 | NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; |
| 111 | SEL sel = NSSelectorFromString( selStr ); |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 112 | arcSafeRelease( selStr ); |
| 113 | id value = performOptionalSelector( cls, sel ); |
| 114 | if( value ) |
| 115 | return [(NSString*)value UTF8String]; |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 116 | return ""; |
| 117 | } |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 120 | inline size_t registerTestMethods() { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 121 | size_t noTestMethods = 0; |
| 122 | int noClasses = objc_getClassList( NULL, 0 ); |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 123 | |
Phil Nash | 861a1e7 | 2012-04-28 12:29:52 +0100 | [diff] [blame] | 124 | Class* classes = (CATCH_UNSAFE_UNRETAINED Class *)malloc( sizeof(Class) * noClasses); |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 125 | objc_getClassList( classes, noClasses ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 126 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 127 | for( int c = 0; c < noClasses; c++ ) { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 128 | Class cls = classes[c]; |
| 129 | { |
| 130 | u_int count; |
| 131 | Method* methods = class_copyMethodList( cls, &count ); |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 132 | for( int m = 0; m < count ; m++ ) { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 133 | SEL selector = method_getName(methods[m]); |
| 134 | std::string methodName = sel_getName(selector); |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 135 | if( Detail::startsWith( methodName, "Catch_TestCase_" ) ) { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 136 | std::string testCaseName = methodName.substr( 15 ); |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 137 | std::string name = Detail::getAnnotation( cls, "Name", testCaseName ); |
| 138 | std::string desc = Detail::getAnnotation( cls, "Description", testCaseName ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 139 | |
Phil Nash | 371db8b | 2012-05-21 18:52:09 +0100 | [diff] [blame^] | 140 | getCurrentContext().getTestCaseRegistry().registerTest( TestCaseInfo( new OcMethod( cls, selector ), name.c_str(), desc.c_str(), SourceLineInfo() ) ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 141 | noTestMethods++; |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | free(methods); |
| 145 | } |
| 146 | } |
| 147 | return noTestMethods; |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 150 | inline std::string toString( NSString* const& nsstring ) { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 151 | return std::string( "@\"" ) + [nsstring UTF8String] + "\""; |
| 152 | } |
| 153 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 154 | namespace Matchers { |
| 155 | namespace Impl { |
| 156 | namespace NSStringMatchers { |
| 157 | |
| 158 | struct StringHolder { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 159 | StringHolder( NSString* substr ) : m_substr( [substr copy] ){} |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 160 | StringHolder() { |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 161 | arcSafeRelease( m_substr ); |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | NSString* m_substr; |
| 165 | }; |
| 166 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 167 | struct Equals : StringHolder { |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 168 | Equals( NSString* substr ) : StringHolder( substr ){} |
| 169 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 170 | bool operator()( NSString* str ) const { |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 171 | return [str isEqualToString:m_substr]; |
| 172 | } |
| 173 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 174 | friend std::ostream& operator<<( std::ostream& os, const Equals& matcher ) { |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 175 | os << "equals string: " << Catch::toString( matcher.m_substr ); |
| 176 | return os; |
| 177 | } |
| 178 | }; |
| 179 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 180 | struct Contains : StringHolder { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 181 | Contains( NSString* substr ) : StringHolder( substr ){} |
| 182 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 183 | bool operator()( NSString* str ) const { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 184 | return [str rangeOfString:m_substr].location != NSNotFound; |
| 185 | } |
| 186 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 187 | friend std::ostream& operator<<( std::ostream& os, const Contains& matcher ) { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 188 | os << "contains: " << Catch::toString( matcher.m_substr ); |
| 189 | return os; |
| 190 | } |
| 191 | }; |
| 192 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 193 | struct StartsWith : StringHolder { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 194 | StartsWith( NSString* substr ) : StringHolder( substr ){} |
| 195 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 196 | bool operator()( NSString* str ) const { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 197 | return [str rangeOfString:m_substr].location == 0; |
| 198 | } |
| 199 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 200 | friend std::ostream& operator<<( std::ostream& os, const StartsWith& matcher ) { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 201 | os << "starts with: " << Catch::toString( matcher.m_substr ); |
| 202 | return os; |
| 203 | } |
| 204 | }; |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 205 | struct EndsWith : StringHolder { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 206 | EndsWith( NSString* substr ) : StringHolder( substr ){} |
| 207 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 208 | bool operator()( NSString* str ) const { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 209 | return [str rangeOfString:m_substr].location == [str length] - [m_substr length]; |
| 210 | } |
| 211 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 212 | friend std::ostream& operator<<( std::ostream& os, const EndsWith& matcher ) { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 213 | os << "ends with: " << Catch::toString( matcher.m_substr ); |
| 214 | return os; |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | } // namespace NSStringMatchers |
| 219 | } // namespace Impl |
| 220 | |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 221 | inline Impl::NSStringMatchers::Equals |
| 222 | Equals( NSString* substr ){ return Impl::NSStringMatchers::Equals( substr ); } |
| 223 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 224 | inline Impl::NSStringMatchers::Contains |
| 225 | Contains( NSString* substr ){ return Impl::NSStringMatchers::Contains( substr ); } |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 226 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 227 | inline Impl::NSStringMatchers::StartsWith |
| 228 | StartsWith( NSString* substr ){ return Impl::NSStringMatchers::StartsWith( substr ); } |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 229 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 230 | inline Impl::NSStringMatchers::EndsWith |
| 231 | EndsWith( NSString* substr ){ return Impl::NSStringMatchers::EndsWith( substr ); } |
| 232 | |
| 233 | } // namespace Matchers |
| 234 | |
| 235 | using namespace Matchers; |
| 236 | |
| 237 | } // namespace Catch |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 238 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 239 | /////////////////////////////////////////////////////////////////////////////// |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 240 | #define OC_TEST_CASE( name, desc )\ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 241 | +(NSString*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Name_test ) \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 242 | {\ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 243 | return @ name; \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 244 | }\ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 245 | +(NSString*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Description_test ) \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 246 | { \ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 247 | return @ desc; \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 248 | } \ |
| 249 | -(void) INTERNAL_CATCH_UNIQUE_NAME( Catch_TestCase_test ) |
| 250 | |
Phil Nash | d10d2d3 | 2012-05-10 21:46:46 +0100 | [diff] [blame] | 251 | #endif // TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED |