Pods/OCHamcrest/Source/Library/Collection/HCHasCount.m
//
//  OCHamcrest - HCHasCount.m
//  Copyright 2013 hamcrest.org. See LICENSE.txt
//
//  Created by: Jon Reid, http://qualitycoding.org/
//  Docs: http://hamcrest.github.com/OCHamcrest/
//  Source: https://github.com/hamcrest/OCHamcrest
//

#import "HCHasCount.h"

#import "HCDescription.h"
#import "HCIsEqualToNumber.h"


@implementation HCHasCount

+ (instancetype)hasCount:(id <HCMatcher>)matcher
{
    return [[self alloc] initWithCount:matcher];
}

- (instancetype)initWithCount:(id <HCMatcher>)matcher
{
    self = [super init];
    if (self)
        countMatcher = matcher;
    return self;
}

- (BOOL)matches:(id)item
{
    if (![item respondsToSelector:@selector(count)])
        return NO;
    
    NSNumber *count = @([item count]);
    return [countMatcher matches:count];
}

- (void)describeMismatchOf:(id)item to:(id<HCDescription>)mismatchDescription
{
    [mismatchDescription appendText:@"was "];
    if ([item respondsToSelector:@selector(count)])
    {
        [[[mismatchDescription appendText:@"count of "]
                               appendDescriptionOf:@([item count])]
                               appendText:@" with "];
    }
    [mismatchDescription appendDescriptionOf:item];
}

- (void)describeTo:(id<HCDescription>)description
{
    [[description appendText:@"a collection with count of "] appendDescriptionOf:countMatcher];
}

@end


id HC_hasCount(id <HCMatcher> matcher)
{
    return [HCHasCount hasCount:matcher];
}

id HC_hasCountOf(NSUInteger value)
{
    return HC_hasCount(HC_equalToUnsignedInteger(value));
}
