Debian Codegraph @

Lib/test/test_tomllib/test_error.py

raw
1 # SPDX-License-Identifier: MIT
2 # SPDX-FileCopyrightText: 2021 Taneli Hukkinen
3 # Licensed to PSF under a Contributor Agreement.
4
5 import unittest
6
7 from . import tomllib
8
9
10 class TestError(unittest.TestCase):
11 def test_line_and_col(self):
12 with self.assertRaises(tomllib.TOMLDecodeError) as exc_info:
13 tomllib.loads("val=.")
14 self.assertEqual(str(exc_info.exception), "Invalid value (at line 1, column 5)")
15
16 with self.assertRaises(tomllib.TOMLDecodeError) as exc_info:
17 tomllib.loads(".")
18 self.assertEqual(
19 str(exc_info.exception), "Invalid statement (at line 1, column 1)"
20 )
21
22 with self.assertRaises(tomllib.TOMLDecodeError) as exc_info:
23 tomllib.loads("\n\nval=.")
24 self.assertEqual(str(exc_info.exception), "Invalid value (at line 3, column 5)")
25
26 with self.assertRaises(tomllib.TOMLDecodeError) as exc_info:
27 tomllib.loads("\n\n.")
28 self.assertEqual(
29 str(exc_info.exception), "Invalid statement (at line 3, column 1)"
30 )
31
32 def test_missing_value(self):
33 with self.assertRaises(tomllib.TOMLDecodeError) as exc_info:
34 tomllib.loads("\n\nfwfw=")
35 self.assertEqual(str(exc_info.exception), "Invalid value (at end of document)")
36
37 def test_invalid_char_quotes(self):
38 with self.assertRaises(tomllib.TOMLDecodeError) as exc_info:
39 tomllib.loads("v = '\n'")
40 self.assertTrue(" '\\n' " in str(exc_info.exception))
41
42 def test_type_error(self):
43 with self.assertRaises(TypeError) as exc_info:
44 tomllib.loads(b"v = 1") # type: ignore[arg-type]
45 self.assertEqual(str(exc_info.exception), "Expected str object, not 'bytes'")
46
47 with self.assertRaises(TypeError) as exc_info:
48 tomllib.loads(False) # type: ignore[arg-type]
49 self.assertEqual(str(exc_info.exception), "Expected str object, not 'bool'")
50
51 def test_module_name(self):
52 self.assertEqual(
53 tomllib.TOMLDecodeError("", "", 0).__module__, tomllib.__name__
54 )
55
56 def test_invalid_parse_float(self):
57 def dict_returner(s: str) -> dict:
58 return {}
59
60 def list_returner(s: str) -> list:
61 return []
62
63 for invalid_parse_float in (dict_returner, list_returner):
64 with self.assertRaises(ValueError) as exc_info:
65 tomllib.loads("f=0.1", parse_float=invalid_parse_float)
66 self.assertEqual(
67 str(exc_info.exception), "parse_float must not return dicts or lists"
68 )
69
70 def test_deprecated_tomldecodeerror(self):
71 for args in [
72 (),
73 ("err msg",),
74 (None,),
75 (None, "doc"),
76 ("err msg", None),
77 (None, "doc", None),
78 ("err msg", "doc", None),
79 ("one", "two", "three", "four"),
80 ("one", "two", 3, "four", "five"),
81 ]:
82 with self.assertWarns(DeprecationWarning):
83 e = tomllib.TOMLDecodeError(*args) # type: ignore[arg-type]
84 self.assertEqual(e.args, args)
85
86 def test_tomldecodeerror(self):
87 msg = "error parsing"
88 doc = "v=1\n[table]\nv='val'"
89 pos = 13
90 formatted_msg = "error parsing (at line 3, column 2)"
91 e = tomllib.TOMLDecodeError(msg, doc, pos)
92 self.assertEqual(e.args, (formatted_msg,))
93 self.assertEqual(str(e), formatted_msg)
94 self.assertEqual(e.msg, msg)
95 self.assertEqual(e.doc, doc)
96 self.assertEqual(e.pos, pos)
97 self.assertEqual(e.lineno, 3)
98 self.assertEqual(e.colno, 2)