Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 1 | import io |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 2 | import shlex |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 3 | import string |
| 4 | import unittest |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 5 | |
Neal Norwitz | aa1ac54 | 2003-04-17 23:04:22 +0000 | [diff] [blame] | 6 | |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 7 | |
| 8 | # The original test data set was from shellwords, by Hartmut Goebel. |
| 9 | |
| 10 | data = r"""x|x| |
| 11 | foo bar|foo|bar| |
| 12 | foo bar|foo|bar| |
| 13 | foo bar |foo|bar| |
| 14 | foo bar bla fasel|foo|bar|bla|fasel| |
| 15 | x y z xxxx|x|y|z|xxxx| |
| 16 | \x bar|\|x|bar| |
| 17 | \ x bar|\|x|bar| |
| 18 | \ bar|\|bar| |
| 19 | foo \x bar|foo|\|x|bar| |
| 20 | foo \ x bar|foo|\|x|bar| |
| 21 | foo \ bar|foo|\|bar| |
| 22 | foo "bar" bla|foo|"bar"|bla| |
| 23 | "foo" "bar" "bla"|"foo"|"bar"|"bla"| |
| 24 | "foo" bar "bla"|"foo"|bar|"bla"| |
| 25 | "foo" bar bla|"foo"|bar|bla| |
| 26 | foo 'bar' bla|foo|'bar'|bla| |
| 27 | 'foo' 'bar' 'bla'|'foo'|'bar'|'bla'| |
| 28 | 'foo' bar 'bla'|'foo'|bar|'bla'| |
| 29 | 'foo' bar bla|'foo'|bar|bla| |
| 30 | blurb foo"bar"bar"fasel" baz|blurb|foo"bar"bar"fasel"|baz| |
| 31 | blurb foo'bar'bar'fasel' baz|blurb|foo'bar'bar'fasel'|baz| |
| 32 | ""|""| |
| 33 | ''|''| |
| 34 | foo "" bar|foo|""|bar| |
| 35 | foo '' bar|foo|''|bar| |
| 36 | foo "" "" "" bar|foo|""|""|""|bar| |
| 37 | foo '' '' '' bar|foo|''|''|''|bar| |
| 38 | \""|\|""| |
| 39 | "\"|"\"| |
| 40 | "foo\ bar"|"foo\ bar"| |
| 41 | "foo\\ bar"|"foo\\ bar"| |
| 42 | "foo\\ bar\"|"foo\\ bar\"| |
| 43 | "foo\\" bar\""|"foo\\"|bar|\|""| |
| 44 | "foo\\ bar\" dfadf"|"foo\\ bar\"|dfadf"| |
| 45 | "foo\\\ bar\" dfadf"|"foo\\\ bar\"|dfadf"| |
| 46 | "foo\\\x bar\" dfadf"|"foo\\\x bar\"|dfadf"| |
| 47 | "foo\x bar\" dfadf"|"foo\x bar\"|dfadf"| |
| 48 | \''|\|''| |
| 49 | 'foo\ bar'|'foo\ bar'| |
| 50 | 'foo\\ bar'|'foo\\ bar'| |
| 51 | "foo\\\x bar\" df'a\ 'df'|"foo\\\x bar\"|df'a|\|'df'| |
| 52 | \"foo"|\|"foo"| |
| 53 | \"foo"\x|\|"foo"|\|x| |
| 54 | "foo\x"|"foo\x"| |
| 55 | "foo\ "|"foo\ "| |
| 56 | foo\ xx|foo|\|xx| |
| 57 | foo\ x\x|foo|\|x|\|x| |
| 58 | foo\ x\x\""|foo|\|x|\|x|\|""| |
| 59 | "foo\ x\x"|"foo\ x\x"| |
| 60 | "foo\ x\x\\"|"foo\ x\x\\"| |
| 61 | "foo\ x\x\\""foobar"|"foo\ x\x\\"|"foobar"| |
| 62 | "foo\ x\x\\"\''"foobar"|"foo\ x\x\\"|\|''|"foobar"| |
| 63 | "foo\ x\x\\"\'"fo'obar"|"foo\ x\x\\"|\|'"fo'|obar"| |
| 64 | "foo\ x\x\\"\'"fo'obar" 'don'\''t'|"foo\ x\x\\"|\|'"fo'|obar"|'don'|\|''|t'| |
| 65 | 'foo\ bar'|'foo\ bar'| |
| 66 | 'foo\\ bar'|'foo\\ bar'| |
| 67 | foo\ bar|foo|\|bar| |
| 68 | foo#bar\nbaz|foobaz| |
| 69 | :-) ;-)|:|-|)|;|-|)| |
Antoine Pitrou | d72402e | 2010-10-27 18:52:48 +0000 | [diff] [blame] | 70 | áéíóú|á|é|í|ó|ú| |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 71 | """ |
| 72 | |
| 73 | posix_data = r"""x|x| |
| 74 | foo bar|foo|bar| |
| 75 | foo bar|foo|bar| |
| 76 | foo bar |foo|bar| |
| 77 | foo bar bla fasel|foo|bar|bla|fasel| |
| 78 | x y z xxxx|x|y|z|xxxx| |
| 79 | \x bar|x|bar| |
| 80 | \ x bar| x|bar| |
| 81 | \ bar| bar| |
| 82 | foo \x bar|foo|x|bar| |
| 83 | foo \ x bar|foo| x|bar| |
| 84 | foo \ bar|foo| bar| |
| 85 | foo "bar" bla|foo|bar|bla| |
| 86 | "foo" "bar" "bla"|foo|bar|bla| |
| 87 | "foo" bar "bla"|foo|bar|bla| |
| 88 | "foo" bar bla|foo|bar|bla| |
| 89 | foo 'bar' bla|foo|bar|bla| |
| 90 | 'foo' 'bar' 'bla'|foo|bar|bla| |
| 91 | 'foo' bar 'bla'|foo|bar|bla| |
| 92 | 'foo' bar bla|foo|bar|bla| |
| 93 | blurb foo"bar"bar"fasel" baz|blurb|foobarbarfasel|baz| |
| 94 | blurb foo'bar'bar'fasel' baz|blurb|foobarbarfasel|baz| |
| 95 | ""|| |
| 96 | ''|| |
| 97 | foo "" bar|foo||bar| |
| 98 | foo '' bar|foo||bar| |
| 99 | foo "" "" "" bar|foo||||bar| |
| 100 | foo '' '' '' bar|foo||||bar| |
| 101 | \"|"| |
| 102 | "\""|"| |
| 103 | "foo\ bar"|foo\ bar| |
| 104 | "foo\\ bar"|foo\ bar| |
| 105 | "foo\\ bar\""|foo\ bar"| |
| 106 | "foo\\" bar\"|foo\|bar"| |
| 107 | "foo\\ bar\" dfadf"|foo\ bar" dfadf| |
| 108 | "foo\\\ bar\" dfadf"|foo\\ bar" dfadf| |
| 109 | "foo\\\x bar\" dfadf"|foo\\x bar" dfadf| |
| 110 | "foo\x bar\" dfadf"|foo\x bar" dfadf| |
| 111 | \'|'| |
| 112 | 'foo\ bar'|foo\ bar| |
| 113 | 'foo\\ bar'|foo\\ bar| |
| 114 | "foo\\\x bar\" df'a\ 'df"|foo\\x bar" df'a\ 'df| |
| 115 | \"foo|"foo| |
| 116 | \"foo\x|"foox| |
| 117 | "foo\x"|foo\x| |
| 118 | "foo\ "|foo\ | |
| 119 | foo\ xx|foo xx| |
| 120 | foo\ x\x|foo xx| |
| 121 | foo\ x\x\"|foo xx"| |
| 122 | "foo\ x\x"|foo\ x\x| |
| 123 | "foo\ x\x\\"|foo\ x\x\| |
| 124 | "foo\ x\x\\""foobar"|foo\ x\x\foobar| |
| 125 | "foo\ x\x\\"\'"foobar"|foo\ x\x\'foobar| |
| 126 | "foo\ x\x\\"\'"fo'obar"|foo\ x\x\'fo'obar| |
| 127 | "foo\ x\x\\"\'"fo'obar" 'don'\''t'|foo\ x\x\'fo'obar|don't| |
| 128 | "foo\ x\x\\"\'"fo'obar" 'don'\''t' \\|foo\ x\x\'fo'obar|don't|\| |
| 129 | 'foo\ bar'|foo\ bar| |
| 130 | 'foo\\ bar'|foo\\ bar| |
| 131 | foo\ bar|foo bar| |
| 132 | foo#bar\nbaz|foo|baz| |
| 133 | :-) ;-)|:-)|;-)| |
Antoine Pitrou | d72402e | 2010-10-27 18:52:48 +0000 | [diff] [blame] | 134 | áéíóú|áéíóú| |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 135 | """ |
| 136 | |
| 137 | class ShlexTest(unittest.TestCase): |
| 138 | def setUp(self): |
| 139 | self.data = [x.split("|")[:-1] |
| 140 | for x in data.splitlines()] |
| 141 | self.posix_data = [x.split("|")[:-1] |
| 142 | for x in posix_data.splitlines()] |
| 143 | for item in self.data: |
| 144 | item[0] = item[0].replace(r"\n", "\n") |
| 145 | for item in self.posix_data: |
| 146 | item[0] = item[0].replace(r"\n", "\n") |
| 147 | |
Gustavo Niemeyer | 48f3dcc | 2003-04-20 01:57:03 +0000 | [diff] [blame] | 148 | def splitTest(self, data, comments): |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 149 | for i in range(len(data)): |
Gustavo Niemeyer | 48f3dcc | 2003-04-20 01:57:03 +0000 | [diff] [blame] | 150 | l = shlex.split(data[i][0], comments=comments) |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 151 | self.assertEqual(l, data[i][1:], |
| 152 | "%s: %s != %s" % |
| 153 | (data[i][0], l, data[i][1:])) |
| 154 | |
| 155 | def oldSplit(self, s): |
| 156 | ret = [] |
Walter Dörwald | 2c849f2 | 2007-06-12 17:43:43 +0000 | [diff] [blame] | 157 | lex = shlex.shlex(io.StringIO(s)) |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 158 | tok = lex.get_token() |
| 159 | while tok: |
| 160 | ret.append(tok) |
| 161 | tok = lex.get_token() |
| 162 | return ret |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 163 | |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 164 | def testSplitPosix(self): |
| 165 | """Test data splitting with posix parser""" |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 166 | self.splitTest(self.posix_data, comments=True) |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 167 | |
| 168 | def testCompat(self): |
| 169 | """Test compatibility interface""" |
| 170 | for i in range(len(self.data)): |
| 171 | l = self.oldSplit(self.data[i][0]) |
| 172 | self.assertEqual(l, self.data[i][1:], |
| 173 | "%s: %s != %s" % |
| 174 | (self.data[i][0], l, self.data[i][1:])) |
| 175 | |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 176 | def testQuote(self): |
| 177 | safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./' |
Éric Araujo | 7fc0394 | 2011-08-09 23:18:06 +0200 | [diff] [blame] | 178 | unicode_sample = '\xe9\xe0\xdf' # e + acute accent, a + grave, sharp s |
| 179 | unsafe = '"`$\\!' + unicode_sample |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 180 | |
| 181 | self.assertEqual(shlex.quote(''), "''") |
| 182 | self.assertEqual(shlex.quote(safeunquoted), safeunquoted) |
| 183 | self.assertEqual(shlex.quote('test file name'), "'test file name'") |
| 184 | for u in unsafe: |
| 185 | self.assertEqual(shlex.quote('test%sname' % u), |
| 186 | "'test%sname'" % u) |
| 187 | for u in unsafe: |
| 188 | self.assertEqual(shlex.quote("test%s'name'" % u), |
| 189 | "'test%s'\"'\"'name'\"'\"''" % u) |
| 190 | |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 191 | # Allow this test to be used with old shlex.py |
| 192 | if not getattr(shlex, "split", None): |
| 193 | for methname in dir(ShlexTest): |
| 194 | if methname.startswith("test") and methname != "testCompat": |
| 195 | delattr(ShlexTest, methname) |
| 196 | |
| 197 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame^] | 198 | unittest.main() |