Use Fast Enumeration
authorWincent Colaiuta <win@wincent.com>
Mon, 30 Jul 2007 17:52:38 +0000 (19:52 +0200)
committerWincent Colaiuta <win@wincent.com>
Mon, 30 Jul 2007 17:52:38 +0000 (19:52 +0200)
Switch to Objective-C 2.0 Fast Enumeration.

Signed-off-by: Wincent Colaiuta <win@wincent.com>
WOEnumerate.h [deleted file]
WOMock.m
WOObjectMock.m
WOProtocolMock.m
WOTest.h
WOTestBundleInjector.m
WOTestClass.m
WOTestRunner/WOTestRunner.m

diff --git a/WOEnumerate.h b/WOEnumerate.h
deleted file mode 100644 (file)
index 1769d3b..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-//
-//  WOEnumerate.h
-//  WOTest (imported from WODebug 28 January 2006)
-//
-//  Created by Wincent Colaiuta on 12 October 2004.
-//
-//  Copyright 2004-2006 Wincent Colaiuta.
-//  This program 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
-//  in the accompanying file, "LICENSE.txt", for more details.
-//
-
-//! \file WOEnumerate.h
-
-// don't attempt to redefine macro (for example, may already be defined by WOCommon)
-#ifndef WO_ENUMERATE
-
-/*! WO_ENUMERATE is a convenience macro for expressing the Objective-C enumerator idiom in more compact form. Instead of the standard, longer form:
-
-<pre>NSEnumerator *enumerator = [collection objectEnumerator];
-id object = nil;
-while ((object = [enumerator nextObject]))
-NSLog(@"Object: %@", object);</pre>
-
-The following, shorter form can be used:
-
-<pre>WO_ENUMERATE(collection, object)
-NSLog(@"Object: %@", object);</pre>
-
-Or for those that prefer a Perl-like syntax:
-
-<pre>foreach (object, collection)
-NSLog(@"Object: %@", object);</pre>
-
-The WO_ENUMERATE macro is also considerably faster than the standard form because is uses a cached IMP (implementation pointer) and selector to speed up repeated invocations of the <tt>nextObject</tt> selector. In informal testing (enumerating over a 10,000,000-item array ten times) the macro performed 49% faster than the standard idiom (averaging 3.6 million objects per second compared with 2.4 million per second).
-
-If passed a nil pointer instead of a valid collection, no iterations are performed. If passed an object which does not respond to the objectEnumerator selector then an exception is raised. Both of these behaviours match the pattern of the standard idiom.
-
-Note that the compiler C dialect must be set to C99 or GNU99 in order to use this macro because of the initialization of variables inside the for expression.
-
-\see  http://mjtsai.com/blog/2003/12/08/cocoa_enumeration and http://rentzsch.com/papers/improvingCocoaObjCEnumeration
-
-*/
-#define WO_ENUMERATE(collection, object)                                                                                        \
-for (id WOMacroEnumerator_ ## object    = [collection objectEnumerator],                                                        \
-     WOMacroSelector_ ## object         = (id)@selector(nextObject),                                                            \
-     WOMacroMethod_ ## object           = (id)[WOMacroEnumerator_ ## object methodForSelector:(SEL)WOMacroSelector_ ## object], \
-     object                             = WOMacroEnumerator_ ## object ?                                                        \
-     ((IMP)WOMacroMethod_ ## object)(WOMacroEnumerator_ ## object, (SEL)WOMacroSelector_ ## object) : nil;                   \
-     object != nil;                                                                                                             \
-     object = ((IMP)WOMacroMethod_ ## object)(WOMacroEnumerator_ ## object, (SEL)WOMacroSelector_ ## object))
-
-/*! Perl-like syntax for WO_ENUMERATE. */
-#define foreach(object, collection) WO_ENUMERATE(collection, object)
-
-#endif /* WO_ENUMERATE */
-
-#ifndef WO_REVERSE_ENUMERATE
-
-#define WO_REVERSE_ENUMERATE(collection, object)                                                                                \
-for (id WOMacroEnumerator_ ## object    = [collection reverseObjectEnumerator],                                                 \
-     WOMacroSelector_ ## object         = (id)@selector(nextObject),                                                            \
-     WOMacroMethod_ ## object           = (id)[WOMacroEnumerator_ ## object methodForSelector:(SEL)WOMacroSelector_ ## object], \
-     object = WOMacroEnumerator_ ## object ?                                                                                    \
-     ((IMP)WOMacroMethod_ ## object)(WOMacroEnumerator_ ## object,(SEL)WOMacroSelector_ ## object) : nil;                    \
-     object != nil;                                                                                                             \
-     object = ((IMP)WOMacroMethod_ ## object)(WOMacroEnumerator_ ## object, (SEL)WOMacroSelector_ ## object))
-
-#endif /* WO_REVERSE_ENUMERATE */
-
-#ifndef WO_KEY_ENUMERATE
-
-#define WO_KEY_ENUMERATE(collection, key)                                                                               \
-for (id WOMacroEnumerator_ ## key   = [collection keyEnumerator],                                                       \
-     WOMacroSelector_ ## key        = (id)@selector(nextObject),                                                        \
-     WOMacroMethod_ ## key          = (id)[WOMacroEnumerator_ ## key methodForSelector:(SEL)WOMacroSelector_ ## key],   \
-     key = WOMacroEnumerator_ ## key ?                                                                                  \
-     ((IMP)WOMacroMethod_ ## key)(WOMacroEnumerator_ ## key, (SEL)WOMacroSelector_ ## key) : nil;                    \
-     key != nil;                                                                                                        \
-     key = ((IMP)WOMacroMethod_ ## key)(WOMacroEnumerator_ ## key, (SEL)WOMacroSelector_ ## key))
-
-/*! Perl-like syntax for WO_KEY_ENUMERATE. */
-#define foreachkey(key, collection) WO_KEY_ENUMERATE(collection, key)
-
-#endif /* WO_KEY_ENUMERATE */
index 9763b1f26b566d3fdf545daf32a1df42ba74c88c..20704c003732ee7251e2c99a1223a1727f942d7d 100644 (file)
--- a/WOMock.m
+++ b/WOMock.m
@@ -32,7 +32,6 @@
 #import "NSProxy+WOTest.h"
 #import "NSValue+WOTest.h"
 #import "WOClassMock.h"
-#import "WOEnumerate.h"
 #import "WOObjectMock.h"
 #import "WOProtocolMock.h"
 
index d1891faa8793ddcd7e21a7e698bf9425f866a90f..5dc4c27859c4ad6b3ed6bb8455550dac7e13ff76 100644 (file)
@@ -25,7 +25,6 @@
 // framework headers
 #import "NSInvocation+WOTest.h"
 #import "NSObject+WOTest.h"
-#import "WOEnumerate.h"
 #import "WOObjectStub.h"
 
 @implementation WOObjectMock
     NSParameterAssert(anInvocation != nil);
 
     // check if in reject list
-    WO_ENUMERATE(rejected, stub)
+    for (WOStub *stub in rejected)
         if ([stub matchesInvocation:anInvocation])
         {
             [NSException raise:NSInternalInconsistencyException format:@"Rejected selector %@ for class %@",
         }
 
     // check if expectedInOrder
-    WO_ENUMERATE(expectedInOrder, stub)
+    for (WOStub *stub in expectedInOrder)
         if ([stub matchesInvocation:anInvocation])
         {
             NSAssert2(([expectedInOrder objectAtIndex:0] == stub), @"Invocation selector %@ class %@ received out of order",
         }
 
     // check if expected once
-    WO_ENUMERATE(expectedOnce, stub)
+    for (WOStub *stub in expectedOnce)
         if ([stub matchesInvocation:anInvocation])
         {
             // move object from "expectedOnce" to "rejected"
         }
 
     // check if expected
-    WO_ENUMERATE(expected, stub)
+    for (WOStub *stub in expected)
         if ([stub matchesInvocation:anInvocation])
         {
             // move from "expected" to "accepted"
         }
 
     // check if accepted once
-    WO_ENUMERATE(acceptedOnce, stub)
+    for (WOStub *stub in acceptedOnce)
         if ([stub matchesInvocation:anInvocation])
         {
             // move from "acceptedOnce" to "rejected"
         }
 
     // check if accepted
-    WO_ENUMERATE(accepted, stub)
+    for (WOStub *stub in accepted)
         if ([stub matchesInvocation:anInvocation])
         {
             [self storeReturnValue:[stub returnValue] forInvocation:anInvocation];
index 2a9502dff7546f794d7976697dc33b13170cffde..55a5ef275517a15eb1bb2426fd5ed44965ccaa17 100644 (file)
@@ -29,7 +29,6 @@
 
 #import "NSInvocation+WOTest.h"
 #import "NSMethodSignature+WOTest.h"
-#import "WOEnumerate.h"
 #import "WOProtocolStub.h"
 
 #pragma mark -
@@ -116,7 +115,7 @@ NSString *WOStringFromProtocol(Protocol *aProtocol)
     NSParameterAssert(anInvocation != nil);
 
     // check if in reject list
-    WO_ENUMERATE(rejected, stub)
+    for (WOStub *stub in rejected)
         if ([anInvocation WOTest_isEqualToInvocation:[stub recordedInvocation]])
         {
             [NSException raise:NSInternalInconsistencyException format:@"Rejected selector %@ for protocol %@",
@@ -125,7 +124,7 @@ NSString *WOStringFromProtocol(Protocol *aProtocol)
         }
 
     // check if expectedInOrder
-    WO_ENUMERATE(expectedInOrder, stub)
+    for (WOStub *stub in expectedInOrder)
         if ([anInvocation WOTest_isEqualToInvocation:[stub recordedInvocation]])
         {
             NSAssert(([expectedInOrder objectAtIndex:0] != stub), @"invocation received out of order");
@@ -141,7 +140,7 @@ NSString *WOStringFromProtocol(Protocol *aProtocol)
         }
 
     // check if expected once
-    WO_ENUMERATE(expectedOnce, stub)
+    for (WOStub *stub in expectedOnce)
         if ([anInvocation WOTest_isEqualToInvocation:[stub recordedInvocation]])
         {
             // move object from "expectedOnce" to "rejected"
@@ -153,7 +152,7 @@ NSString *WOStringFromProtocol(Protocol *aProtocol)
         }
 
     // check if expected
-    WO_ENUMERATE(expected, stub)
+    for (WOStub *stub in expected)
         if ([anInvocation WOTest_isEqualToInvocation:[stub recordedInvocation]])
         {
             // move from "expected" to "accepted"
@@ -165,7 +164,7 @@ NSString *WOStringFromProtocol(Protocol *aProtocol)
         }
 
     // check if accepted once
-    WO_ENUMERATE(acceptedOnce, stub)
+    for (WOStub *stub in acceptedOnce)
         if ([anInvocation WOTest_isEqualToInvocation:[stub recordedInvocation]])
         {
             // move from "acceptedOnce" to "rejected"
@@ -177,7 +176,7 @@ NSString *WOStringFromProtocol(Protocol *aProtocol)
         }
 
     // check if accepted
-    WO_ENUMERATE(accepted, stub)
+    for (WOStub *stub in accepted)
         if ([anInvocation WOTest_isEqualToInvocation:[stub recordedInvocation]])
         {
             [self storeReturnValue:[stub returnValue] forInvocation:anInvocation];
index 8676a7abeba43e7621636dc38dad7732d1577249..8ff81f2ffba090dc5701777cb922227215f1dc2d 100644 (file)
--- a/WOTest.h
+++ b/WOTest.h
@@ -33,7 +33,6 @@
 #pragma mark -
 #pragma mark Macros
 
-#import "WOEnumerate.h"
 #import "WOTestMacros.h"
 
 #pragma mark -
index be1a35ca1c99f0639dff94d9996f4ec3fd1a04bd..9f45db5bae5603df3a33cd1c9cb75336010805b3 100644 (file)
@@ -24,9 +24,6 @@
 // system headers
 #import <libkern/OSAtomic.h>        /* OSAtomicIncrement32Barrier() */
 
-// other headers
-#import "WOEnumerate.h"
-
 @implementation WOTestBundleInjector
 
 + (void)load
@@ -41,7 +38,7 @@
     if (inject)
     {
         NSArray *bundles = [[NSString stringWithUTF8String:inject] componentsSeparatedByString:@":"];
-        WO_ENUMERATE(bundles, bundlePath)
+        for (NSString *bundlePath in bundles)
         {
             NSString *path = [bundlePath stringByStandardizingPath];
             if (![path isAbsolutePath])
index 8fa00543e46dbed5a77a3fa161d2627b1b05fb6f..f503396c33d42d418c815bde4ac46419e0a2c935 100644 (file)
@@ -335,7 +335,7 @@ OSStatus WOLowLevelExceptionHandler(ExceptionInformation *theException)
 {
     int failures = 0;
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    WO_ENUMERATE([self testableClasses], class)
+    for (NSString *class in [self testableClasses])
         [self runTestsForClassName:class] ? : failures++;
     [self printTestResultsSummary];
     [pool release];
@@ -360,8 +360,7 @@ OSStatus WOLowLevelExceptionHandler(ExceptionInformation *theException)
         _WOLog(@"Running tests for class %@", NSStringFromClass(aClass));
         if ([NSObject WOTest_instancesOfClass:aClass conformToProtocol:@protocol(WOTest)])
         {
-            NSArray         *methods    = [self testableMethodsFrom:aClass];
-            WO_ENUMERATE(methods, method)
+            for (NSString *method in [self testableMethodsFrom:aClass])
             {
                 NSAutoreleasePool   *pool           = [[NSAutoreleasePool alloc] init];
                 NSDate              *startMethod    = [NSDate date];
@@ -596,13 +595,12 @@ jump_point:
 
 - (NSArray *)testableClassesFrom:(NSBundle *)aBundle
 {
-    NSArray         *allClasses = [self testableClasses];
     NSMutableArray  *classNames = [NSMutableArray array];
 
     if (aBundle)    // only search if actually passed a non-nil bundle
     {
         // add only classes that match the passed bundle and conform to WOTest
-        WO_ENUMERATE(allClasses, className)
+        for (NSString *className in [self testableClasses])
         {
             Class       aClass          = NSClassFromString(className);
             NSBundle    *classBundle    = [NSBundle bundleForClass:aClass];
index 84b0ef4f82ef2aa75417af1caee83c45175e5fe5..16ad17f5c85af8bb54c16f385fc4401887d33e46 100644 (file)
@@ -124,7 +124,7 @@ int main(int argc, const char *argv[])
 
     if ([testBundles count] > 0) // test only these bundles
     {
-        WO_ENUMERATE(testBundles, bundlePath)
+        for (NSString *bundlePath in testBundles)
         {
             bundlePath = [bundlePath WOTest_stringByConvertingToAbsolutePath];
             NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
@@ -132,13 +132,13 @@ int main(int argc, const char *argv[])
             {
                 if ([testClasses count] > 0) // test only these classes
                 {
-                    WO_ENUMERATE(testClasses, class)
+                    for (NSString *class in testClasses)
                         [WO_TEST_SHARED_INSTANCE runTestsForClassName:class];
                 }
                 else // test all classes
                 {
                     NSArray *classes = [WO_TEST_SHARED_INSTANCE testableClassesFrom:bundle];
-                    WO_ENUMERATE(classes, class)
+                    for (NSString *class in classes)
                     {
                         if ([excludeClasses containsObject:class]) continue;
                         [WO_TEST_SHARED_INSTANCE runTestsForClassName:class];
@@ -153,13 +153,12 @@ int main(int argc, const char *argv[])
     {
         if ([testClasses count] > 0) // test only these classes
         {
-            WO_ENUMERATE(testClasses, class)
+            for (NSString *class in testClasses)
                 [WO_TEST_SHARED_INSTANCE runTestsForClassName:class];
         }
         else // test all classes
         {
-            NSArray *classes = [WO_TEST_SHARED_INSTANCE testableClasses];
-            WO_ENUMERATE(classes, class)
+            for (NSString *class in [WO_TEST_SHARED_INSTANCE testableClasses])
             {
                 if ([excludeClasses containsObject:class]) continue;
                 [WO_TEST_SHARED_INSTANCE runTestsForClassName:class];