xref: /freebsd/contrib/libucl/python/tests/test_load.py (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1from .compat import unittest
2import ucl
3
4class LoadTest(unittest.TestCase):
5    def test_no_args(self):
6        with self.assertRaises(TypeError):
7            ucl.load()
8
9    def test_multi_args(self):
10        with self.assertRaises(TypeError):
11            ucl.load(0,0)
12
13    def test_none(self):
14        self.assertEqual(ucl.load(None), None)
15
16    def test_null(self):
17        data  = "a: null"
18        valid = { "a" : None }
19        self.assertEqual(ucl.load(data), valid)
20
21    def test_int(self):
22        data  = "a : 1"
23        valid = { "a" : 1 }
24        self.assertEqual(ucl.load(data), valid)
25
26    def test_braced_int(self):
27        data  = "{a : 1}"
28        valid = { "a" : 1 }
29        self.assertEqual(ucl.load(data), valid)
30
31    def test_nested_int(self):
32        data  = "a : { b : 1 }"
33        valid = { "a" : { "b" : 1 } }
34        self.assertEqual(ucl.load(data), valid)
35
36    def test_str(self):
37        data  = "a : b"
38        valid = { "a" : "b" }
39        self.assertEqual(ucl.load(data), valid)
40
41    def test_float(self):
42        data  = "a : 1.1"
43        valid = {"a" : 1.1}
44        self.assertEqual(ucl.load(data), valid)
45
46    def test_boolean(self):
47        data = (
48            "a : True;" \
49            "b : False"
50        )
51        valid = { "a" : True, "b" : False }
52        self.assertEqual(ucl.load(data), valid)
53
54    def test_empty_ucl(self):
55        self.assertEqual(ucl.load("{}"), {})
56
57    def test_single_brace(self):
58        self.assertEqual(ucl.load("{"), {})
59
60    def test_single_back_brace(self):
61        self.assertEqual(ucl.load("}"), {})
62
63    def test_single_square_forward(self):
64        self.assertEqual(ucl.load("["), [])
65
66    def test_invalid_ucl(self):
67        with self.assertRaisesRegex(ValueError, "unfinished key$"):
68            ucl.load('{ "var"')
69
70    def test_comment_ignored(self):
71        self.assertEqual(ucl.load("{/*1*/}"), {})
72
73    def test_1_in(self):
74        valid = { 'key1': 'value' }
75        with open("../tests/basic/1.in", "r") as in1:
76            self.assertEqual(ucl.load(in1.read()), valid)
77
78    def test_every_type(self):
79        data = ("""{
80            "key1": value;
81            "key2": value2;
82            "key3": "value;"
83            "key4": 1.0,
84            "key5": -0xdeadbeef
85            "key6": 0xdeadbeef.1
86            "key7": 0xreadbeef
87            "key8": -1e-10,
88            "key9": 1
89            "key10": true
90            "key11": no
91            "key12": yes
92        }""")
93        valid = {
94            'key1': 'value',
95            'key2': 'value2',
96            'key3': 'value;',
97            'key4': 1.0,
98            'key5': -3735928559,
99            'key6': '0xdeadbeef.1',
100            'key7': '0xreadbeef',
101            'key8': -1e-10,
102            'key9': 1,
103            'key10': True,
104            'key11': False,
105            'key12': True,
106            }
107        self.assertEqual(ucl.load(data), valid)
108