Permalink
Browse files

[NEW] Support for findIndex and findIndexWithBlock following Enumerab…

…le#find_index method. Tests to validate. Closes #2
  • Loading branch information...
1 parent f501771 commit 972f83d58004e91f9871a39208863d16265f8648 @crafterm committed Feb 26, 2012
@@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
AA747D9F0F9514B9006C5449 /* MRCEnumerable_Prefix.pch in Headers */ = {isa = PBXBuildFile; fileRef = AA747D9E0F9514B9006C5449 /* MRCEnumerable_Prefix.pch */; };
AACBBE4A0F95108600F1A2B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; };
+ BF14D46614FAE96B00501118 /* NSArray+FindIndex+Test.m in Sources */ = {isa = PBXBuildFile; fileRef = BF14D46414FAE96B00501118 /* NSArray+FindIndex+Test.m */; };
BF1F91DE1341BD8F00F37598 /* NSSet+Collect+Test.m in Sources */ = {isa = PBXBuildFile; fileRef = BF1F91D41341BD8F00F37598 /* NSSet+Collect+Test.m */; };
BF1F91DF1341BD8F00F37598 /* NSSet+CollectWithIndex+Test.m in Sources */ = {isa = PBXBuildFile; fileRef = BF1F91D51341BD8F00F37598 /* NSSet+CollectWithIndex+Test.m */; };
BF1F91E01341BD8F00F37598 /* NSSet+Detect+Test.m in Sources */ = {isa = PBXBuildFile; fileRef = BF1F91D61341BD8F00F37598 /* NSSet+Detect+Test.m */; };
@@ -65,6 +66,7 @@
/* Begin PBXFileReference section */
AA747D9E0F9514B9006C5449 /* MRCEnumerable_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MRCEnumerable_Prefix.pch; sourceTree = SOURCE_ROOT; };
AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
+ BF14D46414FAE96B00501118 /* NSArray+FindIndex+Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSArray+FindIndex+Test.m"; path = "Tests/NSArray+FindIndex+Test.m"; sourceTree = "<group>"; };
BF1F91D41341BD8F00F37598 /* NSSet+Collect+Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSSet+Collect+Test.m"; path = "Tests/NSSet+Collect+Test.m"; sourceTree = "<group>"; };
BF1F91D51341BD8F00F37598 /* NSSet+CollectWithIndex+Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSSet+CollectWithIndex+Test.m"; path = "Tests/NSSet+CollectWithIndex+Test.m"; sourceTree = "<group>"; };
BF1F91D61341BD8F00F37598 /* NSSet+Detect+Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSSet+Detect+Test.m"; path = "Tests/NSSet+Detect+Test.m"; sourceTree = "<group>"; };
@@ -215,6 +217,7 @@
BF8329C112929B24002BF27D /* NSArray+Inject+Test.m */,
BF8329C212929B24002BF27D /* NSArray+Reject+Test.m */,
BF8329C312929B24002BF27D /* NSArray+Select+Test.m */,
+ BF14D46414FAE96B00501118 /* NSArray+FindIndex+Test.m */,
);
name = NSArray;
sourceTree = "<group>";
@@ -396,6 +399,7 @@
BF1F91E51341BD8F00F37598 /* NSSet+Inject+Test.m in Sources */,
BF1F91E61341BD8F00F37598 /* NSSet+Reject+Test.m in Sources */,
BF1F91E71341BD8F00F37598 /* NSSet+Select+Test.m in Sources */,
+ BF14D46614FAE96B00501118 /* NSArray+FindIndex+Test.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -18,5 +18,7 @@
- (NSArray *)select:(BOOL (^)(id obj))block;
- (NSArray *)reject:(BOOL (^)(id obj))block;
- (id)detect:(BOOL (^)(id obj))block;
+- (NSInteger)findIndex:(id)obj;
+- (NSInteger)findIndexWithBlock:(BOOL (^)(id obj))block;
@end
View
@@ -64,4 +64,21 @@ - (id)detect:(BOOL (^)(id obj))block {
return nil;
}
+- (NSInteger)findIndex:(id)obj {
+ return [self findIndexWithBlock:^BOOL(id candidate) {
+ return candidate == obj;
+ }];
+}
+
+- (NSInteger)findIndexWithBlock:(BOOL (^)(id obj))block {
+ for (NSUInteger i = 0; i < [self count]; i++) {
+ id candidate = [self objectAtIndex:i];
+ if (block(candidate)) {
+ return i;
+ }
+ }
+
+ return -1;
+}
+
@end
@@ -0,0 +1,30 @@
+//
+// NSArray+FindIndex+Test.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 27/02/12.
+// Copyright (c) 2012 Red Artisan. All rights reserved.
+//
+
+@interface NSArray_FindIndex_Test : GHTestCase
+@end
+
+@implementation NSArray_FindIndex_Test
+
+- (void)testFindIndexShouldReturnTheIndexOfTheGivenElementWhenPresent {
+ NSArray * array = [NSArray arrayWithObjects:@"dog", @"mouse", nil];
+ GHAssertEquals(1, [array findIndex:@"mouse"], @"should be equal");
+}
+
+- (void)testFindIndexShouldReturnFailWhenNotPresent {
+ NSArray * array = [NSArray arrayWithObjects:@"dog", @"mouse", nil];
+ GHAssertEquals(-1, [array findIndex:@"cat"], @"should not be equal");
+}
+
+- (void)testFindIndexShouldReturnTheIndexOfTheGivenElementUsingBlockSyntax {
+ NSArray * array = [NSArray arrayWithObjects:@"dog", @"mouse", nil];
+ GHAssertEquals(1, [array findIndexWithBlock:^BOOL(id obj) { return obj == @"mouse"; }], @"should be equal");
+ GHAssertEquals(-1, [array findIndexWithBlock:^BOOL(id obj) { return obj == @"cat"; }], @"should not be equal");
+}
+
+@end

0 comments on commit 972f83d

Please sign in to comment.