コード例 #1
0
    def test_next_node(self):
        ele = Element("foo")
        node = PrimitiveNode(position=0,
                             var=XmlText(name="foo", qname=QName("foo")))

        with self.assertRaises(XmlContextError):
            node.next_node(ele, 10, XmlContext())
コード例 #2
0
    def test_bind_derived_mode(self):
        var = XmlVar(text=True, name="foo", qname="foo", types=[int], derived=True)
        ns_map = {"foo": "bar"}
        node = PrimitiveNode(var, ns_map)
        objects = []

        self.assertTrue(node.bind("foo", "13", "Impossible", objects))
        self.assertEqual(DerivedElement("foo", 13), objects[-1][1])
コード例 #3
0
    def test_parse_element(self, mock_parse_value):
        mock_parse_value.return_value = 13
        node = PrimitiveNode(position=0, types=[int], default=100)
        ele = Element("foo", nsmap={"foo": "bar"})
        ele.text = "13"

        self.assertEqual((QName("foo"), 13), node.parse_element(ele, []))
        self.assertFalse(node.tokens)
        mock_parse_value.assert_called_once_with(node.types, ele.text, 100,
                                                 ele.nsmap, node.tokens)
コード例 #4
0
    def test_parse_element(self, mock_parse_value):
        mock_parse_value.return_value = 13
        var = XmlText(name="foo", qname=QName("foo"), default=100)
        node = PrimitiveNode(position=0, var=var)
        ele = Element("foo", nsmap={"foo": "bar"})
        ele.text = "13"

        self.assertEqual((QName("foo"), 13), node.parse_element(ele, []))
        mock_parse_value.assert_called_once_with(var.types, ele.text,
                                                 var.default, ele.nsmap,
                                                 var.is_tokens)
コード例 #5
0
    def test_bind_nillable_content(self):
        var = XmlVar(text=True, name="foo", qname="foo", types=[str], nillable=False)
        ns_map = {"foo": "bar"}
        node = PrimitiveNode(var, ns_map)
        objects = []

        self.assertTrue(node.bind("foo", None, None, objects))
        self.assertEqual("", objects[-1][1])

        var.nillable = True
        self.assertTrue(node.bind("foo", None, None, objects))
        self.assertIsNone(objects[-1][1])
コード例 #6
0
    def test_bind(self, mock_parse_value):
        mock_parse_value.return_value = 13
        var = XmlVar(text=True, name="foo", qname="foo", types=[int], format="Nope")
        ns_map = {"foo": "bar"}
        node = PrimitiveNode(var, ns_map)
        objects = []

        self.assertTrue(node.bind("foo", "13", "Impossible", objects))
        self.assertEqual(("foo", 13), objects[-1])

        mock_parse_value.assert_called_once_with(
            "13", var.types, var.default, ns_map, var.tokens, var.format
        )
コード例 #7
0
    def test_queue(self, mock_emit_event, mock_next_node):
        var = XmlText(name="foo", qname=QName("foo"))
        primitive_node = PrimitiveNode(position=1, var=var)
        mock_next_node.return_value = primitive_node
        element = Element("{urn:books}books")
        config = ParserConfig()
        root_queue_item = RootNode(
            position=0,
            meta=self.parser.context.build(Books),
            config=config,
        )

        objects = []
        queue = []
        queue.append(root_queue_item)
        self.parser.queue(element, queue, objects)

        self.assertEqual(2, len(queue))
        self.assertEqual(root_queue_item, queue[0])
        self.assertEqual(primitive_node, queue[1])

        mock_emit_event.assert_called_once_with(EventType.START,
                                                element.tag,
                                                item=root_queue_item,
                                                element=element)
コード例 #8
0
    def test_queue(self, mock_emit_event, mock_next_node):
        primitive_node = PrimitiveNode(position=1, types=[int])
        mock_next_node.return_value = primitive_node
        element = Element("{urn:books}books")
        config = ParserConfig()
        root_queue_item = RootNode(
            position=0,
            meta=self.parser.context.build(Books),
            default=None,
            config=config,
        )

        objects = list()
        queue = list()
        queue.append(root_queue_item)
        self.parser.queue(element, queue, objects)

        self.assertEqual(2, len(queue))
        self.assertEqual(root_queue_item, queue[0])
        self.assertEqual(primitive_node, queue[1])

        mock_emit_event.assert_called_once_with(EventType.START,
                                                element.tag,
                                                item=root_queue_item,
                                                element=element)
コード例 #9
0
    def test_end(self, mock_emit_event):
        objects = []
        queue = []
        var = XmlVar(text=True, name="foo", qname="foo", types=[bool])
        queue.append(PrimitiveNode(var=var, ns_map={}))

        result = self.parser.end(queue, objects, "enabled", "true", None)
        self.assertTrue(result)
        self.assertEqual(0, len(queue))
        self.assertEqual(("enabled", True), objects[-1])
        mock_emit_event.assert_called_once_with(EventType.END, "enabled", obj=result)
コード例 #10
0
    def test_end(self, mock_assemble):
        parser = NodeParser()
        objects = [("q", "result")]
        queue = []
        var = XmlVar(text=True, name="foo", qname="foo")
        queue.append(PrimitiveNode(var, ns_map={}))

        result = parser.end(queue, objects, "author", "foobar", None)
        self.assertEqual("result", result)
        self.assertEqual(0, len(queue))
        self.assertEqual(("q", result), objects[-1])
        mock_assemble.assert_called_once_with("author", "foobar", None, objects)
コード例 #11
0
    def test_dequeue(self, mock_parse_element, mock_emit_event):
        element = Element("author", nsmap={"prefix": "uri"})
        element.text = "foobar"

        objects = list()
        queue = list()
        queue.append(PrimitiveNode(position=0, types=[str], default=None))

        result = self.parser.dequeue(element, queue, objects)
        self.assertEqual("result", result)
        self.assertEqual(0, len(queue))
        self.assertEqual(("q", result), objects[-1])
        mock_parse_element.assert_called_once_with(element, objects)
        mock_emit_event.assert_called_once_with(EventType.END,
                                                element.tag,
                                                obj=result,
                                                element=element)
コード例 #12
0
    def test_dequeue(self, mock_parse_element, mock_emit_event):
        element = Element("author", nsmap={"prefix": "uri"})
        element.text = "foobar"

        objects = []
        queue = []
        var = XmlText(name="foo", qname=QName("foo"))
        queue.append(PrimitiveNode(position=0, var=var))

        result = self.parser.dequeue(element, queue, objects)
        self.assertEqual("result", result)
        self.assertEqual(0, len(queue))
        self.assertEqual(("q", result), objects[-1])
        mock_parse_element.assert_called_once_with(element, objects)
        mock_emit_event.assert_called_once_with(EventType.END,
                                                element.tag,
                                                obj=result,
                                                element=element)
コード例 #13
0
    def test_child(self):
        var = XmlVar(text=True, name="foo", qname="foo")
        node = PrimitiveNode(var, {})

        with self.assertRaises(XmlContextError):
            node.child("foo", {}, {}, 0)
コード例 #14
0
    def test_next_node(self):
        ele = Element("foo")
        node = PrimitiveNode(position=0, types=[])

        with self.assertRaises(XmlContextError):
            node.next_node(ele, 10, XmlContext())