summaryrefslogtreecommitdiffstats
path: root/python/compare-locales/compare_locales/tests/test_dtd.py
blob: 87ddcde30135040dc58fddc7c32782713f48fb16 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

'''Tests for the DTD parser.
'''

import unittest
import re

from compare_locales.parser import getParser
from compare_locales.tests import ParserTestMixin


class TestDTD(ParserTestMixin, unittest.TestCase):
    '''Tests for the DTD Parser.'''
    filename = 'foo.dtd'

    def test_one_entity(self):
        self._test('''<!ENTITY foo.label "stuff">''',
                   (('foo.label', 'stuff'),))

    quoteContent = '''<!ENTITY good.one "one">
<!ENTITY bad.one "bad " quote">
<!ENTITY good.two "two">
<!ENTITY bad.two "bad "quoted" word">
<!ENTITY good.three "three">
<!ENTITY good.four "good ' quote">
<!ENTITY good.five "good 'quoted' word">
'''
    quoteRef = (
        ('good.one', 'one'),
        ('_junk_\\d_25-56$', '<!ENTITY bad.one "bad " quote">'),
        ('good.two', 'two'),
        ('_junk_\\d_82-119$', '<!ENTITY bad.two "bad "quoted" word">'),
        ('good.three', 'three'),
        ('good.four', 'good \' quote'),
        ('good.five', 'good \'quoted\' word'),)

    def test_quotes(self):
        self._test(self.quoteContent, self.quoteRef)

    def test_apos(self):
        qr = re.compile('[\'"]', re.M)

        def quot2apos(s):
            return qr.sub(lambda m: m.group(0) == '"' and "'" or '"', s)

        self._test(quot2apos(self.quoteContent),
                   map(lambda t: (t[0], quot2apos(t[1])), self.quoteRef))

    def test_parsed_ref(self):
        self._test('''<!ENTITY % fooDTD SYSTEM "chrome://brand.dtd">
  %fooDTD;
''',
                   (('fooDTD', '"chrome://brand.dtd"'),))

    def test_trailing_comment(self):
        self._test('''<!ENTITY first "string">
<!ENTITY second "string">
<!--
<!ENTITY commented "out">
-->
''',
                   (('first', 'string'), ('second', 'string')))

    def test_license_header(self):
        p = getParser('foo.dtd')
        p.readContents(self.resource('triple-license.dtd'))
        for e in p:
            self.assertEqual(e.key, 'foo')
            self.assertEqual(e.val, 'value')
        self.assert_('MPL' in p.header)
        p.readContents('''\
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this file,
   - You can obtain one at http://mozilla.org/MPL/2.0/.  -->
<!ENTITY foo "value">
''')
        for e in p:
            self.assertEqual(e.key, 'foo')
            self.assertEqual(e.val, 'value')
        self.assert_('MPL' in p.header)

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