From d845394631d996326ae7ce1fd2afdbf9843d439f Mon Sep 17 00:00:00 2001 From: Kyle Date: Thu, 16 Apr 2026 03:50:19 +0800 Subject: [PATCH] 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?) argument pair: deterministic cases still use fast `==`, and only the non-deterministic cases pay for `wholeMatch(of:)`. --- .../Core/GestureTraitCompatibilityTests.swift | 69 +++++++++---------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/Tests/OpenGesturesCompatibilityTests/Core/GestureTraitCompatibilityTests.swift b/Tests/OpenGesturesCompatibilityTests/Core/GestureTraitCompatibilityTests.swift index 33e319c..c8d5886 100644 --- a/Tests/OpenGesturesCompatibilityTests/Core/GestureTraitCompatibilityTests.swift +++ b/Tests/OpenGesturesCompatibilityTests/Core/GestureTraitCompatibilityTests.swift @@ -23,23 +23,24 @@ struct GestureTraitCompatibilityTests { // MARK: - Description @Test(arguments: [ - (GestureTrait.pan(), "pan"), - (GestureTrait.tap(), "tap"), - (GestureTrait.longPress(), "longPress"), - (GestureTrait.tap(tapCount: 1), "tap {tapCount: 1}"), - (GestureTrait.tap(pointCount: 2), "tap {pointCount: 2}"), - (GestureTrait.longPress(maximumMovement: 10.0), "longPress {maximumMovement: 10.0}"), - ]) - func description(_ trait: GestureTrait, _ expectedDescription: String) { - #expect("\(trait)" == expectedDescription) - } - - @Test - func descriptionMultipleAttributes() { - let trait = GestureTrait.tap(tapCount: 1, pointCount: 3) + (GestureTrait.pan(), "pan", nil), + (GestureTrait.tap(), "tap", nil), + (GestureTrait.longPress(), "longPress", nil), + (GestureTrait.tap(tapCount: 1), "tap {tapCount: 1}", nil), + (GestureTrait.tap(pointCount: 2), "tap {pointCount: 2}", nil), + (GestureTrait.longPress(maximumMovement: 10.0), "longPress {maximumMovement: 10.0}", nil), + // Dictionary ordering of attributes is non-deterministic, so accept either order. + (GestureTrait.tap(tapCount: 1, pointCount: 3), nil, #/tap \{(?:tapCount: 1, pointCount: 3|pointCount: 3, tapCount: 1)\}/#), + ] as [(GestureTrait, String?, Regex?)]) + func description(_ trait: GestureTrait, _ expected: String?, _ expectedRegex: Regex?) throws { let description = "\(trait)" - // Dictionary ordering of attributes is non-deterministic - #expect(description == "tap {tapCount: 1, pointCount: 3}" || description == "tap {pointCount: 3, tapCount: 1}") + if let expectedRegex { + _ = try #require(description.wholeMatch(of: expectedRegex)) + } else if let expected { + #expect(description == expected) + } else { + Issue.record("neither expected nor expectedRegex is set") + } } // MARK: - GestureTrait Factory Methods @@ -178,27 +179,21 @@ struct GestureTraitCollectionCompatibilityTests { } @Test(arguments: [ - (GestureTraitCollection.withTrait(.pan()), "[pan]"), - (GestureTraitCollection.withTrait(.tap(tapCount: 1)), "[tap {tapCount: 1}]"), - ]) - func description(_ collection: GestureTraitCollection, _ expectedDescription: String) { - #expect("\(collection)" == expectedDescription) - } - - @Test - func descriptionMultipleAttributes() { - let collection = GestureTraitCollection.withTrait(.tap(tapCount: 1, pointCount: 3)) + (GestureTraitCollection.withTrait(.pan()), "[pan]", nil), + (GestureTraitCollection.withTrait(.tap(tapCount: 1)), "[tap {tapCount: 1}]", nil), + // Dictionary ordering of attributes is non-deterministic, so accept either order. + (GestureTraitCollection.withTrait(.tap(tapCount: 1, pointCount: 3)), nil, #/\[tap \{(?:tapCount: 1, pointCount: 3|pointCount: 3, tapCount: 1)\}\]/#), + // Dictionary ordering of traits is non-deterministic, so accept either order. + (GestureTraitCollection(traits: [.pan(), .tap()]), nil, #/\[(?:tap, pan|pan, tap)\]/#), + ] as [(GestureTraitCollection, String?, Regex?)]) + func description(_ collection: GestureTraitCollection, _ expected: String?, _ expectedRegex: Regex?) throws { let description = "\(collection)" - // Dictionary ordering of attributes is non-deterministic - #expect(description == "[tap {tapCount: 1, pointCount: 3}]" || description == "[tap {pointCount: 3, tapCount: 1}]") - - } - - @Test - func descriptionMultipleTraits() { - let collection = GestureTraitCollection(traits: [.pan(), .tap()]) - let description = "\(collection)" - // Dictionary ordering of traits is non-deterministic - #expect(description == "[tap, pan]" || description == "[pan, tap]") + if let expectedRegex { + _ = try #require(description.wholeMatch(of: expectedRegex)) + } else if let expected { + #expect(description == expected) + } else { + Issue.record("neither expected nor expectedRegex is set") + } } }