-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]: Reliant/Swift: Context substitution #18
base: feature/reliant-in-swift
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,17 +54,25 @@ struct ContextNeedingContext : ReliantContext { | |
return ContextNeedingContext() | ||
} | ||
} | ||
class SubWaver : Waver { | ||
func wave(reason: String) -> String { | ||
return "Substitute waving" | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
class SubstituteContext : SimpleReferenceContext { | ||
override init() { | ||
super.init() | ||
waver = SubWaver() | ||
} | ||
} | ||
|
||
class ReliantFrameworkTests: XCTestCase { | ||
|
||
override func setUp() { | ||
super.setUp() | ||
ReliantFrameworkTestsHelper.sharedInsance.reset() | ||
ContextCache.sharedInstance.cache = Dictionary() | ||
ContextCache.standard.removeAll() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See, this I would like to avoid, but I don't know how, since we are tight to the global relyOn function... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll always have global state, I think, even if we opt to bind the the |
||
} | ||
|
||
func testRelyOnReturnsSameInsanceEveryTimeForReferenceTypes() { | ||
|
@@ -84,6 +92,17 @@ class ReliantFrameworkTests: XCTestCase { | |
XCTAssertEqual(needed.needy.decorateGreeting(), "Oh! Hello Needy") | ||
} | ||
|
||
|
||
func testSubstitutions() { | ||
relyOnSubstitute(SimpleReferenceContext)(SubstituteContext) | ||
XCTAssertTrue(ContextCache.substitutions.contains({ (key, value) -> Bool in | ||
return key == String(SimpleReferenceContext) && value == SubstituteContext.self | ||
})) | ||
|
||
let context = relyOn(SimpleReferenceContext) | ||
|
||
// Failing test. | ||
// The createContext() function is static and thus final. Actual context type seems | ||
// to be correct, but createContext() is called on original context class | ||
XCTAssertTrue(context is SubstituteContext) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As noted in the summary, having to subclass feels icky and rules out the use of struct-based contexts. It also requires class members to be mutable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we work through protocols and delegation instead?
Eg: protocol Waver, structs RealWaver and SubWaver, SubWaver initialises RealWaver and delegates to it when needed, overriding it's behaviour when wanted? It would mean people need some knowledge on how to architect this in, but it's better than having the above mentioned restrictions...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the current method is pretty much a no-go. I think there are still some alternatives to explore though.