Skip to content

Commit 270b9d0

Browse files
authored
Fix Semantics.forced crash issue and add test case (#94)
* Fix Semantics.forced crash issue * Add Semantics related test case * Fix non-Darwin test case fail
1 parent c128f45 commit 270b9d0

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

Sources/OpenSwiftUI/Core/Semantic/Semantics.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct Semantics: CustomStringConvertible, Comparable, Hashable {
1818

1919
var value: UInt32
2020

21-
static let forced: Semantics? = {
21+
static var forced: Semantics? = {
2222
if dyld_program_sdk_at_least(.init(semantics: firstRelease)) {
2323
return nil
2424
} else {
@@ -38,7 +38,10 @@ struct Semantics: CustomStringConvertible, Comparable, Hashable {
3838
extension dyld_build_version_t {
3939
@inline(__always)
4040
init(_ value: UInt64) {
41-
self.init(platform: dyld_platform_t(value), version: UInt32(value >> 32))
41+
self.init(
42+
platform: dyld_platform_t(value & 0xFFFF_FFFF),
43+
version: UInt32((value >> 32) & 0xFFFF_FFFF)
44+
)
4245
}
4346

4447
@inline(__always)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// FeatureTests.swift
3+
// OpenSwiftUITests
4+
5+
@testable import OpenSwiftUI
6+
import Testing
7+
8+
struct FeatureTests {
9+
@Test
10+
func defaultValue() {
11+
struct Feature1: Feature {
12+
static var isEnable: Bool { false }
13+
}
14+
15+
struct Feature2: Feature {
16+
static var isEnable: Bool { true }
17+
}
18+
19+
#expect(Feature1.defaultValue == false)
20+
#expect(Feature2.defaultValue == true)
21+
}
22+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// SemanticFeatureTests.swift
3+
// OpenSwiftUITests
4+
5+
@testable import OpenSwiftUI
6+
import Testing
7+
8+
struct SemanticFeatureTests {
9+
/// Represent a minimum version
10+
struct SemanticFeature1: SemanticFeature {
11+
static var introduced: Semantics { .init(value: 0x0000_0000) }
12+
}
13+
14+
/// Represent a middle version
15+
struct SemanticFeature2: SemanticFeature {
16+
static var introduced: Semantics { .init(value: 0xFFFF_0000) }
17+
}
18+
19+
/// Represent a maximum version
20+
struct SemanticFeature3: SemanticFeature {
21+
static var introduced: Semantics { .init(value: 0xFFFF_FFFF) }
22+
}
23+
24+
@Test
25+
func defaultEnable() async throws {
26+
#expect(Semantics.forced == nil)
27+
#if canImport(Darwin)
28+
#expect(SemanticFeature1.isEnable == true)
29+
#expect(SemanticFeature2.isEnable == false)
30+
#expect(SemanticFeature3.isEnable == false)
31+
#else
32+
#expect(SemanticFeature1.isEnable == true)
33+
#expect(SemanticFeature2.isEnable == true)
34+
#expect(SemanticFeature3.isEnable == true)
35+
#endif
36+
}
37+
38+
@Test
39+
func changeForceEnable() async throws {
40+
do {
41+
let oldValue = Semantics.forced
42+
Semantics.forced = SemanticFeature2.introduced
43+
defer { Semantics.forced = oldValue }
44+
#expect(SemanticFeature1.isEnable == true)
45+
#expect(SemanticFeature2.isEnable == true)
46+
#expect(SemanticFeature3.isEnable == false)
47+
}
48+
do {
49+
let oldValue = Semantics.forced
50+
Semantics.forced = SemanticFeature3.introduced
51+
defer { Semantics.forced = oldValue }
52+
#expect(SemanticFeature1.isEnable == true)
53+
#expect(SemanticFeature2.isEnable == true)
54+
#expect(SemanticFeature3.isEnable == true)
55+
}
56+
}
57+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// SemanticsTests.swift
3+
// OpenSwiftUITests
4+
5+
@testable import OpenSwiftUI
6+
import Testing
7+
8+
struct SemanticsTests {
9+
@Test
10+
func forced() {
11+
#if canImport(Darwin)
12+
if #available(iOS 13, macOS 10.15, *) {
13+
#expect(Semantics.forced == nil)
14+
} else {
15+
#expect(Semantics.forced != nil)
16+
}
17+
#else
18+
#expect(Semantics.forced == nil)
19+
#endif
20+
}
21+
22+
@Test
23+
func compare() {
24+
#expect(Semantics.v1 < Semantics.v2)
25+
}
26+
}

0 commit comments

Comments
 (0)