Coverage for tdom / template_utils_test.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-01-12 16:43 +0000

1from string.templatelib import Interpolation 

2 

3import pytest 

4 

5from .template_utils import TemplateRef, template_from_parts, combine_template_refs 

6 

7 

8def test_template_from_parts() -> None: 

9 strings = ("Hello, ", "! Today is ", ".") 

10 interpolations = (Interpolation("Alice"), Interpolation("Monday")) 

11 template = template_from_parts(strings, interpolations) 

12 assert template.strings == strings 

13 assert template.interpolations == interpolations 

14 

15 

16def test_template_ref_is_literal() -> None: 

17 literal_ref = TemplateRef.literal("Hello") 

18 assert literal_ref.is_literal 

19 

20 non_literal_ref = TemplateRef(("", ""), (0,)) 

21 assert not non_literal_ref.is_literal 

22 

23 

24def test_template_ref_is_empty() -> None: 

25 empty_ref = TemplateRef.empty() 

26 assert empty_ref.is_empty 

27 

28 non_empty_ref = TemplateRef.literal("Hello") 

29 assert not non_empty_ref.is_empty 

30 

31 

32def test_template_ref_is_singleton() -> None: 

33 singleton_ref = TemplateRef.singleton(0) 

34 assert singleton_ref.is_singleton 

35 

36 non_singleton_ref = TemplateRef.literal("Hello") 

37 assert not non_singleton_ref.is_singleton 

38 

39 

40def test_template_ref_post_init_validation() -> None: 

41 with pytest.raises(ValueError): 

42 _ = TemplateRef(("Hello",), (0, 1)) 

43 

44 

45def test_combine_template_refs(): 

46 template_refs = map( 

47 TemplateRef.from_naive_template, 

48 [ 

49 t"ab", 

50 t"c{0}d", 

51 t"ef{1}", 

52 t"{2}ghi", 

53 ], 

54 ) 

55 assert combine_template_refs(*template_refs) == TemplateRef.from_naive_template( 

56 t"abc{0}def{1}{2}ghi" 

57 )