restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct SyntaxHighlighterTests {
|
||||
private static let samples: [(String, Language)] = [
|
||||
("<?php\nnamespace App;\n/* a\n block */\nclass A extends B {\n public function run(): string { return \"x $y\"; }\n}\n", .php),
|
||||
("const a = `x ${b} y`\n/* c\n d */ let e = /re/g\n", .javascript),
|
||||
("export const App = () => (\n <div className=\"x\">\n {n.map((i) => <b key={i}>{i}</b>)}\n </div>\n)\n", .tsx),
|
||||
("<html>\n<!-- c\n -->\n<script>\nlet a = 1\n</script>\n<style>a { color: red }</style>\n", .markup),
|
||||
("a {\n color: #fff;\n margin: 0 4px;\n}\n", .css),
|
||||
("{\n \"a\": [1, true, null]\n}\n", .json),
|
||||
("# T\n\n```php\n$x = 1;\n```\n- item `code`\n", .markdown),
|
||||
("#!/bin/sh\nif [ -f x ]; then echo \"$HOME\"; fi\ncat <<EOF\nhi $USER\nEOF\n", .bash),
|
||||
("def f(x):\n \"\"\"doc\n string\"\"\"\n return None\n", .python),
|
||||
("a: 1\nb: |\n text\nc: true # note\n", .yaml),
|
||||
]
|
||||
|
||||
@Test("tokens tile the text end to end with no gaps or overlaps", arguments: samples)
|
||||
func tokensTileTheText(sample: (String, Language)) {
|
||||
let (text, language) = sample
|
||||
let tokens = SyntaxHighlighter.tokens(for: text, language: language)
|
||||
var position = 0
|
||||
for token in tokens {
|
||||
#expect(token.location == position)
|
||||
#expect(token.length > 0)
|
||||
position = token.location + token.length
|
||||
}
|
||||
#expect(position == text.utf16.count)
|
||||
}
|
||||
|
||||
@Test("adjacent runs of one role are merged", arguments: samples)
|
||||
func adjacentRunsAreMerged(sample: (String, Language)) {
|
||||
let tokens = SyntaxHighlighter.tokens(for: sample.0, language: sample.1)
|
||||
for (a, b) in zip(tokens, tokens.dropFirst()) {
|
||||
#expect(a.role != b.role)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("line-by-line tokens with carried state equal whole-buffer tokens", arguments: samples)
|
||||
func incrementalMatchesWholeBuffer(sample: (String, Language)) {
|
||||
let (text, language) = sample
|
||||
var state = HighlightState(language: language, text: text)
|
||||
var offset = 0
|
||||
var painted: [Token] = []
|
||||
for line in text.components(separatedBy: "\n") {
|
||||
for token in SyntaxHighlighter.lineTokens(for: line, state: &state) where token.role != .plain {
|
||||
painted.append(Token(location: token.location + offset, length: token.length, role: token.role))
|
||||
}
|
||||
offset += line.utf16.count + 1
|
||||
}
|
||||
let whole = SyntaxHighlighter.tokens(for: text, language: language).filter { $0.role != .plain }
|
||||
#expect(painted == whole)
|
||||
}
|
||||
|
||||
@Test("an unknown language is one plain run")
|
||||
func unknownLanguageIsPlain() {
|
||||
#expect(SyntaxHighlighter.tokens(for: "a = 1", language: nil) == [Token(location: 0, length: 5, role: .plain)])
|
||||
}
|
||||
|
||||
@Test("an empty buffer has no tokens")
|
||||
func emptyBuffer() {
|
||||
#expect(SyntaxHighlighter.tokens(for: "", language: .php).isEmpty)
|
||||
#expect(SyntaxHighlighter.lineTokens(for: "", language: .javascript).isEmpty)
|
||||
}
|
||||
|
||||
@Test("offsets count UTF-16 units, so text after an emoji lines up")
|
||||
func utf16Offsets() {
|
||||
let text = "const s = \"😀\"; const n = 42"
|
||||
let tokens = SyntaxHighlighter.tokens(for: text, language: .javascript)
|
||||
let number = tokens.first { $0.role == .number }
|
||||
#expect(number?.location == (text as NSString).range(of: "42").location)
|
||||
#expect(number?.length == 2)
|
||||
}
|
||||
|
||||
@Test("CRLF line endings keep the carriage return inside the line")
|
||||
func crlfLines() {
|
||||
let text = "/* a\r\n b */\r\nlet x = 1\r\n"
|
||||
#expect(role("b */", in: text, .javascript) == .comment)
|
||||
#expect(role("let", in: text, .javascript) == .keyword)
|
||||
#expect(role("1", in: text, .javascript, occurrence: 0) == .number)
|
||||
}
|
||||
|
||||
@Test("a single diff line is highlighted on its own, without state from the lines above")
|
||||
func lineStandsAlone() {
|
||||
let tokens = SyntaxHighlighter.lineTokens(for: " end */ let x", language: .javascript)
|
||||
#expect(tokens.contains { $0.role == .keyword })
|
||||
#expect(!tokens.contains { $0.role == .comment })
|
||||
}
|
||||
|
||||
@Test("the state reports an open construct at a line break")
|
||||
func stateReportsOpenConstruct() {
|
||||
var state = HighlightState(language: .javascript)
|
||||
_ = SyntaxHighlighter.lineTokens(for: "/* open", state: &state)
|
||||
#expect(state.isInsideConstruct)
|
||||
_ = SyntaxHighlighter.lineTokens(for: "close */", state: &state)
|
||||
#expect(!state.isInsideConstruct)
|
||||
#expect(state == HighlightState(language: .javascript))
|
||||
}
|
||||
|
||||
@Test("every language survives hostile input without hanging", arguments: Language.allCases)
|
||||
func hostileInput(language: Language) {
|
||||
let text = "<?php \"'`/*<!--<<<EOT\n${{[(\\\n\u{0}é😀\t\r\n?> </script> ``` ---\n" + String(repeating: "\\", count: 20)
|
||||
let tokens = SyntaxHighlighter.tokens(for: text, language: language)
|
||||
#expect(tokens.map(\.length).reduce(0, +) == text.utf16.count)
|
||||
}
|
||||
|
||||
@Test("a 5,000-line file tokenizes well within a second, even in a debug build")
|
||||
func performanceSanity() {
|
||||
let block = """
|
||||
<?php
|
||||
namespace App\\Http\\Controllers;
|
||||
/** Docblock for the class. */
|
||||
final class UserController extends Controller implements HasMiddleware
|
||||
{
|
||||
public function show(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$user = User::query()->where('id', $id)->firstOrFail(); // fetch
|
||||
return response()->json(["name" => "{$user->name} #{$id}", 'count' => 42]);
|
||||
}
|
||||
}
|
||||
|
||||
"""
|
||||
let text = String(repeating: block, count: 5000 / 12 + 1)
|
||||
let clock = ContinuousClock()
|
||||
let elapsed = clock.measure {
|
||||
_ = SyntaxHighlighter.tokens(for: text, language: .php, knownSymbols: ["User"])
|
||||
}
|
||||
#expect(elapsed < .seconds(1))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user