1
1
import ast
2
2
import unittest
3
3
4
- from string .templatelib import Template , Interpolation
5
-
6
-
7
- def convert (value , conversion ):
8
- if conversion == "a" :
9
- return ascii (value )
10
- elif conversion == "r" :
11
- return repr (value )
12
- elif conversion == "s" :
13
- return str (value )
14
- return value
15
-
16
-
17
- def f (template ):
18
- parts = []
19
- for item in template :
20
- match item :
21
- case str () as s :
22
- parts .append (s )
23
- case Interpolation (value , _, conversion , format_spec ):
24
- value = convert (value , conversion )
25
- value = format (value , format_spec )
26
- parts .append (value )
27
- return "" .join (parts )
4
+ from test .test_string ._support import f
28
5
29
6
30
7
class TestTString (unittest .TestCase ):
@@ -34,77 +11,6 @@ def assertAllRaise(self, exception_type, regex, error_strings):
34
11
with self .assertRaisesRegex (exception_type , regex ):
35
12
eval (s )
36
13
37
- def test_template_basic_creation (self ):
38
- # Simple t-string creation
39
- t = t "Hello, world"
40
- self .assertTrue (isinstance (t , Template ))
41
- self .assertEqual (t .strings , ("Hello, world" ,))
42
- self .assertEqual (len (t .interpolations ), 0 )
43
- self .assertEqual (f (t ), "Hello, world" )
44
-
45
- # Empty t-string
46
- t = t ""
47
- self .assertEqual (t .strings , ("" ,))
48
- self .assertEqual (len (t .interpolations ), 0 )
49
- self .assertEqual (f (t ), "" )
50
-
51
- # Multi-line t-string
52
- t = t """Hello,
53
- world"""
54
- self .assertEqual (t .strings , ("Hello,\n world" ,))
55
- self .assertEqual (len (t .interpolations ), 0 )
56
- self .assertEqual (f (t ), "Hello,\n world" )
57
-
58
- def test_template_creation_interleaving (self ):
59
- # Should add strings on either side
60
- t = Template (Interpolation ("Maria" , "name" , None , "" ))
61
- self .assertEqual (t .strings , ("" , "" ))
62
- self .assertEqual (t .interpolations [0 ].value , "Maria" )
63
- self .assertEqual (t .interpolations [0 ].expression , "name" )
64
- self .assertEqual (t .interpolations [0 ].conversion , None )
65
- self .assertEqual (t .interpolations [0 ].format_spec , "" )
66
- self .assertEqual (f (t ), "Maria" )
67
-
68
- # Should prepend empty string
69
- t = Template (Interpolation ("Maria" , "name" , None , "" ), " is my name" )
70
- self .assertEqual (t .strings , ("" , " is my name" ))
71
- self .assertEqual (t .interpolations [0 ].value , "Maria" )
72
- self .assertEqual (t .interpolations [0 ].expression , "name" )
73
- self .assertEqual (t .interpolations [0 ].conversion , None )
74
- self .assertEqual (t .interpolations [0 ].format_spec , "" )
75
- self .assertEqual (f (t ), "Maria is my name" )
76
-
77
- # Should append empty string
78
- t = Template ("Hello, " , Interpolation ("Maria" , "name" , None , "" ))
79
- self .assertEqual (t .strings , ("Hello, " , "" ))
80
- self .assertEqual (t .interpolations [0 ].value , "Maria" )
81
- self .assertEqual (t .interpolations [0 ].expression , "name" )
82
- self .assertEqual (t .interpolations [0 ].conversion , None )
83
- self .assertEqual (t .interpolations [0 ].format_spec , "" )
84
- self .assertEqual (f (t ), "Hello, Maria" )
85
-
86
- # Should concatenate strings
87
- t = Template ("Hello" , ", " , Interpolation ("Maria" , "name" , None , "" ), "!" )
88
- self .assertEqual (t .strings , ("Hello, " , "!" ))
89
- self .assertEqual (t .interpolations [0 ].value , "Maria" )
90
- self .assertEqual (t .interpolations [0 ].expression , "name" )
91
- self .assertEqual (t .interpolations [0 ].conversion , None )
92
- self .assertEqual (t .interpolations [0 ].format_spec , "" )
93
- self .assertEqual (f (t ), "Hello, Maria!" )
94
-
95
- # Should add strings on either side and in between
96
- t = Template (Interpolation ("Maria" , "name" , None , "" ), Interpolation ("Python" , "language" , None , "" ))
97
- self .assertEqual (t .strings , ("" , "" , "" ))
98
- self .assertEqual (t .interpolations [0 ].value , "Maria" )
99
- self .assertEqual (t .interpolations [0 ].expression , "name" )
100
- self .assertEqual (t .interpolations [0 ].conversion , None )
101
- self .assertEqual (t .interpolations [0 ].format_spec , "" )
102
- self .assertEqual (t .interpolations [1 ].value , "Python" )
103
- self .assertEqual (t .interpolations [1 ].expression , "language" )
104
- self .assertEqual (t .interpolations [1 ].conversion , None )
105
- self .assertEqual (t .interpolations [1 ].format_spec , "" )
106
- self .assertEqual (f (t ), "MariaPython" )
107
-
108
14
def test_string_representation (self ):
109
15
# Test __repr__
110
16
t = t "Hello"
0 commit comments