diff --git a/Classes/Stubbing/KWIntercept.m b/Classes/Stubbing/KWIntercept.m index 79107895..7d3f4f57 100644 --- a/Classes/Stubbing/KWIntercept.m +++ b/Classes/Stubbing/KWIntercept.m @@ -37,6 +37,8 @@ void KWInterceptedDealloc(id anObject, SEL aSelector); Class KWInterceptedClass(id anObject, SEL aSelector); Class KWInterceptedSuperclass(id anObject, SEL aSelector); +BOOL KWInterceptedSupportsSecureCodingTrue(id anObject, SEL aSelector); +BOOL KWInterceptedSupportsSecureCodingFalse(id anObject, SEL aSelector); #pragma mark - Getting Forwarding Implementations @@ -120,6 +122,18 @@ Class KWInterceptClassForCanonicalClass(Class canonicalClass) { Class interceptMetaClass = object_getClass(interceptClass); class_addMethod(interceptMetaClass, @selector(forwardInvocation:), (IMP)KWInterceptedForwardInvocation, "v@:@"); + SEL supportsSecureCodingSelector = @selector(supportsSecureCoding); + Method supportsSecureCodingMethod = class_getClassMethod(canonicalClass, supportsSecureCodingSelector); + if (supportsSecureCodingMethod != NULL) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" + BOOL support = (BOOL)[(id)canonicalClass performSelector:supportsSecureCodingSelector]; +#pragma clang diagnostic pop + IMP supportsSecureCodingIMP = support ? (IMP)KWInterceptedSupportsSecureCodingTrue + : (IMP)KWInterceptedSupportsSecureCodingFalse; + class_addMethod(interceptMetaClass, supportsSecureCodingSelector, supportsSecureCodingIMP, "B@:"); + } + return interceptClass; } @@ -223,6 +237,14 @@ Class KWInterceptedSuperclass(id anObject, SEL aSelector) { return originalSuperclass; } +BOOL KWInterceptedSupportsSecureCodingTrue(id anObject, SEL aSelector) { + return YES; +} + +BOOL KWInterceptedSupportsSecureCodingFalse(id anObject, SEL aSelector) { + return NO; +} + #pragma mark - Managing Objects Stubs void KWAssociateObjectStub(id anObject, KWStub *aStub, BOOL overrideExisting) { diff --git a/Tests/KWRealObjectStubTest.m b/Tests/KWRealObjectStubTest.m index 4ed9bd06..a3494842 100644 --- a/Tests/KWRealObjectStubTest.m +++ b/Tests/KWRealObjectStubTest.m @@ -195,6 +195,15 @@ - (void)testItShouldStubWithBlock { XCTAssertEqual([cruiser classification], @"Enterprise", @"expected method to be stubbed with block"); } +- (void)testStubSecureCodingOfDateClass { + NSDate *date = [NSDate date]; + [NSDate stub:@selector(date) andReturn:date]; + if (@available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)) { + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:date requiringSecureCoding:YES error:NULL]; + XCTAssertNotNil(data, @"expected stubbed class to be able to use secure coding"); + } +} + @end #endif // #if KW_TESTS_ENABLED