-
Notifications
You must be signed in to change notification settings - Fork 100
/
HCCollect.m
43 lines (35 loc) · 1.1 KB
/
HCCollect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// OCHamcrest by Jon Reid, http://qualitycoding.org/about/
// Copyright 2017 hamcrest.org. See LICENSE.txt
#import "HCCollect.h"
#import "HCWrapInMatcher.h"
/*!
* @abstract Returns an array of wrapped items from a variable-length comma-separated list
* terminated by <code>nil</code>.
* @discussion Each item is transformed by passing it to the specified <em>wrap</em> function.
*/
static NSArray * HCCollectWrappedItems(id item, va_list args, id (*wrap)(id))
{
NSMutableArray *list = [NSMutableArray arrayWithObject:wrap(item)];
id nextItem = va_arg(args, id);
while (nextItem)
{
[list addObject:wrap(nextItem)];
nextItem = va_arg(args, id);
}
return list;
}
static id passThrough(id value)
{
return value;
}
NSArray * HCCollectItems(id item, va_list args)
{
return HCCollectWrappedItems(item, args, passThrough);
}
NSArray<id <HCMatcher>> * HCWrapIntoMatchers(NSArray *items)
{
NSMutableArray<id <HCMatcher>> *matchers = [[NSMutableArray alloc] init];
for (id item in items)
[matchers addObject:HCWrapInMatcher(item)];
return matchers;
}