(self)
| 736 | ]) |
| 737 | |
| 738 | def test_comments(self): |
| 739 | # These aren't comments, since they're in strings. |
| 740 | d = {'#': 'hash'} |
| 741 | self.assertEqual(f'{"#"}', '#') |
| 742 | self.assertEqual(f'{d["#"]}', 'hash') |
| 743 | |
| 744 | self.assertAllRaise(SyntaxError, "'{' was never closed", |
| 745 | ["f'{1#}'", # error because everything after '#' is a comment |
| 746 | "f'{#}'", |
| 747 | "f'one: {1#}'", |
| 748 | "f'{1# one} {2 this is a comment still#}'", |
| 749 | ]) |
| 750 | self.assertAllRaise(SyntaxError, r"f-string: unmatched '\)'", |
| 751 | ["f'{)#}'", # When wrapped in parens, this becomes |
| 752 | # '()#)'. Make sure that doesn't compile. |
| 753 | ]) |
| 754 | self.assertEqual(f'''A complex trick: { |
| 755 | 2 # two |
| 756 | }''', 'A complex trick: 2') |
| 757 | self.assertEqual(f''' |
| 758 | { |
| 759 | 40 # forty |
| 760 | + # plus |
| 761 | 2 # two |
| 762 | }''', '\n42') |
| 763 | self.assertEqual(f''' |
| 764 | { |
| 765 | 40 # forty |
| 766 | + # plus |
| 767 | 2 # two |
| 768 | }''', '\n42') |
| 769 | |
| 770 | self.assertEqual(f''' |
| 771 | # this is not a comment |
| 772 | { # the following operation it's |
| 773 | 3 # this is a number |
| 774 | * 2}''', '\n# this is not a comment\n6') |
| 775 | self.assertEqual(f''' |
| 776 | {# f'a {comment}' |
| 777 | 86 # constant |
| 778 | # nothing more |
| 779 | }''', '\n86') |
| 780 | |
| 781 | self.assertAllRaise(SyntaxError, r"f-string: valid expression required before '}'", |
| 782 | ["""f''' |
| 783 | { |
| 784 | # only a comment |
| 785 | }''' |
| 786 | """, # this is equivalent to f'{}' |
| 787 | ]) |
| 788 | |
| 789 | def test_many_expressions(self): |
| 790 | # Create a string with many expressions in it. Note that |
nothing calls this directly
no test coverage detected