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 | ddfe963 | 2012-08-14 19:30:30 +0100 | [diff] [blame] | 20 | #include "internal/catch_test_case_info.h" |
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 | |
Phil Nash | 9c6ce97 | 2012-08-14 08:38:22 +0100 | [diff] [blame] | 36 | class OcMethod : public SharedImpl<ITestCase> { |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 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 | } |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 50 | private: |
Phil Nash | 9c6ce97 | 2012-08-14 08:38:22 +0100 | [diff] [blame] | 51 | virtual ~OcMethod() {} |
| 52 | |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 53 | Class m_cls; |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 54 | SEL m_sel; |
| 55 | }; |
| 56 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 57 | namespace Detail{ |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 58 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 59 | inline bool startsWith( std::string const& str, std::string const& sub ) { |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 60 | return str.length() > sub.length() && str.substr( 0, sub.length() ) == sub; |
| 61 | } |
| 62 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 63 | inline std::string getAnnotation( Class cls, |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 64 | std::string const& annotationName, |
| 65 | std::string const& testCaseName ) { |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 66 | NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; |
| 67 | SEL sel = NSSelectorFromString( selStr ); |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 68 | arcSafeRelease( selStr ); |
| 69 | id value = performOptionalSelector( cls, sel ); |
| 70 | if( value ) |
| 71 | return [(NSString*)value UTF8String]; |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 72 | return ""; |
| 73 | } |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 74 | } |
Phil Nash | 49d7ce4 | 2013-03-21 08:58:22 +0000 | [diff] [blame] | 75 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 76 | inline size_t registerTestMethods() { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 77 | size_t noTestMethods = 0; |
| 78 | int noClasses = objc_getClassList( NULL, 0 ); |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 79 | |
Phil Nash | 861a1e7 | 2012-04-28 12:29:52 +0100 | [diff] [blame] | 80 | Class* classes = (CATCH_UNSAFE_UNRETAINED Class *)malloc( sizeof(Class) * noClasses); |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 81 | objc_getClassList( classes, noClasses ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 82 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 83 | for( int c = 0; c < noClasses; c++ ) { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 84 | Class cls = classes[c]; |
| 85 | { |
| 86 | u_int count; |
| 87 | Method* methods = class_copyMethodList( cls, &count ); |
Graham Lee | 8cfe821 | 2012-05-21 18:11:55 +0200 | [diff] [blame] | 88 | for( u_int m = 0; m < count ; m++ ) { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 89 | SEL selector = method_getName(methods[m]); |
| 90 | std::string methodName = sel_getName(selector); |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 91 | if( Detail::startsWith( methodName, "Catch_TestCase_" ) ) { |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 92 | std::string testCaseName = methodName.substr( 15 ); |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 93 | std::string name = Detail::getAnnotation( cls, "Name", testCaseName ); |
| 94 | std::string desc = Detail::getAnnotation( cls, "Description", testCaseName ); |
Phil Nash | 429699e | 2013-02-19 08:39:28 +0000 | [diff] [blame] | 95 | const char* className = class_getName( cls ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 96 | |
Phil Nash | 429699e | 2013-02-19 08:39:28 +0000 | [diff] [blame] | 97 | getMutableRegistryHub().registerTest( makeTestCase( new OcMethod( cls, selector ), className, name.c_str(), desc.c_str(), SourceLineInfo() ) ); |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 98 | noTestMethods++; |
Phil Nash | f59ecbc | 2010-11-16 07:00:08 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | free(methods); |
| 102 | } |
| 103 | } |
| 104 | return noTestMethods; |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 107 | namespace Matchers { |
| 108 | namespace Impl { |
| 109 | namespace NSStringMatchers { |
| 110 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 111 | template<typename MatcherT> |
| 112 | struct StringHolder : MatcherImpl<MatcherT, NSString*>{ |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 113 | StringHolder( NSString* substr ) : m_substr( [substr copy] ){} |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 114 | StringHolder( StringHolder const& other ) : m_substr( [other.m_substr copy] ){} |
Phil Nash | 44fbbb0 | 2012-05-16 15:07:11 +0100 | [diff] [blame] | 115 | StringHolder() { |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 116 | arcSafeRelease( m_substr ); |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Phil Nash | 0dc9e43 | 2012-08-01 08:17:07 +0100 | [diff] [blame] | 119 | NSString* m_substr; |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 122 | struct Equals : StringHolder<Equals> { |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 123 | Equals( NSString* substr ) : StringHolder( substr ){} |
| 124 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 125 | virtual bool match( ExpressionType const& str ) const { |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 126 | return [str isEqualToString:m_substr]; |
| 127 | } |
| 128 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 129 | virtual std::string toString() const { |
| 130 | return "equals string: \"" + Catch::toString( m_substr ) + "\""; |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 131 | } |
| 132 | }; |
| 133 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 134 | struct Contains : StringHolder<Contains> { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 135 | Contains( NSString* substr ) : StringHolder( substr ){} |
| 136 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 137 | virtual bool match( ExpressionType const& str ) const { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 138 | return [str rangeOfString:m_substr].location != NSNotFound; |
| 139 | } |
| 140 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 141 | virtual std::string toString() const { |
| 142 | return "contains string: \"" + Catch::toString( m_substr ) + "\""; |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 143 | } |
| 144 | }; |
| 145 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 146 | struct StartsWith : StringHolder<StartsWith> { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 147 | StartsWith( NSString* substr ) : StringHolder( substr ){} |
| 148 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 149 | virtual bool match( ExpressionType const& str ) const { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 150 | return [str rangeOfString:m_substr].location == 0; |
| 151 | } |
| 152 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 153 | virtual std::string toString() const { |
| 154 | return "starts with: \"" + Catch::toString( m_substr ) + "\""; |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 155 | } |
| 156 | }; |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 157 | struct EndsWith : StringHolder<EndsWith> { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 158 | EndsWith( NSString* substr ) : StringHolder( substr ){} |
| 159 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 160 | virtual bool match( ExpressionType const& str ) const { |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 161 | return [str rangeOfString:m_substr].location == [str length] - [m_substr length]; |
| 162 | } |
| 163 | |
Phil Nash | 4424634 | 2012-12-06 08:41:38 +0000 | [diff] [blame] | 164 | virtual std::string toString() const { |
| 165 | return "ends with: \"" + Catch::toString( m_substr ) + "\""; |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 166 | } |
| 167 | }; |
| 168 | |
| 169 | } // namespace NSStringMatchers |
| 170 | } // namespace Impl |
| 171 | |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 172 | inline Impl::NSStringMatchers::Equals |
| 173 | Equals( NSString* substr ){ return Impl::NSStringMatchers::Equals( substr ); } |
| 174 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 175 | inline Impl::NSStringMatchers::Contains |
| 176 | Contains( NSString* substr ){ return Impl::NSStringMatchers::Contains( substr ); } |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 177 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 178 | inline Impl::NSStringMatchers::StartsWith |
| 179 | StartsWith( NSString* substr ){ return Impl::NSStringMatchers::StartsWith( substr ); } |
Phil Nash | db837a1 | 2012-03-14 20:04:50 +0000 | [diff] [blame] | 180 | |
Phil Nash | 966f5db | 2012-03-04 21:18:46 +0000 | [diff] [blame] | 181 | inline Impl::NSStringMatchers::EndsWith |
| 182 | EndsWith( NSString* substr ){ return Impl::NSStringMatchers::EndsWith( substr ); } |
| 183 | |
| 184 | } // namespace Matchers |
| 185 | |
| 186 | using namespace Matchers; |
| 187 | |
| 188 | } // namespace Catch |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 189 | |
Phil Nash | 58e9a8b | 2011-02-08 08:42:05 +0000 | [diff] [blame] | 190 | /////////////////////////////////////////////////////////////////////////////// |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 191 | #define OC_TEST_CASE( name, desc )\ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 192 | +(NSString*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Name_test ) \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 193 | {\ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 194 | return @ name; \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 195 | }\ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 196 | +(NSString*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Description_test ) \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 197 | { \ |
Phil Nash | 53c990a | 2012-03-17 18:20:06 +0000 | [diff] [blame] | 198 | return @ desc; \ |
Phil Nash | d52f61c | 2010-11-14 22:47:30 +0000 | [diff] [blame] | 199 | } \ |
| 200 | -(void) INTERNAL_CATCH_UNIQUE_NAME( Catch_TestCase_test ) |
| 201 | |
Phil Nash | d10d2d3 | 2012-05-10 21:46:46 +0100 | [diff] [blame] | 202 | #endif // TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED |