~bzr-pqm/bzr/bzr.dev

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