@@ -164,6 +164,64 @@ def sink(message):
164
164
]
165
165
166
166
167
+ def test_non_string_message (writer ):
168
+ logger .add (writer , format = "{message}" )
169
+
170
+ logger .info (1 )
171
+ logger .info ({})
172
+ logger .info (b"test" )
173
+
174
+ assert writer .read () == "1\n {}\n b'test'\n "
175
+
176
+
177
+ @pytest .mark .parametrize ("colors" , [True , False ])
178
+ def test_non_string_message_is_str_in_record (writer , colors ):
179
+ output = ""
180
+
181
+ def sink (message ):
182
+ nonlocal output
183
+ assert isinstance (message .record ["message" ], str )
184
+ output += message
185
+
186
+ def format (record ):
187
+ assert isinstance (record ["message" ], str )
188
+ return "[{message}]\n "
189
+
190
+ logger .add (sink , format = format , catch = False )
191
+ logger .opt (colors = colors ).info (123 )
192
+ assert output == "[123]\n "
193
+
194
+
195
+ @pytest .mark .parametrize ("colors" , [True , False ])
196
+ def test_missing_positional_field_during_formatting (writer , colors ):
197
+ logger .add (writer )
198
+
199
+ with pytest .raises (IndexError ):
200
+ logger .opt (colors = colors ).info ("Foo {} {}" , 123 )
201
+
202
+
203
+ @pytest .mark .parametrize ("colors" , [True , False ])
204
+ def test_missing_named_field_during_formatting (writer , colors ):
205
+ logger .add (writer )
206
+
207
+ with pytest .raises (KeyError ):
208
+ logger .opt (colors = colors ).info ("Foo {bar}" , baz = 123 )
209
+
210
+
211
+ def test_not_formattable_message (writer ):
212
+ logger .add (writer )
213
+
214
+ with pytest .raises (AttributeError ):
215
+ logger .info (123 , baz = 456 )
216
+
217
+
218
+ def test_not_formattable_message_with_colors (writer ):
219
+ logger .add (writer )
220
+
221
+ with pytest .raises (TypeError ):
222
+ logger .opt (colors = True ).info (123 , baz = 456 )
223
+
224
+
167
225
def test_invalid_color_markup (writer ):
168
226
with pytest .raises (ValueError ):
169
227
logger .add (writer , format = "<red>Not closed tag" , colorize = True )
0 commit comments