@@ -150,7 +150,6 @@ def test_raw_tstrings(self):
150150 t = tr "{path}\Documents"
151151 self .assertTStringEqual (t , ("" , r"\Documents" ), [(path , "path" )])
152152
153-
154153 def test_template_concatenation (self ):
155154 # Test template + template
156155 t1 = t "Hello, "
@@ -161,9 +160,10 @@ def test_template_concatenation(self):
161160
162161 # Test template + string
163162 t1 = t "Hello"
164- combined = t1 + ", world"
165- self .assertTStringEqual (combined , ("Hello, world" ,), ())
166- self .assertEqual (fstring (combined ), "Hello, world" )
163+ expected_msg = 'can only concatenate string.templatelib.Template ' \
164+ '\\ (not "str"\\ ) to string.templatelib.Template'
165+ with self .assertRaisesRegex (TypeError , expected_msg ):
166+ t1 + ", world"
167167
168168 # Test template + template with interpolation
169169 name = "Python"
@@ -174,9 +174,10 @@ def test_template_concatenation(self):
174174 self .assertEqual (fstring (combined ), "Hello, Python" )
175175
176176 # Test string + template
177- t = "Hello, " + t "{name}"
178- self .assertTStringEqual (t , ("Hello, " , "" ), [(name , "name" )])
179- self .assertEqual (fstring (t ), "Hello, Python" )
177+ expected_msg = 'can only concatenate str ' \
178+ '\\ (not "string.templatelib.Template"\\ ) to str'
179+ with self .assertRaisesRegex (TypeError , expected_msg ):
180+ "Hello, " + t "{name}"
180181
181182 def test_nested_templates (self ):
182183 # Test a template inside another template expression
@@ -241,52 +242,28 @@ def test_literal_concatenation(self):
241242 self .assertTStringEqual (t , ("Hello, " , "" ), [(name , "name" )])
242243 self .assertEqual (fstring (t ), "Hello, Python" )
243244
244- # Test concatenation with string literal
245- name = "Python"
246- t = t "Hello, {name}" "and welcome!"
247- self .assertTStringEqual (
248- t , ("Hello, " , "and welcome!" ), [(name , "name" )]
249- )
250- self .assertEqual (fstring (t ), "Hello, Pythonand welcome!" )
251-
252- # Test concatenation with Unicode literal
253- name = "Python"
254- t = t "Hello, {name}" u"and welcome!"
255- self .assertTStringEqual (
256- t , ("Hello, " , "and welcome!" ), [(name , "name" )]
257- )
258- self .assertEqual (fstring (t ), "Hello, Pythonand welcome!" )
259-
260- # Test concatenation with f-string literal
261- tab = '\t '
262- t = t "Tab: {tab}. " f"f-tab: { tab } ."
263- self .assertTStringEqual (t , ("Tab: " , ". f-tab: \t ." ), [(tab , "tab" )])
264- self .assertEqual (fstring (t ), "Tab: \t . f-tab: \t ." )
265-
266- # Test concatenation with raw string literal
267- tab = '\t '
268- t = t "Tab: {tab}. " r"Raw tab: \t."
269- self .assertTStringEqual (
270- t , ("Tab: " , r". Raw tab: \t." ), [(tab , "tab" )]
271- )
272- self .assertEqual (fstring (t ), "Tab: \t . Raw tab: \\ t." )
273-
274- # Test concatenation with raw f-string literal
275- tab = '\t '
276- t = t "Tab: {tab}. " rf"f-tab: { tab } . Raw tab: \t."
277- self .assertTStringEqual (
278- t , ("Tab: " , ". f-tab: \t . Raw tab: \\ t." ), [(tab , "tab" )]
279- )
280- self .assertEqual (fstring (t ), "Tab: \t . f-tab: \t . Raw tab: \\ t." )
281-
245+ # Test disallowed mix of t-string and string/f-string (incl. bytes)
282246 what = 't'
283- expected_msg = 'cannot mix bytes and nonbytes literals'
247+ expected_msg = 'cannot mix t-string literals with string or bytes literals'
284248 for case in (
249+ "t'{what}-string literal' 'str literal'" ,
250+ "t'{what}-string literal' u'unicode literal'" ,
251+ "t'{what}-string literal' f'f-string literal'" ,
252+ "t'{what}-string literal' r'raw string literal'" ,
253+ "t'{what}-string literal' rf'raw f-string literal'" ,
285254 "t'{what}-string literal' b'bytes literal'" ,
286255 "t'{what}-string literal' br'raw bytes literal'" ,
256+ "'str literal' t'{what}-string literal'" ,
257+ "u'unicode literal' t'{what}-string literal'" ,
258+ "f'f-string literal' t'{what}-string literal'" ,
259+ "r'raw string literal' t'{what}-string literal'" ,
260+ "rf'raw f-string literal' t'{what}-string literal'" ,
261+ "b'bytes literal' t'{what}-string literal'" ,
262+ "br'raw bytes literal' t'{what}-string literal'" ,
287263 ):
288- with self .assertRaisesRegex (SyntaxError , expected_msg ):
289- eval (case )
264+ with self .subTest (case ):
265+ with self .assertRaisesRegex (SyntaxError , expected_msg ):
266+ eval (case )
290267
291268 def test_triple_quoted (self ):
292269 # Test triple-quoted t-strings
0 commit comments