1
# Copyright (C) 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
from bzrlib.tests.features import backslashdir_feature
24
class TestSplitter(tests.TestCase):
26
def assertAsTokens(self, expected, line, single_quotes_allowed=False):
27
s = cmdline.Splitter(line, single_quotes_allowed=single_quotes_allowed)
28
self.assertEqual(expected, list(s))
30
def test_simple(self):
31
self.assertAsTokens([(False, u'foo'), (False, u'bar'), (False, u'baz')],
34
def test_ignore_multiple_spaces(self):
35
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar')
37
def test_ignore_leading_space(self):
38
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u' foo bar')
40
def test_ignore_trailing_space(self):
41
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar ')
43
def test_posix_quotations(self):
44
self.assertAsTokens([(True, u'foo bar')], u"'foo bar'",
45
single_quotes_allowed=True)
46
self.assertAsTokens([(True, u'foo bar')], u"'fo''o b''ar'",
47
single_quotes_allowed=True)
48
self.assertAsTokens([(True, u'foo bar')], u'"fo""o b""ar"',
49
single_quotes_allowed=True)
50
self.assertAsTokens([(True, u'foo bar')], u'"fo"\'o b\'"ar"',
51
single_quotes_allowed=True)
53
def test_nested_quotations(self):
54
self.assertAsTokens([(True, u'foo"" bar')], u"\"foo\\\"\\\" bar\"")
55
self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"")
56
self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"",
57
single_quotes_allowed=True)
58
self.assertAsTokens([(True, u'foo"" bar')], u"'foo\"\" bar'",
59
single_quotes_allowed=True)
61
def test_empty_result(self):
62
self.assertAsTokens([], u'')
63
self.assertAsTokens([], u' ')
65
def test_quoted_empty(self):
66
self.assertAsTokens([(True, '')], u'""')
67
self.assertAsTokens([(False, u"''")], u"''")
68
self.assertAsTokens([(True, '')], u"''", single_quotes_allowed=True)
70
def test_unicode_chars(self):
71
self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
72
u'f\xb5\xee \u1234\u3456')
74
def test_newline_in_quoted_section(self):
75
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
76
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u"'foo\nbar\nbaz\n'",
77
single_quotes_allowed=True)
79
def test_escape_chars(self):
80
self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
82
def test_escape_quote(self):
83
self.assertAsTokens([(True, u'foo"bar')], u'"foo\\"bar"')
84
self.assertAsTokens([(True, u'foo\\"bar')], u'"foo\\\\\\"bar"')
85
self.assertAsTokens([(True, u'foo\\bar')], u'"foo\\\\"bar"')
87
def test_double_escape(self):
88
self.assertAsTokens([(True, u'foo\\\\bar')], u'"foo\\\\bar"')
89
self.assertAsTokens([(False, u'foo\\\\bar')], u"foo\\\\bar")
91
def test_multiple_quoted_args(self):
92
self.assertAsTokens([(True, u'x x'), (True, u'y y')],
94
self.assertAsTokens([(True, u'x x'), (True, u'y y')],
95
u'"x x" \'y y\'', single_quotes_allowed=True)
97
def test_n_backslashes_handling(self):
98
# https://bugs.launchpad.net/bzr/+bug/528944
99
# actually we care about the doubled backslashes when they're
100
# represents UNC paths.
101
# But in fact there is too much weird corner cases
102
# (see https://bugs.launchpad.net/tortoisebzr/+bug/569050)
103
# so to reproduce every bit of windows command-line handling
104
# could be not worth of efforts?
105
self.requireFeature(backslashdir_feature)
106
self.assertAsTokens([(True, r'\\host\path')], r'"\\host\path"')
107
self.assertAsTokens([(False, r'\\host\path')], r'\\host\path')
108
# handling of " after the 2n and 2n+1 backslashes
109
# inside and outside the quoted string
110
self.assertAsTokens([(True, r'\\'), (False, r'*.py')], r'"\\\\" *.py')
111
self.assertAsTokens([(True, r'\\" *.py')], r'"\\\\\" *.py"')
112
self.assertAsTokens([(True, r'\\ *.py')], r'\\\\" *.py"')
113
self.assertAsTokens([(False, r'\\"'), (False, r'*.py')],
115
self.assertAsTokens([(True, u'\\\\')], u'"\\\\')