Coverage for tests/test_analyze_number_format.py: 100%

219 statements  

« prev     ^ index     » next       coverage.py v7.10.4, created at 2025-08-22 15:57 -0700

1import pytest 

2 

3from fguess import analyze_number_format 

4 

5 

6class TestAlignmentAndPadding: 

7 """Test string alignment and padding detection.""" 

8 

9 @pytest.mark.parametrize("input_str,expected_int,expected_str", [ 

10 (" 34", 'f"{variable:>4d}"', 'f"{variable:>4}"'), 

11 ("34 ", 'f"{variable:<4d}"', 'f"{variable:<4}"'), 

12 (" 34 ", 'f"{variable:^4d}"', 'f"{variable:^4}"'), 

13 ]) 

14 def test_basic_alignment(self, input_str, expected_int, expected_str): 

15 formats = analyze_number_format(input_str) 

16 assert ('int', expected_int) in formats 

17 assert ('str', expected_str) in formats 

18 

19 @pytest.mark.parametrize("input_str,expected_int,expected_str", [ 

20 ("__34", 'f"{variable:_>4d}"', 'f"{variable:_>4}"'), 

21 ("34__", 'f"{variable:_<4d}"', 'f"{variable:_<4}"'), 

22 ("_34_", 'f"{variable:_^4d}"', 'f"{variable:_^4}"'), 

23 ("**34", 'f"{variable:*>4d}"', 'f"{variable:*>4}"'), 

24 ]) 

25 def test_custom_fill_alignment(self, input_str, expected_int, expected_str): 

26 formats = analyze_number_format(input_str) 

27 assert ('int', expected_int) in formats 

28 assert ('str', expected_str) in formats 

29 

30 @pytest.mark.parametrize("input_str,expected_formats", [ 

31 ("0034", [('int', 'f"{variable:04d}"'), ('float', 'f"{variable:04.0f}"')]), 

32 ("0340.00", [('float', 'f"{variable:07.2f}"')]), 

33 ("00000123", [('int', 'f"{variable:08d}"')]), 

34 ("007", [('int', 'f"{variable:03d}"')]), 

35 ]) 

36 def test_zero_padding(self, input_str, expected_formats): 

37 formats = analyze_number_format(input_str) 

38 for expected_format in expected_formats: 

39 assert expected_format in formats 

40 

41 @pytest.mark.parametrize("input_str,should_have_center,should_have_literal", [ 

42 (" 4 ", False, True), # Unequal padding (2 vs 6) 

43 (" 42 ", False, True), # Slightly unequal (1 vs 2) 

44 (" 42 ", False, True), # Off-by-one (2 vs 3) 

45 (" 123 ", False, True), # Large unequal (4 vs 8) 

46 (" 42 ", True, False), # Equal padding (2 vs 2) 

47 (" 42 ", True, False), # Single equal padding (1 vs 1) 

48 ]) 

49 def test_padding_edge_cases(self, input_str, should_have_center, should_have_literal): 

50 """Test that padding is correctly classified as center alignment vs literals.""" 

51 formats = analyze_number_format(input_str) 

52 

53 has_center = any('^' in f for t, f in formats) 

54 assert has_center == should_have_center, f"Center alignment detection failed for '{input_str}'" 

55 

56 if should_have_literal: 

57 # Check that it's treated as literals (contains the actual padding) 

58 expected_literal = input_str.replace(input_str.strip(), '{variable}') 

59 assert any(expected_literal in f for t, f in formats), f"Should treat '{input_str}' as literals" 

60 

61 def test_very_unequal_padding_right_align(self): 

62 """Right-only padding should be left alignment.""" 

63 formats = analyze_number_format("42 ") # 0 left, 6 right 

64 assert ('int', 'f"{variable:<8d}"') in formats, "Should be left alignment" 

65 

66 def test_mixed_padding_as_literals(self): 

67 """Different padding on each side should be treated as literals.""" 

68 formats = analyze_number_format(" 34_") 

69 assert any(' {variable}_' in f for t, f in formats) 

70 

71 

72class TestNumericFormats: 

73 """Test detection of various numeric format types.""" 

74 

75 @pytest.mark.parametrize("input_str,expected_format", [ 

76 ("3400.00", 'f"{variable:.2f}"'), 

77 (" 340.00", 'f"{variable:>7.2f}"'), 

78 ("3.14159", 'f"{variable:.5f}"'), 

79 ("1.5", 'f"{variable:.1f}"'), 

80 ]) 

81 def test_decimal_formats(self, input_str, expected_format): 

82 formats = analyze_number_format(input_str) 

83 assert ('float', expected_format) in formats 

84 

85 @pytest.mark.parametrize("input_str,expected_formats", [ 

86 ("3,400", [('int', 'f"{variable:,}"'), ('float', 'f"{variable:,.0f}"')]), 

87 ("3,400.00", [('float', 'f"{variable:,.2f}"')]), 

88 ("1,234,567", [('int', 'f"{variable:,}"')]), 

89 (" 3,400.00", [('float', 'f"{variable:>10,.2f}"')]), 

90 ]) 

91 def test_comma_separators(self, input_str, expected_formats): 

92 formats = analyze_number_format(input_str) 

93 for expected_format in expected_formats: 

94 assert expected_format in formats 

95 

96 def test_comma_with_width(self): 

97 # When there's a width, 'd' should be kept 

98 formats = analyze_number_format(" 1,234") 

99 assert any(',' in f and 'd' in f for t, f in formats if t == 'int') 

100 

101 @pytest.mark.parametrize("input_str,expected_formats", [ 

102 ("3_400", [('int', 'f"{variable:_}"'), ('float', 'f"{variable:_.0f}"')]), 

103 ("3_400.00", [('float', 'f"{variable:_.2f}"')]), 

104 ("1_234_567", [('int', 'f"{variable:_}"')]), 

105 ("1_000_000", [('int', 'f"{variable:_}"'), ('float', 'f"{variable:_.0f}"')]), 

106 (" 3_400.00", [('float', 'f"{variable:>10_.2f}"')]), 

107 ("1_234.56", [('float', 'f"{variable:_.2f}"')]), 

108 ("$1_000_000", [('int', 'f"${variable:_}"'), ('float', 'f"${variable:_.0f}"')]), 

109 ("1_999.99€", [('float', 'f"{variable:_.2f}€"')]), 

110 ]) 

111 def test_underscore_separators(self, input_str, expected_formats): 

112 formats = analyze_number_format(input_str) 

113 for expected_format in expected_formats: 

114 assert expected_format in formats 

115 

116 def test_underscore_with_width(self): 

117 # When there's a width, 'd' should be kept 

118 formats = analyze_number_format(" 1_234") 

119 assert any('_' in f and 'd' in f for t, f in formats if t == 'int') 

120 

121 @pytest.mark.parametrize("input_str,expected_format", [ 

122 ("0x4", 'f"{variable:#x}"'), 

123 ("0x04", 'f"{variable:#04x}"'), 

124 ("0xff", 'f"{variable:#x}"'), 

125 ("0x00ff", 'f"{variable:#06x}"'), 

126 ("ff", 'f"{variable:x}"'), 

127 ("FF", 'f"{variable:X}"'), 

128 ("00ff", 'f"{variable:04x}"'), 

129 ]) 

130 def test_hex_formats(self, input_str, expected_format): 

131 formats = analyze_number_format(input_str) 

132 assert ('int', expected_format) in formats 

133 

134 @pytest.mark.parametrize("input_str,expected_format", [ 

135 ("34%", 'f"{variable:.0%}"'), 

136 ("34.0%", 'f"{variable:.1%}"'), 

137 ("34.56%", 'f"{variable:.2%}"'), 

138 ("Score: 85%", 'f"Score: {variable:.0%}"'), 

139 ("+12.5%", 'f"{variable:+.1%}"'), 

140 ("-5.5%", 'f"{variable:.1%}"'), 

141 ]) 

142 def test_percentage_formats(self, input_str, expected_format): 

143 formats = analyze_number_format(input_str) 

144 assert ('float', expected_format) in formats 

145 

146 @pytest.mark.parametrize("input_str,expected_format", [ 

147 (" 25%", 'f"{variable:>5.0%}"'), 

148 ("25% ", 'f"{variable:<5.0%}"'), 

149 (" 25% ", 'f"{variable:^5.0%}"'), 

150 (" 34.5%", 'f"{variable:>7.1%}"'), 

151 ("34.5% ", 'f"{variable:<7.1%}"'), 

152 (" 34.5% ", 'f"{variable:^7.1%}"'), 

153 ("**25%", 'f"{variable:*>5.0%}"'), 

154 ("__34.5%", 'f"{variable:_>7.1%}"'), 

155 ("25%**", 'f"{variable:*<5.0%}"'), 

156 (" +12.5%", 'f"{variable:>+7.1%}"'), 

157 ]) 

158 def test_percentage_alignment(self, input_str, expected_format): 

159 formats = analyze_number_format(input_str) 

160 assert ('float', expected_format) in formats 

161 

162 

163class TestLiteralsAndPrefixSuffix: 

164 """Test detection of literal prefixes and suffixes.""" 

165 

166 def test_currency_prefix(self): 

167 formats = analyze_number_format("$34.50") 

168 assert ('float', 'f"${variable:.2f}"') in formats 

169 assert ('str', 'f"${variable}"') in formats 

170 

171 def test_currency_suffix(self): 

172 formats = analyze_number_format("99.99€") 

173 assert ('float', 'f"{variable:.2f}€"') in formats 

174 

175 def test_label_with_number(self): 

176 formats = analyze_number_format("Item #42") 

177 assert ('int', 'f"Item #{variable:d}"') not in formats 

178 assert ('str', 'f"Item #{variable}"') in formats 

179 

180 def test_parentheses(self): 

181 formats = analyze_number_format("(123)") 

182 assert ('int', 'f"({variable:d})"') not in formats 

183 

184 def test_units(self): 

185 formats = analyze_number_format("5.5km") 

186 assert ('float', 'f"{variable:.1f}km"') in formats 

187 

188 def test_complex_suffix(self): 

189 formats = analyze_number_format("123.45 USD/month") 

190 assert ('float', 'f"{variable:.2f} USD/month"') in formats 

191 

192 def test_prefix_and_suffix(self): 

193 formats = analyze_number_format("Total: $50.00 USD") 

194 assert ('float', 'f"Total: ${variable:.2f} USD"') in formats 

195 

196 def test_hex_with_literals(self): 

197 formats = analyze_number_format("Address: 0x1234") 

198 assert ('int', 'f"Address: {variable:#x}"') in formats 

199 

200 def test_hex_with_padding_as_literals(self): 

201 """Hex numbers with literal padding should be treated as such.""" 

202 formats = analyze_number_format(" 0x42") 

203 assert any(' {variable:#x}' in f for t, f in formats) 

204 

205 

206class TestSignAndSpecialCases: 

207 """Test sign handling and special numeric cases.""" 

208 

209 def test_sign_prefix(self): 

210 formats = analyze_number_format("+42") 

211 assert ('int', 'f"{variable:+d}"') in formats 

212 

213 def test_negative_number(self): 

214 formats = analyze_number_format("-42") 

215 assert not any(t == 'int' for t, f in formats) 

216 

217 def test_sign_with_padding(self): 

218 formats = analyze_number_format(" +42") 

219 assert any('+' in f for t, f in formats if t == 'int') 

220 

221 def test_zero_padded_with_sign(self): 

222 formats = analyze_number_format("+0034") 

223 assert any('+' in f and '05' in f for t, f in formats) 

224 

225 def test_underscore_with_left_align(self): 

226 formats = analyze_number_format("1_000 ") 

227 assert ('int', 'f"{variable:<7_d}"') in formats 

228 

229 def test_underscore_with_center_align(self): 

230 formats = analyze_number_format(" 1_000 ") 

231 assert ('int', 'f"{variable:^7_d}"') in formats 

232 

233 

234class TestComplexCombinations: 

235 """Test complex combinations of formatting features.""" 

236 

237 def test_currency_with_comma(self): 

238 formats = analyze_number_format("$1,234.56") 

239 assert ('float', 'f"${variable:,.2f}"') in formats 

240 

241 def test_currency_with_underscore(self): 

242 formats = analyze_number_format("$1_234.56") 

243 assert ('float', 'f"${variable:_.2f}"') in formats 

244 

245 def test_percentage_with_label(self): 

246 formats = analyze_number_format("Accuracy: 98.5%") 

247 assert ('float', 'f"Accuracy: {variable:.1%}"') in formats 

248 

249 def test_right_aligned_with_comma_and_decimal(self): 

250 formats = analyze_number_format(" 1,234.56") 

251 assert any(',' in f and '.2f' in f for t, f in formats if t == 'float') 

252 

253 def test_right_aligned_with_underscore_and_decimal(self): 

254 formats = analyze_number_format(" 1_234.56") 

255 assert any('_' in f and '.2f' in f for t, f in formats if t == 'float') 

256 

257 def test_centered_decimal(self): 

258 formats = analyze_number_format(" 34.5 ") 

259 assert any('^' in f and '.1f' in f for t, f in formats if t == 'float') 

260 

261 def test_custom_fill_with_decimal(self): 

262 formats = analyze_number_format("**34.50") 

263 assert any('*>' in f and '.2f' in f for t, f in formats if t == 'float') 

264 

265 

266class TestEdgeCasesAndValidation: 

267 """Test edge cases and validation logic.""" 

268 

269 def test_single_digit(self): 

270 formats = analyze_number_format("5") 

271 assert ('int', 'f"{variable:d}"') not in formats 

272 

273 def test_number_zero(self): 

274 formats = analyze_number_format("0") 

275 assert not any(t == 'int' for t, f in formats) 

276 

277 def test_very_long_number(self): 

278 formats = analyze_number_format("123456789") 

279 assert not any(t == 'int' for t, f in formats) 

280 

281 def test_plain_string(self): 

282 formats = analyze_number_format("hello") 

283 assert not formats, "No format specifications" 

284 

285 def test_string_with_numbers(self): 

286 formats = analyze_number_format("test123") 

287 assert any('test' in f and 'variable' in f for t, f in formats) 

288 

289 def test_only_padding_characters(self): 

290 formats = analyze_number_format("___") 

291 assert not formats, "No format specifications" 

292 

293 def test_just_decimal_point(self): 

294 formats = analyze_number_format(".") 

295 assert not formats, "No format specifications" 

296 

297 def test_empty_string(self): 

298 formats = analyze_number_format("") 

299 assert not formats, "No format specifications" 

300 

301 def test_trailing_decimal_place(self): 

302 formats = analyze_number_format("5.") 

303 assert ('float', 'f"{variable:.0f}"') not in formats 

304 

305 def test_no_duplicate_formats(self): 

306 """Ensure no duplicate format specifications are returned.""" 

307 formats = analyze_number_format("1234.56") 

308 seen = set() 

309 for type_name, format_spec in formats: 

310 key = (type_name, format_spec) 

311 assert key not in seen, f"Duplicate format found: {key}" 

312 seen.add(key) 

313 

314 def test_no_string_format_with_percent(self): 

315 """String format should always be present when there are literals.""" 

316 formats = analyze_number_format("100%") 

317 assert all(t != 'str' for t, _ in formats), "string format found for 100%" 

318 

319 def test_string_format_always_present_with_literals(self): 

320 """String format should always be present when there are literals.""" 

321 test_cases = ["$100", "Item #5", "(42)"] 

322 for test in test_cases: 

323 formats = analyze_number_format(test) 

324 assert any(t == 'str' for t, _ in formats), f"No string format for {test}" 

325 

326 

327class TestDatetimeFormats: 

328 """Test datetime format detection and priority.""" 

329 

330 @pytest.mark.parametrize("input_str,expected_format", [ 

331 ("2030-01-24", 'f"{variable:%Y-%m-%d}"'), 

332 ("01/24/2030", 'f"{variable:%m/%d/%Y}"'), 

333 ("01/24/30", 'f"{variable:%m/%d/%y}"'), 

334 ("24 Jan 2030", 'f"{variable:%d %b %Y}"'), 

335 ]) 

336 def test_basic_date_formats(self, input_str, expected_format): 

337 formats = analyze_number_format(input_str) 

338 assert ('datetime', expected_format) in formats 

339 # Should prioritize datetime over numeric due to datetime structure 

340 datetime_formats = [f for t, f in formats if t == 'datetime'] 

341 assert len(datetime_formats) > 0 

342 

343 def test_month_day_year_format_with_comma(self): 

344 formats = analyze_number_format("Jan 24, 2030") 

345 # Note: the comma might not be in the specific formats, but similar patterns should be 

346 datetime_formats = [f for t, f in formats if t == 'datetime'] 

347 assert len(datetime_formats) > 0 

348 

349 @pytest.mark.parametrize("input_str,expected_format", [ 

350 ("05:45", 'f"{variable:%H:%M}"'), 

351 ("05:45:13", 'f"{variable:%H:%M:%S}"'), 

352 ("05:45 AM", 'f"{variable:%I:%M %p}"'), 

353 ("05:45AM", 'f"{variable:%I:%M%p}"'), 

354 ]) 

355 def test_time_formats(self, input_str, expected_format): 

356 formats = analyze_number_format(input_str) 

357 assert ('datetime', expected_format) in formats 

358 

359 def test_iso_datetime_format(self): 

360 formats = analyze_number_format("2030-01-24 05:45") 

361 datetime_formats = [f for t, f in formats if t == 'datetime'] 

362 assert len(datetime_formats) > 0 

363 # Should find a format that includes both date and time components 

364 assert any('Y' in f and 'H' in f for f in datetime_formats) 

365 

366 def test_iso_datetime_with_seconds(self): 

367 formats = analyze_number_format("2030-01-24 05:45:13") 

368 assert any('Y-%m-%d %H:%M:%S' in f for t, f in formats if t == 'datetime') 

369 

370 def test_full_weekday_datetime(self): 

371 formats = analyze_number_format("Thursday January 24 2030 05:45:13") 

372 datetime_formats = [f for t, f in formats if t == 'datetime'] 

373 assert len(datetime_formats) > 0 

374 

375 def test_date_with_literals(self): 

376 formats = analyze_number_format("Date: 2030-01-24") 

377 # Should detect datetime part within literals 

378 datetime_formats = [f for t, f in formats if t == 'datetime'] 

379 assert len(datetime_formats) > 0 

380 # Should have format with literal prefix 

381 assert any('Date:' in f for t, f in formats if t == 'datetime') 

382 

383 def test_datetime_priority_over_numbers(self): 

384 """Datetime should be prioritized when structure suggests datetime.""" 

385 formats = analyze_number_format("2030-01-24") 

386 # Datetime should appear early in results due to datetime structure 

387 datetime_index = next((i for i, (t, f) in enumerate(formats) if t == 'datetime'), float('inf')) 

388 numeric_index = next((i for i, (t, f) in enumerate(formats) if t in ('int', 'float')), float('inf')) 

389 assert datetime_index < numeric_index, "Datetime should be prioritized over numeric for structured dates" 

390 

391 def test_single_number_datetime_lower_priority(self): 

392 """Single numeric datetime formats should have lower priority.""" 

393 formats = analyze_number_format("2030") 

394 # Should have datetime format for year, but it should come after numeric formats 

395 assert formats == [ 

396 ('float', 'f"{variable:.0f}"'), 

397 ('datetime', 'f"{variable:%Y}"'), 

398 ]