Skip to content

Commit

Permalink
Fix missing variant cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed May 14, 2024
1 parent 0838bd1 commit fff8ae0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
File renamed without changes.
10 changes: 6 additions & 4 deletions Sources/ThemePark/Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
71 changes: 71 additions & 0 deletions Tests/ThemeParkTests/CodableStylerTests.swift
Original file line number Diff line number Diff line change
@@ -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> {
[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)
)
}
}

0 comments on commit fff8ae0

Please sign in to comment.