Coverage for tests/test_toc.py: 100.00%

25 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-17 14:36 +0200

1"""Tests for the logic updating the table of contents.""" 

2 

3from __future__ import annotations 

4 

5from textwrap import dedent 

6from typing import TYPE_CHECKING 

7 

8from markdown.extensions.toc import TocExtension 

9 

10if TYPE_CHECKING: 

11 from markdown import Markdown 

12 

13 

14def test_updating_toc(md: Markdown) -> None: 

15 """Assert ToC is updated with generated headings. 

16 

17 Parameters: 

18 md: A Markdown instance (fixture). 

19 """ 

20 TocExtension().extendMarkdown(md) 

21 html = md.convert( 

22 dedent( 

23 """ 

24 ```python exec="yes" 

25 print("# big heading") 

26 ``` 

27 """, 

28 ), 

29 ) 

30 assert "<h1" in html 

31 assert "big-heading" in md.toc # type: ignore[attr-defined] 

32 

33 

34def test_not_updating_toc(md: Markdown) -> None: 

35 """Assert ToC is not updated with generated headings. 

36 

37 Parameters: 

38 md: A Markdown instance (fixture). 

39 """ 

40 TocExtension().extendMarkdown(md) 

41 html = md.convert( 

42 dedent( 

43 """ 

44 ```python exec="yes" updatetoc="no" 

45 print("# big heading") 

46 ``` 

47 """, 

48 ), 

49 ) 

50 assert "<h1" in html 

51 assert "big-heading" not in md.toc # type: ignore[attr-defined] 

52 

53 

54def test_both_updating_and_not_updating_toc(md: Markdown) -> None: 

55 """Assert ToC is not updated with generated headings. 

56 

57 Parameters: 

58 md: A Markdown instance (fixture). 

59 """ 

60 TocExtension().extendMarkdown(md) 

61 html = md.convert( 

62 dedent( 

63 """ 

64 ```python exec="yes" updatetoc="no" 

65 print("# big heading") 

66 ``` 

67 

68 ```python exec="yes" updatetoc="yes" 

69 print("## medium heading") 

70 ``` 

71 

72 ```python exec="yes" updatetoc="no" 

73 print("### small heading") 

74 ``` 

75 

76 ```python exec="yes" updatetoc="yes" 

77 print("#### tiny heading") 

78 ``` 

79 """, 

80 ), 

81 ) 

82 assert "<h1" in html 

83 assert "<h2" in html 

84 assert "<h3" in html 

85 assert "<h4" in html 

86 assert "big-heading" not in md.toc # type: ignore[attr-defined] 

87 assert "medium-heading" in md.toc # type: ignore[attr-defined] 

88 assert "small-heading" not in md.toc # type: ignore[attr-defined] 

89 assert "tiny-heading" in md.toc # type: ignore[attr-defined]