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 ESC[4;38;5;81mTestError(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
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_module_name(self):
43 self.assertEqual(tomllib.TOMLDecodeError().__module__, tomllib.__name__)
44
45 def test_invalid_parse_float(self):
46 def dict_returner(s: str) -> dict:
47 return {}
48
49 def list_returner(s: str) -> list:
50 return []
51
52 for invalid_parse_float in (dict_returner, list_returner):
53 with self.assertRaises(ValueError) as exc_info:
54 tomllib.loads("f=0.1", parse_float=invalid_parse_float)
55 self.assertEqual(
56 str(exc_info.exception), "parse_float must not return dicts or lists"
57 )