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

Method test_interpolation_basics

Lib/test/test_tstring.py:19–80  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

17 )
18
19 def test_interpolation_basics(self):
20 # Test basic interpolation
21 name = "Python"
22 t = t"Hello, {name}"
23 self.assertTStringEqual(t, ("Hello, ", ""), [(name, "name")])
24 self.assertEqual(fstring(t), "Hello, Python")
25
26 # Multiple interpolations
27 first = "Python"
28 last = "Developer"
29 t = t"{first} {last}"
30 self.assertTStringEqual(
31 t, ("", " ", ""), [(first, 'first'), (last, 'last')]
32 )
33 self.assertEqual(fstring(t), "Python Developer")
34
35 # Interpolation with expressions
36 a = 10
37 b = 20
38 t = t"Sum: {a + b}"
39 self.assertTStringEqual(t, ("Sum: ", ""), [(a + b, "a + b")])
40 self.assertEqual(fstring(t), "Sum: 30")
41
42 # Interpolation with function
43 def square(x):
44 return x * x
45 t = t"Square: {square(5)}"
46 self.assertTStringEqual(
47 t, ("Square: ", ""), [(square(5), "square(5)")]
48 )
49 self.assertEqual(fstring(t), "Square: 25")
50
51 # Test attribute access in expressions
52 class Person:
53 def __init__(self, name):
54 self.name = name
55
56 def upper(self):
57 return self.name.upper()
58
59 person = Person("Alice")
60 t = t"Name: {person.name}"
61 self.assertTStringEqual(
62 t, ("Name: ", ""), [(person.name, "person.name")]
63 )
64 self.assertEqual(fstring(t), "Name: Alice")
65
66 # Test method calls
67 t = t"Name: {person.upper()}"
68 self.assertTStringEqual(
69 t, ("Name: ", ""), [(person.upper(), "person.upper()")]
70 )
71 self.assertEqual(fstring(t), "Name: ALICE")
72
73 # Test dictionary access
74 data = {"name": "Bob", "age": 30}
75 t = t"Name: {data['name']}, Age: {data['age']}"
76 self.assertTStringEqual(

Callers

nothing calls this directly

Calls 5

upperMethod · 0.95
fstringFunction · 0.90
assertTStringEqualMethod · 0.80
PersonClass · 0.70
assertEqualMethod · 0.45

Tested by

no test coverage detected