51 lines
1.6 KiB
Swift
51 lines
1.6 KiB
Swift
@testable import HelderCore
|
|
import Testing
|
|
|
|
@Suite struct LexerTests {
|
|
private func lexer(_ text: String) -> Lexer {
|
|
Lexer(units: Array(text.utf16), symbols: [])
|
|
}
|
|
|
|
@Test("numbers take prefixes, separators, fractions, exponents and suffixes",
|
|
arguments: ["0x1F", "0b1010", "1_000_000", "3.14", "1e10", "2.5E-3", "10n"])
|
|
func numbers(literal: String) {
|
|
var lx = lexer(literal + " rest")
|
|
lx.number()
|
|
#expect(lx.i == literal.utf16.count)
|
|
#expect(lx.tokens == [Token(location: 0, length: literal.utf16.count, role: .number)])
|
|
}
|
|
|
|
@Test("an unterminated string stops at the line end and says so")
|
|
func unterminatedString() {
|
|
var lx = lexer("'abc")
|
|
lx.i = 1
|
|
let closed = lx.quoted(from: 0, quote: Unit.apostrophe)
|
|
#expect(!closed)
|
|
#expect(lx.i == 4)
|
|
}
|
|
|
|
@Test("an escape at the very end of a line never runs past it")
|
|
func escapeAtLineEnd() {
|
|
var lx = lexer("'ab\\")
|
|
lx.i = 1
|
|
let closed = lx.quoted(from: 0, quote: Unit.apostrophe)
|
|
#expect(!closed)
|
|
#expect(lx.i == 4)
|
|
}
|
|
|
|
@Test("emitting next to a run of the same role extends it")
|
|
func mergesAdjacentRuns() {
|
|
var lx = lexer("abcd")
|
|
lx.emit(0, 2, .keyword)
|
|
lx.emit(2, 4, .keyword)
|
|
#expect(lx.tokens == [Token(location: 0, length: 4, role: .keyword)])
|
|
}
|
|
|
|
@Test("non-ASCII units count as identifier letters")
|
|
func unicodeIdentifiers() {
|
|
#expect(Unit.isIdentStart(Array("é".utf16)[0]))
|
|
#expect(Array("😀".utf16).allSatisfy(Unit.isIdentPart))
|
|
#expect(!Unit.isIdentStart(Unit.minus))
|
|
}
|
|
}
|