Custom OCHamcrest matcher with a primitive function argument

422 Views Asked by At

I'm trying to write my own HCMatcher that I can use to simply some assertions when working with a collection of objects.

Currently my test method is doing this:

__block int totalNumberOfCells = 0;
    [configurations enumerateObjectsUsingBlock:^(Layout *layout, NSUInteger idx, BOOL *stop) {
        totalNumberOfCells += [layout numberOfCells];
}];
assertThatInt(totalNumberOfCells, is(equalToInt(3)));

I'm going to be doing this sort of assertion in many places, so I want to simplify it down to something like this:

assertThat(configurations, hasTotalNumberOfFeedItemCells(3));

Here is my attempt at creating my own OCHamcrest matcher:

@interface HasTotalNumberOfFeedItemCells : HCBaseMatcher {
    NSInteger correctNumberOfCells;
}
- (id)initWithCorrectNumberOfCells:(NSInteger)num;

@end
OBJC_EXPORT id <HCMatcher> hasTotalNumberOfFeedItemCells(int num);

@implementation HasTotalNumberOfFeedItemCells


- (id)initWithCorrectNumberOfCells:(NSInteger)num {
    self = [super init];
    if (self) {
        correctNumberOfCells = num;
    }
    return self;
}

- (BOOL)matches:(id)item {
    __block int totalNumberOfCells = 0;
    [item enumerateObjectsUsingBlock:^(Layout *layout, NSUInteger idx, BOOL *stop) {
        totalNumberOfCells += [layout numberOfCells];
    }];
    return totalNumberOfCells == correctNumberOfCells;
}

- (void)describeTo:(id <HCDescription>)description {
    [description appendText:@"ZOMG"];
}

@end

id <HCMatcher> hasTotalNumberOfFeedItemCells(int num){
    return [[HasTotalNumberOfFeedItemCells alloc] initWithCorrectNumberOfCells:num];
}

When I try to use the hasTotalNumberOfFeedItemCells() function I get warnings and build error saying:

  • Implicit declaration of function 'hasTotalNumberOfFeedItemCells' is invalid in C99
  • Implicit conversion of 'int' to 'id' is disallowed with ARC
  • Conflicting types for 'hasTotalNumberOfFeedItemCells'

Any help wold be much appreciated.

0

There are 0 best solutions below