import CottonWriting import Foundation import XCTest @testable import CottonUI #if os(iOS) /// Words writes in blue (Notes CONTRACT ยง10 "Ink"): the iPhone reads the ink and the chooser's inks, draws red acts in /// the paper's `danger` whatever the ink, and draws links underlined in the paper's link colour, never in the ink. final class NotesInkTests: XCTestCase { private static let colours = ##"{"ground":"#F6F3EC","text":"#2A2A2D","heading":"#1E1E20","secondary":"#77736B","accent":"#2B4A8F","rule":"#E2DDD2","highlight":"#F3E3A6","code_ground":"#EEEAE0","selection":"#2B4A8F33","link":"#2A2A2D","danger":"#C2452D"}"## // RULE: the looks answer the chosen ink and the inks for the chooser; the chosen look carries the ink and `danger`. func testTheLooksAndTheChoiceCarryTheInk() throws { let themes = try JSONDecoder().decode(NotesThemes.self, from: Data(""" {"themes":[{"id":"linen","name":"Linen","light":\(Self.colours),"dark":\(Self.colours)}],"fonts":[], "font_size":{"min":14,"max":24,"step":1},"line_height":{"min":1.2,"max":2,"step":0.1},"line_widths":[], "formatting":[],"bar":[],"words":{"ink":"Ink"},"ink":"blue", "inks":[{"id":"blue","name":"Blue","light":{"accent":"#2B4A8F","selection":"#2B4A8F33"},"dark":{"accent":"#93ABEB"}}, {"id":"red","name":"Red","light":{"accent":"#C2452D"},"dark":{"accent":"#E07A63"}}]} """.utf8)) XCTAssertEqual(themes.ink, "blue") XCTAssertEqual(themes.inks?.map(\.id), ["blue", "red"]) XCTAssertEqual(themes.inks?.first?.dark.accent, "#93ABEB") XCTAssertEqual(themes.word("ink"), "Ink") XCTAssertEqual(themes.themes.first?.light.danger, "#C2452D") let chosen = try JSONDecoder().decode(NotesSettingsChosen.self, from: Data(""" {"light_theme":"linen","dark_theme":"slate","light":\(Self.colours),"dark":\(Self.colours),"font":"sans", "font_size":17,"line_height":1.5,"line_width":0,"hide_marks":true,"ink":"blue"} """.utf8)) XCTAssertEqual(chosen.ink, "blue") } // RULE: a red act is the paper's red whatever the ink; a theme that names no danger falls back to its accent. func testDangerIsThePapersRedAndFallsBackToTheAccent() throws { let inked = try JSONDecoder().decode(WritingTheme.Colours.self, from: Data(Self.colours.utf8)) let theme = WritingTheme(id: "linen", name: "", dark: false, colours: inked) XCTAssertNotEqual(theme.danger, theme.accent) XCTAssertEqual(theme.danger, WritingTheme.ui("#C2452D")) var older = inked older.danger = nil XCTAssertEqual(WritingTheme(id: "linen", name: "", dark: false, colours: older).danger, theme.accent) } // RULE: every red act in Words reads `danger`, never the accent; links are underlined. func testRedActsReadDangerAndLinksAreUnderlined() throws { for file in ["apps/cotton/Sources/CottonUI/Notes/NotesHome.swift", "apps/cotton/Sources/CottonUI/Notes/NotesSide.swift"] { let source = SourceSweep.withoutComments(try SourceSweep.mustRead(file, ownedBy: "launch-ios")) XCTAssertFalse(source.contains("red ? theme.accent") || source.contains("red ? ui(theme.accent)") || source.contains("red == true ? theme.accent"), "\(file) draws a red act in the ink") } let styler = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonWriting/WritingStyler.swift", ownedBy: "launch-ios")) let link = try XCTUnwrap(styler.range(of: "case \"link\", \"note_link\":")) XCTAssertTrue(styler[link.upperBound...].prefix(600).contains(".underlineStyle"), "a link is underlined") } } #endif