2019-12-08 22:43:49 +01:00
|
|
|
# vim: set fileencoding=utf-8
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from bemani.protocol.xml import XmlDecoder
|
|
|
|
|
|
|
|
|
|
|
|
class TestXmlDecoder(unittest.TestCase):
|
|
|
|
def test_detect_encoding(self) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b'<?xml version="1.0" encoding="utf-8"?>', "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(xml.encoding, "utf-8")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree, None)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b'<?xml\nversion = "1.0"\tencoding = "utf-8"?>', "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(xml.encoding, "utf-8")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree, None)
|
|
|
|
|
|
|
|
def test_decode_void(self) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b"<node></node>", "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.attributes, {})
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.data_type, "void")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.value, None)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b"<node />", "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.attributes, {})
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.data_type, "void")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.value, None)
|
|
|
|
|
|
|
|
def test_decode_attributes(self) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b'<node attr1="foo" attr2="bar"></node>', "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
|
|
|
self.assertEqual(tree.attributes, {"attr1": "foo", "attr2": "bar"})
|
|
|
|
self.assertEqual(tree.data_type, "void")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.value, None)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b'<node attr1="foo" attr2="bar" />', "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
|
|
|
self.assertEqual(tree.attributes, {"attr1": "foo", "attr2": "bar"})
|
|
|
|
self.assertEqual(tree.data_type, "void")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.value, None)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b'<node\nattr1="foo"\tattr2="bar"/>', "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
|
|
|
self.assertEqual(tree.attributes, {"attr1": "foo", "attr2": "bar"})
|
|
|
|
self.assertEqual(tree.data_type, "void")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.value, None)
|
|
|
|
|
|
|
|
def test_decode_bin(self) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b'<node __type="bin">DEADBEEF</node>', "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.attributes, {})
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.data_type, "bin")
|
|
|
|
self.assertEqual(tree.value, b"\xDE\xAD\xBE\xEF")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b'<node __type="bin">\nDEADBEEF\n</node>', "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.attributes, {})
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.data_type, "bin")
|
|
|
|
self.assertEqual(tree.value, b"\xDE\xAD\xBE\xEF")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b'<node __type="bin"> D E A D B E E F </node>', "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.attributes, {})
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.data_type, "bin")
|
|
|
|
self.assertEqual(tree.value, b"\xDE\xAD\xBE\xEF")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
def test_decode_array(self) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(b'<node __type="u32" __count="4">1 2 3 4</node>', "ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.attributes, {})
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.data_type, "u32")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.value, [1, 2, 3, 4])
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
xml = XmlDecoder(
|
|
|
|
b'<node __type="u32" __count="4">\n1\n2\n3\n4\n</node>', "ascii"
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
tree = xml.get_tree()
|
|
|
|
|
|
|
|
self.assertEqual(tree.children, [])
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.name, "node")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.attributes, {})
|
2022-10-15 20:56:30 +02:00
|
|
|
self.assertEqual(tree.data_type, "u32")
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(tree.value, [1, 2, 3, 4])
|