File tree Expand file tree Collapse file tree 4 files changed +58
-25
lines changed Expand file tree Collapse file tree 4 files changed +58
-25
lines changed Original file line number Diff line number Diff line change @@ -39,16 +39,16 @@ - (id)getArgumentAtIndexAsObject:(NSInteger)argIndex
3939
4040 if ((strlen (argType) > 1 ) && (strchr (" {^" , argType[0 ]) == NULL ) && (strcmp (" @?" , argType) != 0 ))
4141 [NSException raise: NSInvalidArgumentException format: @" Cannot handle argument type '%s '." , argType];
42-
43- switch (argType[0 ])
42+
43+ if (OCMIsObjectType (argType))
44+ {
45+ id value;
46+ [self getArgument: &value atIndex: argIndex];
47+ return value;
48+ }
49+
50+ switch (argType[0 ])
4451 {
45- case ' #' :
46- case ' @' :
47- {
48- id value;
49- [self getArgument: &value atIndex: argIndex];
50- return value;
51- }
5252 case ' :' :
5353 {
5454 SEL s = (SEL )0 ;
Original file line number Diff line number Diff line change @@ -49,6 +49,11 @@ BOOL OCMIsObjectType(const char *objCType)
4949 if ([regex numberOfMatchesInString: type options: 0 range: NSMakeRange (0 , type.length)] > 0 )
5050 return YES ;
5151
52+ // if the return type is a block we treat it like an object
53+ // TODO: if the runtime were to encode the block's argument and/or return types, this test would not be sufficient
54+ if (strcmp (objCType, @encode (void (^)())) == 0 )
55+ return YES ;
56+
5257 return NO ;
5358}
5459
Original file line number Diff line number Diff line change @@ -35,6 +35,23 @@ - (void)aSpecialMethod:(byref in __unused void *)someArg
3535@end
3636
3737
38+ typedef NSString TypedefString;
39+
40+ @interface TestClassWithTypedefObjectArgument : NSObject
41+
42+ - (NSString *)stringForTypedef : (TypedefString *)string ;
43+
44+ @end
45+
46+ @implementation TestClassWithTypedefObjectArgument
47+
48+ - (NSString *)stringForTypedef : (TypedefString *)string
49+ {
50+ return @" Whatever. Doesn't matter." ;
51+ }
52+ @end
53+
54+
3855@interface TestDelegate : NSObject
3956
4057- (void )go ;
@@ -116,6 +133,15 @@ - (void)testWorksWithTypeQualifiers
116133 XCTAssertNoThrow ([myMock aSpecialMethod: " foo" ], @" Should not complain about method with type qualifiers." );
117134}
118135
136+ - (void )testWorksWithTypedefsToObjects
137+ {
138+ id myMock = [OCMockObject mockForClass: [TestClassWithTypedefObjectArgument class ]];
139+ [[[myMock stub ] andReturn: @" stubbed" ] stringForTypedef: [OCMArg any ]];
140+ id actualReturn = [myMock stringForTypedef: @" Some arg that shouldn't matter" ];
141+ XCTAssertEqualObjects (actualReturn, @" stubbed" , @" Should have matched invocation." );
142+ }
143+
144+
119145#if 0 // can't test this with ARC
120146- (void)testAdjustsRetainCountWhenStubbingMethodsThatCreateObjects
121147{
Original file line number Diff line number Diff line change @@ -63,6 +63,24 @@ - (OpaquePtr)opaquePtrValue;
6363
6464@end
6565
66+ @implementation TestClassWithOpaquePointerMethod
67+
68+ typedef struct TestOpaque {
69+ int i;
70+ int j;
71+ } TestOpaque;
72+
73+ TestOpaque myOpaque;
74+
75+ - (OpaquePtr)opaquePtrValue
76+ {
77+ myOpaque.i = 3 ;
78+ myOpaque.i = 4 ;
79+ return &myOpaque;
80+ }
81+
82+ @end
83+
6684@interface TestClassWithProperty : NSObject
6785
6886@property (nonatomic , retain ) NSString *title;
@@ -889,20 +907,4 @@ - (void)testCanCreateExpectationsAfterInvocations
889907
890908@end
891909
892- @implementation TestClassWithOpaquePointerMethod
893-
894- typedef struct TestOpaque {
895- int i;
896- int j;
897- } TestOpaque;
898-
899- TestOpaque myOpaque;
900910
901- - (OpaquePtr)opaquePtrValue
902- {
903- myOpaque.i = 3 ;
904- myOpaque.i = 4 ;
905- return &myOpaque;
906- }
907-
908- @end
You can’t perform that action at this time.
0 commit comments