mirror of
https://github.com/micropython/micropython.git
synced 2026-03-10 19:00:30 +01:00
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>
113 lines
3.5 KiB
Python
113 lines
3.5 KiB
Python
from string.templatelib import Template, Interpolation
|
|
|
|
print("=== Constructor basic usage ===")
|
|
t = Template("hello ", Interpolation(42, "x"), "world")
|
|
print(f"Template repr: {repr(t)}")
|
|
|
|
t_varargs = Template("Hello ", Interpolation("World", "name"), "!")
|
|
print(f"Varargs constructor: strings={t_varargs.strings}, values={t_varargs.values}")
|
|
|
|
t_concat = Template("A", "B", Interpolation(1, "value"), "C", "D")
|
|
print(f"Varargs merged strings: {t_concat.strings}")
|
|
|
|
t_leading = Template(Interpolation(1, "x"), " tail")
|
|
print(f"Leading interpolation strings: {t_leading.strings}")
|
|
|
|
t_trailing = Template("head ", Interpolation(2, "y"))
|
|
print(f"Trailing interpolation strings: {t_trailing.strings}")
|
|
|
|
t_interps_only = Template(Interpolation(1, "x"), Interpolation(2, "y"))
|
|
print(f"Interpolation only strings: {t_interps_only.strings}")
|
|
|
|
print("\n=== Special cases ===")
|
|
|
|
i = Interpolation(42, "x", "s", ":>10")
|
|
try:
|
|
i.value = 100
|
|
except AttributeError:
|
|
print("Interp read-only: AttributeError")
|
|
|
|
t_ws_trim = Template("", Interpolation(None, " ", None, ""), "")
|
|
print(f"Whitespace trim: '{t_ws_trim}'")
|
|
|
|
t_debug = Template("", Interpolation(42, "x=", None, ""), "")
|
|
print(f"Debug =: {t_debug}")
|
|
|
|
|
|
class Custom:
|
|
def __repr__(self):
|
|
return "CustomRepr"
|
|
|
|
def __str__(self):
|
|
return "CustomStr"
|
|
|
|
|
|
obj = Custom()
|
|
print(f"Custom !r: {t'{obj!r}'}")
|
|
print(f"Custom !s: {t'{obj!s}'}")
|
|
|
|
t_empty_start = Template("", Interpolation(1, "1"), "text")
|
|
print(f"Empty start iter: {[type(x).__name__ for x in t_empty_start]}")
|
|
|
|
t_iter_edge = Template("", Interpolation(1, "1"), "", Interpolation(2, "2"), "")
|
|
iter_items = []
|
|
for item in t_iter_edge:
|
|
iter_items.append(type(item).__name__)
|
|
print(f"Iterator edge: {iter_items}")
|
|
|
|
print("\n=== Values property ===")
|
|
for n in range(7):
|
|
args = []
|
|
for i in range(n):
|
|
args.append("")
|
|
args.append(Interpolation(i, str(i)))
|
|
args.append("")
|
|
t = Template(*args)
|
|
print(f"Values[{n}]: {t.values}")
|
|
|
|
print("\n=== Multiple consecutive strings ===")
|
|
try:
|
|
t = Template("first", "second", "third", Interpolation(42, "x"), "fourth", "fifth")
|
|
if t.strings == ("firstsecondthird", "fourthfifth"):
|
|
print("Multiple strings concatenated: OK")
|
|
else:
|
|
print(f"Multiple strings: strings={t.strings}")
|
|
except Exception as e:
|
|
print(f"Multiple strings error: {e}")
|
|
|
|
print("\n=== Template() constructor with many interpolations ===")
|
|
try:
|
|
exprs = ["a", "b", "c", "d", "e"]
|
|
interps = [Interpolation(i, exprs[i % len(exprs)]) for i in range(20)]
|
|
strings = [""] * 21
|
|
t = Template(*strings, *interps)
|
|
print(f"Template() constructor: OK ({len(t.interpolations)} interpolations)")
|
|
except Exception as e:
|
|
print(f"Template() constructor: {type(e).__name__}")
|
|
|
|
print("\n=== vstr string concatenation ===")
|
|
try:
|
|
t1 = Template("part1", "part2", "part3", "part4", Interpolation(1, "x"), "end")
|
|
result = str(t1)
|
|
print(f"vstr concat: '{result}'")
|
|
except Exception as e:
|
|
print(f"vstr concat error: {e}")
|
|
|
|
print("\n=== High byte handling ===")
|
|
try:
|
|
result = t"\x7f\x80\x81\xfe\xff"
|
|
first_str = result.strings[0]
|
|
print(
|
|
f"High bytes: len={len(first_str)}, first=0x{ord(first_str[0]):02x}, last=0x{ord(first_str[-1]):02x}"
|
|
)
|
|
except Exception as e:
|
|
print(f"High bytes error: {e}")
|
|
|
|
try:
|
|
result = t"\200\201\377"
|
|
print(f"Octal high bytes: OK, len={len(result.strings[0])}")
|
|
except Exception as e:
|
|
print(f"Octal high bytes error: {e}")
|
|
|
|
print("\nConstructor tests completed!")
|