The Artima Developer Community
Sponsored Link

Agile Buzz Forum
English Style Assertions in ObjC

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Keith Ray

Posts: 658
Nickname: keithray
Registered: May, 2003

Keith Ray is multi-platform software developer and Team Leader
English Style Assertions in ObjC Posted: Jun 18, 2006 7:19 AM
Reply to this message Reply

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.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Keith Ray
Latest Posts From MemoRanda

Advertisement

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.

Read: English Style Assertions in ObjC

Topic: Root Failure of Genesis Parachute Previous Topic   Next Topic Topic: Talking to RedMonk

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use