diff --git a/AspectsDemo/AspectsDemoTests/AspectsDemoTests.m b/AspectsDemo/AspectsDemoTests/AspectsDemoTests.m index 2bc32fc..2a8b89d 100644 --- a/AspectsDemo/AspectsDemoTests/AspectsDemoTests.m +++ b/AspectsDemo/AspectsDemoTests/AspectsDemoTests.m @@ -307,6 +307,23 @@ - (void)testDoubleReturn { XCTAssertTrue([aspect remove], @"Must be able to deregister"); } +- (void)testDoubleReturnInstead { + TestClass *testClass = [TestClass new]; + double previousExpectedValue = [testClass callReturnsDouble]; + double expectedValue = 3.5; + + id aspect = [testClass aspect_hookSelector:@selector(callReturnsDouble) withOptions:AspectPositionInstead usingBlock:^(id info){ + double toReturn = 3.5; + void *ptr = &toReturn; + [info.originalInvocation setReturnValue:ptr]; + }error:NULL]; + double actualValue = [testClass callReturnsDouble]; + + XCTAssertNotEqual(previousExpectedValue, actualValue, @"Must not return what it returned before we called our Instead"); + XCTAssertEqual(expectedValue, actualValue, @"Must be equal"); + XCTAssertTrue([aspect remove], @"Must be able to deregister"); +} + - (void)testLongLongReturn { TestClass *testClass = [TestClass new]; long long d1 = [testClass callReturnsLongLong]; diff --git a/README.md b/README.md index 3aa9b59..152ba1c 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ -Aspects v1.4.2 [![Build Status](https://travis-ci.org/steipete/Aspects.svg?branch=master)](https://travis-ci.org/steipete/Aspects) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) +Aspects v1.4.2 🪝 - AOP for Objective-C (10k+ stars) [![Build Status](https://travis-ci.org/steipete/Aspects.svg?branch=master)](https://travis-ci.org/steipete/Aspects) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) ============== A delightful, simple library for aspect oriented programming by [@steipete](http://twitter.com/steipete). **Think of Aspects as method swizzling on steroids. It allows you to add code to existing methods per class or per instance**, whilst thinking of the insertion point e.g. before/instead/after. Aspects automatically deals with calling super and is easier to use than regular method swizzling. -This is stable and used in hundreds of apps since it's part of [PSPDFKit, an iOS PDF framework that ships with apps like Dropbox or Evernote](http://pspdfkit.com), and now I finally made it open source. +Aspects hooks deep into the class hierarchy and creates dynamic subclasses, much like KVO. There's known issues with this approach, and to this date (February 2019) **I STRICTLY DO NOT RECOMMEND TO USE Aspects IN PRODUCTION CODE**. We use it for partial test mocks in, [PSPDFKit, an iOS PDF framework that ships with apps like Dropbox or Evernote](http://pspdfkit.com), it's also very useful for quickly hacking something up. + +Aspects uses `_objc_msgForward` which causes issues with other code that uses message forwarding. Aspects extends `NSObject` with the following methods: