Coverage for tests / test_fmt.py: 100%
108 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 15:04 -0800
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 15:04 -0800
1"""Tests for the fmt formatting module."""
3import fmt
6class TestFmtInt:
7 def test_small_number(self):
8 assert fmt.fmt_int(42) == "42"
10 def test_thousands(self):
11 assert fmt.fmt_int(200124) == "200,124"
13 def test_millions(self):
14 assert fmt.fmt_int(1234567) == "1,234,567"
16 def test_zero(self):
17 assert fmt.fmt_int(0) == "0"
19 def test_float_input(self):
20 assert fmt.fmt_int(200124.7) == "200,124"
23class TestFmtFloat:
24 def test_rounds_to_two(self):
25 assert fmt.fmt_float(30.399999) == "30.40"
27 def test_large_float(self):
28 assert fmt.fmt_float(200124.07) == "200,124.07"
30 def test_zero(self):
31 assert fmt.fmt_float(0.0) == "0.00"
33 def test_custom_decimals(self):
34 assert fmt.fmt_float(0.02345, 4) == "0.0234"
37class TestFmtNumber:
38 def test_int(self):
39 assert fmt.fmt_number(42) == "42"
41 def test_int_large(self):
42 assert fmt.fmt_number(200124) == "200,124"
44 def test_float_whole(self):
45 assert fmt.fmt_number(42.0) == "42"
47 def test_float_fractional(self):
48 assert fmt.fmt_number(30.4) == "30.40"
51class TestColorDisabled:
52 def test_header_plain(self, monkeypatch):
53 monkeypatch.setattr(fmt, "_USE_COLOR", False)
54 assert fmt.header("Test") == "Test"
56 def test_label_plain(self, monkeypatch):
57 monkeypatch.setattr(fmt, "_USE_COLOR", False)
58 assert fmt.label("avg:") == "avg:"
60 def test_num_plain(self, monkeypatch):
61 monkeypatch.setattr(fmt, "_USE_COLOR", False)
62 assert fmt.num("42") == "42"
64 def test_path_plain(self, monkeypatch):
65 monkeypatch.setattr(fmt, "_USE_COLOR", False)
66 assert fmt.path("/some/file.py") == "/some/file.py"
68 def test_func_name_plain(self, monkeypatch):
69 monkeypatch.setattr(fmt, "_USE_COLOR", False)
70 assert fmt.func_name("my_func") == "my_func"
73class TestColorEnabled:
74 def test_header_has_ansi(self, monkeypatch):
75 monkeypatch.setattr(fmt, "_USE_COLOR", True)
76 result = fmt.header("Test")
77 assert "\033[" in result
78 assert "Test" in result
80 def test_num_has_ansi(self, monkeypatch):
81 monkeypatch.setattr(fmt, "_USE_COLOR", True)
82 result = fmt.num("42")
83 assert "\033[" in result
84 assert "42" in result
87class TestRpad:
88 def test_alignment_width(self, monkeypatch):
89 monkeypatch.setattr(fmt, "_USE_COLOR", False)
90 result = fmt.rpad(42, 10)
91 assert len(result) == 10
92 assert result == " 42"
94 def test_alignment_with_thousands(self, monkeypatch):
95 monkeypatch.setattr(fmt, "_USE_COLOR", False)
96 result = fmt.rpad(200124, 10)
97 assert result == " 200,124"
99 def test_alignment_with_float(self, monkeypatch):
100 monkeypatch.setattr(fmt, "_USE_COLOR", False)
101 result = fmt.rpad(30.4, 10)
102 assert result == " 30.40"
104 def test_alignment_with_custom_formatter(self, monkeypatch):
105 monkeypatch.setattr(fmt, "_USE_COLOR", False)
106 result = fmt.rpad(42, 10, lambda x: "X")
107 assert len(result) == 10
108 assert result == " X"
111class TestFmtCompact:
112 def test_small_numbers_no_compaction(self):
113 assert fmt.fmt_compact(42) == "42"
114 assert fmt.fmt_compact(999) == "999"
115 assert fmt.fmt_compact(9999) == "9,999"
116 assert fmt.fmt_compact(99999) == "99,999"
118 def test_six_digit_numbers_compact(self):
119 assert fmt.fmt_compact(100000) == "100 × 10³"
120 assert fmt.fmt_compact(123456) == "123.5 × 10³"
121 assert fmt.fmt_compact(999999) == "1000 × 10³"
123 def test_millions(self):
124 assert fmt.fmt_compact(1000000) == "∞"
125 assert fmt.fmt_compact(1234567) == "∞"
126 assert fmt.fmt_compact(987654321) == "∞"
127 assert fmt.fmt_compact(1500000000) == "∞"
129 def test_zero(self):
130 assert fmt.fmt_compact(0) == "0"
132 def test_float_input(self):
133 assert fmt.fmt_compact(100000.7) == "100.0 × 10³"
136class TestCompositeHelpers:
137 def test_colored_num_int(self, monkeypatch):
138 monkeypatch.setattr(fmt, "_USE_COLOR", False)
139 assert fmt.colored_num(1000) == "1,000"
141 def test_colored_float(self, monkeypatch):
142 monkeypatch.setattr(fmt, "_USE_COLOR", False)
143 assert fmt.colored_float(1234.5) == "1,234.50"
145 def test_colored_float_custom_decimals(self, monkeypatch):
146 monkeypatch.setattr(fmt, "_USE_COLOR", False)
147 assert fmt.colored_float(0.02345, 4) == "0.0234"
149 def test_colored_int(self, monkeypatch):
150 monkeypatch.setattr(fmt, "_USE_COLOR", False)
151 assert fmt.colored_int(1234567) == "1,234,567"