MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_debug_conversion

Method test_debug_conversion

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

Source from the content-addressed store, hash-verified

1560 self.assertEqual(eval('f"\\\r"'), '')
1561
1562 def test_debug_conversion(self):
1563 x = 'A string'
1564 self.assertEqual(f'{x=}', 'x=' + repr(x))
1565 self.assertEqual(f'{x =}', 'x =' + repr(x))
1566 self.assertEqual(f'{x=!s}', 'x=' + str(x))
1567 self.assertEqual(f'{x=!r}', 'x=' + repr(x))
1568 self.assertEqual(f'{x=!a}', 'x=' + ascii(x))
1569
1570 x = 2.71828
1571 self.assertEqual(f'{x=:.2f}', 'x=' + format(x, '.2f'))
1572 self.assertEqual(f'{x=:}', 'x=' + format(x, ''))
1573 self.assertEqual(f'{x=!r:^20}', 'x=' + format(repr(x), '^20'))
1574 self.assertEqual(f'{x=!s:^20}', 'x=' + format(str(x), '^20'))
1575 self.assertEqual(f'{x=!a:^20}', 'x=' + format(ascii(x), '^20'))
1576
1577 x = 9
1578 self.assertEqual(f'{3*x+15=}', '3*x+15=42')
1579
1580 # There is code in ast.c that deals with non-ascii expression values. So,
1581 # use a unicode identifier to trigger that.
1582 tenπ = 31.4
1583 self.assertEqual(f'{tenπ=:.2f}', 'tenπ=31.40')
1584
1585 # Also test with Unicode in non-identifiers.
1586 self.assertEqual(f'{"Σ"=}', '"Σ"=\'Σ\'')
1587
1588 # Make sure nested fstrings still work.
1589 self.assertEqual(f'{f"{3.1415=:.1f}":*^20}', '*****3.1415=3.1*****')
1590
1591 # Make sure text before and after an expression with = works
1592 # correctly.
1593 pi = 'π'
1594 self.assertEqual(f'alpha α {pi=} ω omega', "alpha α pi='π' ω omega")
1595
1596 # Check multi-line expressions.
1597 self.assertEqual(f'''{
15983
1599=}''', '\n3\n=3')
1600
1601 # Since = is handled specially, make sure all existing uses of
1602 # it still work.
1603
1604 self.assertEqual(f'{0==1}', 'False')
1605 self.assertEqual(f'{0!=1}', 'True')
1606 self.assertEqual(f'{0<=1}', 'True')
1607 self.assertEqual(f'{0>=1}', 'False')
1608 self.assertEqual(f'{(x:="5")}', '5')
1609 self.assertEqual(x, '5')
1610 self.assertEqual(f'{(x:=5)}', '5')
1611 self.assertEqual(x, 5)
1612 self.assertEqual(f'{"="}', '=')
1613
1614 x = 20
1615 # This isn't an assignment expression, it's 'x', with a format
1616 # spec of '=10'. See test_walrus: you need to use parens.
1617 self.assertEqual(f'{x:=10}', ' 20')
1618
1619 # Test named function parameters, to make sure '=' parsing works

Callers

nothing calls this directly

Calls 9

assertEqualMethod · 0.95
assertRaisesMethod · 0.95
reprFunction · 0.85
strFunction · 0.85
asciiFunction · 0.85
nowMethod · 0.80
fFunction · 0.70
CClass · 0.70
formatFunction · 0.50

Tested by

no test coverage detected