Coverage for tests / unit / formatters / test_format_registry.py: 100%

75 statements  

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

1"""Unit tests for the centralized format style registry.""" 

2 

3import pytest 

4from assertpy import assert_that 

5 

6from lintro.enums.output_format import OutputFormat 

7from lintro.formatters.core.format_registry import ( 

8 DEFAULT_FORMAT, 

9 get_format_map, 

10 get_string_format_map, 

11 get_style, 

12) 

13from lintro.formatters.styles.csv import CsvStyle 

14from lintro.formatters.styles.github import GitHubStyle 

15from lintro.formatters.styles.grid import GridStyle 

16from lintro.formatters.styles.html import HtmlStyle 

17from lintro.formatters.styles.json import JsonStyle 

18from lintro.formatters.styles.markdown import MarkdownStyle 

19from lintro.formatters.styles.plain import PlainStyle 

20 

21# ============================================================================= 

22# Tests for get_style function 

23# ============================================================================= 

24 

25 

26@pytest.mark.parametrize( 

27 ("output_format", "expected_style"), 

28 [ 

29 pytest.param(OutputFormat.PLAIN, PlainStyle, id="enum-plain"), 

30 pytest.param(OutputFormat.GRID, GridStyle, id="enum-grid"), 

31 pytest.param(OutputFormat.MARKDOWN, MarkdownStyle, id="enum-markdown"), 

32 pytest.param(OutputFormat.HTML, HtmlStyle, id="enum-html"), 

33 pytest.param(OutputFormat.JSON, JsonStyle, id="enum-json"), 

34 pytest.param(OutputFormat.CSV, CsvStyle, id="enum-csv"), 

35 pytest.param(OutputFormat.GITHUB, GitHubStyle, id="enum-github"), 

36 ], 

37) 

38def test_get_style_with_enum(output_format: OutputFormat, expected_style: type) -> None: 

39 """Test getting style with OutputFormat enum. 

40 

41 Args: 

42 output_format: The OutputFormat enum value to test. 

43 expected_style: The expected style class type. 

44 """ 

45 style = get_style(output_format) 

46 assert_that(style).is_instance_of(expected_style) 

47 

48 

49@pytest.mark.parametrize( 

50 ("format_string", "expected_style"), 

51 [ 

52 pytest.param("plain", PlainStyle, id="string-plain"), 

53 pytest.param("grid", GridStyle, id="string-grid"), 

54 pytest.param("markdown", MarkdownStyle, id="string-markdown"), 

55 pytest.param("html", HtmlStyle, id="string-html"), 

56 pytest.param("json", JsonStyle, id="string-json"), 

57 pytest.param("csv", CsvStyle, id="string-csv"), 

58 pytest.param("github", GitHubStyle, id="string-github"), 

59 ], 

60) 

61def test_get_style_with_string(format_string: str, expected_style: type) -> None: 

62 """Test getting style with string key. 

63 

64 Args: 

65 format_string: The string format key to test. 

66 expected_style: The expected style class type. 

67 """ 

68 style = get_style(format_string) 

69 assert_that(style).is_instance_of(expected_style) 

70 

71 

72@pytest.mark.parametrize( 

73 "format_string", 

74 [ 

75 pytest.param("grid", id="lowercase"), 

76 pytest.param("GRID", id="uppercase"), 

77 pytest.param("Grid", id="mixed-case"), 

78 ], 

79) 

80def test_get_style_string_case_insensitive(format_string: str) -> None: 

81 """Test that string keys are case insensitive. 

82 

83 Args: 

84 format_string: The format string with varying case to test. 

85 """ 

86 style = get_style(format_string) 

87 assert_that(style).is_instance_of(GridStyle) 

88 

89 

90@pytest.mark.parametrize( 

91 "format_string", 

92 [ 

93 pytest.param("unknown_format", id="unknown"), 

94 pytest.param("", id="empty"), 

95 ], 

96) 

97def test_get_style_unknown_format_falls_back_to_grid(format_string: str) -> None: 

98 """Test that unknown or empty format string falls back to GridStyle. 

99 

100 Args: 

101 format_string: The unknown or empty format string to test. 

102 """ 

103 style = get_style(format_string) 

104 assert_that(style).is_instance_of(GridStyle) 

105 

106 

107def test_get_style_caches_instances() -> None: 

108 """Test that style instances are cached and reused.""" 

109 style1 = get_style(OutputFormat.GRID) 

110 style2 = get_style(OutputFormat.GRID) 

111 

112 # Same instance should be returned (cached) 

113 assert_that(style1).is_same_as(style2) 

114 

115 

116# ============================================================================= 

117# Tests for get_format_map function 

118# ============================================================================= 

119 

120 

121def test_get_format_map_returns_all_formats() -> None: 

122 """Test that get_format_map returns all output formats.""" 

123 format_map = get_format_map() 

124 

125 assert_that(format_map).contains_key(OutputFormat.PLAIN) 

126 assert_that(format_map).contains_key(OutputFormat.GRID) 

127 assert_that(format_map).contains_key(OutputFormat.MARKDOWN) 

128 assert_that(format_map).contains_key(OutputFormat.HTML) 

129 assert_that(format_map).contains_key(OutputFormat.JSON) 

130 assert_that(format_map).contains_key(OutputFormat.CSV) 

131 assert_that(format_map).contains_key(OutputFormat.GITHUB) 

132 

133 

134def test_get_format_map_values_are_correct_styles() -> None: 

135 """Test that format_map values are the correct style types.""" 

136 format_map = get_format_map() 

137 

138 assert_that(format_map[OutputFormat.PLAIN]).is_instance_of(PlainStyle) 

139 assert_that(format_map[OutputFormat.GRID]).is_instance_of(GridStyle) 

140 assert_that(format_map[OutputFormat.MARKDOWN]).is_instance_of(MarkdownStyle) 

141 assert_that(format_map[OutputFormat.HTML]).is_instance_of(HtmlStyle) 

142 assert_that(format_map[OutputFormat.JSON]).is_instance_of(JsonStyle) 

143 assert_that(format_map[OutputFormat.CSV]).is_instance_of(CsvStyle) 

144 assert_that(format_map[OutputFormat.GITHUB]).is_instance_of(GitHubStyle) 

145 

146 

147def test_get_format_map_length() -> None: 

148 """Test that format_map contains exactly 7 formats.""" 

149 format_map = get_format_map() 

150 assert_that(format_map).is_length(7) 

151 

152 

153# ============================================================================= 

154# Tests for get_string_format_map function 

155# ============================================================================= 

156 

157 

158def test_get_string_format_map_returns_string_keys() -> None: 

159 """Test that get_string_format_map uses string keys.""" 

160 string_map = get_string_format_map() 

161 

162 assert_that(string_map).contains_key("plain") 

163 assert_that(string_map).contains_key("grid") 

164 assert_that(string_map).contains_key("markdown") 

165 assert_that(string_map).contains_key("html") 

166 assert_that(string_map).contains_key("json") 

167 assert_that(string_map).contains_key("csv") 

168 assert_that(string_map).contains_key("github") 

169 

170 

171def test_get_string_format_map_values_are_correct_styles() -> None: 

172 """Test that string_map values are the correct style types.""" 

173 string_map = get_string_format_map() 

174 

175 assert_that(string_map["plain"]).is_instance_of(PlainStyle) 

176 assert_that(string_map["grid"]).is_instance_of(GridStyle) 

177 assert_that(string_map["markdown"]).is_instance_of(MarkdownStyle) 

178 assert_that(string_map["html"]).is_instance_of(HtmlStyle) 

179 assert_that(string_map["json"]).is_instance_of(JsonStyle) 

180 assert_that(string_map["csv"]).is_instance_of(CsvStyle) 

181 assert_that(string_map["github"]).is_instance_of(GitHubStyle) 

182 

183 

184def test_get_string_format_map_length() -> None: 

185 """Test that string_map contains exactly 7 formats.""" 

186 string_map = get_string_format_map() 

187 assert_that(string_map).is_length(7) 

188 

189 

190# ============================================================================= 

191# Tests for DEFAULT_FORMAT constant 

192# ============================================================================= 

193 

194 

195def test_default_format_is_grid() -> None: 

196 """Test that the default format is GRID.""" 

197 assert_that(DEFAULT_FORMAT).is_equal_to(OutputFormat.GRID)