Coverage for tdom/placeholders_test.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-19 21:37 +0000

1import pytest 

2 

3from .placeholders import ( 

4 PlaceholderState, 

5 make_placeholder_config, 

6) 

7 

8 

9def test_make_placeholder() -> None: 

10 config = make_placeholder_config() 

11 assert config.make_placeholder(0) == f"{config.prefix}0{config.suffix}" 

12 assert config.make_placeholder(42) == f"{config.prefix}42{config.suffix}" 

13 

14 

15def test_match_placeholders() -> None: 

16 config = make_placeholder_config() 

17 s = f"Start {config.prefix}0{config.suffix} middle {config.prefix}1{config.suffix} end" 

18 matches = config.match_placeholders(s) 

19 assert len(matches) == 2 

20 assert matches[0].group(0) == f"{config.prefix}0{config.suffix}" 

21 assert matches[0][1] == "0" 

22 assert matches[1].group(0) == f"{config.prefix}1{config.suffix}" 

23 assert matches[1][1] == "1" 

24 

25 

26def test_find_placeholders() -> None: 

27 config = make_placeholder_config() 

28 s = f"Hello {config.prefix}0{config.suffix}, today is {config.prefix}1{config.suffix}." 

29 pt = config.find_placeholders(s) 

30 assert pt.strings == ("Hello ", ", today is ", ".") 

31 assert (pt.i_start, pt.i_stop) == (0, 2) 

32 

33 literal_s = "No placeholders here." 

34 literal_pt = config.find_placeholders(literal_s) 

35 assert literal_pt.strings == (literal_s,) 

36 assert literal_pt.i_count == 0 

37 

38 

39def test_find_placeholders_rejects_discontiguous_indexes() -> None: 

40 config = make_placeholder_config() 

41 text = f"{config.make_placeholder(1)}{config.make_placeholder(3)}" 

42 

43 with pytest.raises(ValueError, match="indexes must be contiguous"): 

44 _ = config.find_placeholders(text) 

45 

46 

47def test_placeholder_state() -> None: 

48 config = make_placeholder_config() 

49 state = PlaceholderState(config=config) 

50 assert state.is_empty 

51 

52 p0 = state.add_placeholder(0) 

53 assert p0 == config.make_placeholder(0) 

54 assert not state.is_empty 

55 

56 p1 = state.add_placeholder(1) 

57 assert p1 == config.make_placeholder(1) 

58 

59 text = f"Values: {p0}, {p1}" 

60 pt = state.remove_placeholders(text) 

61 assert pt.strings == ("Values: ", ", ", "") 

62 assert (pt.i_start, pt.i_stop) == (0, 2) 

63 assert state.is_empty 

64 

65 with pytest.raises(ValueError): 

66 state.remove_placeholders(f"Unknown placeholder: {config.make_placeholder(2)}")