This post originated from an RSS feed registered with Agile Buzz
by Keith Ray.
Original Post: English Style Assertions in ObjC
Feed Title: MemoRanda
Feed URL: http://homepage.mac.com/1/homepage404ErrorPage.html
Feed Description: Keith Ray's notes to be remembered on agile software development, project management, oo programming, and other topics.
Tom White and Robert Chatley have been using org.jmock.MockObjectTestCase for sentence style assertions in java unit tests. Examples:
assertThat(a, eq("3")); // instead of assertEquals("3", a);
assertThat(list, includes("peach", "pear", "plum"));
assertThat(a, eq(true), otherwise("a should be true"));
Objective-C and Smalltalk syntax allows a more english-like syntax with fewer contortions, except, of course, Smalltalk would require an explicit reference to some object to send the assertion messages to. Objective-C could get around of that with a macro or two. Objective-C syntax examples:
[ Assert that: anNSString equals: @"3" ]; // @"3" is an NSString literal.
[ Assert that: listOrSet includes: @"peach", @"pear", @"plum", nil ];
[ Assert that: aBool equalsBool: true otherwise: @"aBool should be true" ];
Since Objective-C doesn't have overloading, and the literal 'true' is not an object, the third example above has to use a method name to let the compiler know the parameters are supposed to boolean. (UNTESTED code follows):
#define Assert self assertLine: __LINE__ file: __FILE__
// thus [ Assert that: a equals: @"3" ];
// gets expanded to:
// [ self assertLine: 120 file: "somefile.mm" that: a equals: @"3" ]
// We use the __LINE__ and __FILE__ preprocessor symbols to identify the
// source-code file and line of code that failed the assertion, because we
// can't easily print nice stack-dumps like Java does.
// header file
#import <Foundation/Foundation.h>
@interface TestBase : NSObject // or whatever...
{
// etc.
}
-(void) assertLine: (int) lineNum file: (const char*) fileName
that: (id) firstArg equals: (id) secondArg;
-(void) assertLine: (int) lineNum file: (const char*) fileName
that: (id) listArg includes: (id) firstObj, ...;
-(void) assertLine: (int) lineNum file: (const char*) fileName
that: (bool) firstArg equalsBool: (bool) secondArg otherwise: (NSString*) aString;
-(void) assertionFailure: (NSString*) aString;
// etc.
@end
// implementation file
#import "TestBase.h"
#include
@implementation TestBase
-(void) assertLine: (int) lineNum file: (const char*) fileName
that: (id) firstArg equals: (id) secondArg
{
if ( [ firstArg respondsToSelector: @selector(isEqual:) ] )
{
if ( not [ firstArg equals: secondArg ] )
[ self assertionFailure: [ NSString stringWithFormat:
@"assertion failed: %@ does not equal %@ at line %d; file %s",
firstArg, secondArg, lineNum, fileName ] ];
}
else if ( [ firstArg respondsToSelector: @selector(isEqualTo:) ] )
{
if ( not [ firstArg isEqualTo: secondArg ] )
[ self assertionFailure: [ NSString stringWithFormat:
@"assertion failed %@ does not equal %@ at line %d; file %s",
firstArg, secondArg, lineNum, fileName ] ];
}
else if ( [ firstArg respondsToSelector: @selector(compare:) ] )
{
if ( NSOrderedSame != [ firstArg compare: secondArg ] )
[ self assertionFailure: [ NSString stringWithFormat:
@"assertion failed %@ does not equal %@ at line %d; file %s",
firstArg, secondArg, lineNum, fileName ] ];
}
else
// ... raise failure to find a known comparison method...
}
-(void) assertLine: (int) lineNum file: (const char*) fileName
that: (id) listArg includes: (id) firstObj, ...;
{
if ( [ firstArg respondsToSelector: @selector(containsObject:) )
{
va_list arglist;
bool doesInclude = false;
va_start(arglist, firstObj);
id arg = va_arg(arglist, id);
while ( arg != nil )
{
if ( [ listArg containsObject: arg ] )
{
doesInclude = true;
break;
}
}
va_end(arglist);
if ( not doesInclude )
[ self assertionFailure: [ NSString stringWithFormat:
@"assertion failed: %@ does not contain any of the expected"
" values at line %d; file %s", listArg, lineNum,
fileName ] ];
}
// maybe try some other selectors like 'containsItem:'
// ... etc.
}
-(void) assertLine: (int) lineNum file: (const char*) fileName
that: (bool) firstArg equalsBool: (bool) secondArg otherwise: (NSString*) aString
{
if ( firstArg != secondArg )
[ self assertionFailure: [ NSString stringWithFormat:
@"assertion failed: %@; boolean %d does not equal %d at"
" line %d; file %s", aString (int) firstArg, (int) secondArg,
lineNum, fileName ] ];
}
-(void) assertionFailure: (NSString*) aString
{
// etc...
}
@end
Writing your own test-framework, or extensions to a test-framework, is not hard. Everybody should do it at least once just for the practice.