Skip to content

Commit d845394

Browse files
committed
Use regex for description tests with non-deterministic ordering
Attribute dictionaries don't guarantee key order, so the description tests previously matched both permutations via `||`-chained equality and lived in separate `descriptionMultipleAttributes` / `descriptionMultipleTraits` methods. Merge those cases into the parameterized `description` tests with a (String?, Regex<Substring>?) argument pair: deterministic cases still use fast `==`, and only the non-deterministic cases pay for `wholeMatch(of:)`.
1 parent 7422712 commit d845394

1 file changed

Lines changed: 32 additions & 37 deletions

File tree

Tests/OpenGesturesCompatibilityTests/Core/GestureTraitCompatibilityTests.swift

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,24 @@ struct GestureTraitCompatibilityTests {
2323
// MARK: - Description
2424

2525
@Test(arguments: [
26-
(GestureTrait.pan(), "pan"),
27-
(GestureTrait.tap(), "tap"),
28-
(GestureTrait.longPress(), "longPress"),
29-
(GestureTrait.tap(tapCount: 1), "tap {tapCount: 1}"),
30-
(GestureTrait.tap(pointCount: 2), "tap {pointCount: 2}"),
31-
(GestureTrait.longPress(maximumMovement: 10.0), "longPress {maximumMovement: 10.0}"),
32-
])
33-
func description(_ trait: GestureTrait, _ expectedDescription: String) {
34-
#expect("\(trait)" == expectedDescription)
35-
}
36-
37-
@Test
38-
func descriptionMultipleAttributes() {
39-
let trait = GestureTrait.tap(tapCount: 1, pointCount: 3)
26+
(GestureTrait.pan(), "pan", nil),
27+
(GestureTrait.tap(), "tap", nil),
28+
(GestureTrait.longPress(), "longPress", nil),
29+
(GestureTrait.tap(tapCount: 1), "tap {tapCount: 1}", nil),
30+
(GestureTrait.tap(pointCount: 2), "tap {pointCount: 2}", nil),
31+
(GestureTrait.longPress(maximumMovement: 10.0), "longPress {maximumMovement: 10.0}", nil),
32+
// Dictionary ordering of attributes is non-deterministic, so accept either order.
33+
(GestureTrait.tap(tapCount: 1, pointCount: 3), nil, #/tap \{(?:tapCount: 1, pointCount: 3|pointCount: 3, tapCount: 1)\}/#),
34+
] as [(GestureTrait, String?, Regex<Substring>?)])
35+
func description(_ trait: GestureTrait, _ expected: String?, _ expectedRegex: Regex<Substring>?) throws {
4036
let description = "\(trait)"
41-
// Dictionary ordering of attributes is non-deterministic
42-
#expect(description == "tap {tapCount: 1, pointCount: 3}" || description == "tap {pointCount: 3, tapCount: 1}")
37+
if let expectedRegex {
38+
_ = try #require(description.wholeMatch(of: expectedRegex))
39+
} else if let expected {
40+
#expect(description == expected)
41+
} else {
42+
Issue.record("neither expected nor expectedRegex is set")
43+
}
4344
}
4445

4546
// MARK: - GestureTrait Factory Methods
@@ -178,27 +179,21 @@ struct GestureTraitCollectionCompatibilityTests {
178179
}
179180

180181
@Test(arguments: [
181-
(GestureTraitCollection.withTrait(.pan()), "[pan]"),
182-
(GestureTraitCollection.withTrait(.tap(tapCount: 1)), "[tap {tapCount: 1}]"),
183-
])
184-
func description(_ collection: GestureTraitCollection, _ expectedDescription: String) {
185-
#expect("\(collection)" == expectedDescription)
186-
}
187-
188-
@Test
189-
func descriptionMultipleAttributes() {
190-
let collection = GestureTraitCollection.withTrait(.tap(tapCount: 1, pointCount: 3))
182+
(GestureTraitCollection.withTrait(.pan()), "[pan]", nil),
183+
(GestureTraitCollection.withTrait(.tap(tapCount: 1)), "[tap {tapCount: 1}]", nil),
184+
// Dictionary ordering of attributes is non-deterministic, so accept either order.
185+
(GestureTraitCollection.withTrait(.tap(tapCount: 1, pointCount: 3)), nil, #/\[tap \{(?:tapCount: 1, pointCount: 3|pointCount: 3, tapCount: 1)\}\]/#),
186+
// Dictionary ordering of traits is non-deterministic, so accept either order.
187+
(GestureTraitCollection(traits: [.pan(), .tap()]), nil, #/\[(?:tap, pan|pan, tap)\]/#),
188+
] as [(GestureTraitCollection, String?, Regex<Substring>?)])
189+
func description(_ collection: GestureTraitCollection, _ expected: String?, _ expectedRegex: Regex<Substring>?) throws {
191190
let description = "\(collection)"
192-
// Dictionary ordering of attributes is non-deterministic
193-
#expect(description == "[tap {tapCount: 1, pointCount: 3}]" || description == "[tap {pointCount: 3, tapCount: 1}]")
194-
195-
}
196-
197-
@Test
198-
func descriptionMultipleTraits() {
199-
let collection = GestureTraitCollection(traits: [.pan(), .tap()])
200-
let description = "\(collection)"
201-
// Dictionary ordering of traits is non-deterministic
202-
#expect(description == "[tap, pan]" || description == "[pan, tap]")
191+
if let expectedRegex {
192+
_ = try #require(description.wholeMatch(of: expectedRegex))
193+
} else if let expected {
194+
#expect(description == expected)
195+
} else {
196+
Issue.record("neither expected nor expectedRegex is set")
197+
}
203198
}
204199
}

0 commit comments

Comments
 (0)