~bzr-pqm/bzr/bzr.dev

1185.85.24 by John Arbash Meinel
Moved run_bzr_decode into TestCase
1
# Copyright (C) 2005 by Canonical Ltd
2
# -*- coding: utf-8 -*-
3
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.
8
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.
13
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
17
18
"""\
19
Tests for the Command.encoding_type interface.
20
"""
21
22
from bzrlib.tests import TestCase
23
from bzrlib.commands import Command, register_command, plugin_cmds
24
25
class cmd_echo_exact(Command):
26
    """This command just repeats what it is given.
27
28
    It decodes the argument, and then writes it to stdout.
29
    """
30
31
    takes_args = ['text']
32
    encoding_type = 'exact'
33
34
    def run(self, text=None):
35
        self.outf.write(text)
36
37
38
class cmd_echo_strict(cmd_echo_exact):
39
    """Replace bogus unicode characters."""
40
41
    encoding_type = 'strict'
42
43
44
class cmd_echo_replace(cmd_echo_exact):
45
    """Replace bogus unicode characters."""
46
47
    encoding_type = 'replace'
48
49
50
class TestCommandEncoding(TestCase):
51
    
52
    def test_exact(self):
53
        def bzr(*args, **kwargs):
54
            return self.run_bzr(*args, **kwargs)[0]
55
56
        register_command(cmd_echo_exact)
57
        try:
58
            self.assertEqual('foo', bzr('echo-exact', 'foo'))
59
            # This is cheating a little bit, because 'foo\xb5' shouldn't
60
            # get past main()
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)
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:
74
            self.assertEqual('foo', bzr('echo-strict', 'foo'))
75
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
76
                bzr('echo-strict', u'foo\xb5'))
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:
87
            self.assertEqual('foo', bzr('echo-strict', 'foo'))
88
            # ascii can't encode \xb5
89
            bzr('echo-strict', u'foo\xb5', retcode=3)
90
        finally:
91
            plugin_cmds.pop('echo-strict')
92
93
    def test_replace_utf8(self):
94
        def bzr(*args, **kwargs):
95
            kwargs['encoding'] = 'utf-8'
96
            return self.run_bzr(*args, **kwargs)[0]
97
98
        register_command(cmd_echo_replace)
99
        try:
100
            self.assertEqual('foo', bzr('echo-replace', 'foo'))
101
            self.assertEqual(u'foo\xb5'.encode('utf-8'),
102
                             bzr('echo-replace', u'foo\xb5'))
103
        finally:
104
            plugin_cmds.pop('echo-replace')
105
106
    def test_replace_ascii(self):
107
        def bzr(*args, **kwargs):
108
            kwargs['encoding'] = 'ascii'
109
            return self.run_bzr(*args, **kwargs)[0]
110
111
        register_command(cmd_echo_replace)
112
        try:
113
            self.assertEqual('foo', bzr('echo-replace', 'foo'))
114
            # ascii can't encode \xb5
115
            self.assertEqual('foo?', bzr('echo-replace', u'foo\xb5'))
116
        finally:
117
            plugin_cmds.pop('echo-replace')
118
119