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
18
"""Tests for the Command.encoding_type interface."""
20
from bzrlib.tests import TestCase
21
from bzrlib.commands import Command, register_command, plugin_cmds
24
class cmd_echo_exact(Command):
25
"""This command just repeats what it is given.
27
It decodes the argument, and then writes it to stdout.
31
encoding_type = 'exact'
33
def run(self, text=None):
37
class cmd_echo_strict(cmd_echo_exact):
38
"""Replace bogus unicode characters."""
40
encoding_type = 'strict'
43
class cmd_echo_replace(cmd_echo_exact):
44
"""Replace bogus unicode characters."""
46
encoding_type = 'replace'
49
class TestCommandEncoding(TestCase):
52
def bzr(*args, **kwargs):
53
return self.run_bzr(*args, **kwargs)[0]
55
register_command(cmd_echo_exact)
57
self.assertEqual('foo', bzr('echo-exact', 'foo'))
58
# This is cheating a little bit, because 'foo\xb5' shouldn't
60
self.assertEqual('foo\xb5', bzr('echo-exact', 'foo\xb5'))
61
# Exact should fail to decode the string
62
bzr('echo-exact', u'foo\xb5', retcode=3)
64
plugin_cmds.pop('echo-exact')
66
def test_strict_utf8(self):
67
def bzr(*args, **kwargs):
68
kwargs['encoding'] = 'utf-8'
69
return self.run_bzr(*args, **kwargs)[0]
71
register_command(cmd_echo_strict)
73
self.assertEqual('foo', bzr('echo-strict', 'foo'))
74
self.assertEqual(u'foo\xb5'.encode('utf-8'),
75
bzr('echo-strict', u'foo\xb5'))
77
plugin_cmds.pop('echo-strict')
79
def test_strict_ascii(self):
80
def bzr(*args, **kwargs):
81
kwargs['encoding'] = 'ascii'
82
return self.run_bzr(*args, **kwargs)[0]
84
register_command(cmd_echo_strict)
86
self.assertEqual('foo', bzr('echo-strict', 'foo'))
87
# ascii can't encode \xb5
88
bzr('echo-strict', u'foo\xb5', retcode=3)
90
plugin_cmds.pop('echo-strict')
92
def test_replace_utf8(self):
93
def bzr(*args, **kwargs):
94
kwargs['encoding'] = 'utf-8'
95
return self.run_bzr(*args, **kwargs)[0]
97
register_command(cmd_echo_replace)
99
self.assertEqual('foo', bzr('echo-replace', 'foo'))
100
self.assertEqual(u'foo\xb5'.encode('utf-8'),
101
bzr('echo-replace', u'foo\xb5'))
103
plugin_cmds.pop('echo-replace')
105
def test_replace_ascii(self):
106
def bzr(*args, **kwargs):
107
kwargs['encoding'] = 'ascii'
108
return self.run_bzr(*args, **kwargs)[0]
110
register_command(cmd_echo_replace)
112
self.assertEqual('foo', bzr('echo-replace', 'foo'))
113
# ascii can't encode \xb5
114
self.assertEqual('foo?', bzr('echo-replace', u'foo\xb5'))
116
plugin_cmds.pop('echo-replace')