xref: /freebsd/contrib/libucl/python/tests/test_dump.py (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1from .compat import unittest
2import ucl
3import sys
4
5class DumpTest(unittest.TestCase):
6    def test_no_args(self):
7        with self.assertRaises(TypeError):
8            ucl.dump()
9
10    def test_none(self):
11        self.assertEqual(ucl.dump(None), None)
12
13    def test_null(self):
14        data = { "a" : None }
15        valid = "a = null;\n"
16        self.assertEqual(ucl.dump(data), valid)
17
18    def test_int(self):
19        data = { "a" : 1 }
20        valid = "a = 1;\n"
21        self.assertEqual(ucl.dump(data), valid)
22
23    def test_nested_int(self):
24        data = { "a" : { "b" : 1 } }
25        valid = "a {\n    b = 1;\n}\n"
26        self.assertEqual(ucl.dump(data), valid)
27
28    def test_int_array(self):
29        data = { "a" : [1,2,3,4] }
30        valid = "a [\n    1,\n    2,\n    3,\n    4,\n]\n"
31        self.assertEqual(ucl.dump(data), valid)
32
33    def test_str(self):
34        data = { "a" : "b" }
35        valid = "a = \"b\";\n"
36        self.assertEqual(ucl.dump(data), valid)
37
38    @unittest.skipIf(sys.version_info[0] > 2, "Python3 uses unicode only")
39    def test_unicode(self):
40        data = { unicode("a") : unicode("b") }
41        valid = unicode("a = \"b\";\n")
42        self.assertEqual(ucl.dump(data), valid)
43
44    def test_float(self):
45        data = { "a" : 1.1 }
46        valid = "a = 1.100000;\n"
47        self.assertEqual(ucl.dump(data), valid)
48
49    def test_boolean(self):
50        data = { "a" : True, "b" : False }
51        valid = [
52            "a = true;\nb = false;\n",
53            "b = false;\na = true;\n"
54            ]
55        self.assertIn(ucl.dump(data), valid)
56
57    def test_empty_ucl(self):
58        self.assertEqual(ucl.dump({}), "")
59
60    def test_json(self):
61        data = { "a" : 1, "b": "bleh;" }
62        valid = [
63            '{\n    "a": 1,\n    "b": "bleh;"\n}',
64            '{\n    "b": "bleh;",\n    "a": 1\n}'
65            ]
66        self.assertIn(ucl.dump(data, ucl.UCL_EMIT_JSON), valid)
67