diff --git a/Tests/VexilTests/BoxedFlagValueDecodingTests.swift b/Tests/VexilTests/BoxedFlagValueDecodingTests.swift index 2784909..8066df1 100644 --- a/Tests/VexilTests/BoxedFlagValueDecodingTests.swift +++ b/Tests/VexilTests/BoxedFlagValueDecodingTests.swift @@ -15,18 +15,6 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class BoxedFlagValueDecodingTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(BoxedFlagValueDecodingTests.self, hostedBy: self) - } -} - -#endif - @Suite("BoxedFlagValue decoding", .tags(.boxing, .codable)) struct BoxedFlagValueDecodingTests { diff --git a/Tests/VexilTests/BoxedFlagValueEncodingTests.swift b/Tests/VexilTests/BoxedFlagValueEncodingTests.swift index 2392c1c..6b2d7e6 100644 --- a/Tests/VexilTests/BoxedFlagValueEncodingTests.swift +++ b/Tests/VexilTests/BoxedFlagValueEncodingTests.swift @@ -15,18 +15,6 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class BoxedFlagValueEncodingTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(BoxedFlagValueEncodingTests.self, hostedBy: self) - } -} - -#endif - @Suite("BoxedFlagValue encoding", .tags(.boxing, .codable)) struct BoxedFlagValueEncodingTests { diff --git a/Tests/VexilTests/EquatableTests.swift b/Tests/VexilTests/EquatableTests.swift index 9a19df1..2fd4b70 100644 --- a/Tests/VexilTests/EquatableTests.swift +++ b/Tests/VexilTests/EquatableTests.swift @@ -18,18 +18,6 @@ import Combine import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class EquatableTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(EquatableTests.self, hostedBy: self) - } -} - -#endif - @Suite("Equatable Tests", .tags(.pole)) struct EquatableTests { diff --git a/Tests/VexilTests/FlagDetailTests.swift b/Tests/VexilTests/FlagDetailTests.swift index 2fd0c83..747cd98 100644 --- a/Tests/VexilTests/FlagDetailTests.swift +++ b/Tests/VexilTests/FlagDetailTests.swift @@ -14,18 +14,6 @@ import Testing import Vexil -#if compiler(<6) - -import XCTest - -final class FlagDetailTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(FlagDetailTests.self, hostedBy: self) - } -} - -#endif - @Suite("Flag Details") struct FlagDetailTests { diff --git a/Tests/VexilTests/FlagPoleTests.swift b/Tests/VexilTests/FlagPoleTests.swift index 1ba7eda..8d7e953 100644 --- a/Tests/VexilTests/FlagPoleTests.swift +++ b/Tests/VexilTests/FlagPoleTests.swift @@ -15,18 +15,6 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class FlagPoleTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(FlagPoleTests.self, hostedBy: self) - } -} - -#endif - @Suite("Flag Pole") struct FlagPoleTests { diff --git a/Tests/VexilTests/FlagValueBoxingTests.swift b/Tests/VexilTests/FlagValueBoxingTests.swift index 4b166bc..c15d946 100644 --- a/Tests/VexilTests/FlagValueBoxingTests.swift +++ b/Tests/VexilTests/FlagValueBoxingTests.swift @@ -15,18 +15,6 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class FlagValueBoxingTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(FlagValueBoxingTests.self, hostedBy: self) - } -} - -#endif - @Suite("Flag Value Boxing", .tags(.boxing)) struct FlagValueBoxingTests { diff --git a/Tests/VexilTests/FlagValueCompilationTests.swift b/Tests/VexilTests/FlagValueCompilationTests.swift index 0f11ee1..aed5111 100644 --- a/Tests/VexilTests/FlagValueCompilationTests.swift +++ b/Tests/VexilTests/FlagValueCompilationTests.swift @@ -15,18 +15,6 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class FlagValueCompilationTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(FlagValueCompilationTests.self, hostedBy: self) - } -} - -#endif - /// A series of trivial equality tests /// /// These are here not because we need to test the setting or diff --git a/Tests/VexilTests/FlagValueDictionaryTests.swift b/Tests/VexilTests/FlagValueDictionaryTests.swift index 23089e3..c0760c7 100644 --- a/Tests/VexilTests/FlagValueDictionaryTests.swift +++ b/Tests/VexilTests/FlagValueDictionaryTests.swift @@ -15,18 +15,6 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class FlagValueDictionaryTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(FlagValueDictionaryTests.self, hostedBy: self) - } -} - -#endif - @Suite("FlagValueDictionary", .tags(.dictionary)) struct FlagValueDictionaryTests { diff --git a/Tests/VexilTests/FlagValueSourceTests.swift b/Tests/VexilTests/FlagValueSourceTests.swift index a148904..9744978 100644 --- a/Tests/VexilTests/FlagValueSourceTests.swift +++ b/Tests/VexilTests/FlagValueSourceTests.swift @@ -15,18 +15,6 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class FlagValueSourceTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(FlagValueSourceTests.self, hostedBy: self) - } -} - -#endif - @Suite("FlagValueSource", .tags(.pole, .source)) struct FlagValueSourceTests { diff --git a/Tests/VexilTests/FlagValueUnboxingTests.swift b/Tests/VexilTests/FlagValueUnboxingTests.swift index b44daef..8d0a212 100644 --- a/Tests/VexilTests/FlagValueUnboxingTests.swift +++ b/Tests/VexilTests/FlagValueUnboxingTests.swift @@ -15,18 +15,6 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class FlagValueUnboxingTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(FlagValueUnboxingTests.self, hostedBy: self) - } -} - -#endif - @Suite("Flag Value Unboxing", .tags(.boxing)) struct FlagValueUnboxingTests { diff --git a/Tests/VexilTests/KeyEncodingTests.swift b/Tests/VexilTests/KeyEncodingTests.swift index 2fc100e..d0c89ee 100644 --- a/Tests/VexilTests/KeyEncodingTests.swift +++ b/Tests/VexilTests/KeyEncodingTests.swift @@ -14,18 +14,6 @@ import Testing import Vexil -#if compiler(<6) - -import XCTest - -final class KeyEncodingTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(KeyEncodingTests.self, hostedBy: self) - } -} - -#endif - @Suite("Key Encoding", .tags(.pole)) struct KeyEncodingTests { diff --git a/Tests/VexilTests/SnapshotTests.swift b/Tests/VexilTests/SnapshotTests.swift index 8ccc54a..4400eb6 100644 --- a/Tests/VexilTests/SnapshotTests.swift +++ b/Tests/VexilTests/SnapshotTests.swift @@ -14,18 +14,6 @@ import Testing import Vexil -#if compiler(<6) - -import XCTest - -final class SnapshotTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(SnapshotTests.self, hostedBy: self) - } -} - -#endif - @Suite("Snapshots", .tags(.pole, .snapshot)) struct SnapshotTests { diff --git a/Tests/VexilTests/UserDefaultsDecodingTests.swift b/Tests/VexilTests/UserDefaultsDecodingTests.swift index 741d825..ca4827f 100644 --- a/Tests/VexilTests/UserDefaultsDecodingTests.swift +++ b/Tests/VexilTests/UserDefaultsDecodingTests.swift @@ -17,44 +17,25 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class UserDefaultsDecodingTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(UserDefaultsDecodingTests.self, hostedBy: self) - } -} - -#endif - @Suite("UserDefaults Decoding", .tags(.userDefaults)) final class UserDefaultsDecodingTests { - private let defaults: UserDefaults - - init() { - self.defaults = UserDefaults(suiteName: "UserDefaultsDecodingTests")! - } - - deinit { - defaults.removePersistentDomain(forName: "UserDefaultsDecodingTests") - } - - // MARK: - Decoding Missing Values @Test("Decodes missing value as nil") func missing() { - #expect(defaults.flagValue(key: #function) as Bool? == nil) + withUserDefaults(#function) { defaults in + #expect(defaults.flagValue(key: "test") as Bool? == nil) + } } @Test("Decodes unset value as nil") func unset() { - defaults.set(true, forKey: #function) - defaults.removeObject(forKey: #function) - #expect(defaults.flagValue(key: #function) as Bool? == nil) + withUserDefaults(#function) { defaults in + defaults.set(true, forKey: "test") + defaults.removeObject(forKey: "test") + #expect(defaults.flagValue(key: "test") as Bool? == nil) + } } @@ -62,32 +43,42 @@ final class UserDefaultsDecodingTests { @Test("Decodes boolean true") func booleanTrue() { - defaults.set(true, forKey: #function) - #expect(defaults.flagValue(key: #function) == true) + withUserDefaults(#function) { defaults in + defaults.set(true, forKey: "test") + #expect(defaults.flagValue(key: "test") == true) + } } @Test("Decodes boolean false") func booleanFalse() { - defaults.set(false, forKey: #function) - #expect(defaults.flagValue(key: #function) == false) + withUserDefaults(#function) { defaults in + defaults.set(false, forKey: "test") + #expect(defaults.flagValue(key: "test") == false) + } } @Test("Decodes integer as boolean") func booleanInteger() { - defaults.set(1, forKey: #function) - #expect(defaults.flagValue(key: #function) == true) + withUserDefaults(#function) { defaults in + defaults.set(1, forKey: "test") + #expect(defaults.flagValue(key: "test") == true) + } } @Test("Decodes double as boolean") func booleanDouble() { - defaults.set(1.0, forKey: #function) - #expect(defaults.flagValue(key: #function) == true) + withUserDefaults(#function) { defaults in + defaults.set(1.0, forKey: "test") + #expect(defaults.flagValue(key: "test") == true) + } } @Test("Decodes string as boolean") func booleanString() { - defaults.set("t", forKey: #function) - #expect(defaults.flagValue(key: #function) == true) + withUserDefaults(#function) { defaults in + defaults.set("t", forKey: "test") + #expect(defaults.flagValue(key: "test") == true) + } } @@ -95,14 +86,18 @@ final class UserDefaultsDecodingTests { @Test("Decodes string") func string() { - defaults.set("abcd1234", forKey: #function) - #expect(defaults.flagValue(key: #function) == "abcd1234") + withUserDefaults(#function) { defaults in + defaults.set("abcd1234", forKey: "test") + #expect(defaults.flagValue(key: "test") == "abcd1234") + } } @Test("Decodes URL") func url() { - defaults.set("https://google.com/", forKey: #function) - #expect(defaults.flagValue(key: #function) == URL(string: "https://google.com/")!) + withUserDefaults(#function) { defaults in + defaults.set("https://google.com/", forKey: "test") + #expect(defaults.flagValue(key: "test") == URL(string: "https://google.com/")!) + } } @@ -110,26 +105,34 @@ final class UserDefaultsDecodingTests { @Test("Decodes double") func double() { - defaults.set(123.456, forKey: #function) - #expect(defaults.flagValue(key: #function) == 123.456) + withUserDefaults(#function) { defaults in + defaults.set(123.456, forKey: "test") + #expect(defaults.flagValue(key: "test") == 123.456) + } } @Test("Decodes float") func float() { - defaults.set(Float(123.456), forKey: #function) - #expect(defaults.flagValue(key: #function) == Float(123.456)) + withUserDefaults(#function) { defaults in + defaults.set(Float(123.456), forKey: "test") + #expect(defaults.flagValue(key: "test") == Float(123.456)) + } } @Test("Decodes integer as double") func doubleinteger() { - defaults.set(1, forKey: #function) - #expect(defaults.flagValue(key: #function) == 1.0) + withUserDefaults(#function) { defaults in + defaults.set(1, forKey: "test") + #expect(defaults.flagValue(key: "test") == 1.0) + } } @Test("Decodes string as double") func doubleString() { - defaults.set("1.23456789", forKey: #function) - #expect(defaults.flagValue(key: #function) == 1.23456789) + withUserDefaults(#function) { defaults in + defaults.set("1.23456789", forKey: "test") + #expect(defaults.flagValue(key: "test") == 1.23456789) + } } @@ -137,74 +140,98 @@ final class UserDefaultsDecodingTests { @Test("Decodes integer") func int() { - defaults.set(1234, forKey: #function) - #expect(defaults.flagValue(key: #function) == 1234) + withUserDefaults(#function) { defaults in + defaults.set(1234, forKey: "test") + #expect(defaults.flagValue(key: "test") == 1234) + } } @Test("Decodes 8-bit integer") func int8() { - defaults.set(Int8(12), forKey: #function) - #expect(defaults.flagValue(key: #function) == Int8(12)) + withUserDefaults(#function) { defaults in + defaults.set(Int8(12), forKey: "test") + #expect(defaults.flagValue(key: "test") == Int8(12)) + } } @Test("Decodes 16-bit integer") func int16() { - defaults.set(Int16(1234), forKey: #function) - #expect(defaults.flagValue(key: #function) == Int16(1234)) + withUserDefaults(#function) { defaults in + defaults.set(Int16(1234), forKey: "test") + #expect(defaults.flagValue(key: "test") == Int16(1234)) + } } @Test("Decodes 32-bit integer") func int32() { - defaults.set(Int32(1234), forKey: #function) - #expect(defaults.flagValue(key: #function) == Int32(1234)) + withUserDefaults(#function) { defaults in + defaults.set(Int32(1234), forKey: "test") + #expect(defaults.flagValue(key: "test") == Int32(1234)) + } } @Test("Decodes 64-bit integer") func int64() { - defaults.set(Int64(1234), forKey: #function) - #expect(defaults.flagValue(key: #function) == Int64(1234)) + withUserDefaults(#function) { defaults in + defaults.set(Int64(1234), forKey: "test") + #expect(defaults.flagValue(key: "test") == Int64(1234)) + } } @Test("Decodes unsigned integer") func uint() { - defaults.set(UInt(1234), forKey: #function) - #expect(defaults.flagValue(key: #function) == UInt(1234)) + withUserDefaults(#function) { defaults in + defaults.set(UInt(1234), forKey: "test") + #expect(defaults.flagValue(key: "test") == UInt(1234)) + } } @Test("Decodes 8-bit unsigned integer") func uint8() { - defaults.set(UInt8(12), forKey: #function) - #expect(defaults.flagValue(key: #function) == UInt8(12)) + withUserDefaults(#function) { defaults in + defaults.set(UInt8(12), forKey: "test") + #expect(defaults.flagValue(key: "test") == UInt8(12)) + } } @Test("Decodes 16-bit unsigned integer") func uint16() { - defaults.set(UInt16(1234), forKey: #function) - #expect(defaults.flagValue(key: #function) == UInt16(1234)) + withUserDefaults(#function) { defaults in + defaults.set(UInt16(1234), forKey: "test") + #expect(defaults.flagValue(key: "test") == UInt16(1234)) + } } @Test("Decodes 32-bit unsigned integer") func uint32() { - defaults.set(UInt32(1234), forKey: #function) - #expect(defaults.flagValue(key: #function) == UInt32(1234)) + withUserDefaults(#function) { defaults in + defaults.set(UInt32(1234), forKey: "test") + #expect(defaults.flagValue(key: "test") == UInt32(1234)) + } } @Test("Decodes 64-bit unsigned integer") func uint64() { - defaults.set(UInt64(1234), forKey: #function) - #expect(defaults.flagValue(key: #function) == UInt64(1234)) + withUserDefaults(#function) { defaults in + defaults.set(UInt64(1234), forKey: "test") + #expect(defaults.flagValue(key: "test") == UInt64(1234)) + } } @Test("Decodes string as integer") func intString() { - defaults.set("1234", forKey: #function) - #expect(defaults.flagValue(key: #function) == 1234) + withUserDefaults(#function) { defaults in + defaults.set("1234", forKey: "test") + #expect(defaults.flagValue(key: "test") == 1234) + } } @Test("Decodes string as unsigned integer") func uintString() { - defaults.set("1234", forKey: #function) - #expect(defaults.flagValue(key: #function) == UInt(1234)) + withUserDefaults(#function) { defaults in + defaults.set("1234", forKey: "test") + #expect(defaults.flagValue(key: "test") == UInt(1234)) + } } @@ -212,21 +239,25 @@ final class UserDefaultsDecodingTests { @Test("Decodes raw representable string") func rawRepresentableString() { - defaults.set("Test Value", forKey: #function) - #expect(defaults.flagValue(key: #function) == TestStruct(rawValue: "Test Value")) + withUserDefaults(#function) { defaults in + defaults.set("Test Value", forKey: "test") + #expect(defaults.flagValue(key: "test") == TestStruct(rawValue: "Test Value")) - struct TestStruct: RawRepresentable, FlagValue, Equatable { - var rawValue: String + struct TestStruct: RawRepresentable, FlagValue, Equatable { + var rawValue: String + } } } @Test("Decodes raw representable boolean") func rawRepresentableBool() { - defaults.set(true, forKey: #function) - #expect(defaults.flagValue(key: #function) == TestStruct(rawValue: true)) + withUserDefaults(#function) { defaults in + defaults.set(true, forKey: "test") + #expect(defaults.flagValue(key: "test") == TestStruct(rawValue: true)) - struct TestStruct: RawRepresentable, FlagValue, Equatable { - var rawValue: Bool + struct TestStruct: RawRepresentable, FlagValue, Equatable { + var rawValue: Bool + } } } @@ -234,40 +265,53 @@ final class UserDefaultsDecodingTests { @Test("Decodes optional boolean") func optionalBool() { - defaults.set(true, forKey: #function) - #expect(defaults.flagValue(key: #function) == Bool??.some(true)) + withUserDefaults(#function) { defaults in + defaults.set(true, forKey: "test") + #expect(defaults.flagValue(key: "test") == Bool??.some(true)) + } } @Test("Decodes optional string") func optionalString() { - defaults.set("Test Value", forKey: #function) - #expect(defaults.flagValue(key: #function) == String??.some("Test Value")) + withUserDefaults(#function) { defaults in + defaults.set("Test Value", forKey: "test") + #expect(defaults.flagValue(key: "test") == String??.some("Test Value")) + } } @Test("Decodes nil") func optionalNil() { - defaults.removeObject(forKey: #function) - #expect(defaults.flagValue(key: #function) == String??.none) + withUserDefaults(#function) { defaults in + defaults.removeObject(forKey: "test") + #expect(defaults.flagValue(key: "test") == String??.none) + } } @Test("Decodes string as optional boolean") func testOptionalBoolString() { - defaults.set("t", forKey: #function) - #expect(defaults.flagValue(key: #function) == Bool??.some(true)) + withUserDefaults(#function) { defaults in + defaults.set("t", forKey: "test") + defaults.synchronize() + #expect(defaults.flagValue(key: "test") == Bool??.some(true)) + } } // MARK: - Array Tests @Test("Decodes string array") - func stringARray() { - defaults.set([ "abc", "123" ], forKey: #function) - #expect(defaults.flagValue(key: #function) == [ "abc", "123" ]) + func stringArray() { + withUserDefaults(#function) { defaults in + defaults.set([ "abc", "123" ], forKey: "test") + #expect(defaults.flagValue(key: "test") == [ "abc", "123" ]) + } } @Test("Decodes integer array") func integerArray() { - defaults.set([ 234, -123 ], forKey: #function) - #expect(defaults.flagValue(key: #function) == [ 234, -123 ]) + withUserDefaults(#function) { defaults in + defaults.set([ 234, -123 ], forKey: "test") + #expect(defaults.flagValue(key: "test") == [ 234, -123 ]) + } } @@ -275,14 +319,18 @@ final class UserDefaultsDecodingTests { @Test("Decodes string dictionary") func stringDictionary() { - defaults.set([ "key1": "value1", "key2": "value2" ], forKey: #function) - #expect(defaults.flagValue(key: #function) == [ "key1": "value1", "key2": "value2" ]) + withUserDefaults(#function) { defaults in + defaults.set([ "key1": "value1", "key2": "value2" ], forKey: "test") + #expect(defaults.flagValue(key: "test") == [ "key1": "value1", "key2": "value2" ]) + } } @Test("Decodes integer dictionary") func integerDictionary() { - defaults.set([ "key1": 123, "key2": -987 ], forKey: #function) - #expect(defaults.flagValue(key: #function) == [ "key1": 123, "key2": -987 ]) + withUserDefaults(#function) { defaults in + defaults.set([ "key1": 123, "key2": -987 ], forKey: "test") + #expect(defaults.flagValue(key: "test") == [ "key1": 123, "key2": -987 ]) + } } @@ -316,8 +364,10 @@ final class UserDefaultsDecodingTests { } """ - defaults.set(Data(input.utf8), forKey: #function) - #expect(defaults.flagValue(key: #function) == expected) + withUserDefaults(#function) { defaults in + defaults.set(Data(input.utf8), forKey: "test") + #expect(defaults.flagValue(key: "test") == expected) + } } @Test("Decodes enum") @@ -327,10 +377,19 @@ final class UserDefaultsDecodingTests { case two } - try defaults.setFlagValue(MyEnum.one, key: #function) - #expect(defaults.flagValue(key: #function) == MyEnum.one) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(MyEnum.one, key: "test") + #expect(defaults.flagValue(key: "test") == MyEnum.one) + } } } +/// Swift Testing runs tests inside a TaskGroup, which means sharing UserDefaults across multiple tests is fraught. +private func withUserDefaults(_ suite: String, _ closure: (UserDefaults) throws -> Void) rethrows { + let defaults = UserDefaults(suiteName: suite)! + try closure(defaults) + defaults.removePersistentDomain(forName: suite) +} + #endif // !os(Linux) diff --git a/Tests/VexilTests/UserDefaultsEncodingTests.swift b/Tests/VexilTests/UserDefaultsEncodingTests.swift index 3088008..79ffd62 100644 --- a/Tests/VexilTests/UserDefaultsEncodingTests.swift +++ b/Tests/VexilTests/UserDefaultsEncodingTests.swift @@ -17,18 +17,6 @@ import Foundation import Testing @testable import Vexil -#if compiler(<6) - -import XCTest - -final class UserDefaultsEncodingTestCase: XCTestCase { - func testSwiftTesting() async { - await XCTestScaffold.runTestsInSuite(UserDefaultsEncodingTests.self, hostedBy: self) - } -} - -#endif - @Suite("UserDefaults Encoding", .tags(.userDefaults)) final class UserDefaultsEncodingTests { @@ -47,11 +35,13 @@ final class UserDefaultsEncodingTests { @Test("Unsets values") func unsets() throws { - defaults.set(true, forKey: #function) - #expect(defaults.object(forKey: #function) != nil) + try withUserDefaults(#function) { defaults in + defaults.set(true, forKey: "test") + #expect(defaults.object(forKey: "test") != nil) - try defaults.setFlagValue(Bool?.none, key: #function) - #expect(defaults.object(forKey: #function) == nil) + try defaults.setFlagValue(Bool?.none, key: "test") + #expect(defaults.object(forKey: "test") == nil) + } } @@ -59,14 +49,18 @@ final class UserDefaultsEncodingTests { @Test("Encodes boolean true") func booleanTrue() throws { - try defaults.setFlagValue(true, key: #function) - #expect(defaults.bool(forKey: #function) == true) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(true, key: "test") + #expect(defaults.bool(forKey: "test") == true) + } } @Test("Encodes boolean false") func booleanFalse() throws { - try defaults.setFlagValue(false, key: #function) - #expect(defaults.bool(forKey: #function) == false) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(false, key: "test") + #expect(defaults.bool(forKey: "test") == false) + } } @@ -74,14 +68,18 @@ final class UserDefaultsEncodingTests { @Test("Encodes string") func string() throws { - try defaults.setFlagValue("abcd1234", key: #function) - #expect(defaults.string(forKey: #function) == "abcd1234") + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue("abcd1234", key: "test") + #expect(defaults.string(forKey: "test") == "abcd1234") + } } @Test("Encodes URL") func url() throws { - try defaults.setFlagValue(URL(string: "https://google.com/")!, key: #function) - #expect(defaults.string(forKey: #function) == "https://google.com/") + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(URL(string: "https://google.com/")!, key: "test") + #expect(defaults.string(forKey: "test") == "https://google.com/") + } } @@ -89,13 +87,17 @@ final class UserDefaultsEncodingTests { @Test("Encodes double") func double() throws { - try defaults.setFlagValue(123.456, key: #function) - #expect(defaults.double(forKey: #function) == 123.456) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(123.456, key: "test") + #expect(defaults.double(forKey: "test") == 123.456) + } } func float() throws { - try defaults.setFlagValue(Float(123.456), key: #function) - #expect(defaults.float(forKey: #function) == Float(123.456)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(Float(123.456), key: "test") + #expect(defaults.float(forKey: "test") == Float(123.456)) + } } @@ -103,62 +105,82 @@ final class UserDefaultsEncodingTests { @Test("Encodes integer") func int() throws { - try defaults.setFlagValue(1234, key: #function) - #expect(defaults.integer(forKey: #function) == 1234) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(1234, key: "test") + #expect(defaults.integer(forKey: "test") == 1234) + } } @Test("Encodes 8-bit integer") func int8() throws { - try defaults.setFlagValue(Int8(12), key: #function) - #expect(defaults.integer(forKey: #function) == Int8(12)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(Int8(12), key: "test") + #expect(defaults.integer(forKey: "test") == Int8(12)) + } } @Test("Encodes 16-bit integer") func int16() throws { - try defaults.setFlagValue(Int16(1234), key: #function) - #expect(defaults.integer(forKey: #function) == Int16(1234)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(Int16(1234), key: "test") + #expect(defaults.integer(forKey: "test") == Int16(1234)) + } } @Test("Encodes 32-bit integer") func int32() throws { - try defaults.setFlagValue(Int32(1234), key: #function) - #expect(defaults.integer(forKey: #function) == Int32(1234)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(Int32(1234), key: "test") + #expect(defaults.integer(forKey: "test") == Int32(1234)) + } } @Test("Encodes 64-bit integer") func int64() throws { - try defaults.setFlagValue(Int64(1234), key: #function) - #expect(defaults.integer(forKey: #function) == Int64(1234)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(Int64(1234), key: "test") + #expect(defaults.integer(forKey: "test") == Int64(1234)) + } } @Test("Encodes unsigned integer") func uint() throws { - try defaults.setFlagValue(UInt(1234), key: #function) - #expect(defaults.integer(forKey: #function) == UInt(1234)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(UInt(1234), key: "test") + #expect(defaults.integer(forKey: "test") == UInt(1234)) + } } @Test("Encodes 8-bit unsigned integer") func uint8() throws { - try defaults.setFlagValue(UInt8(12), key: #function) - #expect(defaults.integer(forKey: #function) == UInt8(12)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(UInt8(12), key: "test") + #expect(defaults.integer(forKey: "test") == UInt8(12)) + } } @Test("Encodes 16-bit unsigned integer") func uint16() throws { - try defaults.setFlagValue(UInt16(1234), key: #function) - #expect(defaults.integer(forKey: #function) == UInt16(1234)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(UInt16(1234), key: "test") + #expect(defaults.integer(forKey: "test") == UInt16(1234)) + } } @Test("Encodes 32-bit unsigned integer") func uint32() throws { - try defaults.setFlagValue(UInt32(1234), key: #function) - #expect(defaults.integer(forKey: #function) == UInt32(1234)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(UInt32(1234), key: "test") + #expect(defaults.integer(forKey: "test") == UInt32(1234)) + } } @Test("Encodes 64-bit unsigned integer") func uint64() throws { - try defaults.setFlagValue(UInt64(1234), key: #function) - #expect(defaults.integer(forKey: #function) == UInt64(1234)) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(UInt64(1234), key: "test") + #expect(defaults.integer(forKey: "test") == UInt64(1234)) + } } @@ -166,28 +188,34 @@ final class UserDefaultsEncodingTests { @Test("Encodes raw representable") func rawRepresentable() throws { - try defaults.setFlagValue(TestStruct(rawValue: "Test Value"), key: #function) - #expect(defaults.string(forKey: #function) == "Test Value") + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(TestStruct(rawValue: "Test Value"), key: "test") + #expect(defaults.string(forKey: "test") == "Test Value") - struct TestStruct: RawRepresentable, FlagValue, Equatable { - var rawValue: String + struct TestStruct: RawRepresentable, FlagValue, Equatable { + var rawValue: String + } } } @Test("Encodes optional") func optional() throws { - let value: String? = "Test Value" + try withUserDefaults(#function) { defaults in + let value: String? = "Test Value" - try defaults.setFlagValue(value, key: #function) - #expect(defaults.string(forKey: #function) == "Test Value") + try defaults.setFlagValue(value, key: "test") + #expect(defaults.string(forKey: "test") == "Test Value") + } } @Test("Encodes nil") func none() throws { - let value: String? = nil + try withUserDefaults(#function) { defaults in + let value: String? = nil - try defaults.setFlagValue(value, key: #function) - #expect(defaults.string(forKey: #function) == nil) + try defaults.setFlagValue(value, key: "test") + #expect(defaults.string(forKey: "test") == nil) + } } @@ -195,14 +223,18 @@ final class UserDefaultsEncodingTests { @Test("Encodes string array") func stringArray() throws { - try defaults.setFlagValue([ "abc", "123" ], key: #function) - #expect(defaults.stringArray(forKey: #function) == [ "abc", "123" ]) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue([ "abc", "123" ], key: "test") + #expect(defaults.stringArray(forKey: "test") == [ "abc", "123" ]) + } } @Test("Encodes integer array") func integerArray() throws { - try defaults.setFlagValue([ 234, -123 ], key: #function) - #expect(defaults.array(forKey: #function) as? [Int] == [ 234, -123 ]) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue([ 234, -123 ], key: "test") + #expect(defaults.array(forKey: "test") as? [Int] == [ 234, -123 ]) + } } @@ -210,14 +242,18 @@ final class UserDefaultsEncodingTests { @Test("Encodes string dictionary") func stringDictionary() throws { - try defaults.setFlagValue([ "key1": "value1", "key2": "value2" ], key: #function) - #expect(defaults.dictionary(forKey: #function) as? [String: String] == [ "key1": "value1", "key2": "value2" ]) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue([ "key1": "value1", "key2": "value2" ], key: "test") + #expect(defaults.dictionary(forKey: "test") as? [String: String] == [ "key1": "value1", "key2": "value2" ]) + } } @Test("Encodes integer dictionary") func integerDictionary() throws { - try defaults.setFlagValue([ "key1": 123, "key2": -987 ], key: #function) - #expect(defaults.dictionary(forKey: #function) as? [String: Int] == [ "key1": 123, "key2": -987 ]) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue([ "key1": 123, "key2": -987 ], key: "test") + #expect(defaults.dictionary(forKey: "test") as? [String: Int] == [ "key1": 123, "key2": -987 ]) + } } // MARK: - Codable Tests @@ -241,10 +277,19 @@ final class UserDefaultsEncodingTests { // manually encoding into json let expected = Data(#"{"wrapped":{"property1":"value1","property2":123,"property3":"🤯"}}"#.utf8) - try defaults.setFlagValue(input, key: #function) - #expect(defaults.data(forKey: #function) == expected) + try withUserDefaults(#function) { defaults in + try defaults.setFlagValue(input, key: "test") + #expect(defaults.data(forKey: "test") == expected) + } } } +/// Swift Testing runs tests inside a TaskGroup, which means sharing UserDefaults across multiple tests is fraught. +private func withUserDefaults(_ suite: String, _ closure: (UserDefaults) throws -> Void) rethrows { + let defaults = UserDefaults(suiteName: suite)! + try closure(defaults) + defaults.removePersistentDomain(forName: suite) +} + #endif // !os(Linux)