Test.h
author Jens Alfke <jens@mooseyard.com>
Mon Apr 14 13:58:48 2008 -0700 (2008-04-14)
changeset 4 64823cdde6a5
parent 0 d84d25d6cdbb
child 9 823e7e74088e
permissions -rw-r--r--
Minor improvements.
     1 //
     2 //  Test.h
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 1/5/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import "CollectionUtils.h"
    11 
    12 
    13 /** Call this first thing in main() to run tests.
    14     This function is a no-op if the DEBUG macro is not defined (i.e. in a release build).
    15     At runtime, to cause a particular test "X" to run, add a command-line argument "Test_X".
    16     To run all tests, add the argument "Test_All".
    17     To run only tests without starting the main program, add the argument "Test_Only". */
    18 #if DEBUG
    19 void RunTestCases( int argc, const char **argv );
    20 extern BOOL gRunningTestCase;
    21 #else
    22 #define RunTestCases(ARGC,ARGV)
    23 #define gRunningTestCase NO
    24 #endif
    25 
    26 /** The TestCase() macro declares a test case.
    27     Its argument is a name for the test case (without quotes), and it's followed with a block
    28     of code implementing the test.
    29     The code should raise an exception if anything fails.
    30     The CAssert, CAssertEqual and CAssertEq macros, below, are the most useful way to do this.
    31     A test case can register a dependency on another test case by calling RequireTestCase().
    32     Example:
    33         TestCase(MyLib) {
    34             RequireTestCase("LibIDependOn");
    35             CAssertEq( myFunction(), 12345 );
    36         }
    37     Test cases are disabled if the DEBUG macro is not defined (i.e. in a release build). */
    38 #if DEBUG
    39 #define TestCase(NAME)      void Test_##NAME(void); \
    40                             struct TestCaseLink linkToTest##NAME = {&Test_##NAME,#NAME}; \
    41                             __attribute__((constructor)) static void registerTestCase##NAME() \
    42                                 {linkToTest##NAME.next = gAllTestCases; gAllTestCases=&linkToTest##NAME; } \
    43                             void Test_##NAME(void)
    44 #else
    45 #define TestCase(NAME)      __attribute__((unused)) static void Test_##NAME(void)
    46 #endif
    47 
    48 /** Can call this in a test case to indicate a prerequisite.
    49     The prerequisite test will be run first, and if it fails, the current test case will be skipped. */
    50 #define RequireTestCase(NAME)   _RequireTestCase(#NAME)
    51 void _RequireTestCase( const char *name );
    52 
    53 
    54 
    55 /** General-purpose assertions, replacing NSAssert etc.. You can use these outside test cases. */
    56 
    57 #define Assert(COND,MSG...)    do{ if( __builtin_expect(!(COND),NO) ) \
    58                                     _AssertFailed(self,(const char*)_cmd, __FILE__, __LINE__,\
    59                                                   #COND,##MSG,NULL); }while(0)
    60 #define CAssert(COND,MSG...)    do{ if( __builtin_expect(!(COND),NO) ) \
    61                                     _AssertFailed(nil, __PRETTY_FUNCTION__, __FILE__, __LINE__,\
    62                                                   #COND,##MSG,NULL); }while(0)
    63 
    64 #define AssertEqual(VAL,EXPECTED)   do{ id _val = VAL, _expected = EXPECTED;\
    65                                         Assert(_val==_expected || [_val isEqual: _expected], @"Unexpected value for %s: %@ (expected %@)", #VAL,_val,_expected); \
    66                                     }while(0)
    67 #define CAssertEqual(VAL,EXPECTED)  do{ id _val = (VAL), _expected = (EXPECTED);\
    68                                         CAssert(_val==_expected || [_val isEqual: _expected], @"Unexpected value for %s: %@ (expected %@)", #VAL,_val,_expected); \
    69                                     }while(0)
    70 
    71 // AssertEq is for scalars (int, float...)
    72 #define AssertEq(VAL,EXPECTED)  do{ typeof(VAL) _val = VAL; typeof(EXPECTED) _expected = EXPECTED;\
    73                                     Assert(_val==_expected, @"Unexpected value for %s: %@ (expected %@)", #VAL,$object(_val),$object(_expected)); \
    74                                 }while(0)
    75 #define CAssertEq(VAL,EXPECTED) do{ typeof(VAL) _val = VAL; typeof(EXPECTED) _expected = EXPECTED;\
    76                                     CAssert(_val==_expected, @"Unexpected value for %s: %@ (expected %@)", #VAL,$object(_val),$object(_expected)); \
    77                                 }while(0)
    78 
    79 
    80 #pragma mark -
    81 #pragma mark EXCEPTION UTILITIES:
    82 
    83 @interface NSException (MooseyardUtil)
    84 /** Returns a textual, human-readable backtrace of the point where the exception was thrown. */
    85 - (NSString*) my_callStack;
    86 @end
    87 
    88 
    89 
    90 // Nasty internals ...
    91 #if DEBUG
    92 void _RunTestCase( void (*testptr)(), const char *name );
    93 
    94 struct TestCaseLink {void (*testptr)(); const char *name; BOOL passed; struct TestCaseLink *next;};
    95 extern struct TestCaseLink *gAllTestCases;
    96 #endif DEBUG
    97 void _AssertFailed( id rcvr, const char *selOrFn, const char *sourceFile, int sourceLine,
    98                    const char *condString, NSString *message, ... ) __attribute__((noreturn));