Commit d290a035 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Implement OCMockComplexTypeHelper as a NSProxy subclass

The class OCMockComplexTypeHelper does not really need any of the
behaviour coming from GTMLightweightProxy, so reimplement it as a
subclass of NSProxy directly.

Bug: none
Change-Id: If66eca04df012853d0e619c44ca3f5bc18f5ffdf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1903135
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Auto-Submit: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713485}
parent 55a751d5
...@@ -67,7 +67,6 @@ source_set("ocmock_support") { ...@@ -67,7 +67,6 @@ source_set("ocmock_support") {
] ]
public_deps = [ public_deps = [
"//third_party/google_toolbox_for_mac",
"//third_party/ocmock", "//third_party/ocmock",
] ]
......
...@@ -3,9 +3,3 @@ include_rules = [ ...@@ -3,9 +3,3 @@ include_rules = [
"+net", "+net",
"+third_party/ocmock", "+third_party/ocmock",
] ]
specific_include_rules = {
"ocmock_complex_type_helper\.h": [
"+third_party/google_toolbox_for_mac/src/Foundation/GTMLightweightProxy.h",
],
}
...@@ -5,20 +5,16 @@ ...@@ -5,20 +5,16 @@
#ifndef IOS_TESTING_OCMOCK_COMPLEX_TYPE_HELPER_H_ #ifndef IOS_TESTING_OCMOCK_COMPLEX_TYPE_HELPER_H_
#define IOS_TESTING_OCMOCK_COMPLEX_TYPE_HELPER_H_ #define IOS_TESTING_OCMOCK_COMPLEX_TYPE_HELPER_H_
#import "third_party/google_toolbox_for_mac/src/Foundation/GTMLightweightProxy.h"
#import "third_party/ocmock/OCMock/OCMock.h" #import "third_party/ocmock/OCMock/OCMock.h"
// OCMock cannot check scalar arguments to method as it requires NSObjects to // OCMock cannot check scalar arguments to method as it requires NSObjects to
// work its magic. This class tries to alleviate this restriction in a crude // work its magic. This class tries to alleviate this restriction in a crude
// way. For an example of use, see the associated unittest class. // way. For an example of use, see the associated unittest class.
@interface OCMockComplexTypeHelper : GTMLightweightProxy @interface OCMockComplexTypeHelper : NSProxy
// As opposed to its parent class the OCMockComplexTypeHelper retains its // Init OCMockComplexTypeHelper with a represented object (retained).
// represented object. - (instancetype)initWithRepresentedObject:(OCMockObject*)object;
- (instancetype)initWithRepresentedObject:(id)object;
// Registers a block to be called when a selector is called. // Registers a block to be called when a selector is called.
- (void)onSelector:(SEL)selector callBlockExpectation:(id)block; - (void)onSelector:(SEL)selector callBlockExpectation:(id)block;
// Unregisters the block associated to the selector.
- (void)removeBlockExpectationOnSelector:(SEL)selector;
// Returns the block for the given selector. Intended for use by subclasses. // Returns the block for the given selector. Intended for use by subclasses.
- (id)blockForSelector:(SEL)selector; - (id)blockForSelector:(SEL)selector;
@end @end
......
...@@ -12,35 +12,26 @@ ...@@ -12,35 +12,26 @@
#endif #endif
@implementation OCMockComplexTypeHelper { @implementation OCMockComplexTypeHelper {
// Same as the superclass -representedObject, but retained. // The represented object.
OCMockObject* _object; OCMockObject* _object;
// All the blocks registered by selector.
NSMutableDictionary* _blocks; // Dictionary holding blocks registered by selector.
NSMutableDictionary<NSString*, id>* _blocks;
} }
#pragma mark - public methods. #pragma mark - Public methods.
- (instancetype)initWithRepresentedObject:(id)object { - (instancetype)initWithRepresentedObject:(OCMockObject*)object {
if ((self = [super initWithRepresentedObject:object])) DCHECK(object);
_object = object; _object = object;
_blocks = [[NSMutableDictionary alloc] init];
return self; return self;
} }
- (void)onSelector:(SEL)selector callBlockExpectation:(id)block { - (void)onSelector:(SEL)selector callBlockExpectation:(id)block {
if (!_blocks)
_blocks = [[NSMutableDictionary alloc] init];
NSString* key = NSStringFromSelector(selector); NSString* key = NSStringFromSelector(selector);
DCHECK(![_blocks objectForKey:key]) << "Only one expectation per signature"; DCHECK(![_blocks objectForKey:key]) << "Only one expectation per signature";
id value = [block copy]; [_blocks setObject:block forKey:key];
[_blocks setObject:value forKey:key];
}
- (void)removeBlockExpectationOnSelector:(SEL)selector {
NSString* key = NSStringFromSelector(selector);
DCHECK([_blocks objectForKey:key])
<< "No expectation for selector " << base::SysNSStringToUTF8(key);
[_blocks removeObjectForKey:key];
} }
- (id)blockForSelector:(SEL)selector { - (id)blockForSelector:(SEL)selector {
...@@ -54,29 +45,50 @@ ...@@ -54,29 +45,50 @@
#pragma mark - OCMockObject forwarding. #pragma mark - OCMockObject forwarding.
// OCMockObject -respondsToSelector responds NO for the OCMock object specific // OCMockObject -respondsToSelector responds NO for the OCMock object specific
// methods. This confuses the GTMLightweightProxy class. In order to forward // methods. This confuses the NSProxy architecture. In order to forward
// those properly the simplest approach is to forward them explicitely. // those properly the simplest approach is to forward them explicitely.
- (id)stub { - (id)stub {
return [_object stub]; return [_object stub];
} }
- (id)expect { - (id)expect {
return [_object expect]; return [_object expect];
} }
- (id)reject { - (id)reject {
return [_object reject]; return [_object reject];
} }
- (void)verify { - (void)verify {
[_object verify]; [_object verify];
} }
- (void)setExpectationOrderMatters:(BOOL)flag { - (void)setExpectationOrderMatters:(BOOL)flag {
[_object setExpectationOrderMatters:flag]; [_object setExpectationOrderMatters:flag];
} }
#pragma mark - Internal methods. #pragma mark - NSProxy implementation.
- (void)forwardInvocation:(NSInvocation*)invocation {
SEL selector = [invocation selector];
if ([_object respondsToSelector:selector])
[invocation invokeWithTarget:_object];
}
- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
return [_object methodSignatureForSelector:selector];
}
- (void)doesNotRecognizeSelector:(SEL)selector {
[(id)_object doesNotRecognizeSelector:selector];
}
- (BOOL)respondsToSelector:(SEL)selector { - (BOOL)respondsToSelector:(SEL)selector {
DCHECK(![_blocks objectForKey:NSStringFromSelector(selector)]); DCHECK(![_blocks objectForKey:NSStringFromSelector(selector)]);
return [super respondsToSelector:selector]; if (selector == @selector(initWithRepresentedObject:))
return YES;
return [_object respondsToSelector:selector];
} }
@end @end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment