Coverage for tests / unit / formatters / styles / test_style_html.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Unit tests for HtmlStyle formatter. 

2 

3Tests verify HtmlStyle correctly formats tabular data as valid HTML tables 

4with proper escaping for security. 

5""" 

6 

7from __future__ import annotations 

8 

9from assertpy import assert_that 

10 

11from lintro.formatters.styles.html import HtmlStyle 

12 

13from .conftest import MULTI_ROW_DATA, SINGLE_ROW_DATA, STANDARD_COLUMNS, TWO_COLUMNS 

14 

15 

16def test_html_style_single_row_produces_valid_table(html_style: HtmlStyle) -> None: 

17 """HtmlStyle formats single row as valid HTML table. 

18 

19 Args: 

20 html_style: The HtmlStyle formatter instance. 

21 """ 

22 result = html_style.format(STANDARD_COLUMNS, SINGLE_ROW_DATA) 

23 

24 assert_that(result).contains("<table>") 

25 assert_that(result).contains("</table>") 

26 assert_that(result).contains("<th>File</th>") 

27 assert_that(result).contains("<td>src/main.py</td>") 

28 

29 

30def test_html_style_multiple_rows_produces_correct_row_count( 

31 html_style: HtmlStyle, 

32) -> None: 

33 """HtmlStyle formats multiple rows with correct number of table rows. 

34 

35 Args: 

36 html_style: The HtmlStyle formatter instance. 

37 """ 

38 result = html_style.format(TWO_COLUMNS, MULTI_ROW_DATA) 

39 

40 assert_that(result).contains("src/a.py") 

41 assert_that(result).contains("src/b.py") 

42 assert_that(result.count("<tr>")).is_equal_to(3) # 1 header + 2 data rows 

43 

44 

45def test_html_style_escapes_script_tags(html_style: HtmlStyle) -> None: 

46 """HtmlStyle escapes HTML script tags to prevent XSS. 

47 

48 Args: 

49 html_style: The HtmlStyle formatter instance. 

50 """ 

51 result = html_style.format(["Message"], [["<script>alert('XSS')</script>"]]) 

52 

53 assert_that(result).contains("&lt;script&gt;") 

54 assert_that(result).does_not_contain("<script>") 

55 

56 

57def test_html_style_escapes_ampersand(html_style: HtmlStyle) -> None: 

58 """HtmlStyle escapes ampersand characters. 

59 

60 Args: 

61 html_style: The HtmlStyle formatter instance. 

62 """ 

63 result = html_style.format(["Message"], [["A & B"]]) 

64 

65 assert_that(result).contains("A &amp; B") 

66 

67 

68def test_html_style_row_shorter_than_columns(html_style: HtmlStyle) -> None: 

69 """HtmlStyle handles row with fewer elements than columns. 

70 

71 Args: 

72 html_style: The HtmlStyle formatter instance. 

73 """ 

74 result = html_style.format(STANDARD_COLUMNS, [["src/main.py"]]) 

75 

76 assert_that(result).contains("<td>src/main.py</td>")