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
class TestSplitter(tests.TestCase):
24
def assertAsTokens(self, expected, line, single_quotes_allowed=False):
25
s = cmdline.Splitter(line, single_quotes_allowed=single_quotes_allowed)
26
self.assertEqual(expected, list(s))
28
def test_simple(self):
29
self.assertAsTokens([(False, u'foo'), (False, u'bar'), (False, u'baz')],
32
def test_ignore_multiple_spaces(self):
33
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar')
35
def test_ignore_leading_space(self):
36
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u' foo bar')
38
def test_ignore_trailing_space(self):
39
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar ')
41
def test_posix_quotations(self):
42
self.assertAsTokens([(True, u'foo bar')], u"'foo bar'",
43
single_quotes_allowed=True)
44
self.assertAsTokens([(True, u'foo bar')], u"'fo''o b''ar'",
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)
51
def test_nested_quotations(self):
52
self.assertAsTokens([(True, u'foo"" bar')], u"\"foo\\\"\\\" bar\"")
53
self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"")
54
self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"",
55
single_quotes_allowed=True)
56
self.assertAsTokens([(True, u'foo"" bar')], u"'foo\"\" bar'",
57
single_quotes_allowed=True)
59
def test_empty_result(self):
60
self.assertAsTokens([], u'')
61
self.assertAsTokens([], u' ')
63
def test_quoted_empty(self):
64
self.assertAsTokens([(True, '')], u'""')
65
self.assertAsTokens([(False, u"''")], u"''")
66
self.assertAsTokens([(True, '')], u"''", single_quotes_allowed=True)
68
def test_unicode_chars(self):
69
self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
70
u'f\xb5\xee \u1234\u3456')
72
def test_newline_in_quoted_section(self):
73
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
74
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u"'foo\nbar\nbaz\n'",
75
single_quotes_allowed=True)
77
def test_escape_chars(self):
78
self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
80
def test_escape_quote(self):
81
self.assertAsTokens([(True, u'foo"bar')], u'"foo\\"bar"')
82
self.assertAsTokens([(True, u'foo\\"bar')], u'"foo\\\\\\"bar"')
83
self.assertAsTokens([(True, u'foo\\bar')], u'"foo\\\\"bar"')
85
def test_double_escape(self):
86
self.assertAsTokens([(True, u'foo\\\\bar')], u'"foo\\\\bar"')
87
self.assertAsTokens([(False, u'foo\\\\bar')], u"foo\\\\bar")
89
def test_multiple_quoted_args(self):
90
self.assertAsTokens([(True, u'x x'), (True, u'y y')],
92
self.assertAsTokens([(True, u'x x'), (True, u'y y')],
93
u'"x x" \'y y\'', single_quotes_allowed=True)