🐐 Escape sequences
This commit is contained in:
57
tests.py
57
tests.py
@@ -1080,6 +1080,63 @@ class TestInterpolation(unittest.TestCase):
|
||||
run_test(self, source, nodes, value, output)
|
||||
|
||||
|
||||
# --- Escape sequences ---
|
||||
|
||||
escape_tests = [
|
||||
# \n → newline
|
||||
('"hello\\nworld"',
|
||||
Program([], [ExpressionStatement(String("hello\nworld"))]),
|
||||
ValStr("hello\nworld")),
|
||||
# \t → tab
|
||||
('"col\\tcol"',
|
||||
Program([], [ExpressionStatement(String("col\tcol"))]),
|
||||
ValStr("col\tcol")),
|
||||
# \r → carriage return
|
||||
('"line\\rover"',
|
||||
Program([], [ExpressionStatement(String("line\rover"))]),
|
||||
ValStr("line\rover")),
|
||||
# \\ → literal backslash
|
||||
('"back\\\\slash"',
|
||||
Program([], [ExpressionStatement(String("back\\slash"))]),
|
||||
ValStr("back\\slash")),
|
||||
# \" → literal double quote
|
||||
('"say \\"salve\\""',
|
||||
Program([], [ExpressionStatement(String('say "salve"'))]),
|
||||
ValStr('say "salve"')),
|
||||
# \' → literal single quote in single-quoted string
|
||||
("'it\\'s'",
|
||||
Program([], [ExpressionStatement(String("it's"))]),
|
||||
ValStr("it's")),
|
||||
# \n in single-quoted string
|
||||
("'hello\\nworld'",
|
||||
Program([], [ExpressionStatement(String("hello\nworld"))]),
|
||||
ValStr("hello\nworld")),
|
||||
# escape inside interpolated string
|
||||
('DESIGNA name VT "Roma"\n"salve\\n{name}"',
|
||||
Program([], [
|
||||
Designa(ID("name"), String("Roma")),
|
||||
ExpressionStatement(InterpolatedString([String("salve\n"), ID("name")]))
|
||||
]), ValStr("salve\nRoma")),
|
||||
# DIC with newline escape
|
||||
('DIC("hello\\nworld")',
|
||||
Program([], [ExpressionStatement(BuiltIn("DIC", [String("hello\nworld")]))]),
|
||||
ValStr("hello\nworld"), "hello\nworld\n"),
|
||||
# multiple escapes in one string
|
||||
('"\\t\\n\\\\"',
|
||||
Program([], [ExpressionStatement(String("\t\n\\"))]),
|
||||
ValStr("\t\n\\")),
|
||||
# unknown escapes pass through (regex backrefs)
|
||||
('"\\1\\2"',
|
||||
Program([], [ExpressionStatement(String("\\1\\2"))]),
|
||||
ValStr("\\1\\2")),
|
||||
]
|
||||
|
||||
class TestEscapeSequences(unittest.TestCase):
|
||||
@parameterized.expand(escape_tests)
|
||||
def test_escape(self, source, nodes, value, output=""):
|
||||
run_test(self, source, nodes, value, output)
|
||||
|
||||
|
||||
# --- Comparison operators ---
|
||||
|
||||
comparison_tests = [
|
||||
|
||||
Reference in New Issue
Block a user