(Cleverly assertive macros for your testing enjoyment.)
The key ideas here are simple:
- Simple API. Fewer assertion macros are better.
- Simple asserting. The macros should give you a smart enough error message that you don’t need to provide your own.
STAssert*. If you need to customize the message being logged, you can fall back to the
STAssert*macros exactly where you need to.
All of these log source for the condition/expression as appropriate. Anything else that gets logged is noted alongside.
conditionis true. Failures look like this:
<condition> was unexpectedly false.
conditionis false. Failures look like this:
<condition> was unexpectedly true.
Asserts that the actual value is equal to the expected value; if it’s not, it logs what it actually was.
This is valid for use with objects, scalars (including floats; see RXRound for more), structs, and really anything that you’ve registered a comparator for. See
RXAssertionHelperfor more info. Failures look like this:
<actual expression> has value <actual value>, not expected value <expected value>.
Asserts that the actual value is not equal to the unexpected value; if it is, it logs what it unexpectedly was. Failures look like this:
<actual expression> has unexpected value <actual value>.
Asserts that the object is nil. If it isn’t, it logs what it actually was. Failures look like this:
<expression> was unexpectedly <expression value>, not nil.
Asserts that the object is not nil.
<expression> was unexpectedly nil.
These are helpful macros which are used by RXAssertions or which are intended for your use.
Casts value to type using an on-the-fly union. This is safe for use with strict aliasing.
Rounds value to place, e.g.
RXRound(M_PI, 0.01)will result in
3.14. You can use this with
RXAssertEqualsto easily test against floating point fixtures without worrying about IEEE float precision problems:
RXAssertEquals(RXRound([thing returnPotentiallyImpreciseFloat], 0.01), 1.23);
+[RXAssertionHelper floatingPointComparisonAccuracy]for an alternative.
True to its name,
RXAssertionHelper helps the assertion macros with some of their tasks, namely:
Comparing values of arbitrary type.
+compareValue:withValue:ofObjCType:are used to add new comparators.
RXAssertions ships with comparators for signed and unsigned integers of 8, 16, 32, and 64 bits of width;
doubles; objects and classes;
CGPoints via the same function); and arbitrary pointers (and thus, unsupported types will be compared with pointer equality, which will presumably fail every time).
To add other comparators, simply write the comparison function (matching the
RXAssertionHelperComparisonFunctiontypedef) and register it with
RXAssertionHelpersomewhere convenient before you call
RXAssertEqualswith values of this type, perhaps in your test suite’s
There is currently no support for comparing values of wildly different types! The expected value is assigned to a variable of the actual value’s type. If there is sufficient need for comparing
CGAffineTransforms with tree frogs, this policy can be revised.
Describing values of arbitrary type.
+descriptionForValue:ofObjCType:are used to return an
NSStringinstance describing the passed-in value.
These are widely used in the assertion macros for logging of actual and expected values. RXAssertions includes descriptors for signed and unsigned integers of 8, 16, 32, and 64 bits of width;
doubles; objects and classes;
CGPoints via the same function); and arbitrary pointers (which includes pointers to any unsupported types—so if you see hex dumps of your compared values, you will want to add a descriptor).
Adding a descriptor is even simpler than adding a comparator; a
RXAssertionHelperDescriptionFunctiontakes a reference to the value to be described, casts it (presumably using
RXUnionCast) to the expected type, and builds an
Controlling the accuracy at which
+[RXAssertionHelper setFloatingPointComparisonAccuracy:]are used to get and set the floating point accuracy, by default 0, used by the comparators for
doublevalues, and thus by
There is no support for managing separate
doubleaccuracies; if this would simplify things for you, it would be a simple change to make.
- Exception interaction of any kind. Continue using the
STAssert*macros for exceptions until this is implemented.
- Comparators and descriptors for
CFTypeinstances, and many other things. Patches welcome.