102 lines · 6.4 KB · SwiftRaw
import CottonPaperimport CoreTextimport SwiftUIimport XCTest@testable import CottonFacesfinal class FaceMarkdownTests: XCTestCase { private func plain(_ source: String) -> String { String(FaceMarkdown.set(source, theme: .paper).characters) } func testRunsUseTheCallersWornThemeAndPlainWordsNeedNoTheme() { var theme = Theme.paper theme.palette.light.inkSecondary = ThemeColor(red: 0.1, green: 0.2, blue: 0.3) theme.palette.light.highlight = ThemeColor(red: 0.3, green: 0.2, blue: 0.1) XCTAssertEqual(FaceMarkdown.set("#tag", theme: theme).runs.first?.foregroundColor, theme.colors.inkSecondary.color) XCTAssertEqual(FaceMarkdown.inline("::mark::", theme: theme).runs.first?.backgroundColor, theme.colors.highlight.color.opacity(0.35)) XCTAssertEqual(FaceMarkdown.plain("# Heading\n**words** #tag"), "Heading\nwords tag") XCTAssertEqual(FaceMarkdown.plain("# Heading", blocks: false), "# Heading") } func testAsterisksSetStrongEmphasisAndBoth() { XCTAssertEqual(plain("**strong** *emphasis* ***both***"), "strong emphasis both") let set = FaceMarkdown.set("**strong** *emphasis* ***both***", theme: .paper) XCTAssertEqual(set.runs.first?.inlinePresentationIntent, .stronglyEmphasized) XCTAssertTrue(set.runs.last?.inlinePresentationIntent?.contains(.emphasized) == true) XCTAssertTrue(set.runs.last?.inlinePresentationIntent?.contains(.stronglyEmphasized) == true) XCTAssertEqual(plain("**bold with _care_**"), "bold with care") XCTAssertEqual(plain("**bold *and italic*** *italic **and bold***"), "bold and italic italic and bold") XCTAssertEqual(plain("**across\nsoft lines**"), "across\nsoft lines") XCTAssertEqual(plain("2 * 3 * 4"), "2 * 3 * 4") XCTAssertEqual(plain("\\*literal\\* and _care_ and `**raw**`"), "*literal* and care and **raw**") } func testBearHighlightUsesTheThemeAndStrikeUsesEitherSpelling() { let set = FaceMarkdown.set("::marked:: ~gone~ ~~also gone~~", theme: .paper) XCTAssertEqual(String(set.characters), "marked gone also gone") XCTAssertEqual(set.runs.first?.backgroundColor, Theme.paper.colors.highlight.color.opacity(0.35)) XCTAssertNotNil(set.runs.last?.strikethroughStyle) } func testNativeAsterisksKeepTheirWeightAndSlantWithARegularOnlyFace() { _ = CottonFonts.caslon(18) let native = FaceMarkdown.native(FaceMarkdown.set("**strong** *emphasis* ***both***", theme: .paper), font: CTFontCreateWithName(CottonFonts.caslonFace as CFString, 18, nil)) for offset in [0, 16] { let face = native.attribute(.init(kCTFontAttributeName as String), at: offset, effectiveRange: nil) as! CTFont let stroke = native.attribute(.strokeWidth, at: offset, effectiveRange: nil) as? Double ?? 0 XCTAssertTrue(CTFontGetSymbolicTraits(face).contains(.traitBold) || stroke < 0) } for offset in [7, 16] { let face = native.attribute(.init(kCTFontAttributeName as String), at: offset, effectiveRange: nil) as! CTFont XCTAssertTrue(CTFontGetSymbolicTraits(face).contains(.traitItalic) || CTFontGetMatrix(face).c != 0) } } func testTasksListsAndNestedLists() { XCTAssertEqual(plain("- [ ] wait\n- [x] done\n - [X] nested\n + deep\n1. first\n2) second"), "☐ wait\n☑ done\n ☑ nested\n • deep\n1. first\n2) second") } func testHeadingsTagsAndQuietInk() { XCTAssertEqual(plain("## Heading\n#tag #nested/tag #தமிழ்/tag"), "Heading\ntag nested/tag தமிழ்/tag") XCTAssertEqual(FaceMarkdown.set("#tag", theme: .paper).runs.first?.foregroundColor, Theme.paper.colors.inkSecondary.color) XCTAssertEqual(FaceMarkdown.set("# Heading", theme: .paper).runs.first?.inlinePresentationIntent, .stronglyEmphasized) XCTAssertEqual(plain("C# and word#part"), "C# and word#part") } func testWikiLinksLinksAndPicturesNeverFetch() { XCTAssertEqual(plain("[[Title]] [a **label**](https://example.com/a_(b))"), "Title a label") XCTAssertEqual(plain("\n"), "A lake\npicture") XCTAssertNotNil(FaceMarkdown.set("", theme: .paper).runs.first?.foregroundColor) XCTAssertTrue(FaceMarkdown.set("[label](https://example.com)", theme: .paper).runs.allSatisfy { $0.link == nil }) } func testQuotesRulesAndFencedCode() { XCTAssertEqual(plain("> a *quote*\n---\n```swift\n**literal** #tag\nlet n = 1\n```\nafter"), "a quote\n\n**literal** #tag\nlet n = 1\nafter") XCTAssertEqual(plain("```swift\n~~~\n```"), "~~~\n") let set = FaceMarkdown.set("~~~text\n**raw**\n~~~", theme: .paper) XCTAssertEqual(String(set.characters), "**raw**\n") XCTAssertTrue(set.runs.first?.inlinePresentationIntent?.contains(.code) == true) let native = FaceMarkdown.native(set, font: CTFontCreateWithName("Helvetica" as CFString, 14, nil)) let font = native.attribute(.init(kCTFontAttributeName as String), at: 0, effectiveRange: nil) as! CTFont XCTAssertEqual(CTFontCopyPostScriptName(font) as String, CottonFonts.typeFace) } func testCoreCutsInsideMarksKeepTheirContextAndEveryVisibleWordOnce() { let source = "**one two three four**\n```text\nfive six seven eight\n```\n[[nine ten]]" let cuts = ["**one two", "three four**", "```text\nfive six", "seven eight\n```", "[[nine", "ten]]"] let pages = FaceMarkdown.Document(source, theme: .paper).pages(cuts) XCTAssertEqual(pages.map { String($0.characters) }, ["one two", "three four", "five six", "seven eight", "nine", "ten"]) XCTAssertEqual(pages[1].runs.first?.inlinePresentationIntent, .stronglyEmphasized) XCTAssertTrue(pages[3].runs.first?.inlinePresentationIntent?.contains(.code) == true) } func testLoomCutsWithInsertedWrapsKeepTheSameSourceContext() async { let raw = "**one two three four five six**" let cuts = await PlainPageCutter().cut(raw, columns: 8, first: 2, rest: 2) let set = FaceMarkdown.Document(raw, theme: .paper).pages(cuts.map(\.text)) XCTAssertEqual(set.flatMap { String($0.characters).split(whereSeparator: \.isWhitespace).map(String.init) }, ["one", "two", "three", "four", "five", "six"]) XCTAssertFalse(set.contains { String($0.characters).contains("*") }) }}