~bzr-pqm/bzr/bzr.dev

2830.2.3 by Martin Pool
Fix up test_command_encoding to expect to see the assertion raised on unicode errors
1
# Copyright (C) 2005, 2007 Canonical Ltd
1685.1.78 by Wouter van Heyst
more code cleanup
2
#
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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.
1685.1.78 by Wouter van Heyst
more code cleanup
7
#
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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.
1685.1.78 by Wouter van Heyst
more code cleanup
12
#
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
16
1685.1.76 by Wouter van Heyst
codecleanup
17
"""Tests for the Command.encoding_type interface."""
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
18
19
from bzrlib.tests import TestCase
20
from bzrlib.commands import Command, register_command, plugin_cmds
21
1685.1.78 by Wouter van Heyst
more code cleanup
22
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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):
2830.2.3 by Martin Pool
Fix up test_command_encoding to expect to see the assertion raised on unicode errors
37
    """Raise a UnicodeError for unrepresentable characters."""
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
49
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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:
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
56
            self.assertEqual('foo', bzr('echo-exact foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
57
            # Exact should fail to decode the string
2830.2.3 by Martin Pool
Fix up test_command_encoding to expect to see the assertion raised on unicode errors
58
            self.assertRaises(UnicodeEncodeError,
59
                bzr,
60
                ['echo-exact', u'foo\xb5'])
5785.1.4 by Martin
Remove testing that expected argv decoding to be done outside run_bzr
61
            # Previously a non-ascii bytestring was also tested, as 'exact'
62
            # outputs bytes untouched, but needed buggy argv parsing to work
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
63
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
64
            plugin_cmds.remove('echo-exact')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
65
66
    def test_strict_utf8(self):
67
        def bzr(*args, **kwargs):
68
            kwargs['encoding'] = 'utf-8'
69
            return self.run_bzr(*args, **kwargs)[0]
70
71
        register_command(cmd_echo_strict)
72
        try:
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
73
            self.assertEqual('foo', bzr('echo-strict foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
74
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
75
                             bzr(['echo-strict', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
76
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
77
            plugin_cmds.remove('echo-strict')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
78
79
    def test_strict_ascii(self):
80
        def bzr(*args, **kwargs):
81
            kwargs['encoding'] = 'ascii'
82
            return self.run_bzr(*args, **kwargs)[0]
83
84
        register_command(cmd_echo_strict)
85
        try:
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
86
            self.assertEqual('foo', bzr('echo-strict foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
87
            # ascii can't encode \xb5
2830.2.3 by Martin Pool
Fix up test_command_encoding to expect to see the assertion raised on unicode errors
88
            self.assertRaises(UnicodeEncodeError,
89
                bzr,
90
                ['echo-strict', u'foo\xb5'])
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
91
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
92
            plugin_cmds.remove('echo-strict')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
93
94
    def test_replace_utf8(self):
95
        def bzr(*args, **kwargs):
96
            kwargs['encoding'] = 'utf-8'
97
            return self.run_bzr(*args, **kwargs)[0]
98
99
        register_command(cmd_echo_replace)
100
        try:
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
101
            self.assertEqual('foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
102
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
103
                             bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
104
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
105
            plugin_cmds.remove('echo-replace')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
106
107
    def test_replace_ascii(self):
108
        def bzr(*args, **kwargs):
109
            kwargs['encoding'] = 'ascii'
110
            return self.run_bzr(*args, **kwargs)[0]
111
112
        register_command(cmd_echo_replace)
113
        try:
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
114
            self.assertEqual('foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
115
            # ascii can't encode \xb5
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
116
            self.assertEqual('foo?', bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
117
        finally:
3785.1.1 by Aaron Bentley
Switch from dict to Registry for plugin_cmds
118
            plugin_cmds.remove('echo-replace')
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
119
120