/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/EncodingAdapter.py

  • Committer: Martin
  • Date: 2010-05-03 20:57:39 UTC
  • mto: This revision was merged to the branch mainline in revision 5204.
  • Revision ID: gzlist@googlemail.com-20100503205739-n326zdvevv0rmruh
Retain original stack and error message when translating to ValueError in bencode

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
# -*- coding: utf-8 -*-
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
18
18
"""Adapter for running test cases against multiple encodings."""
19
19
 
25
25
# prefix for micro (1/1000000)
26
26
_mu = u'\xb5'
27
27
 
 
28
# greek letter omega, not to be confused with
 
29
# the Ohm sign, u'\u2126'. Though they are probably identical
 
30
# cp437 can handle the first, but not the second
 
31
_omega = u'\u03a9'
 
32
 
 
33
# smallest error possible, epsilon
 
34
# cp437 handles u03b5, but not u2208 the 'element of' operator
 
35
_epsilon = u'\u03b5'
 
36
 
28
37
# Swedish?
29
38
_erik = u'Erik B\xe5gfors'
30
39
 
39
48
 
40
49
# Russian, 'Alexander' in russian
41
50
_alexander = u'\u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440'
42
 
# No idea if this means anything, but we need another string
43
 
_alex = u'\u0410\u043b\u0435\u043a'
 
51
# The word 'test' in Russian
 
52
_russian_test = u'\u0422\u0435\u0441\u0442'
44
53
 
45
54
# Kanji
46
55
# It is a kanji sequence for nihonjin, or Japanese in English.
47
 
 
56
#
48
57
# '\u4eba' being person, 'u\65e5' sun and '\u672c' origin. Ie,
49
58
# sun-origin-person, 'native from the land where the sun rises'. Note, I'm
50
59
# not a fluent speaker, so this is just my crude breakdown.
51
 
 
60
#
52
61
# Wouter van Heyst
53
62
_nihonjin = u'\u65e5\u672c\u4eba'
54
63
 
70
79
_shalom = u'\u05e9\u05dc\u05d5\u05dd'
71
80
 
72
81
 
73
 
class EncodingTestAdapter(object):
74
 
    """A tool to generate a suite, testing multiple encodings for a single test.
75
 
    
76
 
    This is similar to bzrlib.transport.TransportTestProviderAdapter.
77
 
    It is done by copying the test once for each encoding, and injecting
78
 
    the encoding name, and the list of valid strings for that encoding.
79
 
    Each copy is also given a new id() to make it easy to identify.
80
 
    """
81
 
 
82
 
    _encodings = [
 
82
encoding_scenarios = [
83
83
        # Permutation 1 of utf-8
84
 
        ('utf-8', 1, {'committer':_erik
85
 
                  , 'message':_yellow_horse
86
 
                  , 'filename':_shrimp_sandwich
87
 
                  , 'directory':_nihonjin}),
 
84
        ('utf-8,1', {
 
85
            'info': {
 
86
                'committer': _erik,
 
87
                'message': _yellow_horse,
 
88
                'filename': _shrimp_sandwich,
 
89
                'directory': _nihonjin,
 
90
                },
 
91
            'encoding': 'utf-8',
 
92
            }),
88
93
        # Permutation 2 of utf-8
89
 
        ('utf-8', 2, {'committer':_alexander
90
 
                  , 'message':u'Testing ' + _mu
91
 
                  , 'filename':_shalom
92
 
                  , 'directory':_juju}),
93
 
        ('iso-8859-1', 0, {'committer':_erik
94
 
                  , 'message':u'Testing ' + _mu
95
 
                  , 'filename':_juju_alt
96
 
                  , 'directory':_shrimp_sandwich}),
97
 
        ('iso-8859-2', 0, {'committer':_someone
98
 
                  , 'message':_yellow_horse
99
 
                  , 'filename':_yellow
100
 
                  , 'directory':_something}),
101
 
        ('cp1251', 0, {'committer':_alexander
102
 
                  , 'message':u'Testing ' + _mu
103
 
                  , 'filename':_alex
104
 
                  , 'directory':_alex + 'dir'}),
 
94
        ('utf-8,2', {
 
95
            'info': {
 
96
                'committer': _alexander,
 
97
                'message': u'Testing ' + _mu,
 
98
                'filename': _shalom,
 
99
                'directory': _juju,
 
100
                },
 
101
            'encoding': 'utf-8',
 
102
            }),
 
103
        ('iso-8859-1', {
 
104
            'info': {
 
105
                'committer': _erik,
 
106
                'message': u'Testing ' + _mu,
 
107
                'filename': _juju_alt,
 
108
                'directory': _shrimp_sandwich,
 
109
                },
 
110
            'encoding': 'iso-8859-1',
 
111
            }),
 
112
        ('iso-8859-2', {
 
113
            'info': {
 
114
                'committer': _someone,
 
115
                'message': _yellow_horse,
 
116
                'filename': _yellow,
 
117
                'directory': _something,
 
118
                },
 
119
            'encoding': 'iso-8859-2',
 
120
            }),
 
121
        ('cp1251', {
 
122
            'info': {
 
123
                'committer': _alexander,
 
124
                'message': u'Testing ' + _mu,
 
125
                'filename': _russian_test,
 
126
                'directory': _russian_test + 'dir',
 
127
                },
 
128
            'encoding': 'cp1251',
 
129
            }),
 
130
# The iso-8859-1 tests run on a default windows cp437 installation
 
131
# and it takes a long time to run an extra permutation of the tests
 
132
# But just in case we want to add this back in:
 
133
#        ('cp437', {'committer':_erik
 
134
#                  , 'message':u'Testing ' + _mu
 
135
#                  , 'filename':'file_' + _omega
 
136
#                  , 'directory':_epsilon + '_dir',
 
137
#            'encoding': 'cp437'}),
105
138
    ]
106
 
 
107
 
    def adapt(self, test):
108
 
        result = TestSuite()
109
 
        for encoding, count, info in self._encodings:
110
 
            new_test = deepcopy(test)
111
 
            new_test.encoding = encoding
112
 
            new_test.info = info
113
 
            def make_new_test_id():
114
 
                if count:
115
 
                    new_id = "%s(%s,%s)" % (new_test.id(), encoding, count)
116
 
                else:
117
 
                    new_id = "%s(%s)" % (new_test.id(), encoding)
118
 
                return lambda: new_id
119
 
            new_test.id = make_new_test_id()
120
 
            result.addTest(new_test)
121
 
        return result
122
 
 
123