1 # -*- coding: iso-8859-1 -*-
5 from test
import test_support
8 from cStringIO
import StringIO
10 from StringIO
import StringIO
13 # The original test data set was from shellwords, by Hartmut Goebel.
19 foo bar bla fasel|foo|bar|bla|fasel|
20 x y z xxxx|x|y|z|xxxx|
24 foo \x bar|foo|\|x|bar|
25 foo \ x bar|foo|\|x|bar|
27 foo "bar" bla|foo|"bar"|bla|
28 "foo" "bar" "bla"|"foo"|"bar"|"bla"|
29 "foo" bar "bla"|"foo"|bar|"bla"|
30 "foo" bar bla|"foo"|bar|bla|
31 foo 'bar' bla|foo|'bar'|bla|
32 'foo' 'bar' 'bla'|'foo'|'bar'|'bla'|
33 'foo' bar 'bla'|'foo'|bar|'bla'|
34 'foo' bar bla|'foo'|bar|bla|
35 blurb foo"bar"bar"fasel" baz|blurb|foo"bar"bar"fasel"|baz|
36 blurb foo'bar'bar'fasel' baz|blurb|foo'bar'bar'fasel'|baz|
39 foo "" bar|foo|""|bar|
40 foo '' bar|foo|''|bar|
41 foo "" "" "" bar|foo|""|""|""|bar|
42 foo '' '' '' bar|foo|''|''|''|bar|
45 "foo\ bar
"|"foo\ bar
"|
46 "foo
\\ bar
"|"foo
\\ bar
"|
47 "foo
\\ bar
\"|
"foo\\ bar\"|
48 "foo
\\" bar\""|
"foo\\"|bar|\|
""|
49 "foo\\ bar\" dfadf"|
"foo\\ bar\"|dfadf"|
50 "foo\\\ bar\" dfadf"|
"foo\\\ bar\"|dfadf"|
51 "foo\\\x bar\" dfadf"|
"foo\\\x bar\"|dfadf"|
52 "foo\x bar\" dfadf"|
"foo\x bar\"|dfadf"|
54 'foo\ bar
'|'foo\ bar
'|
55 'foo
\\ bar
'|'foo
\\ bar
'|
56 "foo\\\x bar\" df'a\
'df'|
"foo\\\x bar\"|df'a|\|'df'|
63 foo\ x\x\""|foo|\|x|\|x|\|
""|
64 "foo\ x\x"|
"foo\ x\x"|
65 "foo\ x\x\\"|
"foo\ x\x\\"|
66 "foo\ x\x\\""foobar"|
"foo\ x\x\\"|
"foobar"|
67 "foo\ x\x\\"\''"foobar"|"foo\ x\x\\"|\|''|"foobar"|
68 "foo\ x\x\\"\'"fo'obar
"|"foo\ x\x
\\"|\|'"fo
'|obar"|
69 "foo\ x\x\\"\'"fo'obar
" 'don'\''t'|"foo\ x\x
\\"|\|'"fo
'|obar"|'don
'|\|''|t'|
70 'foo\ bar'|
'foo\ bar'|
71 'foo\\ bar'|
'foo\\ bar'|
82 foo bar bla fasel|foo|bar|bla|fasel|
83 x y z xxxx|x|y|z|xxxx|
88 foo \ x bar|foo| x|bar|
90 foo
"bar" bla|foo|bar|bla|
91 "foo" "bar" "bla"|foo|bar|bla|
92 "foo" bar
"bla"|foo|bar|bla|
93 "foo" bar bla|foo|bar|bla|
94 foo
'bar' bla|foo|bar|bla|
95 'foo' 'bar' 'bla'|foo|bar|bla|
96 'foo' bar
'bla'|foo|bar|bla|
97 'foo' bar bla|foo|bar|bla|
98 blurb foo
"bar"bar
"fasel" baz|blurb|foobarbarfasel|baz|
99 blurb foo
'bar'bar
'fasel' baz|blurb|foobarbarfasel|baz|
104 foo
"" "" "" bar|foo||||bar|
105 foo
'' '' '' bar|foo||||bar|
109 "foo\\ bar"|foo\ bar|
110 "foo\\ bar\""|foo\ bar
"|
111 "foo
\\" bar\"|foo\|bar"|
112 "foo\\ bar\" dfadf"|foo\ bar
" dfadf|
113 "foo
\\\ bar
\" dfadf
"|foo\\ bar" dfadf|
114 "foo\\\x bar\" dfadf"|foo
\\x bar
" dfadf|
115 "foo\x bar
\" dfadf
"|foo\x bar" dfadf|
118 'foo
\\ bar
'|foo\\ bar|
119 "foo\\\x bar\" df'a\
'df"|foo\\x bar" df'a\
'df|
128 "foo\ x\x\\"|foo\ x\x\|
129 "foo\ x\x\\""foobar"|foo\ x\x\foobar|
130 "foo\ x\x\\"\'"foobar"|foo\ x\x\'foobar|
131 "foo\ x\x\\"\'"fo'obar
"|foo\ x\x\'fo'obar|
132 "foo\ x\x
\\"\'"fo
'obar" 'don
'\''t
'|foo\ x\x\'fo'obar|don
't|
133 "foo\ x\x\\"\'"fo'obar
" 'don'\''t' \\|foo\ x\x\'fo'obar|don't|\|
135 'foo\\ bar'|foo\\ bar|
137 foo#bar\nbaz|foo|baz|
142 class ShlexTest(unittest.TestCase):
144 self.data = [x.split("|
")[:-1]
145 for x in data.splitlines()]
146 self.posix_data = [x.split("|
")[:-1]
147 for x in posix_data.splitlines()]
148 for item in self.data:
149 item[0] = item[0].replace(r"\n", "\n")
150 for item in self.posix_data:
151 item[0] = item[0].replace(r"\n", "\n")
153 def splitTest(self, data, comments):
154 for i in range(len(data)):
155 l = shlex.split(data[i][0], comments=comments)
156 self.assertEqual(l, data[i][1:],
158 (data[i][0], l, data[i][1:]))
160 def oldSplit(self, s):
162 lex = shlex.shlex(StringIO(s))
163 tok = lex.get_token()
166 tok = lex.get_token()
169 def testSplitPosix(self):
170 """Test data splitting with posix parser"""
171 self.splitTest(self.posix_data, comments=True)
173 def testCompat(self):
174 """Test compatibility interface"""
175 for i in range(len(self.data)):
176 l = self.oldSplit(self.data[i][0])
177 self.assertEqual(l, self.data[i][1:],
179 (self.data[i][0], l, self.data[i][1:]))
181 # Allow this test to be used with old shlex.py
182 if not getattr(shlex, "split
", None):
183 for methname in dir(ShlexTest):
184 if methname.startswith("test
") and methname != "testCompat
":
185 delattr(ShlexTest, methname)
188 test_support.run_unittest(ShlexTest)
190 if __name__ == "__main__
":