Skip to content
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
18 changes: 8 additions & 10 deletions Sources/OpenSwiftUICore/Shape/ShapeStyle/ContentStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ContentStyle.swift
// OpenSwiftUICore
//
// Audited for 6.0.87
// Audited for 6.5.4
// Status: Complete
// ID: BE30CA4BBA5F98638AD9D34F1557FB4D (SwiftUICore)

Expand Down Expand Up @@ -286,17 +286,15 @@ extension ContentStyle.ID: ColorProvider {
}

package init?(level: Int) {
self.init(rawValue: Int8(level & 0xFF))
guard (0..<5).contains(level) else {
return nil
}
self.init(rawValue: Int8(level))
}

package init(truncatingLevel level: Int) {
switch level {
case 0: self = .primary
case 1: self = .secondary
case 2: self = .tertiary
case 3: self = .quaternary
default: self = .quinary
}
let rawValue = Int8(truncatingIfNeeded: min(level, 4))
self = Self(rawValue: rawValue)!
}
}

Expand Down Expand Up @@ -345,7 +343,7 @@ public struct HierarchicalShapeStyleModifier<Base>: ShapeStyle where Base: Shape
}

public func _apply(to shape: inout _ShapeStyle_Shape) {
if let primaryStyle = primaryStyle(in: shape.environment) {
if let primaryStyle = base.primaryStyle(in: shape.environment) {
primaryStyle.offset(by: level)._apply(to: &shape)
} else {
base.offset(by: level)._apply(to: &shape)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// ContentStyleTests.swift
// OpenSwiftUICoreTests

import OpenSwiftUICore
import OpenSwiftUITestsSupport
import Testing

@Suite(.tags(.aigc))
struct ContentStyleTests {
@Test(arguments: [
(0, ContentStyle.ID.primary),
(1, .secondary),
(2, .tertiary),
(3, .quaternary),
(4, .quinary),
])
func levelSelectsStyle(level: Int, expected: ContentStyle.ID) {
#expect(ContentStyle.ID(level: level) == expected)
}

@Test(arguments: [Int.min, -256, -1, 5, 127, 128, 255, 256, 260, Int.max])
func invalidLevelReturnsNil(level: Int) {
#expect(ContentStyle.ID(level: level) == nil)
}

@Test(arguments: [
(0, ContentStyle.ID.primary),
(1, .secondary),
(2, .tertiary),
(3, .quaternary),
(4, .quinary),
(5, .quinary),
(256, .quinary),
(Int.max, .quinary),
(-256, .primary),
(-255, .secondary),
(-252, .quinary),
])
func truncatingLevelSelectsStyle(level: Int, expected: ContentStyle.ID) {
#expect(ContentStyle.ID(truncatingLevel: level) == expected)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// HierarchicalShapeStyleTests.swift
// OpenSwiftUICoreTests

@testable import OpenSwiftUICore
import OpenSwiftUITestsSupport
import Testing

@Suite(.tags(.aigc))
struct HierarchicalShapeStyleTests {
@Test
func secondaryColorUsesBaseColor() {
var environment = EnvironmentValues()
environment.setTestSystemColorDefinition()
var shape = _ShapeStyle_Shape(
operation: .fallbackColor(level: 0),
environment: environment
)

Color.red.secondary._apply(to: &shape)

guard case let .color(color) = shape.result else {
Issue.record("Expected secondary fallback color")
return
}
#expect(color == .red.opacity(0.75))
}

@Test
func nestedStyleUsesPrimaryBaseColor() {
var environment = EnvironmentValues()
environment.setTestSystemColorDefinition()
var shape = _ShapeStyle_Shape(
operation: .fallbackColor(level: 0),
environment: environment
)

Color.red.tertiary.secondary._apply(to: &shape)

guard case let .color(color) = shape.result else {
Issue.record("Expected secondary fallback color")
return
}
#expect(color == .red.opacity(0.75))
}
}
Loading