Cotton code
118 lines · 4.4 KB · RustRaw
//! The colour arithmetic, and the shape of the colorsets it writes.use super::*;#[test]fn the_colour_arithmetic_keeps_a_byte_a_byte_and_rounds_alpha_so_it_round_trips() {    assert_eq!(        channels("#2E2E2E").expect("a colour"),        ["0x2E", "0x2E", "0x2E", "1.000"].map(String::from)    );    // 46/255 = 0.18039…, and 0.180 × 255 = 45.9 → 46. Three decimals is what    // Xcode writes, and it is enough to survive the trip back.    assert_eq!(channels("#FFFFFF2E").expect("a colour")[3], "0.180");    assert_eq!(channels("#00000000").expect("a colour")[3], "0.000");    assert_eq!(channels("#ffffff").expect("a colour")[0], "0xFF");}#[test]fn the_colour_arithmetic_refuses_anything_that_is_not_a_colour() {    // Silence here would be a colorset full of NaN, which renders as black    // with no build error at all.    for wrong in ["#FFF", "rebeccapurple", "#GGGGGG", "#FFFFFFF"] {        let refused = channels(wrong).expect_err(wrong);        assert!(refused.contains("not a #RRGGBB"), "{wrong}: {refused}");    }}#[test]fn a_colorset_writes_one_colour_and_names_no_appearance() {    // The app is LIGHT-ONLY. A second entry tagged `luminosity: dark` is what    // let the operating system repaint the app, so its absence is the guard.    let json = render_colorset("#FFFFFF").expect("a colorset");    assert_eq!(json.matches("\"idiom\": \"universal\"").count(), 1);    assert!(        !json.contains("appearances"),        "a colorset names no appearance: {json}"    );    assert!(!json.contains("luminosity"));    assert!(json.contains("\"red\": \"0xFF\""));    // Two-space JSON with a trailing newline, keys unspaced around the colon:    // the shape the committed catalogue carries. Xcode writes `"key" : value`,    // so saving from Xcode reads as drift — the fix is to run the generator.    assert!(json.ends_with("}\n"));    assert!(!json.contains("\" : "), "no Xcode-style spacing");}#[test]fn the_catalogue_root_is_the_bare_info_block() {    let root = render_catalogue_root();    assert!(root.contains("\"author\": \"xcode\""));    assert!(!root.contains("colors"), "the root names no colour");    assert!(root.ends_with("}\n"));}#[test]fn the_palette_gives_every_colorset_a_typed_accessor_in_both_frameworks() {    let swift = render_palette();    for (asset, _) in colorsets() {        assert!(            swift.contains(&format!("public static var {asset}: Color")),            "{asset} has no SwiftUI accessor"        );        assert!(            swift.contains(&format!("public static var {asset}: UIColor")),            "{asset} has no UIKit accessor"        );        assert!(            swift.contains(&format!("    case {asset}\n")),            "{asset} is not a ColorSlot case"        );    }}#[test]fn the_palette_says_it_is_the_one_place_a_colour_may_be_written() {    let swift = render_palette();    assert!(swift.contains("THE ONE PLACE A COLOUR MAY BE WRITTEN"));    assert!(swift.contains("public static let paper"));    assert!(swift.contains("public static let graphite"));    assert!(swift.contains("public static let status"));    // `authored` is total over `ColorSlot`: both halves generated from one    // record is what makes the catalogue and the value type unable to drift.    assert!(swift.contains("public static func authored(_ slot: ColorSlot) -> CottonColor"));    assert_eq!(        swift.matches("        case .").count(),        colorsets().len(),        "the authored switch names every case"    );}#[test]fn the_generated_palette_carries_no_appearance_axis() {    // Light-only: no `CottonAppearance`, no `Palettes.of(_:)`, no second    // status set, and no dynamic `NSColor` provider on the Mac. A reader that    // wants the brand's charcoal has to name `Palettes.graphite`.    let swift = render_palette();    for gone in [        "CottonAppearance",        "public static func of(",        "darkStatus",        "lightStatus",        "bestMatch",        "darkAqua",    ] {        assert!(!swift.contains(gone), "{gone} is still generated");    }    assert!(swift.contains("public let name: String"));    assert!(!swift.contains("public let appearance"));}#[test]fn the_palette_is_deterministic() {    assert_eq!(render_palette(), render_palette());    assert_eq!(        render_colorset("#C6534B").expect("a colorset"),        render_colorset("#C6534B").expect("a colorset")    );}