Skip to content

Commit e7aa40a

Browse files
committed
Update Environment test case
1 parent 7e62588 commit e7aa40a

File tree

4 files changed

+199
-1
lines changed

4 files changed

+199
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// EnvironmentKey+Test.swift
3+
// OpenSwiftUITestsSupport
4+
5+
package import OpenSwiftUI
6+
7+
package struct StringEnvironmentKey: EnvironmentKey {
8+
package static var defaultValue: String { "" }
9+
}
10+
11+
package struct IntEnvironmentKey: EnvironmentKey {
12+
package static var defaultValue: Int { 0 }
13+
}
14+
15+
package struct BoolEnvironmentKey: EnvironmentKey {
16+
package static var defaultValue: Bool { false }
17+
}
18+
19+
package struct DerivedStringEnvironmentKey: DerivedEnvironmentKey {
20+
package static func value(in environment: EnvironmentValues) -> String {
21+
"d:\(environment[StringEnvironmentKey.self])"
22+
}
23+
}
24+
25+
package struct OptionalStringEnvironmentKey: EnvironmentKey {
26+
package static let defaultValue: String? = nil
27+
}
28+
29+
package struct CustomStructEnvironmentKey: EnvironmentKey {
30+
package struct CustomStruct {
31+
package let value: Int
32+
33+
package init(value: Int) {
34+
self.value = value
35+
}
36+
}
37+
38+
package static let defaultValue = CustomStruct(value: 100)
39+
}

Sources/OpenSwiftUITestsSupport/Data/PropertyList+Test.swift renamed to Sources/OpenSwiftUITestsSupport/Data/PropertyListKey+Test.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// PropertyList+Test.swift
2+
// PropertyListKey+Test.swift
33
// OpenSwiftUITestsSupport
44

55
package import OpenSwiftUI
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// EnvironmentKeyTests.swift
3+
// OpenSwiftUICoreTests
4+
5+
import OpenSwiftUICore
6+
import OpenSwiftUITestsSupport
7+
import Testing
8+
9+
struct EnvironmentKeyTests {
10+
// MARK: - Default Value Tests
11+
12+
@Test
13+
func defaultValues() {
14+
#expect(StringEnvironmentKey.defaultValue == "")
15+
#expect(IntEnvironmentKey.defaultValue == 0)
16+
#expect(OptionalStringEnvironmentKey.defaultValue == nil)
17+
#expect(CustomStructEnvironmentKey.defaultValue.value == 100)
18+
}
19+
20+
// MARK: - Value Equality Tests
21+
22+
@Test
23+
func equatableValuesComparison() {
24+
#expect(StringEnvironmentKey._valuesEqual("test", "test") == true)
25+
#expect(StringEnvironmentKey._valuesEqual("test", "different") == false)
26+
27+
#expect(IntEnvironmentKey._valuesEqual(42, 42) == true)
28+
#expect(IntEnvironmentKey._valuesEqual(42, 43) == false)
29+
30+
#expect(OptionalStringEnvironmentKey._valuesEqual(nil, nil) == true)
31+
#expect(OptionalStringEnvironmentKey._valuesEqual("test", "test") == true)
32+
#expect(OptionalStringEnvironmentKey._valuesEqual("test", nil) == false)
33+
#expect(OptionalStringEnvironmentKey._valuesEqual(nil, "test") == false)
34+
}
35+
36+
@Test
37+
func nonEquatableValuesComparison() {
38+
let struct1 = CustomStructEnvironmentKey.CustomStruct(value: 100)
39+
let struct2 = CustomStructEnvironmentKey.CustomStruct(value: 100)
40+
let struct3 = CustomStructEnvironmentKey.CustomStruct(value: 200)
41+
42+
#expect(CustomStructEnvironmentKey._valuesEqual(struct1, struct2) == true)
43+
#expect(CustomStructEnvironmentKey._valuesEqual(struct1, struct3) == false)
44+
}
45+
46+
// MARK: - DerivedEnvironmentKey Tests
47+
48+
@Test
49+
func derivedEnvironmentKey() {
50+
var environment = EnvironmentValues()
51+
environment[StringEnvironmentKey.self] = "test value"
52+
let derived = DerivedStringEnvironmentKey.value(in: environment)
53+
#expect(derived == "d:test value")
54+
}
55+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//
2+
// EnvironmentValuesTests.swift
3+
// OpenSwiftUICoreTests
4+
5+
import OpenSwiftUICore
6+
import OpenSwiftUITestsSupport
7+
import Testing
8+
9+
// MARK: - Environment Values Extension
10+
11+
extension EnvironmentValues {
12+
var testString: String {
13+
get { self[StringEnvironmentKey.self] }
14+
set { self[StringEnvironmentKey.self] = newValue }
15+
}
16+
17+
var testInt: Int {
18+
get { self[IntEnvironmentKey.self] }
19+
set { self[IntEnvironmentKey.self] = newValue }
20+
}
21+
22+
var testBool: Bool {
23+
get { self[BoolEnvironmentKey.self] }
24+
set { self[BoolEnvironmentKey.self] = newValue }
25+
}
26+
27+
var derivedString: String {
28+
self[DerivedStringEnvironmentKey.self]
29+
}
30+
}
31+
32+
struct EnvironmentValuesTests {
33+
// MARK: - Init Tests
34+
35+
@Test
36+
func defaultInit() {
37+
let environment = EnvironmentValues()
38+
39+
#expect(environment[StringEnvironmentKey.self] == "")
40+
#expect(environment[IntEnvironmentKey.self] == 0)
41+
#expect(environment[BoolEnvironmentKey.self] == false)
42+
}
43+
44+
// MARK: - Value Setting and Retrieval Tests
45+
46+
@Test
47+
func setAndGetValues() {
48+
var environment = EnvironmentValues()
49+
50+
#expect(environment.testString == "")
51+
#expect(environment.testInt == 0)
52+
#expect(environment.testBool == false)
53+
54+
environment[StringEnvironmentKey.self] = "custom string"
55+
environment[IntEnvironmentKey.self] = 100
56+
environment[BoolEnvironmentKey.self] = true
57+
58+
#expect(environment[StringEnvironmentKey.self] == "custom string")
59+
#expect(environment[IntEnvironmentKey.self] == 100)
60+
#expect(environment[BoolEnvironmentKey.self] == true)
61+
62+
environment.testString = "another string"
63+
environment.testInt = 200
64+
environment.testBool = false
65+
66+
#expect(environment.testString == "another string")
67+
#expect(environment.testInt == 200)
68+
#expect(environment.testBool == false)
69+
}
70+
71+
// MARK: - DerivedEnvironmentKey Tests
72+
73+
@Test
74+
func derivedEnvironmentValues() {
75+
var environment = EnvironmentValues()
76+
77+
#expect(environment.derivedString == "d:")
78+
79+
environment.testString = "custom value"
80+
#expect(environment.derivedString == "d:custom value")
81+
}
82+
83+
// MARK: - Description Test
84+
85+
@Test
86+
func description() {
87+
var environment = EnvironmentValues()
88+
environment.testString = "test description"
89+
#expect(environment.description.contains("test description"))
90+
}
91+
92+
// MARK: - PropertyList Tests
93+
94+
@Test
95+
func propertyListConsistency() {
96+
var environment = EnvironmentValues()
97+
environment.testString = "property list test"
98+
environment.testInt = 300
99+
let plist = environment.plist
100+
let newEnvironment = EnvironmentValues(plist)
101+
#expect(newEnvironment[StringEnvironmentKey.self] == "property list test")
102+
#expect(newEnvironment[IntEnvironmentKey.self] == 300)
103+
}
104+
}

0 commit comments

Comments
 (0)