summaryrefslogtreecommitdiffstats
path: root/python/compare-locales/compare_locales/tests/__init__.py
blob: 8808d78f4c0fe4275ac96a46fc4a76f28b639e43 (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
# 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/.

'''Mixins for parser tests.
'''

from itertools import izip_longest
from pkg_resources import resource_string
import re

from compare_locales.parser import getParser


class ParserTestMixin():
    '''Utility methods used by the parser tests.
    '''
    filename = None

    def setUp(self):
        '''Create a parser for this test.
        '''
        self.parser = getParser(self.filename)

    def tearDown(self):
        'tear down this test'
        del self.parser

    def resource(self, name):
        testcontent = resource_string(__name__, 'data/' + name)
        # fake universal line endings
        testcontent = re.sub('\r\n?', lambda m: '\n', testcontent)
        return testcontent

    def _test(self, content, refs):
        '''Helper to test the parser.
        Compares the result of parsing content with the given list
        of reference keys and values.
        '''
        self.parser.readContents(content)
        entities = [entity for entity in self.parser]
        for entity, ref in izip_longest(entities, refs):
            self.assertTrue(entity, 'excess reference entity')
            self.assertTrue(ref, 'excess parsed entity')
            self.assertEqual(entity.val, ref[1])
            if ref[0].startswith('_junk'):
                self.assertTrue(re.match(ref[0], entity.key))
            else:
                self.assertEqual(entity.key, ref[0])