home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / test_shlex.py < prev    next >
Text File  |  2003-12-30  |  5KB  |  193 lines

  1. # -*- coding: iso-8859-1 -*-
  2. import unittest
  3. import os, sys
  4. import shlex
  5.  
  6. from test import test_support
  7.  
  8. try:
  9.     from cStringIO import StringIO
  10. except ImportError:
  11.     from StringIO import StringIO
  12.  
  13.  
  14. # The original test data set was from shellwords, by Hartmut Goebel.
  15.  
  16. data = r"""x|x|
  17. foo bar|foo|bar|
  18.  foo bar|foo|bar|
  19.  foo bar |foo|bar|
  20. foo   bar    bla     fasel|foo|bar|bla|fasel|
  21. x y  z              xxxx|x|y|z|xxxx|
  22. \x bar|\|x|bar|
  23. \ x bar|\|x|bar|
  24. \ bar|\|bar|
  25. foo \x bar|foo|\|x|bar|
  26. foo \ x bar|foo|\|x|bar|
  27. foo \ bar|foo|\|bar|
  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. 'foo' bar bla|'foo'|bar|bla|
  36. blurb foo"bar"bar"fasel" baz|blurb|foo"bar"bar"fasel"|baz|
  37. blurb foo'bar'bar'fasel' baz|blurb|foo'bar'bar'fasel'|baz|
  38. ""|""|
  39. ''|''|
  40. foo "" bar|foo|""|bar|
  41. foo '' bar|foo|''|bar|
  42. foo "" "" "" bar|foo|""|""|""|bar|
  43. foo '' '' '' bar|foo|''|''|''|bar|
  44. \""|\|""|
  45. "\"|"\"|
  46. "foo\ bar"|"foo\ bar"|
  47. "foo\\ bar"|"foo\\ bar"|
  48. "foo\\ bar\"|"foo\\ bar\"|
  49. "foo\\" bar\""|"foo\\"|bar|\|""|
  50. "foo\\ bar\" dfadf"|"foo\\ bar\"|dfadf"|
  51. "foo\\\ bar\" dfadf"|"foo\\\ bar\"|dfadf"|
  52. "foo\\\x bar\" dfadf"|"foo\\\x bar\"|dfadf"|
  53. "foo\x bar\" dfadf"|"foo\x bar\"|dfadf"|
  54. \''|\|''|
  55. 'foo\ bar'|'foo\ bar'|
  56. 'foo\\ bar'|'foo\\ bar'|
  57. "foo\\\x bar\" df'a\ 'df'|"foo\\\x bar\"|df'a|\|'df'|
  58. \"foo"|\|"foo"|
  59. \"foo"\x|\|"foo"|\|x|
  60. "foo\x"|"foo\x"|
  61. "foo\ "|"foo\ "|
  62. foo\ xx|foo|\|xx|
  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\\"|"foo\ x\x\\"|
  67. "foo\ x\x\\""foobar"|"foo\ x\x\\"|"foobar"|
  68. "foo\ x\x\\"\''"foobar"|"foo\ x\x\\"|\|''|"foobar"|
  69. "foo\ x\x\\"\'"fo'obar"|"foo\ x\x\\"|\|'"fo'|obar"|
  70. "foo\ x\x\\"\'"fo'obar" 'don'\''t'|"foo\ x\x\\"|\|'"fo'|obar"|'don'|\|''|t'|
  71. 'foo\ bar'|'foo\ bar'|
  72. 'foo\\ bar'|'foo\\ bar'|
  73. foo\ bar|foo|\|bar|
  74. foo#bar\nbaz|foobaz|
  75. :-) ;-)|:|-|)|;|-|)|
  76. ßΘφ≤·|ß|Θ|φ|≤|·|
  77. """
  78.  
  79. posix_data = r"""x|x|
  80. foo bar|foo|bar|
  81.  foo bar|foo|bar|
  82.  foo bar |foo|bar|
  83. foo   bar    bla     fasel|foo|bar|bla|fasel|
  84. x y  z              xxxx|x|y|z|xxxx|
  85. \x bar|x|bar|
  86. \ x bar| x|bar|
  87. \ bar| bar|
  88. foo \x bar|foo|x|bar|
  89. foo \ x bar|foo| x|bar|
  90. foo \ bar|foo| bar|
  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. 'foo' bar bla|foo|bar|bla|
  99. blurb foo"bar"bar"fasel" baz|blurb|foobarbarfasel|baz|
  100. blurb foo'bar'bar'fasel' baz|blurb|foobarbarfasel|baz|
  101. ""||
  102. ''||
  103. foo "" bar|foo||bar|
  104. foo '' bar|foo||bar|
  105. foo "" "" "" bar|foo||||bar|
  106. foo '' '' '' bar|foo||||bar|
  107. \"|"|
  108. "\""|"|
  109. "foo\ bar"|foo\ bar|
  110. "foo\\ bar"|foo\ bar|
  111. "foo\\ bar\""|foo\ bar"|
  112. "foo\\" bar\"|foo\|bar"|
  113. "foo\\ bar\" dfadf"|foo\ bar" dfadf|
  114. "foo\\\ bar\" dfadf"|foo\\ bar" dfadf|
  115. "foo\\\x bar\" dfadf"|foo\\x bar" dfadf|
  116. "foo\x bar\" dfadf"|foo\x bar" dfadf|
  117. \'|'|
  118. 'foo\ bar'|foo\ bar|
  119. 'foo\\ bar'|foo\\ bar|
  120. "foo\\\x bar\" df'a\ 'df"|foo\\x bar" df'a\ 'df|
  121. \"foo|"foo|
  122. \"foo\x|"foox|
  123. "foo\x"|foo\x|
  124. "foo\ "|foo\ |
  125. foo\ xx|foo xx|
  126. foo\ x\x|foo xx|
  127. foo\ x\x\"|foo xx"|
  128. "foo\ x\x"|foo\ x\x|
  129. "foo\ x\x\\"|foo\ x\x\|
  130. "foo\ x\x\\""foobar"|foo\ x\x\foobar|
  131. "foo\ x\x\\"\'"foobar"|foo\ x\x\'foobar|
  132. "foo\ x\x\\"\'"fo'obar"|foo\ x\x\'fo'obar|
  133. "foo\ x\x\\"\'"fo'obar" 'don'\''t'|foo\ x\x\'fo'obar|don't|
  134. "foo\ x\x\\"\'"fo'obar" 'don'\''t' \\|foo\ x\x\'fo'obar|don't|\|
  135. 'foo\ bar'|foo\ bar|
  136. 'foo\\ bar'|foo\\ bar|
  137. foo\ bar|foo bar|
  138. foo#bar\nbaz|foo|baz|
  139. :-) ;-)|:-)|;-)|
  140. ßΘφ≤·|ßΘφ≤·|
  141. """
  142.  
  143. class ShlexTest(unittest.TestCase):
  144.     def setUp(self):
  145.         self.data = [x.split("|")[:-1]
  146.                      for x in data.splitlines()]
  147.         self.posix_data = [x.split("|")[:-1]
  148.                            for x in posix_data.splitlines()]
  149.         for item in self.data:
  150.             item[0] = item[0].replace(r"\n", "\n")
  151.         for item in self.posix_data:
  152.             item[0] = item[0].replace(r"\n", "\n")
  153.  
  154.     def splitTest(self, data, comments):
  155.         for i in range(len(data)):
  156.             l = shlex.split(data[i][0], comments=comments)
  157.             self.assertEqual(l, data[i][1:],
  158.                              "%s: %s != %s" %
  159.                              (data[i][0], l, data[i][1:]))
  160.  
  161.     def oldSplit(self, s):
  162.         ret = []
  163.         lex = shlex.shlex(StringIO(s))
  164.         tok = lex.get_token()
  165.         while tok:
  166.             ret.append(tok)
  167.             tok = lex.get_token()
  168.         return ret
  169.  
  170.     def testSplitPosix(self):
  171.         """Test data splitting with posix parser"""
  172.         self.splitTest(self.posix_data, comments=True)
  173.  
  174.     def testCompat(self):
  175.         """Test compatibility interface"""
  176.         for i in range(len(self.data)):
  177.             l = self.oldSplit(self.data[i][0])
  178.             self.assertEqual(l, self.data[i][1:],
  179.                              "%s: %s != %s" %
  180.                              (self.data[i][0], l, self.data[i][1:]))
  181.  
  182. # Allow this test to be used with old shlex.py
  183. if not getattr(shlex, "split", None):
  184.     for methname in dir(ShlexTest):
  185.         if methname.startswith("test") and methname != "testCompat":
  186.             delattr(ShlexTest, methname)
  187.  
  188. def test_main():
  189.     test_support.run_unittest(ShlexTest)
  190.  
  191. if __name__ == "__main__":
  192.     test_main()
  193.