Files

40 lines
1.6 KiB
Swift

import HelderCore
import Testing
@Suite struct PythonGrammarTests {
@Test("keywords, builtins, constants and comments")
func basics() {
let text = "def run(x):\n if x is None: # guard\n return len(x)\n return True"
#expect(role("def", in: text, .python) == .keyword)
#expect(role("run", in: text, .python) == .plain)
#expect(role("None", in: text, .python) == .number)
#expect(role("# guard", in: text, .python) == .comment)
#expect(role("len", in: text, .python) == .number)
#expect(role("True", in: text, .python) == .number)
}
@Test("a triple-quoted string carries over lines")
func tripleQuoted() {
let text = "x = '''one\ntwo # not a comment\nthree'''\ny = 1"
#expect(role("two # not a comment", in: text, .python) == .string)
#expect(role("1", in: text, .python) == .number)
}
@Test("string prefixes belong to the string", arguments: ["f\"a{b}\"", "rb'raw'", "u\"x\""])
func prefixes(literal: String) {
#expect(role(literal, in: "v = \(literal)", .python) == .string)
}
@Test("a decorator at the start of a line reads as a keyword; matrix @ does not")
func decorators() {
let text = "@app.route('/')\ndef f(): return a @ b"
#expect(role("@app.route", in: text, .python) == .keyword)
#expect(role("@", in: text, .python, occurrence: 1) == .plain)
}
@Test("the name after class reads as a class")
func className() {
#expect(role("Agent", in: "class Agent(Base):", .python) == .variable)
}
}