MCPcopy Index your code
hub / github.com/python/cpython / test_format_specifier_expressions

Method test_format_specifier_expressions

Lib/test/test_fstring.py:814–845  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

812 self.assertEqual(eval(s), '1xy' * 1024)
813
814 def test_format_specifier_expressions(self):
815 width = 10
816 precision = 4
817 value = decimal.Decimal('12.34567')
818 self.assertEqual(f'result: {value:{width}.{precision}}', 'result: 12.35')
819 self.assertEqual(f'result: {value:{width!r}.{precision}}', 'result: 12.35')
820 self.assertEqual(f'result: {value:{width:0}.{precision:1}}', 'result: 12.35')
821 self.assertEqual(f'result: {value:{1}{0:0}.{precision:1}}', 'result: 12.35')
822 self.assertEqual(f'result: {value:{ 1}{ 0:0}.{ precision:1}}', 'result: 12.35')
823 self.assertEqual(f'{10:#{1}0x}', ' 0xa')
824 self.assertEqual(f'{10:{"#"}1{0}{"x"}}', ' 0xa')
825 self.assertEqual(f'{-10:-{"#"}1{0}x}', ' -0xa')
826 self.assertEqual(f'{-10:{"-"}#{1}0{"x"}}', ' -0xa')
827 self.assertEqual(f'{10:#{3 != {4:5} and width}x}', ' 0xa')
828 self.assertEqual(f'result: {value:{width:{0}}.{precision:1}}', 'result: 12.35')
829
830 self.assertAllRaise(SyntaxError, "f-string: expecting ':' or '}'",
831 ["""f'{"s"!r{":10"}}'""",
832 # This looks like a nested format spec.
833 ])
834
835 self.assertAllRaise(SyntaxError,
836 "f-string: expecting a valid expression after '{'",
837 [# Invalid syntax inside a nested spec.
838 "f'{4:{/5}}'",
839 ])
840
841 self.assertAllRaise(SyntaxError, 'f-string: invalid conversion character',
842 [# No expansion inside conversion or for
843 # the : or ! itself.
844 """f'{"s"!{"r"}}'""",
845 ])
846
847 def test_custom_format_specifier(self):
848 class CustomFormat:

Callers

nothing calls this directly

Calls 2

assertEqualMethod · 0.95
assertAllRaiseMethod · 0.95

Tested by

no test coverage detected