File: test_utils.py

package info (click to toggle)
xmldiff 2.6.3-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 732 kB
  • sloc: python: 4,617; xml: 855; makefile: 219; sh: 16
file content (160 lines) | stat: -rw-r--r-- 4,972 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import unittest

from lxml import etree
from xmldiff import utils


class TraverseTests(unittest.TestCase):
    def test_post_order(self):
        xml = """<document>
    <story firstPageTemplate='FirstPage'>
        <section xml:id='oldfirst' ref='3' single-ref='3'>
            <para>First paragraph</para>
        </section>
        <section xml:id='oldlast' ref='4' single-ref='4'>
            <para>Last paragraph</para>
        </section>
    </story>
</document>
"""
        root = etree.fromstring(xml)
        tree = root.getroottree()
        res = [tree.getpath(x) for x in utils.post_order_traverse(root)]
        self.assertEqual(
            res,
            [
                "/document/story/section[1]/para",
                "/document/story/section[1]",
                "/document/story/section[2]/para",
                "/document/story/section[2]",
                "/document/story",
                "/document",
            ],
        )

    def test_reverse_post_order(self):
        xml = """<document>
    <story firstPageTemplate='FirstPage'>
        <section xml:id='oldfirst' ref='3' single-ref='3'>
            <para>First paragraph</para>
        </section>
        <section xml:id='oldlast' ref='4' single-ref='4'>
            <para>Last paragraph</para>
        </section>
    </story>
</document>
"""
        root = etree.fromstring(xml)
        tree = root.getroottree()
        res = [tree.getpath(x) for x in utils.reverse_post_order_traverse(root)]
        self.assertEqual(
            res,
            [
                "/document/story/section[2]/para",
                "/document/story/section[2]",
                "/document/story/section[1]/para",
                "/document/story/section[1]",
                "/document/story",
                "/document",
            ],
        )

    def test_breadth_first(self):
        xml = """<document>
    <story>
        <section>
            <para>First <i>paragraph</i></para>
            <para>Second paragraph</para>
        </section>
        <section>
            <para>Third paragraph</para>
            <para>Fourth <b>paragraph</b></para>
        </section>
    </story>
    <story>
        <section>
            <para>Fifth paragraph</para>
        </section>
    </story>
</document>
"""
        root = etree.fromstring(xml)
        tree = root.getroottree()
        res = [tree.getpath(x) for x in utils.breadth_first_traverse(root)]
        self.assertEqual(
            res,
            [
                "/document",
                "/document/story[1]",
                "/document/story[2]",
                "/document/story[1]/section[1]",
                "/document/story[1]/section[2]",
                "/document/story[2]/section",
                "/document/story[1]/section[1]/para[1]",
                "/document/story[1]/section[1]/para[2]",
                "/document/story[1]/section[2]/para[1]",
                "/document/story[1]/section[2]/para[2]",
                "/document/story[2]/section/para",
                "/document/story[1]/section[1]/para[1]/i",
                "/document/story[1]/section[2]/para[2]/b",
            ],
        )


class LongestCommonSubsequenceTests(unittest.TestCase):
    def _diff(self, left, right, result):
        res = []
        for x, y in utils.longest_common_subsequence(left, right):
            self.assertEqual(left[x], right[y])
            res.append(left[x])

        self.assertEqual("".join(res), result)

    def test_lcs(self):

        self._diff("ABCDEF", "ABCDEF", "ABCDEF")

        self._diff("ABCDEF", "GHIJKL", "")

        self._diff("ABCDEF", "ACDQRB", "ACD")

        self._diff("CXCDEFX", "CDEFX", "CDEFX")

        self._diff("HUMAN", "CHIMPANZEE", "HMAN")

        self._diff("ABCDEF", "A", "A")

        self._diff("123AAAAAAAAA", "123BBBBBBBBB", "123")

        self._diff("AAAAAAAAA123", "BBBBBBBBB123", "123")

        self._diff("ABCDE1", "1FGHIJK", "1")

        # There are several correct options here, make sure that doesn't
        # confuse it, we want just one, and don't care which.
        self._diff("HORSEBACK", "SNOWFLAKE", "SAK")

        # Empty sequences:
        self._diff("", "", "")


class MakeAsciiTreeTests(unittest.TestCase):
    def test_make_ascii_tree(self):
        xml = """<document xmlns:diff="http://namespaces.shoobx.com/diff">
    <story firstPageTemplate='FirstPage'>
        <section xml:id='oldfirst' ref='3' single-ref='3'>
            <para diff:delete="">First paragraph</para>
        </section>
        <section xml:id='oldlast' ref='4' single-ref='4'>
            <para><diff:insert>Last paragraph</diff:insert></para>
        </section>
    </story>
</document>
"""
        root = etree.fromstring(xml)
        tree = utils.make_ascii_tree(root)
        self.assertEqual(
            tree,
            " document \n   story \n     section \n       para (delete)\n"
            "     section \n       para \n         diff:insert ",
        )