~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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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):
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:
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
            # This is cheating a little bit, because 'foo\xb5' shouldn't
58
            # get past main()
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
59
            self.assertEqual('foo\xb5', bzr('echo-exact foo\xb5'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
60
            # 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
61
            self.assertRaises(UnicodeEncodeError,
62
                bzr,
63
                ['echo-exact', u'foo\xb5'])
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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:
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
74
            self.assertEqual('foo', bzr('echo-strict foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
75
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
76
                             bzr(['echo-strict', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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:
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
87
            self.assertEqual('foo', bzr('echo-strict foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
88
            # 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
89
            self.assertRaises(UnicodeEncodeError,
90
                bzr,
91
                ['echo-strict', u'foo\xb5'])
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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:
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
102
            self.assertEqual('foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
103
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
104
                             bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
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:
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
115
            self.assertEqual('foo', bzr('echo-replace foo'))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
116
            # ascii can't encode \xb5
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
117
            self.assertEqual('foo?', bzr(['echo-replace', u'foo\xb5']))
1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
118
        finally:
119
            plugin_cmds.pop('echo-replace')
120
121