1
# Copyright (C) 2005 by Canonical Ltd
2
# -*- coding: utf-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
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
19
Tests for the Command.encoding_type interface.
22
from bzrlib.tests import TestCase
23
from bzrlib.commands import Command, register_command, plugin_cmds
25
class cmd_echo_exact(Command):
26
"""This command just repeats what it is given.
28
It decodes the argument, and then writes it to stdout.
32
encoding_type = 'exact'
34
def run(self, text=None):
38
class cmd_echo_strict(cmd_echo_exact):
39
"""Replace bogus unicode characters."""
41
encoding_type = 'strict'
44
class cmd_echo_replace(cmd_echo_exact):
45
"""Replace bogus unicode characters."""
47
encoding_type = 'replace'
50
class TestCommandEncoding(TestCase):
53
def bzr(*args, **kwargs):
54
return self.run_bzr(*args, **kwargs)[0]
56
register_command(cmd_echo_exact)
58
self.assertEqual('foo', bzr('echo-exact', 'foo'))
59
# This is cheating a little bit, because 'foo\xb5' shouldn't
61
self.assertEqual('foo\xb5', bzr('echo-exact', 'foo\xb5'))
62
# Exact should fail to decode the string
63
bzr('echo-exact', u'foo\xb5', retcode=3)
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
bzr('echo-strict', u'foo\xb5', retcode=3)
91
plugin_cmds.pop('echo-strict')
93
def test_replace_utf8(self):
94
def bzr(*args, **kwargs):
95
kwargs['encoding'] = 'utf-8'
96
return self.run_bzr(*args, **kwargs)[0]
98
register_command(cmd_echo_replace)
100
self.assertEqual('foo', bzr('echo-replace', 'foo'))
101
self.assertEqual(u'foo\xb5'.encode('utf-8'),
102
bzr('echo-replace', u'foo\xb5'))
104
plugin_cmds.pop('echo-replace')
106
def test_replace_ascii(self):
107
def bzr(*args, **kwargs):
108
kwargs['encoding'] = 'ascii'
109
return self.run_bzr(*args, **kwargs)[0]
111
register_command(cmd_echo_replace)
113
self.assertEqual('foo', bzr('echo-replace', 'foo'))
114
# ascii can't encode \xb5
115
self.assertEqual('foo?', bzr('echo-replace', u'foo\xb5'))
117
plugin_cmds.pop('echo-replace')