Monotone-Parent: 9b3ca8d1aaa3b15df09c58887d1e37a7aa3e21cc

Monotone-Revision: 198521b8ef49be92b78cac25495a2439d4763792

Monotone-Author: wsourdeau@inverse.ca
Monotone-Date: 2010-01-08T15:46:00
Monotone-Branch: ca.inverse.sogo
This commit is contained in:
Wolfgang Sourdeau
2010-01-08 15:46:00 +00:00
parent 38c31ba2aa
commit 29ddd6719e
7 changed files with 255 additions and 131 deletions

View File

@@ -1,5 +1,9 @@
2010-01-08 Wolfgang Sourdeau <wsourdeau@inverse.ca>
* UnitTests/SOGoTestRunner.m: separated class "SOGoTestRunner"
from sogo-tests.m. Moved reporting and logging methods from
SOGoTest into SOGoTestRunner.m
* SoObjects/SOGo/NSString+Utilities.m (-_setupCSSEscaping): we
must initialize cssEscapingCount before using it. Also, fixed a
typo preventing the character buffer from being initialized

View File

@@ -12,6 +12,7 @@ $(TEST_TOOL)_OBJC_FILES += \
sogo-tests.m \
\
SOGoTest.m \
SOGoTestRunner.m \
\
TestBSJSONAdditions.m

View File

@@ -29,23 +29,18 @@
@class NSArray;
@class NSMutableArray;
typedef enum {
SOGoTestFailureSuccess = 0,
SOGoTestFailureFailure = 1,
SOGoTestFailureError = 2,
} SOGoTestFailureCode;
@class SOGoTestRunner;
@interface SOGoTest : NSObject
{
NSMutableArray *messages;
int testCount;
int failuresCount;
int errorsCount;
SOGoTestRunner *testRunner;
BOOL hasFailed;
}
+ (NSArray *) allTestClasses;
- (void) setTestRunner: (SOGoTestRunner *) newTestRunner;
- (void) setUp;
- (void) tearDown;

View File

@@ -20,7 +20,12 @@
* Boston, MA 02111-1307, USA.
*/
#import <Foundation/Foundation.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import "SOGoTestRunner.h"
#import "SOGoTest.h"
@@ -52,11 +57,7 @@ static NSString *SOGoTestAssertException = @"SOGoTestAssertException";
{
if ((self = [super init]))
{
messages = [NSMutableArray new];
testCount = 0;
failuresCount = 0;
errorsCount = 0;
hasFailed = NO;
testRunner = nil;
}
return self;
@@ -64,10 +65,15 @@ static NSString *SOGoTestAssertException = @"SOGoTestAssertException";
- (void) dealloc
{
[messages release];
[testRunner release];
[super dealloc];
}
- (void) setTestRunner: (SOGoTestRunner *) newTestRunner
{
ASSIGN (testRunner, newTestRunner);
}
- (void) setUp
{
}
@@ -97,44 +103,8 @@ static NSString *SOGoTestAssertException = @"SOGoTestAssertException";
}
}
- (void) reportException: (NSException *) exception
method: (NSString *) methodName
withCode: (SOGoTestFailureCode) failureCode
{
static NSString *failurePrefixs[] = { @"", @"FAIL", @"ERROR" };
NSMutableString *message;
NSString *fileInfo;
hasFailed = YES;
message = [NSMutableString stringWithFormat: @"%@: %@",
failurePrefixs[failureCode], methodName];
fileInfo = [[exception userInfo] objectForKey: @"fileInfo"];
if (fileInfo)
[message appendFormat: @" (%@)\n", fileInfo];
else
[message appendString: @"\n"];
[message appendString: @"----------------------------------------------------------------------\n"];
if (failureCode == SOGoTestFailureFailure)
{
[message appendString: [exception reason]];
failuresCount++;
}
else if (failureCode == SOGoTestFailureError)
{
[message appendFormat: @"an exception occured: %@\n"
@" reason: %@",
[exception name], [exception reason]];
errorsCount++;
}
[message appendString: @"\n"];
[messages addObject: message];
}
- (void) performTest: (SEL) testMethod
{
static char failureChars[] = { '.', 'F', 'E' };
SOGoTestFailureCode failureCode;
failureCode = SOGoTestFailureSuccess;
@@ -145,9 +115,9 @@ static NSString *SOGoTestAssertException = @"SOGoTestAssertException";
NS_HANDLER
{
failureCode = SOGoTestFailureError;
[self reportException: localException
method: @"setUp"
withCode: failureCode];
[testRunner reportException: localException
method: @"setUp"
withCode: failureCode];
}
NS_ENDHANDLER;
@@ -164,9 +134,9 @@ static NSString *SOGoTestAssertException = @"SOGoTestAssertException";
failureCode = SOGoTestFailureFailure;
else
failureCode = SOGoTestFailureError;
[self reportException: localException
method: NSStringFromSelector (testMethod)
withCode: failureCode];
[testRunner reportException: localException
method: NSStringFromSelector (testMethod)
withCode: failureCode];
}
NS_ENDHANDLER;
}
@@ -178,35 +148,13 @@ static NSString *SOGoTestAssertException = @"SOGoTestAssertException";
NS_HANDLER
{
failureCode = SOGoTestFailureError;
[self reportException: localException
method: @"tearDown"
withCode: failureCode];
[testRunner reportException: localException
method: @"tearDown"
withCode: failureCode];
}
NS_ENDHANDLER;
testCount++;
fprintf (stderr, "%c", failureChars[failureCode]);
}
- (void) displayReport
{
static NSString *separator = @"\n======================================================================\n";
NSString *reportMessage;
if ([messages count])
{
fprintf (stderr, "%s", [separator UTF8String]);
reportMessage = [messages componentsJoinedByString: separator];
fprintf (stderr, "%s", [reportMessage UTF8String]);
}
fprintf (stderr,
"\n----------------------------------------------------------------------\n"
"Ran %d tests\n\n", testCount);
if (hasFailed)
fprintf (stderr, "FAILED (%d failures, %d errors)\n",
failuresCount, errorsCount);
else
fprintf (stderr, "OK\n");
[testRunner incrementTestCounter: failureCode];
}
- (BOOL) run
@@ -229,8 +177,6 @@ static NSString *SOGoTestAssertException = @"SOGoTestAssertException";
}
}
[self displayReport];
return YES;
}

View File

@@ -0,0 +1,60 @@
/* SOGoTestRunner.h - this file is part of SOGo
*
* Copyright (C) 2010 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef SOGOTESTRUNNER_H
#define SOGOTESTRUNNER_H
#import <Foundation/NSObject.h>
@class NSException;
@class NSMutableArray;
@class NSString;
typedef enum {
SOGoTestFailureSuccess = 0,
SOGoTestFailureFailure = 1,
SOGoTestFailureError = 2,
} SOGoTestFailureCode;
@interface SOGoTestRunner : NSObject
{
NSMutableArray *messages;
int testCount;
int failuresCount;
int errorsCount;
BOOL hasFailed;
}
+ (SOGoTestRunner *) testRunner;
- (int) run;
- (void) incrementTestCounter: (SOGoTestFailureCode) failureCode;
- (void) reportException: (NSException *) exception
method: (NSString *) methodName
withCode: (SOGoTestFailureCode) failureCode;
- (void) displayReport;
@end
#endif /* SOGOTESTRUNNER_H */

162
UnitTests/SOGoTestRunner.m Normal file
View File

@@ -0,0 +1,162 @@
/* SOGoTestRunner.m - this file is part of SOGo
*
* Copyright (C) 2010 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import "SOGoTest.h"
#import "SOGoTestRunner.h"
@implementation SOGoTestRunner
+ (SOGoTestRunner *) testRunner
{
SOGoTestRunner *testRunner;
testRunner = [self new];
[testRunner autorelease];
return testRunner;
}
- (id) init
{
if ((self = [super init]))
{
testCount = 0;
failuresCount = 0;
errorsCount = 0;
hasFailed = NO;
messages = nil;
}
return self;
}
- (void) dealloc
{
[messages release];
[super dealloc];
}
- (int) run
{
NSEnumerator *allTestClasses;
NSString *class;
SOGoTest *test;
NSAutoreleasePool *pool;
int rc;
rc = 0;
pool = [NSAutoreleasePool currentPool];
[self retain];
allTestClasses = [[SOGoTest allTestClasses] objectEnumerator];
[allTestClasses retain];
while ((class = [allTestClasses nextObject]))
{
test = [NSClassFromString (class) new];
[test setTestRunner: self];
if (![test run])
rc |= -1;
[test autorelease];
[pool emptyPool];
}
[self displayReport];
[allTestClasses autorelease];
[self autorelease];
return rc;
}
- (void) incrementTestCounter: (SOGoTestFailureCode) failureCode
{
static char failureChars[] = { '.', 'F', 'E' };
testCount++;
fprintf (stderr, "%c", failureChars[failureCode]);
}
- (void) reportException: (NSException *) exception
method: (NSString *) methodName
withCode: (SOGoTestFailureCode) failureCode
{
static NSString *failurePrefixs[] = { @"", @"FAIL", @"ERROR" };
NSMutableString *message;
NSString *fileInfo;
hasFailed = YES;
message = [NSMutableString stringWithFormat: @"%@: %@",
failurePrefixs[failureCode], methodName];
fileInfo = [[exception userInfo] objectForKey: @"fileInfo"];
if (fileInfo)
[message appendFormat: @" (%@)\n", fileInfo];
else
[message appendString: @"\n"];
[message appendString: @"----------------------------------------------------------------------\n"];
if (failureCode == SOGoTestFailureFailure)
{
[message appendString: [exception reason]];
failuresCount++;
}
else if (failureCode == SOGoTestFailureError)
{
[message appendFormat: @"an exception occured: %@\n"
@" reason: %@",
[exception name], [exception reason]];
errorsCount++;
}
[message appendString: @"\n"];
[messages addObject: message];
}
- (void) displayReport
{
static NSString *separator = @"\n======================================================================\n";
NSString *reportMessage;
if ([messages count])
{
fprintf (stderr, "%s", [separator UTF8String]);
reportMessage = [messages componentsJoinedByString: separator];
fprintf (stderr, "%s", [reportMessage UTF8String]);
}
fprintf (stderr,
"\n----------------------------------------------------------------------\n"
"Ran %d tests\n\n", testCount);
if (hasFailed)
fprintf (stderr, "FAILED (%d failures, %d errors)\n",
failuresCount, errorsCount);
else
fprintf (stderr, "OK\n");
}
@end

View File

@@ -22,51 +22,7 @@
#import <Foundation/Foundation.h>
#import "SOGoTest.h"
@interface SOGoTestRunner : NSObject
@end
@implementation SOGoTestRunner
+ (SOGoTestRunner *) testRunner
{
SOGoTestRunner *testRunner;
testRunner = [self new];
[testRunner autorelease];
return testRunner;
}
- (int) run
{
NSEnumerator *allTestClasses;
NSString *class;
SOGoTest *test;
NSAutoreleasePool *pool;
int rc;
rc = 0;
pool = [NSAutoreleasePool currentPool];
[self retain];
allTestClasses = [[SOGoTest allTestClasses] objectEnumerator];
[allTestClasses retain];
while ((class = [allTestClasses nextObject]))
{
test = [NSClassFromString (class) new];
if (![test run])
rc |= -1;
[pool emptyPool];
}
[allTestClasses autorelease];
[self autorelease];
return rc;
}
@end
#import "SOGoTestRunner.h"
int main()
{