1 from __future__ import annotations

2

3

4 from muutils.errormode import ErrorMode, ERROR_MODE_ALIASES

5

6 import pytest

7

8

9 def test_invalid_mode():

10 with pytest.raises(ValueError):

11 ErrorMode("invalid")

12

13

14@pytest.mark.parametrize(

15 "mode, expected_mode",

16 [

17 ("except", ErrorMode.EXCEPT),

18 ("warn", ErrorMode.WARN),

19 ("ignore", ErrorMode.IGNORE),

20 ("Except", ErrorMode.EXCEPT),

21 ("Warn", ErrorMode.WARN),

22 ("Ignore", ErrorMode.IGNORE),

23 (" \teXcEpT \n", ErrorMode.EXCEPT),

24 ("WaRn \t", ErrorMode.WARN),

25 (" \tIGNORE", ErrorMode.IGNORE),

26 ],

27)

28 def test_from_any_strict_ok(mode, expected_mode):

29 assert ErrorMode.from_any(mode, allow_aliases=False) == expected_mode

30

31

32@pytest.mark.parametrize(

33 "mode, excepted_error",

34 [

35 (42, TypeError),

36 (42.0, TypeError),

37 (None, TypeError),

38 (object(), TypeError),

39 (True, TypeError),

40 (False, TypeError),

41 (["except"], TypeError),

42 ("invalid", KeyError),

43 (" \tinvalid", KeyError),

44 ("e", KeyError),

45 (" E", KeyError),

46 ("w", KeyError),

47 ("W", KeyError),

48 ("i", KeyError),

49 ("I", KeyError),

50 ("silent", KeyError),

51 ("Silent", KeyError),

52 ("quiet", KeyError),

53 ("Quiet", KeyError),

54 ("raise", KeyError),

55 ("Raise", KeyError),

56 ("error", KeyError),

57 ("Error", KeyError),

58 ("err", KeyError),

59 ("ErR\t", KeyError),

60 ("warning", KeyError),

61 ("Warning", KeyError),

62 ("ErrorMode.EXCEPT", KeyError),

63 ("ErrorMode.WARN", KeyError),

64 ("ErrorMode.IGNORE", KeyError),

65 (" \tErrorMode.EXCEPT \t", KeyError),

66 (" \tErrorMode.WARN \t", KeyError),

67 (" \tErrorMode.IGNORE \t", KeyError),

68 ],

69)

70 def test_from_any_strict_error(mode, excepted_error):

71 with pytest.raises(excepted_error):

72 ErrorMode.from_any(mode, allow_aliases=False, allow_prefix=False)

73

74

75@pytest.mark.parametrize(

76 "mode, expected_mode",

77 [

78 *list(ERROR_MODE_ALIASES.items()),

79 *list((a.upper(), b) for a, b in ERROR_MODE_ALIASES.items()),

80 *list((a.title(), b) for a, b in ERROR_MODE_ALIASES.items()),

81 *list((a.capitalize(), b) for a, b in ERROR_MODE_ALIASES.items()),

82 *list((f" \t{a} \t", b) for a, b in ERROR_MODE_ALIASES.items()),

83 ],

84)

85 def test_from_any_aliases_ok(mode, expected_mode):

86 assert ErrorMode.from_any(mode) == expected_mode

87 assert ErrorMode.from_any(mode, allow_aliases=True) == expected_mode

88

89

90@pytest.mark.parametrize(

91 "mode, excepted_error",

92 [

93 (42, TypeError),

94 (42.0, TypeError),

95 (None, TypeError),

96 (object(), TypeError),

97 (True, TypeError),

98 (False, TypeError),

99 (["except"], TypeError),

100 ("invalid", KeyError),

101 (" \tinvalid", KeyError),

102 ],

103)

104 def test_from_any_aliases_error(mode, excepted_error):

105 with pytest.raises(excepted_error):

106 ErrorMode.from_any(mode, allow_aliases=True)

107

108

109@pytest.mark.parametrize(

110 "mode, excepted_error",

111 [

112 (42, TypeError),

113 (42.0, TypeError),

114 (None, TypeError),

115 (object(), TypeError),

116 (True, TypeError),

117 (False, TypeError),

118 (["except"], TypeError),

119 ("invalid", KeyError),

120 (" \tinvalid", KeyError),

121 ("ErrorMode.EXCEPT", KeyError),

122 ("ErrorMode.WARN", KeyError),

123 ("ErrorMode.IGNORE", KeyError),

124 (" \tErrorMode.EXCEPT \t", KeyError),

125 (" \tErrorMode.WARN \t", KeyError),

126 (" \tErrorMode.IGNORE \t", KeyError),

127 ],

128)

129 def test_from_any_no_prefix_error(mode, excepted_error):

130 with pytest.raises(excepted_error):

131 ErrorMode.from_any(mode, allow_aliases=True, allow_prefix=False)

132

133

134@pytest.mark.parametrize(

135 "mode, expected_mode",

136 [

137 ("ErrorMode.EXCEPT", ErrorMode.EXCEPT),

138 ("ErrorMode.WARN", ErrorMode.WARN),

139 ("ErrorMode.IGNORE", ErrorMode.IGNORE),

140 (" ErrorMode.EXCEPT ", ErrorMode.EXCEPT),

141 ("\tErrorMode.WARN\n", ErrorMode.WARN),

142 (" \t ErrorMode.IGNORE \n ", ErrorMode.IGNORE),

143 ],

144)

145 def test_from_any_with_prefix(mode, expected_mode):

146 assert ErrorMode.from_any(mode, allow_prefix=True) == expected_mode

147

148

149@pytest.mark.parametrize(

150 "mode, expected_error",

151 [

152 ("ErrorMode.EXCEPT", KeyError),

153 ("ErrorMode.WARN", KeyError),

154 ("ErrorMode.IGNORE", KeyError),

155 (" ErrorMode.EXCEPT ", KeyError),

156 ("\tErrorMode.WARN\n", KeyError),

157 (" \t ErrorMode.IGNORE \n ", KeyError),

158 ],

159)

160 def test_from_any_without_prefix(mode, expected_error):

161 with pytest.raises(expected_error):

162 ErrorMode.from_any(mode, allow_prefix=False)

163

164

165@pytest.mark.parametrize(

166 "mode, expected_mode",

167 [

168 ("ErrorMode.except", ErrorMode.EXCEPT),

169 ("ErrorMode.warn", ErrorMode.WARN),

170 ("ErrorMode.ignore", ErrorMode.IGNORE),

171 ("ErrorMode.EXCEPT", ErrorMode.EXCEPT),

172 ("ErrorMode.WARN", ErrorMode.WARN),

173 ("ErrorMode.IGNORE", ErrorMode.IGNORE),

174 (" ErrorMode.eXcEpT ", ErrorMode.EXCEPT),

175 ("\tErrorMode.WaRn\n", ErrorMode.WARN),

176 (" \t ErrorMode.IgNoRe \n ", ErrorMode.IGNORE),

177 ],

178)

179 def test_from_any_with_prefix_case_insensitive(mode, expected_mode):

180 assert ErrorMode.from_any(mode, allow_prefix=True) == expected_mode

181

182

183@pytest.mark.parametrize(

184 "mode, expected_error",

185 [

186 ("ErrorMode.invalid", KeyError),

187 ("ErrorMode.", KeyError),

188 ("ErrorMode", KeyError),

189 ("errormode.EXCEPT", KeyError),

190 ("ErrorMode.E", KeyError),

191 ("ErrorMode.W", KeyError),

192 ("ErrorMode.I", KeyError),

193 ],

194)

195 def test_from_any_with_prefix_invalid(mode, expected_error):

196 with pytest.raises(expected_error):

197 ErrorMode.from_any(mode, allow_prefix=False)

198

199

200 def test_from_any_with_prefix_and_aliases():

201 assert (

202 ErrorMode.from_any("ErrorMode.e", allow_prefix=True, allow_aliases=True)

203 == ErrorMode.EXCEPT

204 )

205 assert (

206 ErrorMode.from_any("ErrorMode.w", allow_prefix=True, allow_aliases=True)

207 == ErrorMode.WARN

208 )

209 assert (

210 ErrorMode.from_any("ErrorMode.i", allow_prefix=True, allow_aliases=True)

211 == ErrorMode.IGNORE

212 )

213

214

215 def test_from_any_with_prefix_no_aliases():

216 with pytest.raises(KeyError):

217 ErrorMode.from_any("ErrorMode.e", allow_prefix=True, allow_aliases=False)

218 with pytest.raises(KeyError):

219 ErrorMode.from_any("ErrorMode.w", allow_prefix=True, allow_aliases=False)

220 with pytest.raises(KeyError):

221 ErrorMode.from_any("ErrorMode.i", allow_prefix=True, allow_aliases=False)

222

223

224# Tests for new string representation methods

225

226

227 def test_str_representation():

228 assert str(ErrorMode.EXCEPT) == "ErrorMode.Except"

229 assert str(ErrorMode.WARN) == "ErrorMode.Warn"

230 assert str(ErrorMode.IGNORE) == "ErrorMode.Ignore"

231

232

233 def test_repr_representation():

234 assert repr(ErrorMode.EXCEPT) == "ErrorMode.Except"

235 assert repr(ErrorMode.WARN) == "ErrorMode.Warn"

236 assert repr(ErrorMode.IGNORE) == "ErrorMode.Ignore"

237

238

239 def test_serialize():

240 assert ErrorMode.EXCEPT.serialize() == "ErrorMode.Except"

241 assert ErrorMode.WARN.serialize() == "ErrorMode.Warn"

242 assert ErrorMode.IGNORE.serialize() == "ErrorMode.Ignore"

243

244

245 def test_load():

246 assert ErrorMode.load("ErrorMode.Except") == ErrorMode.EXCEPT

247 assert ErrorMode.load("ErrorMode.Warn") == ErrorMode.WARN

248 assert ErrorMode.load("ErrorMode.Ignore") == ErrorMode.IGNORE

249

250 with pytest.raises(KeyError):

251 ErrorMode.load("ErrorMode.Invalid")