Coverage for tests/test_fmt.py: 100%

1"""Tests for the fmt formatting module."""

2

3 import fmt

4

5

6 class TestFmtInt:

7 def test_small_number(self):

8 assert fmt.fmt_int(42) == "42"

9

10 def test_thousands(self):

11 assert fmt.fmt_int(200124) == "200,124"

12

13 def test_millions(self):

14 assert fmt.fmt_int(1234567) == "1,234,567"

15

16 def test_zero(self):

17 assert fmt.fmt_int(0) == "0"

18

19 def test_float_input(self):

20 assert fmt.fmt_int(200124.7) == "200,124"

21

22

23 class TestFmtFloat:

24 def test_rounds_to_two(self):

25 assert fmt.fmt_float(30.399999) == "30.40"

26

27 def test_large_float(self):

28 assert fmt.fmt_float(200124.07) == "200,124.07"

29

30 def test_zero(self):

31 assert fmt.fmt_float(0.0) == "0.00"

32

33 def test_custom_decimals(self):

34 assert fmt.fmt_float(0.02345, 4) == "0.0234"

35

36

37 class TestFmtNumber:

38 def test_int(self):

39 assert fmt.fmt_number(42) == "42"

40

41 def test_int_large(self):

42 assert fmt.fmt_number(200124) == "200,124"

43

44 def test_float_whole(self):

45 assert fmt.fmt_number(42.0) == "42"

46

47 def test_float_fractional(self):

48 assert fmt.fmt_number(30.4) == "30.40"

49

50

51 class TestColorDisabled:

52 def test_header_plain(self, monkeypatch):

53 monkeypatch.setattr(fmt, "_USE_COLOR", False)

54 assert fmt.header("Test") == "Test"

55

56 def test_label_plain(self, monkeypatch):

57 monkeypatch.setattr(fmt, "_USE_COLOR", False)

58 assert fmt.label("avg:") == "avg:"

59

60 def test_num_plain(self, monkeypatch):

61 monkeypatch.setattr(fmt, "_USE_COLOR", False)

62 assert fmt.num("42") == "42"

63

64 def test_path_plain(self, monkeypatch):

65 monkeypatch.setattr(fmt, "_USE_COLOR", False)

66 assert fmt.path("/some/file.py") == "/some/file.py"

67

68 def test_func_name_plain(self, monkeypatch):

69 monkeypatch.setattr(fmt, "_USE_COLOR", False)

70 assert fmt.func_name("my_func") == "my_func"

71

72

73 class 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

79

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

85

86

87 class 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"

93

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"

98

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"

103

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"

109

110

111 class 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"

117

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

122

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

128

129 def test_zero(self):

130 assert fmt.fmt_compact(0) == "0"

131

132 def test_float_input(self):

133 assert fmt.fmt_compact(100000.7) == "100.0 × 10³"

134

135

136 class TestCompositeHelpers:

137 def test_colored_num_int(self, monkeypatch):

138 monkeypatch.setattr(fmt, "_USE_COLOR", False)

139 assert fmt.colored_num(1000) == "1,000"

140

141 def test_colored_float(self, monkeypatch):

142 monkeypatch.setattr(fmt, "_USE_COLOR", False)

143 assert fmt.colored_float(1234.5) == "1,234.50"

144

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"

148

149 def test_colored_int(self, monkeypatch):

150 monkeypatch.setattr(fmt, "_USE_COLOR", False)

151 assert fmt.colored_int(1234567) == "1,234,567"