Coverage for tests / unit / parsers / streaming / test_json_array.py: 100%
24 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_json_array_fallback function."""
3from __future__ import annotations
5from typing import TYPE_CHECKING
7import pytest
8from assertpy import assert_that
10from lintro.parsers.streaming import stream_json_array_fallback
12if TYPE_CHECKING:
13 from collections.abc import Callable
15 from tests.unit.parsers.streaming.conftest import SimpleIssue
18def test_parses_array(
19 parse_test_item: Callable[[dict[str, object]], SimpleIssue | None],
20) -> None:
21 """Parse standard JSON array.
23 Args:
24 parse_test_item: Fixture providing a parser function for test items.
25 """
26 output = '[{"file": "a.py"}, {"file": "b.py"}]'
27 results = list(stream_json_array_fallback(output, parse_test_item))
29 assert_that(results).is_length(2)
30 assert_that(results[0].file).is_equal_to("a.py")
31 assert_that(results[1].file).is_equal_to("b.py")
34def test_parses_with_trailing_data(
35 parse_test_item: Callable[[dict[str, object]], SimpleIssue | None],
36) -> None:
37 """Parse JSON array with trailing non-JSON data.
39 Args:
40 parse_test_item: Fixture providing a parser function for test items.
41 """
42 output = '[{"file": "a.py"}]\nSome extra text'
43 results = list(stream_json_array_fallback(output, parse_test_item))
45 assert_that(results).is_length(1)
46 assert_that(results[0].file).is_equal_to("a.py")
49def test_falls_back_to_json_lines(
50 parse_test_item: Callable[[dict[str, object]], SimpleIssue | None],
51) -> None:
52 """Fall back to JSON Lines when array parsing fails.
54 Args:
55 parse_test_item: Fixture providing a parser function for test items.
56 """
57 output = '{"file": "a.py"}\n{"file": "b.py"}\n'
58 results = list(stream_json_array_fallback(output, parse_test_item))
60 assert_that(results).is_length(2)
63@pytest.mark.parametrize(
64 "output",
65 [
66 "[]",
67 "{}",
68 "",
69 ],
70)
71def test_empty_yields_nothing(
72 output: str,
73 parse_test_item: Callable[[dict[str, object]], SimpleIssue | None],
74) -> None:
75 """Empty array, object, or string yields no results.
77 Args:
78 output: Parameterized output string to parse.
79 parse_test_item: Fixture providing a parser function for test items.
80 """
81 results = list(stream_json_array_fallback(output, parse_test_item))
83 assert_that(results).is_empty()