Skip to content

Commit 7932a18

Browse files
committed
Made some refactorings.
1 parent 34dc3f8 commit 7932a18

File tree

5 files changed

+39
-67
lines changed

5 files changed

+39
-67
lines changed

Source/OCMock/OCMBoxedReturnValueProvider.m

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@
1818
#import "OCMFunctionsPrivate.h"
1919
#import "NSValue+OCMAdditions.h"
2020

21-
static BOOL IsZeroBuffer(const char* buffer, size_t length)
22-
{
23-
for(size_t i = 0; i < length; ++i)
24-
{
25-
if(buffer[i] != 0)
26-
{
27-
return NO;
28-
}
29-
}
30-
return YES;
31-
}
3221

3322
@implementation OCMBoxedReturnValueProvider
3423

@@ -40,10 +29,8 @@ - (void)handleInvocation:(NSInvocation *)anInvocation
4029
NSValue *returnValueAsNSValue = (NSValue *)returnValue;
4130
[returnValueAsNSValue getValue:valueBuffer];
4231

43-
if([self isMethodReturnType:returnType
44-
compatibleWithValueType:[returnValueAsNSValue objCType]
45-
value:valueBuffer
46-
valueSize:returnTypeSize])
32+
if([self isMethodReturnType:returnType compatibleWithValueType:[returnValueAsNSValue objCType]
33+
value:valueBuffer valueSize:returnTypeSize])
4734
{
4835
[anInvocation setReturnValue:valueBuffer];
4936
}
@@ -58,37 +45,15 @@ - (void)handleInvocation:(NSInvocation *)anInvocation
5845
}
5946
}
6047

61-
- (BOOL)isMethodReturnType:(const char *)returnType compatibleWithValueType:(const char *)valueType value:(const char*)value valueSize:(size_t)valueSize
48+
- (BOOL)isMethodReturnType:(const char *)returnType compatibleWithValueType:(const char *)valueType value:(const void *)value valueSize:(size_t)valueSize
6249
{
6350
/* Same types are obviously compatible */
6451
if(strcmp(returnType, valueType) == 0)
6552
return YES;
6653

67-
// Special casing for nil and Nil
54+
/* Special treatment for nil and Nil */
6855
if(strcmp(returnType, @encode(id)) == 0 || strcmp(returnType, @encode(Class)) == 0)
69-
{
70-
// Check to verify that the value is actually zero.
71-
if(IsZeroBuffer(value, valueSize))
72-
{
73-
// nil and Nil get potentially different encodings depending on the compilation
74-
// settings of the file where the return value gets recorded. We check to verify
75-
// against all the values we know of.
76-
const char *validNilEncodings[] =
77-
{
78-
@encode(void *), // Standard Obj C
79-
@encode(int), // 32 bit C++ (before nullptr)
80-
@encode(long long), // 64 bit C++ (before nullptr)
81-
@encode(char *), // C++ with nullptr
82-
};
83-
for(size_t i = 0; i < sizeof(validNilEncodings) / sizeof(validNilEncodings[0]); ++i)
84-
{
85-
if(strcmp(valueType, validNilEncodings[i]) == 0)
86-
{
87-
return YES;
88-
}
89-
}
90-
}
91-
}
56+
return OCMIsNilValue(valueType, value, valueSize);
9257

9358
return OCMEqualTypesAllowingOpaqueStructs(returnType, valueType);
9459
}

Source/OCMock/OCMFunctions.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,24 @@ BOOL OCMEqualTypesAllowingOpaqueStructs(const char *type1, const char *type2)
296296
}
297297
}
298298

299+
BOOL OCMIsNilValue(const char *objectCType, const void *value, size_t valueSize)
300+
{
301+
// First, check value itself
302+
for(size_t i = 0; i < valueSize; i++)
303+
if(((const char *)value)[i] != 0)
304+
return NO;
305+
306+
// Depending on the compilation settings of the file where the return value gets recorded,
307+
// nil and Nil get potentially different encodings. Check all known encodings.
308+
if((strcmp(objectCType, @encode(void *)) == 0) || // Standard Objective-C
309+
(strcmp(objectCType, @encode(int)) == 0) || // 32 bit C++ (before nullptr)
310+
(strcmp(objectCType, @encode(long long)) == 0) || // 64 bit C++ (before nullptr)
311+
(strcmp(objectCType, @encode(char *)) == 0)) // C++ with nullptr
312+
return YES;
313+
314+
return NO;
315+
}
316+
299317

300318
BOOL OCMIsAppleBaseClass(Class cls)
301319
{

Source/OCMock/OCMFunctionsPrivate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ BOOL OCMIsObjectType(const char *objCType);
2727
const char *OCMTypeWithoutQualifiers(const char *objCType);
2828
BOOL OCMEqualTypesAllowingOpaqueStructs(const char *type1, const char *type2);
2929
CFNumberType OCMNumberTypeForObjCType(const char *objcType);
30+
BOOL OCMIsNilValue(const char *objectCType, const void *value, size_t valueSize);
3031

3132
BOOL OCMIsAppleBaseClass(Class cls);
3233
BOOL OCMIsApplePrivateMethod(Class cls, SEL sel);

Source/OCMockTests/OCMBoxedReturnValueProviderTests.m

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,13 @@ @interface OCMBoxedReturnValueProvider(Private)
2121
- (BOOL)isMethodReturnType:(const char *)returnType compatibleWithValueType:(const char *)valueType value:(const char*)value valueSize:(size_t)valueSize;
2222
@end
2323

24+
2425
@interface OCMBoxedReturnValueProviderTests : XCTestCase
25-
{
26-
char value;
27-
size_t valueSize;
28-
}
26+
2927
@end
3028

3129
@implementation OCMBoxedReturnValueProviderTests
3230

33-
- (void)setUp {
34-
[super setUp];
35-
value = 'A';
36-
valueSize = 1;
37-
}
38-
3931
- (void)testCorrectEqualityForCppProperty
4032
{
4133
// see https://github.com/erikdoe/ocmock/issues/96
@@ -62,12 +54,12 @@ - (void)testCorrectEqualityForCppProperty
6254
"r^{GURL}";
6355

6456
OCMBoxedReturnValueProvider *boxed = [OCMBoxedReturnValueProvider new];
65-
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type2 value:&value valueSize:valueSize]);
66-
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type3 value:&value valueSize:valueSize]);
67-
XCTAssertTrue([boxed isMethodReturnType:type2 compatibleWithValueType:type1 value:&value valueSize:valueSize]);
68-
XCTAssertTrue([boxed isMethodReturnType:type2 compatibleWithValueType:type3 value:&value valueSize:valueSize]);
69-
XCTAssertTrue([boxed isMethodReturnType:type3 compatibleWithValueType:type1 value:&value valueSize:valueSize]);
70-
XCTAssertTrue([boxed isMethodReturnType:type3 compatibleWithValueType:type2 value:&value valueSize:valueSize]);
57+
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type2 value:NULL valueSize:0]);
58+
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type3 value:NULL valueSize:0]);
59+
XCTAssertTrue([boxed isMethodReturnType:type2 compatibleWithValueType:type1 value:NULL valueSize:0]);
60+
XCTAssertTrue([boxed isMethodReturnType:type2 compatibleWithValueType:type3 value:NULL valueSize:0]);
61+
XCTAssertTrue([boxed isMethodReturnType:type3 compatibleWithValueType:type1 value:NULL valueSize:0]);
62+
XCTAssertTrue([boxed isMethodReturnType:type3 compatibleWithValueType:type2 value:NULL valueSize:0]);
7163
}
7264

7365

@@ -87,7 +79,7 @@ - (void)testCorrectEqualityForCppReturnTypesWithVtables
8779
"ar> >={__rep=(?={__long=QQ*}{__short=(?=Cc)[23c]}{__raw=[3Q]})}}}}";
8880

8981
OCMBoxedReturnValueProvider *boxed = [OCMBoxedReturnValueProvider new];
90-
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type2 value:&value valueSize:valueSize]);
82+
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type2 value:NULL valueSize:0]);
9183
}
9284

9385

@@ -98,8 +90,7 @@ - (void)testCorrectEqualityForStructureWithUnknownName
9890
const char *type2 = "{CLLocationCoordinate2D=dd}";
9991

10092
OCMBoxedReturnValueProvider *boxed = [OCMBoxedReturnValueProvider new];
101-
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type2 value:&value valueSize:valueSize]);
102-
93+
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type2 value:NULL valueSize:0]);
10394
}
10495

10596

@@ -124,8 +115,7 @@ - (void)testCorrectEqualityForStructureWithoutName
124115
"pressed_pair<GURL *, std::__1::default_delete<GURL> >=^{GURL}}}}";
125116

126117
OCMBoxedReturnValueProvider *boxed = [OCMBoxedReturnValueProvider new];
127-
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type2 value:&value valueSize:valueSize]);
128-
118+
XCTAssertTrue([boxed isMethodReturnType:type1 compatibleWithValueType:type2 value:NULL valueSize:0]);
129119
}
130120

131121
@end

Source/OCMockTests/OCMockObjectMacroTests.m

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,19 @@ - (void)testSetsUpStubReturningNilForIdReturnType
195195
{
196196
id mock = OCMPartialMock([NSArray arrayWithObject:@"Foo"]);
197197

198-
OCMExpect([mock lastObject]).andReturn(nil);
199-
XCTAssertNil([mock lastObject], @"Should have returned stubbed value");
200-
201-
OCMExpect([mock lastObject]).andReturn(Nil);
198+
OCMStub([mock lastObject]).andReturn(nil);
202199
XCTAssertNil([mock lastObject], @"Should have returned stubbed value");
203200
}
204201

205202
- (void)testSetsUpStubReturningNilForClassReturnType
206203
{
207204
id mock = OCMPartialMock([[TestClassWithClassReturnMethod alloc] init]);
208205

209-
OCMExpect([mock method]).andReturn(nil);
206+
OCMStub([mock method]).andReturn(Nil);
210207
XCTAssertNil([mock method], @"Should have returned stubbed value");
211208

212-
OCMExpect([mock method]).andReturn(Nil);
209+
// sometimes nil is used where Nil should be used
210+
OCMStub([mock method]).andReturn(nil);
213211
XCTAssertNil([mock method], @"Should have returned stubbed value");
214212
}
215213

0 commit comments

Comments
 (0)