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

1"""Tests for stream_json_array_fallback function.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7import pytest 

8from assertpy import assert_that 

9 

10from lintro.parsers.streaming import stream_json_array_fallback 

11 

12if TYPE_CHECKING: 

13 from collections.abc import Callable 

14 

15 from tests.unit.parsers.streaming.conftest import SimpleIssue 

16 

17 

18def test_parses_array( 

19 parse_test_item: Callable[[dict[str, object]], SimpleIssue | None], 

20) -> None: 

21 """Parse standard JSON array. 

22 

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)) 

28 

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") 

32 

33 

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. 

38 

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)) 

44 

45 assert_that(results).is_length(1) 

46 assert_that(results[0].file).is_equal_to("a.py") 

47 

48 

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. 

53 

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)) 

59 

60 assert_that(results).is_length(2) 

61 

62 

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. 

76 

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)) 

82 

83 assert_that(results).is_empty()