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