Home | History | Annotate | Download | only in test
      1 # -*- coding: iso-8859-1 -*-
      2 import unittest
      3 import shlex
      4 
      5 from test import test_support
      6 
      7 try:
      8     from cStringIO import StringIO
      9 except ImportError:
     10     from StringIO import StringIO
     11 
     12 
     13 # The original test data set was from shellwords, by Hartmut Goebel.
     14 
     15 data = r"""x|x|
     16 foo bar|foo|bar|
     17  foo bar|foo|bar|
     18  foo bar |foo|bar|
     19 foo   bar    bla     fasel|foo|bar|bla|fasel|
     20 x y  z              xxxx|x|y|z|xxxx|
     21 \x bar|\|x|bar|
     22 \ x bar|\|x|bar|
     23 \ bar|\|bar|
     24 foo \x bar|foo|\|x|bar|
     25 foo \ x bar|foo|\|x|bar|
     26 foo \ bar|foo|\|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|
     37 ""|""|
     38 ''|''|
     39 foo "" bar|foo|""|bar|
     40 foo '' bar|foo|''|bar|
     41 foo "" "" "" bar|foo|""|""|""|bar|
     42 foo '' '' '' bar|foo|''|''|''|bar|
     43 \""|\|""|
     44 "\"|"\"|
     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"|
     53 \''|\|''|
     54 'foo\ bar'|'foo\ bar'|
     55 'foo\\ bar'|'foo\\ bar'|
     56 "foo\\\x bar\" df'a\ 'df'|"foo\\\x bar\"|df'a|\|'df'|
     57 \"foo"|\|"foo"|
     58 \"foo"\x|\|"foo"|\|x|
     59 "foo\x"|"foo\x"|
     60 "foo\ "|"foo\ "|
     61 foo\ xx|foo|\|xx|
     62 foo\ x\x|foo|\|x|\|x|
     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'|
     72 foo\ bar|foo|\|bar|
     73 foo#bar\nbaz|foobaz|
     74 :-) ;-)|:|-|)|;|-|)|
     75 ||||||
     76 """
     77 
     78 posix_data = r"""x|x|
     79 foo bar|foo|bar|
     80  foo bar|foo|bar|
     81  foo bar |foo|bar|
     82 foo   bar    bla     fasel|foo|bar|bla|fasel|
     83 x y  z              xxxx|x|y|z|xxxx|
     84 \x bar|x|bar|
     85 \ x bar| x|bar|
     86 \ bar| bar|
     87 foo \x bar|foo|x|bar|
     88 foo \ x bar|foo| x|bar|
     89 foo \ bar|foo| 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|
    100 ""||
    101 ''||
    102 foo "" bar|foo||bar|
    103 foo '' bar|foo||bar|
    104 foo "" "" "" bar|foo||||bar|
    105 foo '' '' '' bar|foo||||bar|
    106 \"|"|
    107 "\""|"|
    108 "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|
    116 \'|'|
    117 'foo\ bar'|foo\ bar|
    118 'foo\\ bar'|foo\\ bar|
    119 "foo\\\x bar\" df'a\ 'df"|foo\\x bar" df'a\ 'df|
    120 \"foo|"foo|
    121 \"foo\x|"foox|
    122 "foo\x"|foo\x|
    123 "foo\ "|foo\ |
    124 foo\ xx|foo xx|
    125 foo\ x\x|foo xx|
    126 foo\ x\x\"|foo xx"|
    127 "foo\ x\x"|foo\ x\x|
    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|\|
    134 'foo\ bar'|foo\ bar|
    135 'foo\\ bar'|foo\\ bar|
    136 foo\ bar|foo bar|
    137 foo#bar\nbaz|foo|baz|
    138 :-) ;-)|:-)|;-)|
    139 ||
    140 """
    141 
    142 class ShlexTest(unittest.TestCase):
    143     def setUp(self):
    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")
    152 
    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:],
    157                              "%s: %s != %s" %
    158                              (data[i][0], l, data[i][1:]))
    159 
    160     def oldSplit(self, s):
    161         ret = []
    162         lex = shlex.shlex(StringIO(s))
    163         tok = lex.get_token()
    164         while tok:
    165             ret.append(tok)
    166             tok = lex.get_token()
    167         return ret
    168 
    169     def testSplitPosix(self):
    170         """Test data splitting with posix parser"""
    171         self.splitTest(self.posix_data, comments=True)
    172 
    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:],
    178                              "%s: %s != %s" %
    179                              (self.data[i][0], l, self.data[i][1:]))
    180 
    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)
    186 
    187 def test_main():
    188     test_support.run_unittest(ShlexTest)
    189 
    190 if __name__ == "__main__":
    191     test_main()
    192