~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_command_encoding.py

Merge with prepare-shelf

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for the Command.encoding_type interface."""
 
18
 
 
19
from bzrlib.tests import TestCase
 
20
from bzrlib.commands import Command, register_command, plugin_cmds
 
21
 
 
22
 
 
23
class cmd_echo_exact(Command):
 
24
    """This command just repeats what it is given.
 
25
 
 
26
    It decodes the argument, and then writes it to stdout.
 
27
    """
 
28
 
 
29
    takes_args = ['text']
 
30
    encoding_type = 'exact'
 
31
 
 
32
    def run(self, text=None):
 
33
        self.outf.write(text)
 
34
 
 
35
 
 
36
class cmd_echo_strict(cmd_echo_exact):
 
37
    """Raise a UnicodeError for unrepresentable characters."""
 
38
 
 
39
    encoding_type = 'strict'
 
40
 
 
41
 
 
42
class cmd_echo_replace(cmd_echo_exact):
 
43
    """Replace bogus unicode characters."""
 
44
 
 
45
    encoding_type = 'replace'
 
46
 
 
47
 
 
48
class TestCommandEncoding(TestCase):
 
49
    
 
50
    def test_exact(self):
 
51
        def bzr(*args, **kwargs):
 
52
            return self.run_bzr(*args, **kwargs)[0]
 
53
 
 
54
        register_command(cmd_echo_exact)
 
55
        try:
 
56
            self.assertEqual('foo', bzr('echo-exact foo'))
 
57
            # This is cheating a little bit, because 'foo\xb5' shouldn't
 
58
            # get past main()
 
59
            self.assertEqual('foo\xb5', bzr('echo-exact foo\xb5'))
 
60
            # Exact should fail to decode the string
 
61
            self.assertRaises(UnicodeEncodeError,
 
62
                bzr,
 
63
                ['echo-exact', u'foo\xb5'])
 
64
        finally:
 
65
            plugin_cmds.pop('echo-exact')
 
66
 
 
67
    def test_strict_utf8(self):
 
68
        def bzr(*args, **kwargs):
 
69
            kwargs['encoding'] = 'utf-8'
 
70
            return self.run_bzr(*args, **kwargs)[0]
 
71
 
 
72
        register_command(cmd_echo_strict)
 
73
        try:
 
74
            self.assertEqual('foo', bzr('echo-strict foo'))
 
75
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
 
76
                             bzr(['echo-strict', u'foo\xb5']))
 
77
        finally:
 
78
            plugin_cmds.pop('echo-strict')
 
79
 
 
80
    def test_strict_ascii(self):
 
81
        def bzr(*args, **kwargs):
 
82
            kwargs['encoding'] = 'ascii'
 
83
            return self.run_bzr(*args, **kwargs)[0]
 
84
 
 
85
        register_command(cmd_echo_strict)
 
86
        try:
 
87
            self.assertEqual('foo', bzr('echo-strict foo'))
 
88
            # ascii can't encode \xb5
 
89
            self.assertRaises(UnicodeEncodeError,
 
90
                bzr,
 
91
                ['echo-strict', u'foo\xb5'])
 
92
        finally:
 
93
            plugin_cmds.pop('echo-strict')
 
94
 
 
95
    def test_replace_utf8(self):
 
96
        def bzr(*args, **kwargs):
 
97
            kwargs['encoding'] = 'utf-8'
 
98
            return self.run_bzr(*args, **kwargs)[0]
 
99
 
 
100
        register_command(cmd_echo_replace)
 
101
        try:
 
102
            self.assertEqual('foo', bzr('echo-replace foo'))
 
103
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
 
104
                             bzr(['echo-replace', u'foo\xb5']))
 
105
        finally:
 
106
            plugin_cmds.pop('echo-replace')
 
107
 
 
108
    def test_replace_ascii(self):
 
109
        def bzr(*args, **kwargs):
 
110
            kwargs['encoding'] = 'ascii'
 
111
            return self.run_bzr(*args, **kwargs)[0]
 
112
 
 
113
        register_command(cmd_echo_replace)
 
114
        try:
 
115
            self.assertEqual('foo', bzr('echo-replace foo'))
 
116
            # ascii can't encode \xb5
 
117
            self.assertEqual('foo?', bzr(['echo-replace', u'foo\xb5']))
 
118
        finally:
 
119
            plugin_cmds.pop('echo-replace')
 
120
 
 
121