Skip to content

Commit e6fc18e

Browse files
committed
First attempt to select class method in new syntax.
1 parent 6759e85 commit e6fc18e

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

Source/OCMock.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@
392392
03B316291463350E0052CD09 /* OCObserverMockObjectTests.m */,
393393
03B3161F1463350E0052CD09 /* NSInvocationOCMAdditionsTests.m */,
394394
2FA28DEDB9163597B7C49F3D /* NSMethodSignatureOCMAdditionsTests.m */,
395-
03565A3418F0566F003AE91E /* Supporting Files */,
396395
037ECD5318FAD84100AF0E4C /* OCMInvocationMatcherTests.m */,
396+
03565A3418F0566F003AE91E /* Supporting Files */,
397397
);
398398
path = OCMockTests;
399399
sourceTree = "<group>";

Source/OCMock/OCMMacroState.h

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@interface OCMMacroState : NSObject
1111
{
1212
BOOL shouldRecordExpectation;
13+
BOOL shouldRecordAsClassMethod;
1314
OCMockRecorder *recorder;
1415
}
1516

@@ -24,6 +25,9 @@
2425
- (void)setShouldRecordExpectation:(BOOL)flag;
2526
- (BOOL)shouldRecordExpectation;
2627

28+
- (void)setShouldRecordAsClassMethod:(BOOL)flag;
29+
- (BOOL)shouldRecordAsClassMethod;
30+
2731
- (void)setRecorder:(OCMockRecorder *)aRecorder;
2832
- (OCMockRecorder *)recorder;
2933

Source/OCMock/OCMMacroState.m

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ - (BOOL)shouldRecordExpectation
5252
}
5353

5454

55+
- (void)setShouldRecordAsClassMethod:(BOOL)flag
56+
{
57+
shouldRecordAsClassMethod = YES;
58+
}
59+
60+
- (BOOL)shouldRecordAsClassMethod
61+
{
62+
return shouldRecordAsClassMethod;
63+
}
64+
65+
5566
- (void)setRecorder:(OCMockRecorder *)aRecorder
5667
{
5768
if(recorder != nil)

Source/OCMock/OCMock.h

+4
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@
3838
[OCMMacroState endExpectMacro]; \
3939
})
4040

41+
#define classMethod(invocation) \
42+
[[OCMMacroState globalState] setShouldRecordAsClassMethod:YES]; \
43+
invocation;
44+
4145
#define OCMVerifyAll(mock) [mock verifyAtLocation:OCMMakeLocation(self, __FILE__, __LINE__)]

Source/OCMock/OCMockObject.m

+2
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation
219219
recorder = [self expect];
220220
else
221221
recorder = [self stub];
222+
if([macroState shouldRecordAsClassMethod])
223+
[recorder classMethod];
222224
[recorder forwardInvocation:anInvocation];
223225
[macroState setRecorder:recorder];
224226
}

Source/OCMockTests/OCMockObjectMacroTests.m

+21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ - (NSString *)stringValue
2424
@end
2525

2626

27+
// implemented in OCMockObjectClassMethodMockingTests
28+
29+
@interface TestClassWithClassMethods : NSObject
30+
+ (NSString *)foo;
31+
+ (NSString *)bar;
32+
- (NSString *)bar;
33+
@end
34+
35+
2736

2837
@interface OCMockObjectMacroTests : XCTestCase
2938
{
@@ -197,6 +206,18 @@ - (void)testCanChainPropertyBasedActions
197206
}
198207

199208

209+
- (void)testCanExplicitlySelectClassMethod
210+
{
211+
id mock = OCMClassMock([TestClassWithClassMethods class]);
212+
213+
OCMStub(classMethod([mock bar])).andReturn(@"mocked-class");
214+
OCMStub([mock bar]).andReturn(@"mocked-instance");
215+
216+
XCTAssertEqualObjects(@"mocked-class", [TestClassWithClassMethods bar], @"Should have stubbed class method.");
217+
XCTAssertEqualObjects(@"mocked-instance", [mock bar], @"Should have stubbed instance method.");
218+
}
219+
220+
200221
- (void)testSetsUpExpectations
201222
{
202223
id mock = OCMClassMock([TestClassForMacroTesting class]);

0 commit comments

Comments
 (0)