|
1 | 1 | import unittest |
2 | 2 |
|
3 | 3 |
|
| 4 | +_dstring_prefixes = "d db df dt dr drb drf drt".split() |
| 5 | +_dstring_prefixes += [p.upper() for p in _dstring_prefixes] |
| 6 | + |
4 | 7 | class DStringTestCase(unittest.TestCase): |
5 | 8 | def assertAllRaise(self, exception_type, regex, error_strings): |
6 | 9 | for str in error_strings: |
7 | 10 | with self.subTest(str=str): |
8 | 11 | with self.assertRaisesRegex(exception_type, regex) as cm: |
9 | 12 | eval(str) |
10 | | - # print("Testing expression:", repr(str)) |
11 | | - # print(repr(cm.exception)) |
12 | | - # print(repr(cm.exception.text)) |
13 | 13 |
|
14 | 14 | def test_single_quote(self): |
15 | 15 | exprs = [ |
16 | | - "d'hello'", |
17 | | - 'D"hello"', |
18 | | - "d'hello\\nworld'", |
| 16 | + f"{p}'hello, world'" for p in _dstring_prefixes |
| 17 | + ] + [ |
| 18 | + f'{p}"hello, world"' for p in _dstring_prefixes |
19 | 19 | ] |
20 | 20 | self.assertAllRaise(SyntaxError, "d-string must be triple-quoted", exprs) |
21 | 21 |
|
22 | 22 | def test_empty_dstring(self): |
23 | 23 | exprs = [ |
24 | | - "d''''''", |
25 | | - 'D""""""', |
| 24 | + f"{p}''''''" for p in _dstring_prefixes |
| 25 | + ] + [ |
| 26 | + f'{p}""""""' for p in _dstring_prefixes |
26 | 27 | ] |
27 | 28 | self.assertAllRaise(SyntaxError, "d-string must start with a newline", exprs) |
28 | 29 |
|
29 | | - def test_simple_dstring(self): |
30 | | - self.assertEqual(eval('d"""\n hello world\n """'), "hello world\n") |
31 | | - self.assertEqual(eval('d"""\n hello world\n """'), " hello world\n") |
32 | | - self.assertEqual(eval('d"""\n hello world\n"""'), " hello world\n") |
33 | | - self.assertEqual(eval('d"""\n hello world\\\n """'), " hello world") |
34 | | - self.assertEqual(eval('dr"""\n hello world\\\n """'), " hello world\\\n") |
| 30 | + for prefix in _dstring_prefixes: |
| 31 | + expr = f"{prefix}'''\n'''" |
| 32 | + expr2 = f'{prefix}"""\n"""' |
| 33 | + with self.subTest(expr=expr): |
| 34 | + v = eval(expr) |
| 35 | + v2 = eval(expr2) |
| 36 | + if 't' in prefix.lower(): |
| 37 | + self.assertEqual(v.strings, ("",)) |
| 38 | + self.assertEqual(v2.strings, ("",)) |
| 39 | + elif 'b' in prefix.lower(): |
| 40 | + self.assertEqual(v, b"") |
| 41 | + self.assertEqual(v2, b"") |
| 42 | + else: |
| 43 | + self.assertEqual(v, "") |
| 44 | + self.assertEqual(v2, "") |
| 45 | + |
| 46 | + def test_dedent(self): |
| 47 | + s = d""" |
| 48 | + line 1 |
| 49 | + line 2 |
| 50 | + line 3 |
| 51 | + """ |
| 52 | + self.assertEqual("line 1\n line 2\nline 3\n", s) |
| 53 | + |
| 54 | + s = d""" |
| 55 | + line 1 |
| 56 | + line 2 |
| 57 | + line 3 |
| 58 | + """ |
| 59 | + self.assertEqual(" line 1\n line 2\n line 3\n", s) |
| 60 | + |
| 61 | + s = d""" |
| 62 | + line 1 |
| 63 | + line 2 |
| 64 | + line 3 |
| 65 | + """ |
| 66 | + self.assertEqual("line 1\n line 2\nline 3\n ", s) |
| 67 | + |
| 68 | + s = d""" |
| 69 | + line 1 |
| 70 | + line 2 |
| 71 | + line 3""" |
| 72 | + self.assertEqual("line 1\n line 2\nline 3", s) |
35 | 73 |
|
36 | 74 |
|
37 | 75 | if __name__ == '__main__': |
|
0 commit comments