Coverage for tests / unit / parsers / streaming / test_text_lines.py: 100%
26 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"""Tests for stream_text_lines function."""
3from __future__ import annotations
5from typing import TYPE_CHECKING
7from assertpy import assert_that
9from lintro.parsers.streaming import stream_text_lines
11if TYPE_CHECKING:
12 from collections.abc import Callable
14 from tests.unit.parsers.streaming.conftest import SimpleIssue
17def test_parses_string(
18 parse_error_line: Callable[[str], SimpleIssue | None],
19) -> None:
20 """Parse lines from string.
22 Args:
23 parse_error_line: Fixture providing parser function for error lines.
24 """
25 output = "INFO: ok\nERROR: bad thing\nINFO: fine\nERROR: another\n"
26 results = list(stream_text_lines(output, parse_error_line))
28 assert_that(results).is_length(2)
29 assert_that(results[0].message).is_equal_to("bad thing")
30 assert_that(results[1].message).is_equal_to("another")
33def test_parses_iterable(
34 parse_error_line: Callable[[str], SimpleIssue | None],
35) -> None:
36 """Parse lines from iterable.
38 Args:
39 parse_error_line: Fixture providing parser function for error lines.
40 """
41 lines = ["ERROR: first", "OK", "ERROR: second"]
42 results = list(stream_text_lines(lines, parse_error_line))
44 assert_that(results).is_length(2)
47def test_strips_ansi_by_default(
48 identity_line_parser: Callable[[str], SimpleIssue],
49) -> None:
50 """Strip ANSI codes by default.
52 Args:
53 identity_line_parser: Fixture providing identity parser for lines.
54 """
55 output = "\x1b[31mError\x1b[0m: message\n"
56 results = list(stream_text_lines(output, identity_line_parser))
58 assert_that(results[0].message).is_equal_to("Error: message")
61def test_preserves_ansi_when_disabled(
62 identity_line_parser: Callable[[str], SimpleIssue],
63) -> None:
64 """Preserve ANSI codes when strip_ansi=False.
66 Args:
67 identity_line_parser: Fixture providing identity parser for lines.
68 """
69 output = "\x1b[31mError\x1b[0m\n"
70 results = list(stream_text_lines(output, identity_line_parser, strip_ansi=False))
72 assert_that(results[0].message).contains("\x1b[31m")
75def test_skips_empty_lines(
76 identity_line_parser: Callable[[str], SimpleIssue],
77) -> None:
78 """Skip empty lines.
80 Args:
81 identity_line_parser: Fixture providing identity parser for lines.
82 """
83 output = "line1\n\n\nline2\n"
84 results = list(stream_text_lines(output, identity_line_parser))
86 assert_that(results).is_length(2)