1
# Copyright (C) 2005, 2007 Canonical Ltd
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.
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.
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
17
"""Tests for the Command.encoding_type interface."""
19
from bzrlib.tests import TestCase
20
from bzrlib.commands import Command, register_command, plugin_cmds
23
class cmd_echo_exact(Command):
24
"""This command just repeats what it is given.
26
It decodes the argument, and then writes it to stdout.
30
encoding_type = 'exact'
32
def run(self, text=None):
36
class cmd_echo_strict(cmd_echo_exact):
37
"""Raise a UnicodeError for unrepresentable characters."""
39
encoding_type = 'strict'
42
class cmd_echo_replace(cmd_echo_exact):
43
"""Replace bogus unicode characters."""
45
encoding_type = 'replace'
48
class TestCommandEncoding(TestCase):
51
def bzr(*args, **kwargs):
52
return self.run_bzr(*args, **kwargs)[0]
54
register_command(cmd_echo_exact)
56
self.assertEqual('foo', bzr('echo-exact foo'))
57
# This is cheating a little bit, because 'foo\xb5' shouldn't
59
self.assertEqual('foo\xb5', bzr('echo-exact foo\xb5'))
60
# Exact should fail to decode the string
61
self.assertRaises(UnicodeEncodeError,
63
['echo-exact', u'foo\xb5'])
65
plugin_cmds.pop('echo-exact')
67
def test_strict_utf8(self):
68
def bzr(*args, **kwargs):
69
kwargs['encoding'] = 'utf-8'
70
return self.run_bzr(*args, **kwargs)[0]
72
register_command(cmd_echo_strict)
74
self.assertEqual('foo', bzr('echo-strict foo'))
75
self.assertEqual(u'foo\xb5'.encode('utf-8'),
76
bzr(['echo-strict', u'foo\xb5']))
78
plugin_cmds.pop('echo-strict')
80
def test_strict_ascii(self):
81
def bzr(*args, **kwargs):
82
kwargs['encoding'] = 'ascii'
83
return self.run_bzr(*args, **kwargs)[0]
85
register_command(cmd_echo_strict)
87
self.assertEqual('foo', bzr('echo-strict foo'))
88
# ascii can't encode \xb5
89
self.assertRaises(UnicodeEncodeError,
91
['echo-strict', u'foo\xb5'])
93
plugin_cmds.pop('echo-strict')
95
def test_replace_utf8(self):
96
def bzr(*args, **kwargs):
97
kwargs['encoding'] = 'utf-8'
98
return self.run_bzr(*args, **kwargs)[0]
100
register_command(cmd_echo_replace)
102
self.assertEqual('foo', bzr('echo-replace foo'))
103
self.assertEqual(u'foo\xb5'.encode('utf-8'),
104
bzr(['echo-replace', u'foo\xb5']))
106
plugin_cmds.pop('echo-replace')
108
def test_replace_ascii(self):
109
def bzr(*args, **kwargs):
110
kwargs['encoding'] = 'ascii'
111
return self.run_bzr(*args, **kwargs)[0]
113
register_command(cmd_echo_replace)
115
self.assertEqual('foo', bzr('echo-replace foo'))
116
# ascii can't encode \xb5
117
self.assertEqual('foo?', bzr(['echo-replace', u'foo\xb5']))
119
plugin_cmds.pop('echo-replace')