Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 115 lines (87 sloc) 3.617 kb
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//
// KWUserDefinedMatcherTest.m
// Kiwi
//
// Created by Luke Redpath on 16/06/2011.
// Copyright 2011 Allen Ding. All rights reserved.
//

#import "Kiwi.h"
#import "KiwiTestConfiguration.h"
#import "TestClasses.h"

#if KW_TESTS_ENABLED

@interface KWUserDefinedMatcherTest : SenTestCase
@end

@implementation KWUserDefinedMatcherTest

- (void)testShouldPassTheSubjectInAsTheFirstBlockArgument
{
    __block id blockSubject = nil;

    KWUserDefinedMatcher *matcher = [KWUserDefinedMatcher matcherWithSubject:@"subject" block:^(id subject) {
        blockSubject = subject;
        return YES;
    }];
    [matcher evaluate];

    STAssertEquals(@"subject", blockSubject, @"expected subject to be passed into matcher block");
}

- (void)testShouldPassWhenBlockSpecificationReturnsYes
{
    KWUserDefinedMatcher *matcher = [KWUserDefinedMatcher matcherWithSubject:nil block:^(id subject) {
        return YES;
    }];
    STAssertTrue([matcher evaluate], @"expected positive match");
}

- (void)testShouldFailWhenBlockSpecificationReturnsNo
{
    KWUserDefinedMatcher *matcher = [KWUserDefinedMatcher matcherWithSubject:nil block:^(id subject) {
        return NO;
    }];
    STAssertFalse([matcher evaluate], @"expected negative match");
}

- (void)testShouldYieldMessageArgumentToTheBlock
{
    KWUserDefinedMatcher *matcher = [KWUserDefinedMatcher matcherWithSubject:@"string" block:^(id subject, id object) {
        return [(NSString *)object isEqualToString:subject];
    }];
    matcher.selector = NSSelectorFromString(@"matchTheString:");
    [matcher performSelector:matcher.selector withObject:@"string"];

    STAssertTrue([matcher evaluate], @"expected subject to match yielded argument");
}

@end

#pragma mark -

@interface KWUserDefinedMatcherBuilderTest : SenTestCase
@end

@implementation KWUserDefinedMatcherBuilderTest

- (void)testShouldDefineTheBlockToEvaluate
{
    KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builder];

    [builder match:^(id subject) {
        return [subject isEqual:@"foo"];
    }];

    KWUserDefinedMatcher *matcher = [builder buildMatcherWithSubject:@"foo"];
    STAssertTrue([matcher evaluate], @"expected positive match");
}

- (void)testShouldSetTheSelectorForTheMatcher
{
    KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builderForSelector:NSSelectorFromString(@"dummySelector")];
    KWUserDefinedMatcher *matcher = [builder buildMatcherWithSubject:@"foo"];
    STAssertEquals(NSSelectorFromString(@"dummySelector"), matcher.selector, @"should set selector");

}

- (void)testCanSetTheFailureMessageForShould
{
    KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builder];

    [builder failureMessageForShould:^(id subject) {
        return [NSString stringWithFormat:@"failure message containing subject %@", subject];
    }];

    KWUserDefinedMatcher *matcher = [builder buildMatcherWithSubject:@"foo"];
    STAssertEqualObjects(@"failure message containing subject foo", [matcher failureMessageForShould], @"should set failure message for should");
}

- (void)testCanSetTheFailureMessageForShouldNot
{
    KWUserDefinedMatcherBuilder *builder = [KWUserDefinedMatcherBuilder builder];

    [builder failureMessageForShouldNot:^(id subject) {
        return [NSString stringWithFormat:@"failure message containing subject %@", subject];
    }];

    KWUserDefinedMatcher *matcher = [builder buildMatcherWithSubject:@"foo"];
    STAssertEqualObjects(@"failure message containing subject foo", [matcher failureMessageForShouldNot], @"should set failure message for should");
}

@end

#endif
Something went wrong with that request. Please try again.