-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement withContext * Add ConfidenceContextProvider protocol * Remove context on child instance * Remove clear from Contextual
- Loading branch information
1 parent
56f8130
commit d0dddee
Showing
5 changed files
with
230 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Foundation | ||
|
||
/// A Contextual implementer returns the current context | ||
public protocol ConfidenceContextProvider { | ||
func getContext() -> ConfidenceStruct | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import Foundation | ||
|
||
/// A Contextual implementer maintains context data and can create child instances | ||
/// A Contextual implementer maintains local context data and can create child instances | ||
/// that can still access their parent's data | ||
public protocol Contextual { | ||
var context: ConfidenceStruct { get set } | ||
|
||
/// Each ConfidenceContextProvider returns local data reconciled with parents' data. Local data has precedence | ||
public protocol Contextual: ConfidenceContextProvider { | ||
/// Adds/override entry to local data | ||
func updateContextEntry(key: String, value: ConfidenceValue) | ||
/// Removes entry from local data | ||
/// It hides entries with this key from parents' data (without modifying parents' data) | ||
func removeContextEntry(key: String) | ||
func clearContext() | ||
/// Creates a child Contextual instance that still has access | ||
/// to its parent context | ||
/// Creates a child Contextual instance that maintains access to its parent's data | ||
func withContext(_ context: ConfidenceStruct) -> Self | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import Confidence | ||
import XCTest | ||
|
||
final class ConfidenceTests: XCTestCase { | ||
func testWithContext() { | ||
let confidenceParent = Confidence.init( | ||
clientSecret: "", | ||
timeout: TimeInterval(), | ||
region: .europe, | ||
initializationStrategy: .activateAndFetchAsync, | ||
context: ["k1": ConfidenceValue(string: "v1")] | ||
) | ||
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext( | ||
["k2": ConfidenceValue(string: "v2")] | ||
) | ||
let expected = [ | ||
"k1": ConfidenceValue(string: "v1"), | ||
"k2": ConfidenceValue(string: "v2") | ||
] | ||
XCTAssertEqual(confidenceChild.getContext(), expected) | ||
} | ||
|
||
func testWithContextUpdateParent() { | ||
let confidenceParent = Confidence.init( | ||
clientSecret: "", | ||
timeout: TimeInterval(), | ||
region: .europe, | ||
initializationStrategy: .activateAndFetchAsync, | ||
context: ["k1": ConfidenceValue(string: "v1")] | ||
) | ||
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext( | ||
["k2": ConfidenceValue(string: "v2")] | ||
) | ||
confidenceParent.updateContextEntry( | ||
key: "k3", | ||
value: ConfidenceValue(string: "v3")) | ||
let expected = [ | ||
"k1": ConfidenceValue(string: "v1"), | ||
"k2": ConfidenceValue(string: "v2"), | ||
"k3": ConfidenceValue(string: "v3"), | ||
] | ||
XCTAssertEqual(confidenceChild.getContext(), expected) | ||
} | ||
|
||
func testUpdateLocalContext() { | ||
let confidence = Confidence.init( | ||
clientSecret: "", | ||
timeout: TimeInterval(), | ||
region: .europe, | ||
initializationStrategy: .activateAndFetchAsync, | ||
context: ["k1": ConfidenceValue(string: "v1")] | ||
) | ||
confidence.updateContextEntry( | ||
key: "k1", | ||
value: ConfidenceValue(string: "v3")) | ||
let expected = [ | ||
"k1": ConfidenceValue(string: "v3"), | ||
] | ||
XCTAssertEqual(confidence.getContext(), expected) | ||
} | ||
|
||
func testUpdateLocalContextWithoutOverride() { | ||
let confidenceParent = Confidence.init( | ||
clientSecret: "", | ||
timeout: TimeInterval(), | ||
region: .europe, | ||
initializationStrategy: .activateAndFetchAsync, | ||
context: ["k1": ConfidenceValue(string: "v1")] | ||
) | ||
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext( | ||
["k2": ConfidenceValue(string: "v2")] | ||
) | ||
confidenceChild.updateContextEntry( | ||
key: "k2", | ||
value: ConfidenceValue(string: "v4")) | ||
let expected = [ | ||
"k1": ConfidenceValue(string: "v1"), | ||
"k2": ConfidenceValue(string: "v4"), | ||
] | ||
XCTAssertEqual(confidenceChild.getContext(), expected) | ||
} | ||
|
||
func testUpdateParentContextWithOverride() { | ||
let confidenceParent = Confidence.init( | ||
clientSecret: "", | ||
timeout: TimeInterval(), | ||
region: .europe, | ||
initializationStrategy: .activateAndFetchAsync, | ||
context: ["k1": ConfidenceValue(string: "v1")] | ||
) | ||
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext( | ||
["k2": ConfidenceValue(string: "v2")] | ||
) | ||
confidenceParent.updateContextEntry( | ||
key: "k2", | ||
value: ConfidenceValue(string: "v4")) | ||
let expected = [ | ||
"k1": ConfidenceValue(string: "v1"), | ||
"k2": ConfidenceValue(string: "v2"), | ||
] | ||
XCTAssertEqual(confidenceChild.getContext(), expected) | ||
} | ||
|
||
func testRemoveContextEntry() { | ||
let confidence = Confidence.init( | ||
clientSecret: "", | ||
timeout: TimeInterval(), | ||
region: .europe, | ||
initializationStrategy: .activateAndFetchAsync, | ||
context: [ | ||
"k1": ConfidenceValue(string: "v1"), | ||
"k2": ConfidenceValue(string: "v2") | ||
] | ||
) | ||
confidence.removeContextEntry(key: "k2") | ||
let expected = [ | ||
"k1": ConfidenceValue(string: "v1") | ||
] | ||
XCTAssertEqual(confidence.getContext(), expected) | ||
} | ||
|
||
func testRemoveContextEntryFromParent() { | ||
let confidenceParent = Confidence.init( | ||
clientSecret: "", | ||
timeout: TimeInterval(), | ||
region: .europe, | ||
initializationStrategy: .activateAndFetchAsync, | ||
context: ["k1": ConfidenceValue(string: "v1")] | ||
) | ||
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext( | ||
["k2": ConfidenceValue(string: "v2")] | ||
) | ||
confidenceChild.removeContextEntry(key: "k1") | ||
let expected = [ | ||
"k2": ConfidenceValue(string: "v2") | ||
] | ||
XCTAssertEqual(confidenceChild.getContext(), expected) | ||
} | ||
|
||
func testRemoveContextEntryFromParentAndChild() { | ||
let confidenceParent = Confidence.init( | ||
clientSecret: "", | ||
timeout: TimeInterval(), | ||
region: .europe, | ||
initializationStrategy: .activateAndFetchAsync, | ||
context: ["k1": ConfidenceValue(string: "v1")] | ||
) | ||
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext( | ||
[ | ||
"k2": ConfidenceValue(string: "v2"), | ||
"k1": ConfidenceValue(string: "v3"), | ||
] | ||
) | ||
confidenceChild.removeContextEntry(key: "k1") | ||
let expected = [ | ||
"k2": ConfidenceValue(string: "v2") | ||
] | ||
XCTAssertEqual(confidenceChild.getContext(), expected) | ||
} | ||
|
||
func testRemoveContextEntryFromParentAndChildThenUpdate() { | ||
let confidenceParent = Confidence.init( | ||
clientSecret: "", | ||
timeout: TimeInterval(), | ||
region: .europe, | ||
initializationStrategy: .activateAndFetchAsync, | ||
context: ["k1": ConfidenceValue(string: "v1")] | ||
) | ||
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext( | ||
[ | ||
"k2": ConfidenceValue(string: "v2"), | ||
"k1": ConfidenceValue(string: "v3"), | ||
] | ||
) | ||
confidenceChild.removeContextEntry(key: "k1") | ||
confidenceChild.updateContextEntry(key: "k1", value: ConfidenceValue(string: "v4")) | ||
let expected = [ | ||
"k2": ConfidenceValue(string: "v2"), | ||
"k1": ConfidenceValue(string: "v4"), | ||
] | ||
XCTAssertEqual(confidenceChild.getContext(), expected) | ||
} | ||
} |