5 // Created by Wincent Colaiuta on 10 February 2006.
7 // Copyright 2006-2007 Wincent Colaiuta.
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #import "WOMockTests.h"
24 @implementation WOMockTests
26 // no real tests in this method: examples of how to use mocks without warnings
29 // using mocks in the following way will cause a compiler warning:
30 //WOObjectMock *mock = [WOMock mockForObjectClass:[NSString class]];
31 //[[mock expect] lowercaseString];
32 //[mock lowercaseString]; // "warning: 'WOObjectMock' may not respond to '-lowercaseString'"
35 // one way of avoiding compiler warnings when using mocks: cast to id
36 WOObjectMock *mock1 = [WOMock mockForObjectClass:[NSString class]];
37 [[mock1 expect] lowercaseString];
38 [(id)mock1 lowercaseString];
41 // another way of avoiding compiler warnings: use id type from beginning
42 id mock2 = [WOMock mockForObjectClass:[NSString class]];
43 [[mock2 expect] lowercaseString];
44 [mock2 lowercaseString];
47 // another way: cast to mocked class
48 NSString *mock3 = [WOMock mockForObjectClass:[NSString class]];
49 [[(WOMock *)mock3 expect] lowercaseString];
50 [mock3 lowercaseString];
51 [(WOMock *)mock3 verify];
53 // another way: alternative way of casting to mocked class
54 WOObjectMock *mock4 = [WOMock mockForObjectClass:[NSString class]];
55 [[mock4 expect] lowercaseString];
56 [(NSString *)mock4 lowercaseString];
59 // yet another way: use objc_msgSend
60 WOObjectMock *mock5 = [WOMock mockForObjectClass:[NSString class]];
61 [[mock5 expect] lowercaseString];
62 objc_msgSend(mock5, @selector(lowercaseString));
66 - (void)testMockForObjectClass
68 WOObjectMock *mock = [WOMock mockForObjectClass:[self class]];
70 // make sure WOObjectMock class is returned
71 WO_TEST_EQ([mock class], [WOObjectMock class]);
73 // make sure mocked class is correctly set
74 WO_TEST_EQ([mock mockedClass], [self class]);
76 // should throw exception instead of entering infinite loop
77 WO_TEST_THROWS([WOObjectMock mockForObjectClass:[self class]]);
80 - (void)testMockForClass
82 WOClassMock *mock = [WOMock mockForClass:[self class]];
84 // make sure WOClassMock class is returned
85 WO_TEST_EQ([mock class], [WOClassMock class]);
87 // make sure mocked class is correctly set
88 Class class = [self class];
89 Class metaclass = object_getClass(class);
90 WO_TEST_EQ([mock mockedClass], metaclass);
92 // should throw exception instead of entering infinite loop
93 // cannot test this because subclass implements that method directly
94 //WO_TEST_THROWS([WOClassMock mockForClass:[self class]]);
97 - (void)testMockForProtocol
99 WOProtocolMock *mock = [WOMock mockForProtocol:@protocol(WOTest)];
101 // make sure WOProtocolMock class is returned
102 WO_TEST_EQ([mock class], [WOProtocolMock class]);
104 // make sure mocked protocol is correctly set
105 WO_TEST_EQ([mock mockedProtocol], @protocol(WOTest));
107 // should throw exception instead of entering infinite loop
108 // cannot test this because subclass implements that method directly
109 //WO_TEST_THROWS([WOProtocolMock mockForProtocol:@protocol(WOTest)]);
112 - (void)testInitWithObjectClass
114 WOObjectMock *mock = [[WOMock alloc] initWithObjectClass:[self class]];
116 // make sure WOObjectMock class is returned
117 WO_TEST_EQ([mock class], [WOObjectMock class]);
119 // make sure mocked class is correctly set
120 WO_TEST_EQ([mock mockedClass], [self class]);
122 // should throw exception instead of entering infinite loop
123 mock = [WOObjectMock alloc];
124 WO_TEST_THROWS([mock initWithObjectClass:[self class]]);
127 - (void)testInitWithClass
129 WOClassMock *mock = [[WOMock alloc] initWithClass:[self class]];
131 // make sure WOClassMock class is returned
132 WO_TEST_EQ([mock class], [WOClassMock class]);
134 // make sure mocked class is correctly set
135 Class class = [self class];
136 Class metaclass = object_getClass(class);
137 WO_TEST_EQ([mock mockedClass], metaclass);
139 // should throw exception instead of entering infinite loop
140 // cannot test this because subclass implements that method directly
141 //mock = [WOClassMock alloc];
142 //WO_TEST_THROWS([mock initWithClass:[self class]]);
145 - (void)testInitWithProtocol
147 WOProtocolMock *mock = [[WOMock alloc] initWithProtocol:@protocol(WOTest)];
149 // make sure WOProtocolMock class is returned
150 WO_TEST_EQ([mock class], [WOProtocolMock class]);
152 // make sure mocked protocol is correctly set
153 WO_TEST_EQ([mock mockedProtocol], @protocol(WOTest));
155 // should throw exception instead of entering infinite loop
156 // cannot test this because subclass implements that method directly
157 //mock = [WOProtocolMock alloc];
158 //WO_TEST_THROWS([mock initWithProtocol:@protocol(WOTest)]);
161 - (void)testRecordingMethods
163 // all recording methods should throw an exception (use subclasses instead)
164 WOMock *mock = [[WOMock alloc] init];
165 WO_TEST_THROWS([mock reject]);
166 WO_TEST_THROWS([mock expectInOrder]);
167 WO_TEST_THROWS([mock expectOnce]);
168 WO_TEST_THROWS([mock expect]);
169 WO_TEST_THROWS([mock acceptOnce]);
170 WO_TEST_THROWS([mock accept]);