import AppKit import Testing @testable import Conjure @Test @MainActor func fencedCodeHighlightsTokensWithoutChangingSourceOrApplyingMarkdown() throws { _ = NSApplication.shared for fence in ["```", "~~~~"] { let text = "# Heading 🦊\n\(fence)py\ndef main():\n # comment\n return \"**not bold**\"\n\(fence)\nAfter\n```unknown-language\n# literal\n```" let editor = NSTextView() editor.string = text MarkdownHighlighting.apply(to: editor) let storage = try #require(editor.textStorage) func attributes(_ token: String) -> [NSAttributedString.Key: Any] { storage.attributes(at: (text as NSString).range(of: token).location, effectiveRange: nil) } let keyword = try #require(attributes("def")[.foregroundColor] as? NSColor) let name = try #require(attributes("main")[.foregroundColor] as? NSColor) let comment = try #require(attributes("# comment")[.foregroundColor] as? NSColor) #expect(keyword != name) #expect(comment != keyword) #expect((attributes("**not bold**")[.font] as? NSFont)?.isFixedPitch == true) #expect(storage.string == text) editor.string = "```python\ndef main():\n pass" MarkdownHighlighting.apply(to: editor) let unfinished = try #require(editor.textStorage) #expect(unfinished.attribute(.foregroundColor, at: 10, effectiveRange: nil) as? NSColor == keyword) } } @Test @MainActor func noteHeadingsShrinkByLevelWithoutChangingMarkdownOrCode() throws { _ = NSApplication.shared let editor = NSTextView() let text = "# One\n## Two\n### Three\n#### Four\n##### Five\n###### Six\nBody\n## **Bold** and *italic*\n```text\n# Literal\n```\n####### Not a heading" editor.string = text MarkdownHighlighting.apply(to: editor) let storage = try #require(editor.textStorage) func font(_ token: String) throws -> NSFont { try #require(storage.attribute(.font, at: (editor.string as NSString).range(of: token).location, effectiveRange: nil) as? NSFont) } let sizes = try ["One", "Two", "Three", "Four", "Five", "Six"].map { try font($0).pointSize } for (larger, smaller) in zip(sizes, sizes.dropFirst()) { #expect(larger > smaller) } #expect(try font("Six").pointSize >= font("Body").pointSize) #expect(try font("**Bold**").pointSize == font("Two").pointSize) #expect(try font("*italic*").pointSize == font("Two").pointSize) #expect(try font("Literal").pointSize == font("Body").pointSize) #expect(try font("Not a heading").pointSize == font("Body").pointSize) #expect(try font("Three").isFixedPitch) #expect(editor.string == text) storage.replaceCharacters(in: (editor.string as NSString).range(of: "### Three"), with: "Three") MarkdownHighlighting.apply(to: editor) #expect(try font("Three").pointSize == font("Body").pointSize) }