Merge pull request #2866 from mrcarlberg/cpset_enumerator_use_stop_variable

Fixed: CPSet enumerateObjectsUsingBlock: will honor the stop variable
This commit is contained in:
cacaodev authored and GitHub committed 2019-12-16 11:27:37 +01:00
commit 0c706cfed2
2 files changed
+39 -6

No files matched your search

+9 -6
View File
@@ -320,16 +320,19 @@
/*!
Enumberates over the objects in a set using a given function.
@param aFunction a callback for each itteration, should be of the format: function(anObject).
@param aFunction a callback for each itteration, should be of the format: function(anObject, stop).
*/
- (void)enumerateObjectsUsingBlock:(Function)aFunction
- (void)enumerateObjectsUsingBlock:(Function /*(id anObject, @ref BOOL stop)*/)aFunction
{
var object,
objectEnumerator = [self objectEnumerator];
objectEnumerator = [self objectEnumerator],
shouldStop = NO;
while ((object = [objectEnumerator nextObject]) !== nil)
if (aFunction(object))
break;
while (!shouldStop && (object = [objectEnumerator nextObject]) != nil) {
if (aFunction(object, @ref(shouldStop)) !== undefined) {
throw "DEPRECATED: The method enumerateObjectsUsingBlock: does not support returning a value in the block to stop the iteration. Please use the stop variable";
}
}
}
// FIXME: stop is broken.
+30
View File
@@ -368,4 +368,34 @@
[self assertTrue:[set2 member:dict2] === dict1];
}
- (void)testEnumateObjectsUsingBlock
{
var input0 = [],
input1 = [CPSet setWithArray:[1, 3, "b"]],
output = [CPMutableDictionary dictionary],
idx = 0,
outputFunction = function(anObject)
{
[output setValue:anObject forKey:"" + idx++];
};
[input0 enumerateObjectsUsingBlock:outputFunction];
[self assert:0 equals:[output count] message:@"output when enumerating empty set"];
[input1 enumerateObjectsUsingBlock:outputFunction];
[self assert:3 equals:[output count] message:@"output when enumerating input1"];
var stoppingFunction = function(anObject, stop)
{
[output setValue:anObject forKey:"" + idx++];
if ([output count] > 1)
@deref(stop) = YES;
}
output = [CPMutableDictionary dictionary];
idx = 0;
[input1 enumerateObjectsUsingBlock:stoppingFunction];
[self assert:2 equals:[output count] message:@"output when enumerating input1 and stopping after 2"];
}
@end