Permalink
Browse files

support for testing all implementations of a protocol

  • Loading branch information...
1 parent 9150dcc commit cf3b517c2dc56e615407ba15c6a8a27e3ef3bdc4 @toaster committed Feb 23, 2012
Showing with 47 additions and 9 deletions.
  1. +12 −0 src/ProtocolTester.m
  2. +35 −9 src/TTestCase.m
View
@@ -0,0 +1,12 @@
+//
+// (C) Copyright Tilo Prütz
+//
+
+
+#pragma .h @protocol ProtocolTester
+
+
+#pragma .h + (Protocol *)protocolUnderTest;
+
+
+#pragma .h @end
View
@@ -13,6 +13,7 @@
#include <objc/objc.h>
#include <objc/objc-api.h>
+#include "TUnit/ProtocolTester.h"
#include "TUnit/TTestException.h"
#include "TUnit/TMockController.h"
#include "TUnit/TMockMessage.h"
@@ -462,25 +463,30 @@ - (void)noTest
}
-- (void)printRunning
+- (void)printRunning: (Class)classUnderTest
{
[TUserIO print: @"objc."];
if ([__package containsData]) {
[TUserIO print: __package];
[TUserIO print: @"."];
}
[TUserIO print: [self className]];
+ if (classUnderTest) {
+ [TUserIO print: @"("];
+ [TUserIO print: [classUnderTest className]];
+ [TUserIO print: @")"];
+ }
[TUserIO print: @" "];
}
-- (int)run: (TString *)methodFilter
+- (int)run: (TString *)methodFilter for: (Class)classUnderTest
{
int failures = 0;
TAutoreleasePool *pool = [[TAutoreleasePool alloc] init];
struct objc_method_list *list = [self class]->methods;
- [self printRunning];
+ [self printRunning: classUnderTest];
@try {
[self clearHint];
[self beforeAll];
@@ -603,12 +609,32 @@ int objcmain(int argc, char *argv[])
}
for (id <TIterator> i = [[[testClasses allKeys] sortedArrayUsingSelector:
@selector(caseInsensitiveCompare:)] iterator]; [i hasCurrent]; [i next]) {
- TTestCase *test = nil;
- @try {
- test = [[[testClasses objectForKey: [i current]] alloc] init];
- result += [test run: methodFilter];
- } @finally {
- [test release];
+ Class testClass = [testClasses objectForKey: [i current]];
+ if ([testClass conformsTo: @protocol(ProtocolTester)]) {
+ classIterator = NULL;
+ while ((class = objc_next_class(&classIterator)) != Nil) {
+ // GCC's “conformsTo:” is broken
+ if (class_get_class_method(class->class_pointer, @selector(isKindOf:)) &&
+ [class isKindOf: [TObject class]] &&
+ [class conformsTo: [testClass protocolUnderTest]]) {
+ TTestCase *test = nil;
+ @try {
+ test = [[testClass alloc] perform: [TUtils selectorFromString:
+ [TString stringWithFormat: @"initFor%@", [class className]]]];
+ result += [test run: methodFilter for: class];
+ } @finally {
+ [test release];
+ }
+ }
+ }
+ } else {
+ TTestCase *test = nil;
+ @try {
+ test = [[testClass alloc] init];
+ result += [test run: methodFilter for: Nil];
+ } @finally {
+ [test release];
+ }
}
}
return result;

0 comments on commit cf3b517

Please sign in to comment.