summaryrefslogtreecommitdiff
path: root/tests/scanner/test_xmlwriter.py
blob: c6748c00bd3e0968acfbf196b753cbc93364207f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import unittest

from giscanner.xmlwriter import XMLWriter, collect_attributes, build_xml_tag


class TestXMLWriter(unittest.TestCase):

    def test_various(self):
        w = XMLWriter()
        w.push_tag('repository')
        w.push_tag('namespace')
        w.push_tag('enumeration')
        w.push_tag('member',
                   [('name', 'west'),
                    ('value', '7'),
                    ('c:identifier', 'GTK_ANCHOR_WEST'),
                    ('glib:nick', 'west')])

        w.pop_tag()
        w.pop_tag()
        w.pop_tag()
        x = w.get_xml()
        self.assertTrue('GTK_ANCHOR_WEST' in x)
        lines = x.split('\n')
        self.assertTrue(len(lines[3]) < 80)

    def test_collect_attributes(self):
        ca = collect_attributes
        res = ca('parameters', [], 6, ' ', 12)
        self.assertEqual(res, "")

        res = ca('type', [('name', 'utf8')], 12, ' ', 7)
        self.assertEqual(res, ' name="utf8"')

        res = ca('type', [('name', 'GLib.SList'), ('c:type', 'const GSList*')], 8, ' ', 6)
        self.assertEqual(res, ' name="GLib.SList" c:type="const GSList*"')

    def test_build_xml_tag(self):
        res = build_xml_tag('tag', [('attr', 'utf8')])
        self.assertEqual(res, '<tag attr="utf8"/>')

        res = build_xml_tag('tag', [('attr', 'foo\nbar')])
        self.assertEqual(res, '<tag attr="foo&#10;bar"/>')

        res = build_xml_tag('tag', [('attr', 'foo\tbar')])
        self.assertEqual(res, '<tag attr="foo&#9;bar"/>')

        res = build_xml_tag('tag', [('attr', '\004')])
        self.assertEqual(res, '<tag attr="\x04"/>')

        res = build_xml_tag('tag', [('attr', 'limba1\t\034')])
        self.assertEqual(res, '<tag attr="limba1&#9;\034"/>')

        res = build_xml_tag('tag', [('attr', '')])
        self.assertEqual(res, '<tag attr=""/>')

        res = build_xml_tag('tag', [('attr', ' ')])
        self.assertEqual(res, '<tag attr=" "/>')

        res = build_xml_tag('tag', [('attr', '>&<')])
        self.assertEqual(res, '<tag attr="&gt;&amp;&lt;"/>')

        res = build_xml_tag('tag', [('a', 'b'), ('c', 'd')])
        self.assertEqual(res, '<tag a="b" c="d"/>')

        res = build_xml_tag('tag', [('foo', None), ('bar', 'quux')])
        self.assertEqual(res, '<tag bar="quux"/>')

    def test_build_xml_tag_data(self):
        res = build_xml_tag('tag', [], b'foo')
        self.assertEqual(res, '<tag>foo</tag>')

        res = build_xml_tag('tag', [], u'\xf6\xe4\xfc')
        self.assertEqual(res, u'<tag>\xf6\xe4\xfc</tag>')

        res = build_xml_tag('tag', [], '>&<')
        self.assertEqual(res, '<tag>&gt;&amp;&lt;</tag>')

    def test_build_xml_tag_indent(self):
        res = build_xml_tag(
            'tag', [('a' * 10, 'b' * 30), ('c' * 30, 'd' * 10)], None)
        self.assertEqual(res, '''\
<tag aaaaaaaaaa="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
     cccccccccccccccccccccccccccccc="dddddddddd"/>''')

        res = build_xml_tag(
            'tag', [('a' * 10, 'b' * 30), ('c' * 30, 'd' * 10)], None, 3)
        self.assertEqual(res, '''\
<tag aaaaaaaaaa="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
        cccccccccccccccccccccccccccccc="dddddddddd"/>''')


if __name__ == '__main__':
    unittest.main()