0001 # -*- coding: iso-8859-1 -*- 0002 import unittest 0003 import os, sys 0004 import shlex 0005 0006 from test import test_support 0007 0008 try: 0009 from cStringIO import StringIO 0010 except ImportError: 0011 from StringIO import StringIO 0012 0013 0014 # The original test data set was from shellwords, by Hartmut Goebel. 0015 0016 data = r"""x|x| 0017 foo bar|foo|bar| 0018 foo bar|foo|bar| 0019 foo bar |foo|bar| 0020 foo bar bla fasel|foo|bar|bla|fasel| 0021 x y z xxxx|x|y|z|xxxx| 0022 \x bar|\|x|bar| 0023 \ x bar|\|x|bar| 0024 \ bar|\|bar| 0025 foo \x bar|foo|\|x|bar| 0026 foo \ x bar|foo|\|x|bar| 0027 foo \ bar|foo|\|bar| 0028 foo "bar" bla|foo|"bar"|bla| 0029 "foo" "bar" "bla"|"foo"|"bar"|"bla"| 0030 "foo" bar "bla"|"foo"|bar|"bla"| 0031 "foo" bar bla|"foo"|bar|bla| 0032 foo 'bar' bla|foo|'bar'|bla| 0033 'foo' 'bar' 'bla'|'foo'|'bar'|'bla'| 0034 'foo' bar 'bla'|'foo'|bar|'bla'| 0035 'foo' bar bla|'foo'|bar|bla| 0036 blurb foo"bar"bar"fasel" baz|blurb|foo"bar"bar"fasel"|baz| 0037 blurb foo'bar'bar'fasel' baz|blurb|foo'bar'bar'fasel'|baz| 0038 ""|""| 0039 ''|''| 0040 foo "" bar|foo|""|bar| 0041 foo '' bar|foo|''|bar| 0042 foo "" "" "" bar|foo|""|""|""|bar| 0043 foo '' '' '' bar|foo|''|''|''|bar| 0044 \""|\|""| 0045 "\"|"\"| 0046 "foo\ bar"|"foo\ bar"| 0047 "foo\\ bar"|"foo\\ bar"| 0048 "foo\\ bar\"|"foo\\ bar\"| 0049 "foo\\" bar\""|"foo\\"|bar|\|""| 0050 "foo\\ bar\" dfadf"|"foo\\ bar\"|dfadf"| 0051 "foo\\\ bar\" dfadf"|"foo\\\ bar\"|dfadf"| 0052 "foo\\\x bar\" dfadf"|"foo\\\x bar\"|dfadf"| 0053 "foo\x bar\" dfadf"|"foo\x bar\"|dfadf"| 0054 \''|\|''| 0055 'foo\ bar'|'foo\ bar'| 0056 'foo\\ bar'|'foo\\ bar'| 0057 "foo\\\x bar\" df'a\ 'df'|"foo\\\x bar\"|df'a|\|'df'| 0058 \"foo"|\|"foo"| 0059 \"foo"\x|\|"foo"|\|x| 0060 "foo\x"|"foo\x"| 0061 "foo\ "|"foo\ "| 0062 foo\ xx|foo|\|xx| 0063 foo\ x\x|foo|\|x|\|x| 0064 foo\ x\x\""|foo|\|x|\|x|\|""| 0065 "foo\ x\x"|"foo\ x\x"| 0066 "foo\ x\x\\"|"foo\ x\x\\"| 0067 "foo\ x\x\\""foobar"|"foo\ x\x\\"|"foobar"| 0068 "foo\ x\x\\"\''"foobar"|"foo\ x\x\\"|\|''|"foobar"| 0069 "foo\ x\x\\"\'"fo'obar"|"foo\ x\x\\"|\|'"fo'|obar"| 0070 "foo\ x\x\\"\'"fo'obar" 'don'\''t'|"foo\ x\x\\"|\|'"fo'|obar"|'don'|\|''|t'| 0071 'foo\ bar'|'foo\ bar'| 0072 'foo\\ bar'|'foo\\ bar'| 0073 foo\ bar|foo|\|bar| 0074 foo#bar\nbaz|foobaz| 0075 :-) ;-)|:|-|)|;|-|)| 0076 áéíóú|á|é|í|ó|ú| 0077 """ 0078 0079 posix_data = r"""x|x| 0080 foo bar|foo|bar| 0081 foo bar|foo|bar| 0082 foo bar |foo|bar| 0083 foo bar bla fasel|foo|bar|bla|fasel| 0084 x y z xxxx|x|y|z|xxxx| 0085 \x bar|x|bar| 0086 \ x bar| x|bar| 0087 \ bar| bar| 0088 foo \x bar|foo|x|bar| 0089 foo \ x bar|foo| x|bar| 0090 foo \ bar|foo| bar| 0091 foo "bar" bla|foo|bar|bla| 0092 "foo" "bar" "bla"|foo|bar|bla| 0093 "foo" bar "bla"|foo|bar|bla| 0094 "foo" bar bla|foo|bar|bla| 0095 foo 'bar' bla|foo|bar|bla| 0096 'foo' 'bar' 'bla'|foo|bar|bla| 0097 'foo' bar 'bla'|foo|bar|bla| 0098 'foo' bar bla|foo|bar|bla| 0099 blurb foo"bar"bar"fasel" baz|blurb|foobarbarfasel|baz| 0100 blurb foo'bar'bar'fasel' baz|blurb|foobarbarfasel|baz| 0101 ""|| 0102 ''|| 0103 foo "" bar|foo||bar| 0104 foo '' bar|foo||bar| 0105 foo "" "" "" bar|foo||||bar| 0106 foo '' '' '' bar|foo||||bar| 0107 \"|"| 0108 "\""|"| 0109 "foo\ bar"|foo\ bar| 0110 "foo\\ bar"|foo\ bar| 0111 "foo\\ bar\""|foo\ bar"| 0112 "foo\\" bar\"|foo\|bar"| 0113 "foo\\ bar\" dfadf"|foo\ bar" dfadf| 0114 "foo\\\ bar\" dfadf"|foo\\ bar" dfadf| 0115 "foo\\\x bar\" dfadf"|foo\\x bar" dfadf| 0116 "foo\x bar\" dfadf"|foo\x bar" dfadf| 0117 \'|'| 0118 'foo\ bar'|foo\ bar| 0119 'foo\\ bar'|foo\\ bar| 0120 "foo\\\x bar\" df'a\ 'df"|foo\\x bar" df'a\ 'df| 0121 \"foo|"foo| 0122 \"foo\x|"foox| 0123 "foo\x"|foo\x| 0124 "foo\ "|foo\ | 0125 foo\ xx|foo xx| 0126 foo\ x\x|foo xx| 0127 foo\ x\x\"|foo xx"| 0128 "foo\ x\x"|foo\ x\x| 0129 "foo\ x\x\\"|foo\ x\x\| 0130 "foo\ x\x\\""foobar"|foo\ x\x\foobar| 0131 "foo\ x\x\\"\'"foobar"|foo\ x\x\'foobar| 0132 "foo\ x\x\\"\'"fo'obar"|foo\ x\x\'fo'obar| 0133 "foo\ x\x\\"\'"fo'obar" 'don'\''t'|foo\ x\x\'fo'obar|don't| 0134 "foo\ x\x\\"\'"fo'obar" 'don'\''t' \\|foo\ x\x\'fo'obar|don't|\| 0135 'foo\ bar'|foo\ bar| 0136 'foo\\ bar'|foo\\ bar| 0137 foo\ bar|foo bar| 0138 foo#bar\nbaz|foo|baz| 0139 :-) ;-)|:-)|;-)| 0140 áéíóú|áéíóú| 0141 """ 0142 0143 class ShlexTest(unittest.TestCase): 0144 def setUp(self): 0145 self.data = [x.split("|")[:-1] 0146 for x in data.splitlines()] 0147 self.posix_data = [x.split("|")[:-1] 0148 for x in posix_data.splitlines()] 0149 for item in self.data: 0150 item[0] = item[0].replace(r"\n", "\n") 0151 for item in self.posix_data: 0152 item[0] = item[0].replace(r"\n", "\n") 0153 0154 def splitTest(self, data, comments): 0155 for i in range(len(data)): 0156 l = shlex.split(data[i][0], comments=comments) 0157 self.assertEqual(l, data[i][1:], 0158 "%s: %s != %s" % 0159 (data[i][0], l, data[i][1:])) 0160 0161 def oldSplit(self, s): 0162 ret = [] 0163 lex = shlex.shlex(StringIO(s)) 0164 tok = lex.get_token() 0165 while tok: 0166 ret.append(tok) 0167 tok = lex.get_token() 0168 return ret 0169 0170 def testSplitPosix(self): 0171 """Test data splitting with posix parser""" 0172 self.splitTest(self.posix_data, comments=True) 0173 0174 def testCompat(self): 0175 """Test compatibility interface""" 0176 for i in range(len(self.data)): 0177 l = self.oldSplit(self.data[i][0]) 0178 self.assertEqual(l, self.data[i][1:], 0179 "%s: %s != %s" % 0180 (self.data[i][0], l, self.data[i][1:])) 0181 0182 # Allow this test to be used with old shlex.py 0183 if not getattr(shlex, "split", None): 0184 for methname in dir(ShlexTest): 0185 if methname.startswith("test") and methname != "testCompat": 0186 delattr(ShlexTest, methname) 0187 0188 def test_main(): 0189 test_support.run_unittest(ShlexTest) 0190 0191 if __name__ == "__main__": 0192 test_main() 0193
Generated by PyXR 0.9.4