• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2020 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Test XML diff."""
15import unittest
16import xml.etree.ElementTree as ET
17
18import xml_diff
19
20
21class XmlDiffTest(unittest.TestCase):
22
23  def test_attribute_changes(self):
24    e1 = ET.fromstring('<node attr1="hello" attr2="hello2" ignored="me"/>')
25    e2 = ET.fromstring('<node attr3="hello3" attr2="bye2"/>')
26    changes = xml_diff.attribute_changes(e1, e2, set(['ignored']))
27    self.assertEqual(
28        xml_diff.ChangeMap(
29            added={'attr3': 'hello3'},
30            removed={'attr1': 'hello'},
31            modified={'attr2': xml_diff.Change('hello2', 'bye2')}), changes)
32
33  def test_compare_subelements(self):
34    p1 = ET.fromstring("""<parent>
35      <tag1 attr="newfile2" attrkey="notneeded" />
36      <tag1 attr="oldfile1" attrkey="dest1" />
37      <tag2 attr="oldfile2" attrkey="dest2" />
38    </parent>
39    """)
40    p2 = ET.fromstring("""<parent>
41      <tag1 attr="newfile1" attrkey="dest1" />
42      <tag2 attr="newfile2" attrkey="dest2" />
43      <tag2 attr="somefile" attrkey="addedfile" />
44    </parent>
45    """)
46
47    changes = xml_diff.compare_subelements(
48        tag='tag1',
49        p1=p1,
50        p2=p2,
51        ignored_attrs=set(),
52        key_fn=lambda x: x.get('attrkey'),
53        diff_fn=xml_diff.attribute_changes)
54    self.assertEqual(changes.added, {})
55    self.assertEqual(
56        changes.removed,
57        {'notneeded': '<tag1 attr="newfile2" attrkey="notneeded" />'})
58    self.assertEqual(
59        changes.modified, {
60            'dest1':
61                xml_diff.ChangeMap(
62                    modified={'attr': xml_diff.Change('oldfile1', 'newfile1')})
63        })
64