From 05ed2cd2e278d33662fe544d7a64826234907b88 Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 17 Sep 2024 22:51:36 +0800 Subject: [PATCH] Fix EnvironmentValuesTest on iOS 18 --- Package.swift | 1 + .../Environment/EnvironmentValuesTest.swift | 31 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index 33bd9482..f58cae1d 100644 --- a/Package.swift +++ b/Package.swift @@ -72,6 +72,7 @@ var sharedSwiftSettings: [SwiftSetting] = [ .define("OPENSWIFTUI_SUPPRESS_DEPRECATED_WARNINGS"), .define("OPENSWIFTUI_RELEASE_\(releaseVersion)"), .swiftLanguageMode(.v5), + .enableUpcomingFeature("BareSlashRegexLiterals"), ] if releaseVersion >= 2021 { diff --git a/Tests/OpenSwiftUICompatibilityTests/Data/Environment/EnvironmentValuesTest.swift b/Tests/OpenSwiftUICompatibilityTests/Data/Environment/EnvironmentValuesTest.swift index aa530451..7b0d018f 100644 --- a/Tests/OpenSwiftUICompatibilityTests/Data/Environment/EnvironmentValuesTest.swift +++ b/Tests/OpenSwiftUICompatibilityTests/Data/Environment/EnvironmentValuesTest.swift @@ -45,23 +45,44 @@ struct EnvironmentValuesTest { return } #endif + func compareDictDescription(result: String, initial: String, expectNew: String) { + guard #available(iOS 16.0, macOS 13.0, *) else { + #expect(result == expectNew) + return + } + guard !expectNew.isEmpty else { + #expect(result == initial) + return + } + guard let expectNewContent = expectNew.wholeMatch(of: /\[(.*)\]/)?.output.1 else { + Issue.record("Non empty string and does not contain [] in expectNew") + return + } + let expectResult = "[\(expectNewContent), " + initial.dropFirst() + #expect(result == expectResult) + } + var env = EnvironmentValues() - #expect(env.description == "[]") + + let initialDescription = env.description + if #unavailable(iOS 18, macOS 15) { + #expect(env.description == "[]") + } var bool = env[BoolKey.self] #expect(bool == BoolKey.defaultValue) - #expect(env.description == "[]") + compareDictDescription(result: env.description, initial: initialDescription, expectNew: "") env[BoolKey.self] = bool - #expect(env.description == "[\(BoolKey.name) = \(bool)]") + compareDictDescription(result: env.description, initial: initialDescription, expectNew: "[\(BoolKey.name) = \(bool)]") env[BoolKey.self] = !bool bool = env[BoolKey.self] #expect(bool == !BoolKey.defaultValue) - #expect(env.description == "[\(BoolKey.name) = \(bool), \(BoolKey.name) = \(BoolKey.defaultValue)]") + compareDictDescription(result: env.description, initial: initialDescription, expectNew: "[\(BoolKey.name) = \(bool), \(BoolKey.name) = \(BoolKey.defaultValue)]") let value = 1 env[IntKey.self] = value - #expect(env.description == "[\(IntKey.name) = \(value), \(BoolKey.name) = \(bool), \(BoolKey.name) = \(BoolKey.defaultValue)]") + compareDictDescription(result: env.description, initial: initialDescription, expectNew: "[\(IntKey.name) = \(value), \(BoolKey.name) = \(bool), \(BoolKey.name) = \(BoolKey.defaultValue)]") } }