diff --git a/Sources/ThemePark/CodableTheme.swift b/Sources/ThemePark/CodableStyler.swift similarity index 100% rename from Sources/ThemePark/CodableTheme.swift rename to Sources/ThemePark/CodableStyler.swift diff --git a/Sources/ThemePark/Style.swift b/Sources/ThemePark/Style.swift index 0dd1129..6ab40dc 100644 --- a/Sources/ThemePark/Style.swift +++ b/Sources/ThemePark/Style.swift @@ -118,10 +118,12 @@ public struct Variant: Hashable, Sendable { } extension Variant: CaseIterable { - public static var allCases: [Variant] { - zip(ColorScheme.allCases, ColorSchemeContrast.allCases) - .map { Variant(colorScheme: $0, colorSchemeContrast: $1) } - } + public static let allCases: [Variant] = [ + Variant(colorScheme: .light, colorSchemeContrast: .standard), + Variant(colorScheme: .light, colorSchemeContrast: .increased), + Variant(colorScheme: .dark, colorSchemeContrast: .standard), + Variant(colorScheme: .dark, colorSchemeContrast: .increased), + ] } extension Variant: Codable { diff --git a/Tests/ThemeParkTests/CodableStylerTests.swift b/Tests/ThemeParkTests/CodableStylerTests.swift new file mode 100644 index 0000000..7744774 --- /dev/null +++ b/Tests/ThemeParkTests/CodableStylerTests.swift @@ -0,0 +1,71 @@ +import XCTest +import ThemePark + +struct MockStyler: Styling { + func style(for query: Query) -> Style { + switch query.key { + case .editor(.background), .editor(.accessoryBackground): + return Style(color: .red) + default: + return Style(color: .blue) + } + } + + var supportedVariants: Set { + [Variant(colorScheme: .light)] + } +} + +final class CodableStylerTests: XCTestCase { + func testPassthrough() throws { + let mock = MockStyler() + let theme = CodableStyler(mock) + let context = Query.Context(controlState: .active, variant: Variant(colorScheme: .light)) + + XCTAssertEqual( + mock.style(for: .editor(.background), context: context), + theme.style(for: .editor(.background), context: context) + ) + + XCTAssertEqual( + mock.style(for: .syntax(.text), context: context), + theme.style(for: .syntax(.text), context: context) + ) + } + + func testPassthroughMismatchedVariant() throws { + let mock = MockStyler() + let theme = CodableStyler(mock) + let context = Query.Context(controlState: .active, variant: Variant(colorScheme: .dark)) + + XCTAssertEqual( + mock.style(for: .editor(.background), context: context), + theme.style(for: .editor(.background), context: context) + ) + + XCTAssertEqual( + mock.style(for: .syntax(.text), context: context), + theme.style(for: .syntax(.text), context: context) + ) + } + + func testEncodedDecodedPassthrough() throws { + let mock = MockStyler() + let theme = CodableStyler(mock) + let context = Query.Context(controlState: .active, variant: Variant(colorScheme: .light)) + + let data = try JSONEncoder().encode(theme) + let decodedTheme = try JSONDecoder().decode(CodableStyler.self, from: data) + + + XCTAssertEqual( + mock.style(for: .editor(.background), context: context), + decodedTheme.style(for: .editor(.background), context: context) + ) + + XCTAssertEqual( + mock.style(for: .syntax(.text), context: context), + decodedTheme.style(for: .syntax(.text), context: context) + ) + } +}