Files
micropython/tests/basics/string_tstring_operations.py
Koudai Aono bdef10a92b tests: Add full feature and coverage tests for PEP 750 template strings.
Includes corresponding .exp files because this feature is only available in
Python 3.14+.

Tests for `!a` conversion specifier and space after `!` are not included
because they are not supported by MicroPython.

Signed-off-by: Koudai Aono <koxudaxi@gmail.com>
Signed-off-by: Damien George <damien@micropython.org>
2026-03-09 23:47:33 +11:00

56 lines
1.4 KiB
Python

from string.templatelib import Template, Interpolation
print("\n=== Binary operations ===")
t1 = t"template"
try:
t1 + "string"
except TypeError as e:
print(f"Template+str: TypeError")
try:
"string" + t1
except TypeError as e:
print(f"str+Template: TypeError")
try:
42 + t1
except TypeError:
print("int+Template: TypeError")
for op in ["-", "*", "/", "%", "**", "&", "|", "^", "<<", ">>"]:
try:
eval(f"t1 {op} t1")
except TypeError:
print(f"{op}: unsupported")
print("\n=== Template.__add__ with multiple interpolations ===")
try:
exprs = ["a", "b", "c", "d", "e"]
interps1 = [Interpolation(i, exprs[i % len(exprs)]) for i in range(20)]
strings1 = [""] * 21
t1 = Template(*strings1, *interps1)
interps2 = [Interpolation(i + 20, exprs[i % len(exprs)]) for i in range(20)]
strings2 = [""] * 21
t2 = Template(*strings2, *interps2)
result = t1 + t2
print(f"Template.__add__: OK ({len(result.interpolations)} interpolations)")
except Exception as e:
print(f"Template.__add__: {type(e).__name__}")
print("\n=== Template + non-string object ===")
try:
class CustomObj:
pass
t1 = t"hello"
result = t1 + CustomObj()
print("ERROR: Should have raised TypeError")
except TypeError as e:
print(f"Template + custom object: TypeError (correct)")
print("\n=== 2-tuple constructor overflow ===")