Files
conjure/Tests/ConjureKitTests/CodeHighlightingTests.swift
T
2026-10-19 15:59:03 +02:00

30 lines
1.5 KiB
Swift

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((attributes("After")[.font] as? NSFont)?.isFixedPitch == false)
#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)
}
}