forked from kiwi-bdd/Kiwi
/
KWStubTest.m
109 lines (93 loc) · 5.07 KB
/
KWStubTest.m
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
//
// Licensed under the terms in License.txt
//
// Copyright 2010 Allen Ding. All rights reserved.
//
#import "Kiwi.h"
#import "KiwiTestConfiguration.h"
#import "TestClasses.h"
#import "KWIntercept.h"
#import "NSInvocation+KiwiAdditions.h"
#if KW_TESTS_ENABLED
@interface KWStubTest : SenTestCase
@end
@implementation KWStubTest
- (void)tearDown {
KWClearStubsAndSpies();
}
- (void)testItShouldProcessMatchedInvocations {
id subject = [Cruiser cruiser];
KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(crewComplement)];
id stub = [KWStub stubWithMessagePattern:messagePattern value:[KWValue valueWithUnsignedInt:42]];
id invocation = [NSInvocation invocationWithTarget:subject selector:@selector(crewComplement)];
STAssertTrue([stub processInvocation:invocation], @"expected stub to process invocation");
}
- (void)testItShouldNotProcessNonMatchedInvocations {
id subject = [Cruiser cruiser];
id argumentFilters = [NSArray arrayWithObject:[KWValue valueWithUnsignedInt:15]];
KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(energyLevelInWarpCore:) argumentFilters:argumentFilters];
id stub = [KWStub stubWithMessagePattern:messagePattern value:[KWValue valueWithFloat:13.0f]];
NSUInteger index = 17;
id invocation = [NSInvocation invocationWithTarget:subject selector:@selector(energyLevelInWarpCore:) messageArguments:&index];
STAssertFalse([stub processInvocation:invocation], @"expected stub not to process invocation");
}
- (void)testItShouldWriteWrappedInvocationReturnValues {
id subject = [Cruiser cruiser];
KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(crewComplement)];
id stub = [KWStub stubWithMessagePattern:messagePattern value:[KWValue valueWithUnsignedInt:42]];
id invocation = [NSInvocation invocationWithTarget:subject selector:@selector(crewComplement)];
[stub processInvocation:invocation];
NSUInteger crewComplement = 0;
[invocation getReturnValue:&crewComplement];
STAssertEquals(crewComplement, 42u, @"expected stub to write return value");
}
- (void)testItShouldWriteObjectInvocationReturnValues {
id subject = [Cruiser cruiser];
KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(callsign)];
id stub = [KWStub stubWithMessagePattern:messagePattern value:@"Green 1"];
id invocation = [NSInvocation invocationWithTarget:subject selector:@selector(callsign)];
[stub processInvocation:invocation];
id callsign = nil;
[invocation getReturnValue:&callsign];
STAssertEqualObjects(callsign, @"Green 1", @"expected stub to write return value");
}
- (void)testItShouldRetainValueWhenProcessingInvocationsThatBeginsWithAlloc {
id subject = [Cruiser mock];
id messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(alloc)];
id stub = [KWStub stubWithMessagePattern:messagePattern value:subject];
id invocation = [NSInvocation invocationWithTarget:[Cruiser class] selector:@selector(alloc)];
NSUInteger retainCountBefore = [subject retainCount];
[stub processInvocation:invocation];
NSUInteger retainCountAfter = [subject retainCount];
STAssertEquals(retainCountAfter, retainCountBefore + 1, @"expected stub to retain value");
}
- (void)testItShouldRetainValueWhenProcessingInvocationsThatBeginsWithNew {
id subject = [Cruiser mock];
id messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(new)];
id stub = [KWStub stubWithMessagePattern:messagePattern value:subject];
id invocation = [NSInvocation invocationWithTarget:[Cruiser class] selector:@selector(new)];
NSUInteger retainCountBefore = [subject retainCount];
[stub processInvocation:invocation];
NSUInteger retainCountAfter = [subject retainCount];
STAssertEquals(retainCountAfter, retainCountBefore + 1, @"expected stub to retain value");
}
- (void)testItShouldRetainValueWhenProcessingInvocationsThatContainsCopy {
id subject = [Cruiser mock];
id messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(copy)];
id stub = [KWStub stubWithMessagePattern:messagePattern value:subject];
id invocation = [NSInvocation invocationWithTarget:[Cruiser class] selector:@selector(copy)];
NSUInteger retainCountBefore = [subject retainCount];
[stub processInvocation:invocation];
NSUInteger retainCountAfter = [subject retainCount];
STAssertEquals(retainCountAfter, retainCountBefore + 1, @"expected stub to retain value");
subject = [Cruiser mock];
messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(mutableCopy)];
stub = [KWStub stubWithMessagePattern:messagePattern value:subject];
invocation = [NSInvocation invocationWithTarget:[Cruiser class] selector:@selector(mutableCopy)];
retainCountBefore = [subject retainCount];
[stub processInvocation:invocation];
retainCountAfter = [subject retainCount];
STAssertEquals(retainCountAfter, retainCountBefore + 1, @"expected stub to retain value");
}
@end
#endif // #if KW_TESTS_ENABLED