forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dstring.py
More file actions
47 lines (38 loc) · 1.58 KB
/
test_dstring.py
File metadata and controls
47 lines (38 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import unittest
class DStringTestCase(unittest.TestCase):
def assertAllRaise(self, exception_type, regex, error_strings):
for str in error_strings:
with self.subTest(str=str):
with self.assertRaisesRegex(exception_type, regex) as cm:
eval(str)
# print("Testing expression:", repr(str))
# print(repr(cm.exception))
# print(repr(cm.exception.text))
def test_single_quote(self):
exprs = [
"d'hello'",
'D"hello"',
"d'hello\\nworld'",
]
self.assertAllRaise(SyntaxError, "d-string must be triple-quoted", exprs)
def test_empty_dstring(self):
exprs = [
"d''''''",
'D""""""',
]
self.assertAllRaise(SyntaxError, "d-string must start with a newline", exprs)
def test_no_last_newline(self):
exprs = [
"d'''\nhello world'''",
'D"""\nhello world"""',
"df'''\nhello {42}'''",
]
self.assertAllRaise(SyntaxError, "d-string must end with an indent line", exprs)
def test_simple_dstring(self):
self.assertEqual(eval('d"""\n hello world\n """'), "hello world\n")
self.assertEqual(eval('d"""\n hello world\n """'), " hello world\n")
self.assertEqual(eval('d"""\n hello world\n"""'), " hello world\n")
self.assertEqual(eval('d"""\n hello world\\\n """'), " hello world")
self.assertEqual(eval('dr"""\n hello world\\\n """'), " hello world\\\n")
if __name__ == '__main__':
unittest.main()