2020-06-19 10:06:21 +00:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
2020-06-20 12:48:32 +00:00
|
|
|
import os
|
2017-02-10 11:55:30 +00:00
|
|
|
import unittest
|
|
|
|
|
2020-06-19 10:06:21 +00:00
|
|
|
from flightgear.meta import sgprops
|
|
|
|
|
2017-02-10 11:55:30 +00:00
|
|
|
|
2020-06-20 12:48:32 +00:00
|
|
|
baseDir = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
def testData(*args):
|
|
|
|
return os.path.join(baseDir, "testData", *args)
|
|
|
|
|
|
|
|
|
2017-02-10 11:55:30 +00:00
|
|
|
class SGProps(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_parse(self):
|
2020-06-20 12:48:32 +00:00
|
|
|
parsed = sgprops.readProps(testData("props1.xml"))
|
2017-02-10 11:55:30 +00:00
|
|
|
|
|
|
|
self.assertEqual(parsed.getValue("value"), 42)
|
2020-06-19 10:06:21 +00:00
|
|
|
self.assertEqual(type(parsed.getValue("value")), int)
|
2017-02-10 11:55:30 +00:00
|
|
|
|
|
|
|
valNode = parsed.getChild("value")
|
|
|
|
self.assertEqual(valNode.parent, parsed)
|
|
|
|
self.assertEqual(valNode.name, "value")
|
|
|
|
|
|
|
|
self.assertEqual(valNode.value, 42)
|
2020-06-19 10:06:21 +00:00
|
|
|
self.assertEqual(type(valNode.value), int)
|
2017-02-10 11:55:30 +00:00
|
|
|
|
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
missingNode = parsed.getChild("missing")
|
|
|
|
|
|
|
|
things = parsed.getChildren("thing")
|
|
|
|
self.assertEqual(len(things), 3)
|
|
|
|
|
|
|
|
self.assertEqual(things[0], parsed.getChild("thing", 0));
|
|
|
|
self.assertEqual(things[1], parsed.getChild("thing", 1));
|
|
|
|
self.assertEqual(things[2], parsed.getChild("thing", 2));
|
|
|
|
|
|
|
|
self.assertEqual(things[0].getValue("value"), "apple");
|
|
|
|
self.assertEqual(things[1].getValue("value"), "lemon");
|
|
|
|
self.assertEqual(things[2].getValue("value"), "pear");
|
|
|
|
|
|
|
|
def test_create(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_invalidIndex(self):
|
|
|
|
with self.assertRaises(IndexError):
|
2020-06-20 12:48:32 +00:00
|
|
|
parsed = sgprops.readProps(testData("bad-index.xml"))
|
2017-02-10 11:55:30 +00:00
|
|
|
|
|
|
|
def test_include(self):
|
2020-06-20 12:48:32 +00:00
|
|
|
parsed = sgprops.readProps(testData("props2.xml"))
|
2017-02-10 11:55:30 +00:00
|
|
|
|
|
|
|
# test that value in main file over-rides the one in the include
|
|
|
|
self.assertEqual(parsed.getValue("value"), 33)
|
|
|
|
|
|
|
|
# but these come from the included file
|
|
|
|
self.assertEqual(parsed.getValue("value[1]"), 43)
|
|
|
|
self.assertEqual(parsed.getValue("value[2]"), 44)
|
|
|
|
|
|
|
|
subNode = parsed.getChild("sub")
|
|
|
|
widgets = subNode.getChildren("widget")
|
|
|
|
self.assertEqual(len(widgets), 4)
|
|
|
|
|
|
|
|
self.assertEqual(widgets[2].value, 44)
|
|
|
|
self.assertEqual(widgets[3].value, 99)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|