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 | 861a1e7 | 2012-04-28 12:29:52 +0100 | [diff] [blame] | 31 | inline void arcSafeRelease( NSObject* obj ) |
| 32 | { |
| 33 | [obj release]; |
| 34 | } |
| 35 | inline id performOptionalSelector( id obj, SEL sel ) |
| 36 | { |
| 37 | if( [obj respondsToSelector: sel] ) |
| 38 | return [obj performSelector: sel]; |
| 39 | return nil; |
| 40 | } |
| 41 | #define CATCH_UNSAFE_UNRETAINED |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 42 | #else |
Phil Nash | 861a1e7 | 2012-04-28 12:29:52 +0100 | [diff] [blame] | 43 | inline void arcSafeRelease( NSObject* ){} |
| 44 | inline id performOptionalSelector( id obj, SEL sel ) |
| 45 | { |
| 46 | #pragma clang diagnostic push |
| 47 | #pragma clang diagnostic ignored "-Warc-performSelector-leaks" |
| 48 | if( [obj respondsToSelector: sel] ) |
| 49 | return [obj performSelector: sel]; |
| 50 | #pragma clang diagnostic pop |
| 51 | return nil; |
| 52 | } |
| 53 | #define CATCH_UNSAFE_UNRETAINED __unsafe_unretained |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 54 | #endif |
| 55 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | // This protocol is really only here for (self) documenting purposes, since |
| 58 | // all its methods are optional. |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 59 | @protocol OcFixture |
| 60 | |
| 61 | @optional |
| 62 | |
| 63 | -(void) setUp; |
| 64 | -(void) tearDown; |
| 65 | |
| 66 | @end |
| 67 | |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 68 | namespace Catch |
| 69 | { |
Phil Nash | 0847a0f | 2011-02-01 16:09:18 +0000 | [diff] [blame] | 70 | class OcMethod : public ITestCase |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 71 | { |
| 72 | public: |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 73 | /////////////////////////////////////////////////////////////////////// |
| 74 | OcMethod |
| 75 | ( |
| 76 | Class cls, |
| 77 | SEL sel |
| 78 | ) |
| 79 | : m_cls( cls ), |
| 80 | m_sel( sel ) |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 81 | { |
| 82 | } |
| 83 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 84 | /////////////////////////////////////////////////////////////////////// |
| 85 | virtual void invoke |
| 86 | () |
| 87 | const |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 88 | { |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 89 | id obj = [[m_cls alloc] init]; |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 90 | |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 91 | performOptionalSelector( obj, @selector(setUp) ); |
| 92 | performOptionalSelector( obj, m_sel ); |
| 93 | performOptionalSelector( obj, @selector(tearDown) ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 94 | |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 95 | arcSafeRelease( obj ); |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 98 | /////////////////////////////////////////////////////////////////////// |
| 99 | virtual ITestCase* clone |
| 100 | () |
| 101 | const |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 102 | { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 103 | return new OcMethod( m_cls, m_sel ); |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 106 | /////////////////////////////////////////////////////////////////////// |
| 107 | virtual bool operator == |
| 108 | ( |
| 109 | const ITestCase& other |
| 110 | ) |
| 111 | const |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 112 | { |
| 113 | const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); |
| 114 | return ocmOther && ocmOther->m_sel == m_sel; |
| 115 | } |
| 116 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 117 | /////////////////////////////////////////////////////////////////////// |
| 118 | virtual bool operator < |
| 119 | ( |
| 120 | const ITestCase& other |
| 121 | ) |
| 122 | const |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 123 | { |
| 124 | const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); |
| 125 | return ocmOther && ocmOther->m_sel < m_sel; |
| 126 | } |
| 127 | |
| 128 | private: |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 129 | Class m_cls; |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 130 | SEL m_sel; |
| 131 | }; |
| 132 | |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 133 | namespace Detail |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 134 | { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 135 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 136 | /////////////////////////////////////////////////////////////////////// |
| 137 | inline bool startsWith |
| 138 | ( |
| 139 | const std::string& str, |
| 140 | const std::string& sub |
| 141 | ) |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 142 | { |
| 143 | return str.length() > sub.length() && str.substr( 0, sub.length() ) == sub; |
| 144 | } |
| 145 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 146 | /////////////////////////////////////////////////////////////////////// |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 147 | inline std::string getAnnotation |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 148 | ( |
| 149 | Class cls, |
| 150 | const std::string& annotationName, |
| 151 | const std::string& testCaseName |
| 152 | ) |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 153 | { |
| 154 | NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; |
| 155 | SEL sel = NSSelectorFromString( selStr ); |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 156 | arcSafeRelease( selStr ); |
| 157 | id value = performOptionalSelector( cls, sel ); |
| 158 | if( value ) |
| 159 | return [(NSString*)value UTF8String]; |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 160 | return ""; |
| 161 | } |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 164 | /////////////////////////////////////////////////////////////////////////// |
| 165 | inline size_t registerTestMethods |
| 166 | () |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 167 | { |
| 168 | size_t noTestMethods = 0; |
| 169 | int noClasses = objc_getClassList( NULL, 0 ); |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 170 | |
Phil Nash | 861a1e7 | 2012-04-28 12:29:52 +0100 | [diff] [blame] | 171 | Class* classes = (CATCH_UNSAFE_UNRETAINED Class *)malloc( sizeof(Class) * noClasses); |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 172 | objc_getClassList( classes, noClasses ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 173 | |
| 174 | for( int c = 0; c < noClasses; c++ ) |
| 175 | { |
| 176 | Class cls = classes[c]; |
| 177 | { |
| 178 | u_int count; |
| 179 | Method* methods = class_copyMethodList( cls, &count ); |
| 180 | for( int m = 0; m < count ; m++ ) |
| 181 | { |
| 182 | SEL selector = method_getName(methods[m]); |
| 183 | std::string methodName = sel_getName(selector); |
| 184 | if( Detail::startsWith( methodName, "Catch_TestCase_" ) ) |
| 185 | { |
| 186 | std::string testCaseName = methodName.substr( 15 ); |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 187 | std::string name = Detail::getAnnotation( cls, "Name", testCaseName ); |
| 188 | std::string desc = Detail::getAnnotation( cls, "Description", testCaseName ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 189 | |
Phil Nash | b84444c | 2012-05-10 08:16:30 +0100 | [diff] [blame] | 190 | Context::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] | 191 | noTestMethods++; |
| 192 | |
| 193 | } |
| 194 | } |
| 195 | free(methods); |
| 196 | } |
| 197 | } |
| 198 | return noTestMethods; |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 201 | inline std::string toString( NSString* const& nsstring ) |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 202 | { |
| 203 | return std::string( "@\"" ) + [nsstring UTF8String] + "\""; |
| 204 | } |
| 205 | |
| 206 | namespace Matchers |
| 207 | { |
| 208 | namespace Impl |
| 209 | { |
| 210 | namespace NSStringMatchers |
| 211 | { |
| 212 | struct StringHolder |
| 213 | { |
| 214 | StringHolder( NSString* substr ) : m_substr( [substr copy] ){} |
| 215 | StringHolder() |
| 216 | { |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 217 | arcSafeRelease( m_substr ); |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | NSString* m_substr; |
| 221 | }; |
| 222 | |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 223 | struct Equals : StringHolder |
| 224 | { |
| 225 | Equals( NSString* substr ) : StringHolder( substr ){} |
| 226 | |
| 227 | bool operator()( NSString* str ) const |
| 228 | { |
| 229 | return [str isEqualToString:m_substr]; |
| 230 | } |
| 231 | |
| 232 | friend std::ostream& operator<<( std::ostream& os, const Equals& matcher ) |
| 233 | { |
| 234 | os << "equals string: " << Catch::toString( matcher.m_substr ); |
| 235 | return os; |
| 236 | } |
| 237 | }; |
| 238 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 239 | struct Contains : StringHolder |
| 240 | { |
| 241 | Contains( NSString* substr ) : StringHolder( substr ){} |
| 242 | |
| 243 | bool operator()( NSString* str ) const |
| 244 | { |
| 245 | return [str rangeOfString:m_substr].location != NSNotFound; |
| 246 | } |
| 247 | |
| 248 | friend std::ostream& operator<<( std::ostream& os, const Contains& matcher ) |
| 249 | { |
| 250 | os << "contains: " << Catch::toString( matcher.m_substr ); |
| 251 | return os; |
| 252 | } |
| 253 | }; |
| 254 | |
| 255 | struct StartsWith : StringHolder |
| 256 | { |
| 257 | StartsWith( NSString* substr ) : StringHolder( substr ){} |
| 258 | |
| 259 | bool operator()( NSString* str ) const |
| 260 | { |
| 261 | return [str rangeOfString:m_substr].location == 0; |
| 262 | } |
| 263 | |
| 264 | friend std::ostream& operator<<( std::ostream& os, const StartsWith& matcher ) |
| 265 | { |
| 266 | os << "starts with: " << Catch::toString( matcher.m_substr ); |
| 267 | return os; |
| 268 | } |
| 269 | }; |
| 270 | struct EndsWith : StringHolder |
| 271 | { |
| 272 | EndsWith( NSString* substr ) : StringHolder( substr ){} |
| 273 | |
| 274 | bool operator()( NSString* str ) const |
| 275 | { |
| 276 | return [str rangeOfString:m_substr].location == [str length] - [m_substr length]; |
| 277 | } |
| 278 | |
| 279 | friend std::ostream& operator<<( std::ostream& os, const EndsWith& matcher ) |
| 280 | { |
| 281 | os << "ends with: " << Catch::toString( matcher.m_substr ); |
| 282 | return os; |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | } // namespace NSStringMatchers |
| 287 | } // namespace Impl |
| 288 | |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 289 | inline Impl::NSStringMatchers::Equals |
| 290 | Equals( NSString* substr ){ return Impl::NSStringMatchers::Equals( substr ); } |
| 291 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 292 | inline Impl::NSStringMatchers::Contains |
| 293 | Contains( NSString* substr ){ return Impl::NSStringMatchers::Contains( substr ); } |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 294 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 295 | inline Impl::NSStringMatchers::StartsWith |
| 296 | StartsWith( NSString* substr ){ return Impl::NSStringMatchers::StartsWith( substr ); } |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 297 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 298 | inline Impl::NSStringMatchers::EndsWith |
| 299 | EndsWith( NSString* substr ){ return Impl::NSStringMatchers::EndsWith( substr ); } |
| 300 | |
| 301 | } // namespace Matchers |
| 302 | |
| 303 | using namespace Matchers; |
| 304 | |
| 305 | } // namespace Catch |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 306 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 307 | /////////////////////////////////////////////////////////////////////////////// |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 308 | #define OC_TEST_CASE( name, desc )\ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 309 | +(NSString*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Name_test ) \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 310 | {\ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 311 | return @ name; \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 312 | }\ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 313 | +(NSString*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Description_test ) \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 314 | { \ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 315 | return @ desc; \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 316 | } \ |
| 317 | -(void) INTERNAL_CATCH_UNIQUE_NAME( Catch_TestCase_test ) |
| 318 | |
Phil Nash | d10d2d3 | 2012-05-10 21:46:46 +0100 | [diff] [blame] | 319 | #endif // TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED |