Skip to content

Commit 094dbcd

Browse files
committed
Fix tests.
1 parent 993355b commit 094dbcd

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

BlueprintUI/Sources/Environment/Environment.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ extension Environment: ContextuallyEquivalent {
124124
Logger.logEnvironmentEquivalencyFingerprintEqual(environment: self)
125125
return true
126126
}
127-
if let evaluated = cacheStorage.environmentComparisonCache[other.fingerprint, context] ?? other.cacheStorage.environmentComparisonCache[
128-
fingerprint,
129-
context
130-
] {
127+
if let evaluated = cacheStorage.environmentComparisonCache[fingerprint, other.fingerprint, context] {
131128
Logger.logEnvironmentEquivalencyFingerprintCacheHit(environment: self)
132129
return evaluated
133130
}
@@ -136,7 +133,7 @@ extension Environment: ContextuallyEquivalent {
136133
let keys = Set(values.keys).union(other.values.keys)
137134
for key in keys {
138135
guard key.isEquivalent(self[key], other[key], context) else {
139-
cacheStorage.environmentComparisonCache[other.fingerprint, context] = false
136+
cacheStorage.environmentComparisonCache[fingerprint, other.fingerprint, context] = false
140137
Logger.logEnvironmentEquivalencyCompletedWithNonEquivalence(
141138
environment: self,
142139
key: key,
@@ -148,7 +145,7 @@ extension Environment: ContextuallyEquivalent {
148145
}
149146
Logger.logEnvironmentEquivalencyComparisonEnd(token, environment: self)
150147
Logger.logEnvironmentEquivalencyCompletedWithEquivalence(environment: self, context: context)
151-
cacheStorage.environmentComparisonCache[other.fingerprint, context] = true
148+
cacheStorage.environmentComparisonCache[fingerprint, other.fingerprint, context] = true
152149
return true
153150
}
154151

@@ -160,15 +157,15 @@ extension Environment: ContextuallyEquivalent {
160157
return true
161158
}
162159
let scope = Set(snapshot.values.keys.map(\.objectIdentifier))
163-
if let evaluated = cacheStorage.environmentComparisonCache[snapshot.fingerprint, context, scope] {
160+
if let evaluated = cacheStorage.environmentComparisonCache[fingerprint, snapshot.fingerprint, context, scope] {
164161
Logger.logEnvironmentEquivalencyFingerprintCacheHit(environment: self)
165162
return evaluated
166163
}
167164
Logger.logEnvironmentEquivalencyFingerprintCacheMiss(environment: self)
168165
let token = Logger.logEnvironmentEquivalencyComparisonStart(environment: self)
169166
for (key, value) in snapshot.values {
170167
guard key.isEquivalent(self[key], value, context) else {
171-
cacheStorage.environmentComparisonCache[snapshot.fingerprint, context, scope] = false
168+
cacheStorage.environmentComparisonCache[fingerprint, snapshot.fingerprint, context, scope] = false
172169
Logger.logEnvironmentEquivalencyCompletedWithNonEquivalence(
173170
environment: self,
174171
key: key,
@@ -180,7 +177,7 @@ extension Environment: ContextuallyEquivalent {
180177
}
181178
Logger.logEnvironmentEquivalencyComparisonEnd(token, environment: self)
182179
Logger.logEnvironmentEquivalencyCompletedWithEquivalence(environment: self, context: context)
183-
cacheStorage.environmentComparisonCache[snapshot.fingerprint, context, scope] = true
180+
cacheStorage.environmentComparisonCache[fingerprint, snapshot.fingerprint, context, scope] = true
184181
return true
185182

186183
}
@@ -193,20 +190,29 @@ extension CacheStorage {
193190
fileprivate struct EnvironmentFingerprintCache {
194191

195192
struct Key: Hashable {
196-
let fingerprint: ComparableFingerprint.Value
193+
let lhs: ComparableFingerprint.Value
194+
let rhs: ComparableFingerprint.Value
197195
let scope: Set<ObjectIdentifier>?
196+
197+
init(_ lhs: ComparableFingerprint.Value, _ rhs: ComparableFingerprint.Value, scope: Set<ObjectIdentifier>?) {
198+
// Sort lhs/rhs so we don't have diff results based on caller.
199+
self.lhs = min(lhs, rhs)
200+
self.rhs = max(lhs, rhs)
201+
self.scope = scope
202+
}
198203
}
199204

200205
typealias EquivalencyResult = [EquivalencyContext: Bool]
201206
var storage: [Key: [EquivalencyContext: Bool]] = [:]
202207

203208
public subscript(
204-
fingerprint: ComparableFingerprint,
209+
lhs: ComparableFingerprint,
210+
rhs: ComparableFingerprint,
205211
context: EquivalencyContext,
206212
scope: Set<ObjectIdentifier>? = nil
207213
) -> Bool? {
208214
get {
209-
let key = Key(fingerprint: fingerprint.value, scope: scope)
215+
let key = Key(lhs.value, rhs.value, scope: scope)
210216
if let exact = storage[key]?[context] {
211217
return exact
212218
} else if let allComparisons = storage[key] {
@@ -231,7 +237,7 @@ extension CacheStorage {
231237
}
232238
}
233239
set {
234-
storage[Key(fingerprint: fingerprint.value, scope: scope), default: [:]][context] = newValue
240+
storage[Key(lhs.value, rhs.value, scope: scope), default: [:]][context] = newValue
235241
}
236242
}
237243

BlueprintUI/Tests/ValidatingCacheTests.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,28 @@ struct EnvironmentValidatingCacheTests {
7878
var environment = Environment()
7979
environment[ExampleKey.self] = 1
8080
let one = cache.retrieveOrCreate(key: "Hello", environment: environment, context: .all) {
81-
"One"
81+
_ = $0[ExampleKey.self]
82+
return "One"
8283
}
8384
#expect(one == "One")
8485

8586
let two = cache.retrieveOrCreate(key: "Hello", environment: environment, context: .all) {
86-
"Two"
87+
_ = $0[ExampleKey.self]
88+
return "Two"
8789
}
8890
#expect(two == "One")
8991

9092
let three = cache.retrieveOrCreate(key: "KeyMiss", environment: environment, context: .all) {
91-
"Three"
93+
_ = $0[ExampleKey.self]
94+
return "Three"
9295
}
9396
#expect(three == "Three")
9497

9598
var differentEnvironment = environment
9699
differentEnvironment[ExampleKey.self] = 2
97100
let four = cache.retrieveOrCreate(key: "Hello", environment: differentEnvironment, context: .all) {
98-
"Four"
101+
_ = $0[ExampleKey.self]
102+
return "Four"
99103
}
100104
#expect(four == "Four")
101105
}
@@ -116,7 +120,8 @@ struct EnvironmentAndValueValidatingCacheTests {
116120
validationValue: "Validate",
117121
context: .all
118122
) {
119-
"One"
123+
_ = $0[ExampleKey.self]
124+
return "One"
120125
}
121126
#expect(one == "One")
122127

@@ -126,7 +131,8 @@ struct EnvironmentAndValueValidatingCacheTests {
126131
validationValue: "Validate",
127132
context: .all
128133
) {
129-
"Two"
134+
_ = $0[ExampleKey.self]
135+
return "Two"
130136
}
131137
#expect(two == "One")
132138

@@ -136,7 +142,8 @@ struct EnvironmentAndValueValidatingCacheTests {
136142
validationValue: "Validate",
137143
context: .all
138144
) {
139-
"Three"
145+
_ = $0[ExampleKey.self]
146+
return "Three"
140147
}
141148
#expect(three == "Three")
142149

@@ -148,7 +155,8 @@ struct EnvironmentAndValueValidatingCacheTests {
148155
validationValue: "Validate",
149156
context: .all
150157
) {
151-
"Four"
158+
_ = $0[ExampleKey.self]
159+
return "Four"
152160
}
153161
#expect(four == "Four")
154162

@@ -157,7 +165,7 @@ struct EnvironmentAndValueValidatingCacheTests {
157165
environment: differentEnvironment,
158166
validationValue: "Invalid",
159167
context: .all
160-
) {
168+
) { _ in
161169
"Five"
162170
}
163171
#expect(five == "Five")
@@ -186,7 +194,7 @@ struct EnvironmentAndValueValidatingCacheTests {
186194
environment: environment,
187195
validationValue: elementOne,
188196
context: .elementSizing
189-
) {
197+
) { _ in
190198
sizeForElement(element: elementOne)
191199
}
192200
#expect(firstSize == CGSize(width: 50, height: 100))
@@ -198,7 +206,7 @@ struct EnvironmentAndValueValidatingCacheTests {
198206
environment: environment,
199207
validationValue: elementTwo,
200208
context: .elementSizing
201-
) {
209+
) { _ in
202210
sizeForElement(element: elementTwo)
203211
}
204212
#expect(secondSize == CGSize(width: 20, height: 100))
@@ -210,7 +218,7 @@ struct EnvironmentAndValueValidatingCacheTests {
210218
environment: environment,
211219
validationValue: elementOne,
212220
context: .elementSizing
213-
) {
221+
) { _ in
214222
sizeForElement(element: elementOne)
215223
}
216224
#expect(firstSizeAgain == CGSize(width: 50, height: 100))
@@ -222,7 +230,7 @@ struct EnvironmentAndValueValidatingCacheTests {
222230
environment: environment,
223231
validationValue: elementOneModified,
224232
context: .elementSizing
225-
) {
233+
) { _ in
226234
sizeForElement(element: elementOneModified)
227235
}
228236
#expect(firstSizeWithNewElement == CGSize(width: 110, height: 100))
@@ -234,7 +242,7 @@ struct EnvironmentAndValueValidatingCacheTests {
234242
environment: environment,
235243
validationValue: elementOneModified,
236244
context: .elementSizing
237-
) {
245+
) { _ in
238246
sizeForElement(element: elementOneModified)
239247
}
240248
#expect(firstSizeWithNewElementAgain == CGSize(width: 110, height: 100))
@@ -246,7 +254,7 @@ struct EnvironmentAndValueValidatingCacheTests {
246254
environment: environment,
247255
validationValue: elementOne,
248256
context: .elementSizing
249-
) {
257+
) { _ in
250258
sizeForElement(element: elementOne)
251259
}
252260
#expect(originalFirstSizeAgain == CGSize(width: 50, height: 100))
@@ -259,7 +267,7 @@ struct EnvironmentAndValueValidatingCacheTests {
259267
environment: environment,
260268
validationValue: elementOneModified,
261269
context: .elementSizing
262-
) {
270+
) { _ in
263271
sizeForElement(element: elementOne)
264272
}
265273
#expect(firstSizeWithNewEnvironment == CGSize(width: 50, height: 100))

0 commit comments

Comments
 (0)