xref: /freebsd/contrib/libucl/python/tests/test_load.py (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 = {
75            'key1': [
76                'value',
77                'value2',
78                'value;',
79                1.0,
80                -0xdeadbeef,
81                '0xdeadbeef.1',
82                '0xreadbeef',
83                -1e-10,
84                1,
85                True,
86                False,
87                True,
88            ]
89        }
90        with open("../tests/basic/1.in", "r") as in1:
91            self.assertEqual(ucl.load(in1.read()), valid)
92
93    def test_every_type(self):
94        data = ("""{
95            "key1": value;
96            "key2": value2;
97            "key3": "value;"
98            "key4": 1.0,
99            "key5": -0xdeadbeef
100            "key6": 0xdeadbeef.1
101            "key7": 0xreadbeef
102            "key8": -1e-10,
103            "key9": 1
104            "key10": true
105            "key11": no
106            "key12": yes
107        }""")
108        valid = {
109            'key1': 'value',
110            'key2': 'value2',
111            'key3': 'value;',
112            'key4': 1.0,
113            'key5': -3735928559,
114            'key6': '0xdeadbeef.1',
115            'key7': '0xreadbeef',
116            'key8': -1e-10,
117            'key9': 1,
118            'key10': True,
119            'key11': False,
120            'key12': True,
121            }
122        self.assertEqual(ucl.load(data), valid)
123