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 278 lines (214 sloc) 9.266 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
//
// KWFunctionalTests.m
// Kiwi
//
// Created by Marin Usalj on 1/24/13.
// Copyright (c) 2013 Allen Ding. All rights reserved.
//

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

@interface KWExampleSuiteBuilder ()

@property (nonatomic, retain, readwrite) NSString *focusedContextNodeDescription;
@property (nonatomic, retain, readwrite) NSString *focusedItNodeDescription;

@end

static BOOL tests_were_run = NO;

SPEC_BEGIN(Functional)

NSMutableArray *calls = @[@"OuterTestCase",
@"BeforeAll", @"AfterAll", @"BeforeEach", @"AfterEach", @"TestCase",
@"InnerBeforeAll", @"InnerAfterAll", @"InnerBeforeEach", @"InnerAfterEach", @"InnerTestCase",
@"DoubleInnerBeforeAll", @"DoubleInnerAfterAll", @"DoubleInnerBeforeEach", @"DoubleInnerAfterEach", @"DoubleInnerTestCase"
].mutableCopy;

//There should not be a focused call site at this point
assert(![[KWExampleSuiteBuilder sharedExampleSuiteBuilder] focusedCallSite]);

describe(@"OuterDescribe", ^{
    it(@"OuterTestCase", ^{ [calls removeObject:@"OuterTestCase"]; });
    context(@"OuterContext", ^{
        beforeAll(^{ [calls removeObject:@"BeforeAll"]; });
        afterAll(^{ [calls removeObject:@"AfterAll"]; });
        beforeEach(^{ [calls removeObject:@"BeforeEach"]; });
        afterEach(^{ [calls removeObject:@"AfterEach"]; });
        it(@"TestCase", ^{ [calls removeObject:@"TestCase"]; });
        context(@"Context", ^{
            beforeAll(^{ [calls removeObject:@"InnerBeforeAll"]; });
            afterAll(^{ [calls removeObject:@"InnerAfterAll"]; });
            beforeEach(^{ [calls removeObject:@"InnerBeforeEach"]; });
            afterEach(^{ [calls removeObject:@"InnerAfterEach"]; });
            it(@"InnerTestCase", ^{ [calls removeObject:@"InnerTestCase"]; });
            context(@"InnerContext", ^{
                beforeAll(^{ [calls removeObject:@"DoubleInnerBeforeAll"]; });
                afterAll(^{ [calls removeObject:@"DoubleInnerAfterAll"]; });
                beforeEach(^{ [calls removeObject:@"DoubleInnerBeforeEach"]; });
                afterEach(^{ [calls removeObject:@"DoubleInnerAfterEach"]; });
                it(@"DoubleInnerTestCase", ^{ [calls removeObject:@"DoubleInnerTestCase"]; });
            });
        });
    });
});

describe(@"Check", ^{
    it(@"All the blocks were called", ^{
        [[calls should] haveCountOf:0];
        tests_were_run = YES;
    });
});

SPEC_END

SPEC_BEGIN(FunctionalFocusedDescribe)

#pragma mark - Focused describe blocks

NSMutableArray *focusedContextCalls = @[@"InnerBeforeAll", @"InnerAfterAll", @"InnerBeforeEach", @"InnerAfterEach", @"InnerTestCase", @"DoubleInnerBeforeAll", @"DoubleInnerAfterAll", @"DoubleInnerBeforeEach", @"DoubleInnerAfterEach", @"DoubleInnerTestCase"
].mutableCopy;

NSMutableArray *unFocusedContextCalls = @[ @"OuterTestCase", @"BeforeAll", @"AfterAll", @"BeforeEach", @"AfterEach"
].mutableCopy;

[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setFocusedCallSite:[KWCallSite callSiteWithFilename:@"KWFunctionalTests.m" lineNumber:84]];

describe(@"UnFocusedContext", ^{
    it(@"OuterTestCase", ^{ [unFocusedContextCalls removeObject:@"OuterTestCase"]; });
    beforeAll(^{ [unFocusedContextCalls removeObject:@"BeforeAll"]; });
    afterAll(^{ [unFocusedContextCalls removeObject:@"AfterAll"]; });
    beforeEach(^{ [unFocusedContextCalls removeObject:@"BeforeEach"]; });
    afterEach(^{ [unFocusedContextCalls removeObject:@"AfterEach"]; });
    describe(@"FocusedContext", ^{
        beforeAll(^{ [focusedContextCalls removeObject:@"InnerBeforeAll"]; });
        afterAll(^{ [focusedContextCalls removeObject:@"InnerAfterAll"]; });
        beforeEach(^{ [focusedContextCalls removeObject:@"InnerBeforeEach"]; });
        afterEach(^{ [focusedContextCalls removeObject:@"InnerAfterEach"]; });
        it(@"InnerTestCase", ^{ [focusedContextCalls removeObject:@"InnerTestCase"]; });
        context(@"InnerContext", ^{
            beforeAll(^{ [focusedContextCalls removeObject:@"DoubleInnerBeforeAll"]; });
            afterAll(^{ [focusedContextCalls removeObject:@"DoubleInnerAfterAll"]; });
            beforeEach(^{ [focusedContextCalls removeObject:@"DoubleInnerBeforeEach"]; });
            afterEach(^{ [focusedContextCalls removeObject:@"DoubleInnerAfterEach"]; });
            it(@"DoubleInnerTestCase", ^{ [focusedContextCalls removeObject:@"DoubleInnerTestCase"]; });
        });
    });
});

[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setFocusedCallSite:nil];

describe(@"FocusedContextCheck", ^{
    it(@"All the blocks were called", ^{
        [[focusedContextCalls should] haveCountOf:0];
        [[unFocusedContextCalls should] haveCountOf:1];
    });
});

SPEC_END

SPEC_BEGIN(FunctionalFocusedIt)

#pragma mark - Focused it blocks

NSMutableArray *focusedItCalls = @[@"FocusedTestCase"].mutableCopy;

NSMutableArray *unFocusedItCalls = @[@"UnFocusedTestCase"].mutableCopy;

[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setFocusedCallSite:[KWCallSite callSiteWithFilename:@"KWFunctionalTests.m" lineNumber:122]];

describe(@"FocusedIt", ^{
    it(@"FocusedTestCase", ^{ [focusedItCalls removeObject:@"FocusedTestCase"]; });
    it(@"UnFocusedTestCase", ^{ [unFocusedItCalls removeObject:@"UnFocusedTestCase"]; });
});

[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setFocusedCallSite:nil];

describe(@"FocusedItCheck", ^{
    it(@"All the blocks were called", ^{
        [[focusedItCalls should] haveCountOf:0];
        [[unFocusedItCalls should] haveCountOf:1];
    });
});

SPEC_END

SPEC_BEGIN(FunctionalLet)

describe(@"Greeting", ^{
    let(subject, ^{ return @""; });
    let(greeting, ^{ return [NSString stringWithFormat:@"Hello, %@!", subject]; });

    describe(@"default subject", ^{
        specify(^{ [[subject should] beEmpty]; });
    });

    context(@"with the subject \"world\"", ^{
        let(subject, ^{ return @"world"; });

        specify(^{ [[greeting should] equal:@"Hello, world!"]; });
    });

    context(@"with the subject \"Kiwi\"", ^{
        let(subject, ^{ return @"Kiwi"; });

        specify(^{ [[greeting should] equal:@"Hello, Kiwi!"]; });
    });
});

describe(@"Let context tree", ^{
    let(number, ^{ return @0; });
    let(string, ^{ return [number stringValue]; });

    describe(@"number 1", ^{
        let(number, ^{ return @1; });

        context(@"number 2", ^{
            let(number, ^{ return @2; });

            context(@"number 3", ^{
                let(number, ^{ return @3; });

                context(@"number 4", ^{
                    let(number, ^{ return @4; });

                    specify(^{ [[number should] equal:@4]; });
                    specify(^{ [[string should] equal:@"4"]; });
                });

                specify(^{ [[number should] equal:@3]; });
                specify(^{ [[string should] equal:@"3"]; });
            });

            specify(^{ [[number should] equal:@2];});
            specify(^{ [[string should] equal:@"2"]; });
        });

        context(@"number 5", ^{
            let(number, ^{ return @5; });

            context(@"number 6", ^{
                let(number, ^{ return @6; });

                specify(^{ [[number should] equal:@6]; });
                specify(^{ [[string should] equal:@"6"]; });
            });

            specify(^{ [[number should] equal:@5]; });
            specify(^{ [[string should] equal:@"5"]; });
        });

        specify(^{ [[number should] equal:@1]; });
        specify(^{ [[string should] equal:@"1"]; });
    });
});

SPEC_END

SPEC_BEGIN(NilMatchers)

describe(@"nil matchers", ^{
    __block NSObject *object;

    context(@"when object is nil", ^{
        beforeEach(^{
            object = nil;
        });

        it(@"passes a test for [[x should] beNil]", ^{
            [[object should] beNil];
        });
        it(@"passes a test for [[x shouldNot] beNonNil]", ^{
            [[object shouldNot] beNonNil];
        });
        it(@"passes a test for [x shouldBeNil]", ^{
            [object shouldBeNil];
        });
    });

    context(@"when object is non-nil", ^{
        beforeEach(^{
            object = [NSObject new];
        });
        it(@"passes a test for [[x should] beNonNil]", ^{
            [[object should] beNonNil];
        });
        it(@"passes a test for [[x shouldNot] beNil]", ^{
            [[object shouldNot] beNil];
        });
        it(@"passes a test for [x shouldNotBeNil]", ^{
            [object shouldNotBeNil];
        });
    });
});

SPEC_END

SPEC_BEGIN(NSDateStub)

describe(@"NSDate stubs", ^{
__block id dateMock = nil;
context(@"shouldEventually should work when [NSDate date] is mocked and stubbed", ^{
beforeEach(^{
dateMock = [NSDate mock];
[dateMock stub: @selector(timeIntervalSince1970) andReturn: [KWValue valueWithDouble: 1.0f]];
[NSDate stub: @selector(date) andReturn: dateMock];
});

it(@"should not hang", ^{
[[theValue([[NSDate date] timeIntervalSince1970]) shouldNotEventually] equal: 5.0f withDelta: 0.5f];
});
});

});

SPEC_END

#if KW_TESTS_ENABLED
@interface KWFunctionalTests : SenTestCase
@end
@implementation KWFunctionalTests

- (void)testSuiteWasExecuted {
    STAssertEquals(YES, tests_were_run, @"Test suite hasn't run!");
}

@end
#endif // #if KW_TESTS_ENABLED
Something went wrong with that request. Please try again.