Coverage for tests / unit / utils / output / test_helpers.py: 100%

53 statements  

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

1"""Tests for lintro.utils.output.helpers module.""" 

2 

3from __future__ import annotations 

4 

5import pytest 

6from assertpy import assert_that 

7 

8from lintro.utils.output.helpers import html_escape, markdown_escape, sanitize_csv_value 

9 

10# ============================================================================= 

11# markdown_escape tests 

12# ============================================================================= 

13 

14 

15def test_markdown_escape_escapes_pipe() -> None: 

16 """markdown_escape escapes pipe characters.""" 

17 result = markdown_escape("A | B") 

18 assert_that(result).is_equal_to(r"A \| B") 

19 

20 

21def test_markdown_escape_replaces_newline_with_space() -> None: 

22 """markdown_escape replaces newlines with spaces.""" 

23 result = markdown_escape("line1\nline2") 

24 assert_that(result).is_equal_to("line1 line2") 

25 

26 

27def test_markdown_escape_handles_multiple_pipes() -> None: 

28 """markdown_escape handles multiple pipe characters.""" 

29 result = markdown_escape("A | B | C") 

30 assert_that(result).is_equal_to(r"A \| B \| C") 

31 

32 

33def test_markdown_escape_handles_multiple_newlines() -> None: 

34 """markdown_escape handles multiple newlines.""" 

35 result = markdown_escape("a\nb\nc") 

36 assert_that(result).is_equal_to("a b c") 

37 

38 

39def test_markdown_escape_handles_empty_string() -> None: 

40 """markdown_escape returns empty string for empty input.""" 

41 result = markdown_escape("") 

42 assert_that(result).is_equal_to("") 

43 

44 

45def test_markdown_escape_no_change_for_normal_text() -> None: 

46 """markdown_escape returns unchanged text without special chars.""" 

47 result = markdown_escape("normal text") 

48 assert_that(result).is_equal_to("normal text") 

49 

50 

51# ============================================================================= 

52# html_escape tests 

53# ============================================================================= 

54 

55 

56def test_html_escape_escapes_less_than() -> None: 

57 """html_escape escapes less than character.""" 

58 result = html_escape("<script>") 

59 assert_that(result).is_equal_to("&lt;script&gt;") 

60 

61 

62def test_html_escape_escapes_greater_than() -> None: 

63 """html_escape escapes greater than character.""" 

64 result = html_escape("a > b") 

65 assert_that(result).is_equal_to("a &gt; b") 

66 

67 

68def test_html_escape_escapes_ampersand() -> None: 

69 """html_escape escapes ampersand character.""" 

70 result = html_escape("A & B") 

71 assert_that(result).is_equal_to("A &amp; B") 

72 

73 

74def test_html_escape_escapes_quotes() -> None: 

75 """html_escape escapes quote characters.""" 

76 result = html_escape('say "hello"') 

77 assert_that(result).is_equal_to("say &quot;hello&quot;") 

78 

79 

80def test_html_escape_handles_empty_string() -> None: 

81 """html_escape returns empty string for empty input.""" 

82 result = html_escape("") 

83 assert_that(result).is_equal_to("") 

84 

85 

86def test_html_escape_no_change_for_normal_text() -> None: 

87 """html_escape returns unchanged text without special chars.""" 

88 result = html_escape("normal text") 

89 assert_that(result).is_equal_to("normal text") 

90 

91 

92# ============================================================================= 

93# sanitize_csv_value tests 

94# ============================================================================= 

95 

96 

97@pytest.mark.parametrize( 

98 ("input_value", "expected"), 

99 [ 

100 pytest.param("=SUM(A1)", "'=SUM(A1)", id="equals_sign"), 

101 pytest.param("+1234", "'+1234", id="plus_sign"), 

102 pytest.param("-5678", "'-5678", id="minus_sign"), 

103 pytest.param("@mention", "'@mention", id="at_sign"), 

104 ], 

105) 

106def test_sanitize_csv_value_prefixes_formula_chars( 

107 input_value: str, 

108 expected: str, 

109) -> None: 

110 """sanitize_csv_value prefixes formula-starting characters with quote. 

111 

112 Args: 

113 input_value: Input value to sanitize. 

114 expected: Expected sanitized output. 

115 """ 

116 result = sanitize_csv_value(input_value) 

117 assert_that(result).is_equal_to(expected) 

118 

119 

120def test_sanitize_csv_value_no_change_for_normal_text() -> None: 

121 """sanitize_csv_value returns unchanged text without formula chars.""" 

122 result = sanitize_csv_value("normal text") 

123 assert_that(result).is_equal_to("normal text") 

124 

125 

126def test_sanitize_csv_value_handles_empty_string() -> None: 

127 """sanitize_csv_value returns empty string for empty input.""" 

128 result = sanitize_csv_value("") 

129 assert_that(result).is_equal_to("") 

130 

131 

132def test_sanitize_csv_value_does_not_prefix_middle_chars() -> None: 

133 """sanitize_csv_value only checks start of string.""" 

134 result = sanitize_csv_value("a=b+c-d@e") 

135 assert_that(result).is_equal_to("a=b+c-d@e")