# vim: set fileencoding=utf-8 import unittest from bemani.protocol.xml import XmlDecoder class TestXmlDecoder(unittest.TestCase): def test_detect_encoding(self) -> None: xml = XmlDecoder(b'', 'ascii') tree = xml.get_tree() self.assertEqual(xml.encoding, 'utf-8') self.assertEqual(tree, None) xml = XmlDecoder(b'', 'ascii') tree = xml.get_tree() self.assertEqual(xml.encoding, 'utf-8') self.assertEqual(tree, None) def test_decode_void(self) -> None: xml = XmlDecoder(b'', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {}) self.assertEqual(tree.data_type, 'void') self.assertEqual(tree.value, None) xml = XmlDecoder(b'', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {}) self.assertEqual(tree.data_type, 'void') self.assertEqual(tree.value, None) def test_decode_attributes(self) -> None: xml = XmlDecoder(b'', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {'attr1': 'foo', 'attr2': 'bar'}) self.assertEqual(tree.data_type, 'void') self.assertEqual(tree.value, None) xml = XmlDecoder(b'', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {'attr1': 'foo', 'attr2': 'bar'}) self.assertEqual(tree.data_type, 'void') self.assertEqual(tree.value, None) xml = XmlDecoder(b'', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {'attr1': 'foo', 'attr2': 'bar'}) self.assertEqual(tree.data_type, 'void') self.assertEqual(tree.value, None) def test_decode_bin(self) -> None: xml = XmlDecoder(b'DEADBEEF', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {}) self.assertEqual(tree.data_type, 'bin') self.assertEqual(tree.value, b'\xDE\xAD\xBE\xEF') xml = XmlDecoder(b'\nDEADBEEF\n', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {}) self.assertEqual(tree.data_type, 'bin') self.assertEqual(tree.value, b'\xDE\xAD\xBE\xEF') xml = XmlDecoder(b' D E A D B E E F ', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {}) self.assertEqual(tree.data_type, 'bin') self.assertEqual(tree.value, b'\xDE\xAD\xBE\xEF') def test_decode_array(self) -> None: xml = XmlDecoder(b'1 2 3 4', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {}) self.assertEqual(tree.data_type, 'u32') self.assertEqual(tree.value, [1, 2, 3, 4]) xml = XmlDecoder(b'\n1\n2\n3\n4\n', 'ascii') tree = xml.get_tree() self.assertEqual(tree.children, []) self.assertEqual(tree.name, 'node') self.assertEqual(tree.attributes, {}) self.assertEqual(tree.data_type, 'u32') self.assertEqual(tree.value, [1, 2, 3, 4])