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)
69
self.assertAsTokens([(False, u'a'), (True, u''), (False, u'c')],
71
self.assertAsTokens([(False, u'a'), (True, u''), (False, u'c')],
72
u"a '' c", single_quotes_allowed=True)
74
def test_unicode_chars(self):
75
self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
76
u'f\xb5\xee \u1234\u3456')
78
def test_newline_in_quoted_section(self):
79
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
80
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u"'foo\nbar\nbaz\n'",
81
single_quotes_allowed=True)
83
def test_escape_chars(self):
84
self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
86
def test_escape_quote(self):
87
self.assertAsTokens([(True, u'foo"bar')], u'"foo\\"bar"')
88
self.assertAsTokens([(True, u'foo\\"bar')], u'"foo\\\\\\"bar"')
89
self.assertAsTokens([(True, u'foo\\bar')], u'"foo\\\\"bar"')
91
def test_double_escape(self):
92
self.assertAsTokens([(True, u'foo\\\\bar')], u'"foo\\\\bar"')
93
self.assertAsTokens([(False, u'foo\\\\bar')], u"foo\\\\bar")
95
def test_multiple_quoted_args(self):
96
self.assertAsTokens([(True, u'x x'), (True, u'y y')],
98
self.assertAsTokens([(True, u'x x'), (True, u'y y')],
99
u'"x x" \'y y\'', single_quotes_allowed=True)
101
def test_n_backslashes_handling(self):
102
# https://bugs.launchpad.net/bzr/+bug/528944
103
# actually we care about the doubled backslashes when they're
104
# represents UNC paths.
105
# But in fact there is too much weird corner cases
106
# (see https://bugs.launchpad.net/tortoisebzr/+bug/569050)
107
# so to reproduce every bit of windows command-line handling
108
# could be not worth of efforts?
109
self.requireFeature(backslashdir_feature)
110
self.assertAsTokens([(True, r'\\host\path')], r'"\\host\path"')
111
self.assertAsTokens([(False, r'\\host\path')], r'\\host\path')
112
# handling of " after the 2n and 2n+1 backslashes
113
# inside and outside the quoted string
114
self.assertAsTokens([(True, r'\\'), (False, r'*.py')], r'"\\\\" *.py')
115
self.assertAsTokens([(True, r'\\" *.py')], r'"\\\\\" *.py"')
116
self.assertAsTokens([(True, r'\\ *.py')], r'\\\\" *.py"')
117
self.assertAsTokens([(False, r'\\"'), (False, r'*.py')],
119
self.assertAsTokens([(True, u'\\\\')], u'"\\\\')