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
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Unit tests for HtmlStyle formatter.
3Tests verify HtmlStyle correctly formats tabular data as valid HTML tables
4with proper escaping for security.
5"""
7from __future__ import annotations
9from assertpy import assert_that
11from lintro.formatters.styles.html import HtmlStyle
13from .conftest import MULTI_ROW_DATA, SINGLE_ROW_DATA, STANDARD_COLUMNS, TWO_COLUMNS
16def test_html_style_single_row_produces_valid_table(html_style: HtmlStyle) -> None:
17 """HtmlStyle formats single row as valid HTML table.
19 Args:
20 html_style: The HtmlStyle formatter instance.
21 """
22 result = html_style.format(STANDARD_COLUMNS, SINGLE_ROW_DATA)
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>")
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.
35 Args:
36 html_style: The HtmlStyle formatter instance.
37 """
38 result = html_style.format(TWO_COLUMNS, MULTI_ROW_DATA)
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
45def test_html_style_escapes_script_tags(html_style: HtmlStyle) -> None:
46 """HtmlStyle escapes HTML script tags to prevent XSS.
48 Args:
49 html_style: The HtmlStyle formatter instance.
50 """
51 result = html_style.format(["Message"], [["<script>alert('XSS')</script>"]])
53 assert_that(result).contains("<script>")
54 assert_that(result).does_not_contain("<script>")
57def test_html_style_escapes_ampersand(html_style: HtmlStyle) -> None:
58 """HtmlStyle escapes ampersand characters.
60 Args:
61 html_style: The HtmlStyle formatter instance.
62 """
63 result = html_style.format(["Message"], [["A & B"]])
65 assert_that(result).contains("A & B")
68def test_html_style_row_shorter_than_columns(html_style: HtmlStyle) -> None:
69 """HtmlStyle handles row with fewer elements than columns.
71 Args:
72 html_style: The HtmlStyle formatter instance.
73 """
74 result = html_style.format(STANDARD_COLUMNS, [["src/main.py"]])
76 assert_that(result).contains("<td>src/main.py</td>")