Skip to content

Fix Semantics.forced crash issue and add test case #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Sources/OpenSwiftUI/Core/Semantic/Semantics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Semantics: CustomStringConvertible, Comparable, Hashable {

var value: UInt32

static let forced: Semantics? = {
static var forced: Semantics? = {
if dyld_program_sdk_at_least(.init(semantics: firstRelease)) {
return nil
} else {
Expand All @@ -38,7 +38,10 @@ struct Semantics: CustomStringConvertible, Comparable, Hashable {
extension dyld_build_version_t {
@inline(__always)
init(_ value: UInt64) {
self.init(platform: dyld_platform_t(value), version: UInt32(value >> 32))
self.init(
platform: dyld_platform_t(value & 0xFFFF_FFFF),
version: UInt32((value >> 32) & 0xFFFF_FFFF)
)
}

@inline(__always)
Expand Down
22 changes: 22 additions & 0 deletions Tests/OpenSwiftUITests/Core/Semantics/FeatureTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// FeatureTests.swift
// OpenSwiftUITests

@testable import OpenSwiftUI
import Testing

struct FeatureTests {
@Test
func defaultValue() {
struct Feature1: Feature {
static var isEnable: Bool { false }
}

struct Feature2: Feature {
static var isEnable: Bool { true }
}

#expect(Feature1.defaultValue == false)
#expect(Feature2.defaultValue == true)
}
}
57 changes: 57 additions & 0 deletions Tests/OpenSwiftUITests/Core/Semantics/SemanticFeatureTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// SemanticFeatureTests.swift
// OpenSwiftUITests

@testable import OpenSwiftUI
import Testing

struct SemanticFeatureTests {
/// Represent a minimum version
struct SemanticFeature1: SemanticFeature {
static var introduced: Semantics { .init(value: 0x0000_0000) }
}

/// Represent a middle version
struct SemanticFeature2: SemanticFeature {
static var introduced: Semantics { .init(value: 0xFFFF_0000) }
}

/// Represent a maximum version
struct SemanticFeature3: SemanticFeature {
static var introduced: Semantics { .init(value: 0xFFFF_FFFF) }
}

@Test
func defaultEnable() async throws {
#expect(Semantics.forced == nil)
#if canImport(Darwin)
#expect(SemanticFeature1.isEnable == true)
#expect(SemanticFeature2.isEnable == false)
#expect(SemanticFeature3.isEnable == false)
#else
#expect(SemanticFeature1.isEnable == true)
#expect(SemanticFeature2.isEnable == true)
#expect(SemanticFeature3.isEnable == true)
#endif
}

@Test
func changeForceEnable() async throws {
do {
let oldValue = Semantics.forced
Semantics.forced = SemanticFeature2.introduced
defer { Semantics.forced = oldValue }
#expect(SemanticFeature1.isEnable == true)
#expect(SemanticFeature2.isEnable == true)
#expect(SemanticFeature3.isEnable == false)
}
do {
let oldValue = Semantics.forced
Semantics.forced = SemanticFeature3.introduced
defer { Semantics.forced = oldValue }
#expect(SemanticFeature1.isEnable == true)
#expect(SemanticFeature2.isEnable == true)
#expect(SemanticFeature3.isEnable == true)
}
}
}
26 changes: 26 additions & 0 deletions Tests/OpenSwiftUITests/Core/Semantics/SemanticsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// SemanticsTests.swift
// OpenSwiftUITests

@testable import OpenSwiftUI
import Testing

struct SemanticsTests {
@Test
func forced() {
#if canImport(Darwin)
if #available(iOS 13, macOS 10.15, *) {
#expect(Semantics.forced == nil)
} else {
#expect(Semantics.forced != nil)
}
#else
#expect(Semantics.forced == nil)
#endif
}

@Test
func compare() {
#expect(Semantics.v1 < Semantics.v2)
}
}
Loading