1

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.

lukestringer90
  • 492
  • 7
  • 16
  • 1
    I assume you implement your matcher using a .h and a .m. Could you split your example, and show all import statements? – Jon Reid Jul 23 '13 at 04:29
  • I was declaring the `HCBaseMatcher` subclass inline in my `SenTestCase` subclass. Splitting them up into a regualr .h and .m and importing them solved the problem. Thanks for the pointer and thanks for the awesome framework! Your tutorials over on http://qualitycoding.org are also super helpful. I recomend them to anyone wanting to do unit testing in Obj-C. – lukestringer90 Jul 23 '13 at 13:44

0 Answers0