This repository has been archived by the owner on May 19, 2018. It is now read-only.
/
RXAssertions.h
79 lines (59 loc) · 3.93 KB
/
RXAssertions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// RXAssertions.h
// Created by Rob Rix on 2009-08-20
// Copyright 2009 Decimus Software, Inc.
#import <SenTestingKit/SenTestingKit.h>
// Assertion macros that don’t require you to describe the assertion. Perfect for use with intention-revealing code.
// Don’t use this unless you’re writing your own assertions. The first argument is ignored, so the assertions can have optional messages appended to them without suffering a compiler error.
#define RXOptionalMessageString(ignored, format, ...) [NSString stringWithFormat: (format), ## __VA_ARGS__]
#define RXAssert(_expression, ...) {\
__typeof__(_expression) __condition = (_expression);\
if(!__condition)\
STFail(RXOptionalMessageString(, ## __VA_ARGS__, @"%s was unexpectedly false.", #_expression));\
}
#define RXAssertFalse(_expression, ...) {\
__typeof__(_expression) __condition = (_expression);\
if(__condition)\
STFail(RXOptionalMessageString(, ## __VA_ARGS__, @"%s was unexpectedly true.", #_expression));\
}
// casts the expected value to the type of the actual value. will fail (and rightly so) if you try crazy casts like struct to pointer.
#define RXAssertEquals(_actual, _expected, ...) {\
__typeof__(_actual) __actual = (_actual), __expected = (__typeof__(__actual))(_expected);\
if(![RXAssertionHelper compareValue: &__actual withValue: &__expected ofObjCType: @encode(__typeof__(__actual))]) {\
STFail(@"%s has value %@, not expected value %@. %@", #_actual, [RXAssertionHelper descriptionForValue: &__actual ofObjCType: @encode(__typeof__(__actual))], [RXAssertionHelper descriptionForValue: &__expected ofObjCType: @encode(__typeof__(__actual))], RXOptionalMessageString(, ## __VA_ARGS__, @""));\
}\
}
#define RXAssertNotEquals(_actual, _expected, ...) {\
__typeof__(_actual) __actual = (_actual), __expected = (__typeof__(__actual))(_expected);\
if([RXAssertionHelper compareValue: &__actual withValue: &__expected ofObjCType: @encode(__typeof__(__actual))]) {\
STFail(@"%s has unexpected value %@. %@", #_actual, [RXAssertionHelper descriptionForValue: &__actual ofObjCType: @encode(__typeof__(__actual))], RXOptionalMessageString(, ## __VA_ARGS__, @""));\
}\
}
#define RXAssertNil(_thing, ...) {\
__typeof__(_thing) __thing = (_thing);\
if(__thing != nil) STFail(RXOptionalMessageString(, ## __VA_ARGS__, @"%s was unexpectedly %@, not nil.", #_thing, __thing));\
}
#define RXAssertNotNil(_thing, ...) if((_thing) == nil) STFail(RXOptionalMessageString(, ## __VA_ARGS__, @"%s was unexpectedly nil.", #_thing))
#ifdef __clang__
// this is bad, as strict aliasing will break it, but clang doesn’t handle union casts correctly
#define RXCast(x, toType) *(toType *)&(x)
#else
#define RXCast(x, toType) (((union{__typeof__(x) a; toType b;})x).b)
#endif
#define RXRound(value, place) (round((value) / (place)) * (place))
typedef BOOL (*RXAssertionHelperComparisonFunction)(const void *aRef, const void *bRef);
typedef NSString *(*RXAssertionHelperDescriptionFunction)(const void *ref);
// making these functions available for registering Polymorph types
BOOL RXAssertionHelperObjectComparison(const void *a, const void *b);
NSString *RXAssertionHelperObjectDescription(const void *ref);
BOOL RXAssertionHelperCFTypeRefComparison(const void *a, const void *b);
NSString *RXAssertionHelperCFTypeRefDescription(const void *ref);
@interface RXAssertionHelper : NSObject
+(void)registerComparisonFunction:(RXAssertionHelperComparisonFunction)comparator forObjCType:(const char *)type;
+(BOOL)compareValue:(const void *)aRef withValue:(const void *)bRef ofObjCType:(const char *)type;
+(void)registerDescriptionFunction:(RXAssertionHelperDescriptionFunction)descriptor forObjCType:(const char *)type;
+(NSString *)descriptionForValue:(const void *)ref ofObjCType:(const char *)type;
+(double)floatingPointComparisonAccuracy;
+(void)setFloatingPointComparisonAccuracy:(double)epsilon;
// returns a nicely formatted name for the test case selector
+(NSString *)humanReadableNameForTestCaseSelector:(SEL)selector;
@end