home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Amiga_Misc / testset / test_argparser.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  1998-11-12  |  5.6 KB  |  259 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import Dos
  5. TestError = 'FAILED --- ArgParser'
  6.  
  7. def test(a, b):
  8.     if a != b:
  9.         raise TestError
  10.     
  11.  
  12. print 'TESTING ARGPARSER SETUP...'
  13. ap = Dos.ArgParser('ONE')
  14. test(ap.defaults, {
  15.     'ONE': None })
  16. test(ap.types, (('ONE', 'X'),))
  17. ap.new('ONE,TWO')
  18. test(ap.defaults, {
  19.     'TWO': None,
  20.     'ONE': None })
  21. test(ap.types, (('ONE', 'X'), ('TWO', 'X')))
  22. ap.new('STR,INT/N')
  23. test(ap.defaults, {
  24.     'STR': None,
  25.     'INT': None })
  26. test(ap.types, (('STR', 'X'), ('INT', 'N')))
  27. ap.new('STR/K,INT/N/K')
  28. test(ap.defaults, {
  29.     'STR': None,
  30.     'INT': None })
  31. test(ap.types, (('STR', 'X'), ('INT', 'N')))
  32. ap.new('REQ/A,INTLIST/M/N')
  33. test(ap.defaults, {
  34.     'INTLIST': [] })
  35. test(ap.types, (('REQ', 'X'), ('INTLIST', 'I')))
  36. ap.new('REQ/A/K,STRLIST/M')
  37. test(ap.defaults, {
  38.     'STRLIST': [] })
  39. test(ap.types, (('REQ', 'X'), ('STRLIST', 'A')))
  40. ap.new('REQ/A/K,REST/F,FLAG/S')
  41. test(ap.defaults, {
  42.     'FLAG': 0,
  43.     'REST': None })
  44. test(ap.types, (('REQ', 'X'), ('REST', 'X'), ('FLAG', 'S')))
  45. ap.new('BLA/M,REST/F,FLAG/S/A')
  46. test(ap.defaults, {
  47.     'BLA': [],
  48.     'REST': None })
  49. test(ap.types, (('BLA', 'A'), ('REST', 'X'), ('FLAG', 'S')))
  50. print 'TESTING ARGPARSER PARSE...'
  51. print '1) STRING'
  52. ap.new('STR')
  53. test(ap.parse('string'), {
  54.     'STR': 'string' })
  55. test(ap.parse('STR string'), {
  56.     'STR': 'string' })
  57. test(ap.parse(''), {
  58.     'STR': None })
  59. print '2) NUMBER'
  60. ap.new('NUM/N')
  61. test(ap.parse('1234'), {
  62.     'NUM': 1234 })
  63. test(ap.parse('NUM=1234'), {
  64.     'NUM': 1234 })
  65. test(ap.parse(''), {
  66.     'NUM': None })
  67. print '3) TOGGLE'
  68. print '(not yet supported)'
  69. print '4) SWITCH'
  70. ap.new('SW/S')
  71. test(ap.parse('SW'), {
  72.     'SW': -1 })
  73. ap.defaults['SW'] = 999
  74. test(ap.parse('SW'), {
  75.     'SW': -1 })
  76. test(ap.parse(''), {
  77.     'SW': 999 })
  78. print '5) STRING LIST'
  79. ap.new('STRL/M')
  80. test(ap.parse('foo bar foobar'), {
  81.     'STRL': [
  82.         'foo',
  83.         'bar',
  84.         'foobar'] })
  85. test(ap.parse('foo'), {
  86.     'STRL': [
  87.         'foo'] })
  88. test(ap.parse('STRL foo'), {
  89.     'STRL': [
  90.         'foo'] })
  91. ap.defaults['STRL'] = [
  92.     'dflt']
  93. test(ap.parse('blabla'), {
  94.     'STRL': [
  95.         'blabla'] })
  96. test(ap.parse(''), {
  97.     'STRL': [
  98.         'dflt'] })
  99. print '6) NUMBER LIST'
  100. ap.new('NL/M/N')
  101. test(ap.parse('1 2 3'), {
  102.     'NL': [
  103.         1,
  104.         2,
  105.         3] })
  106. test(ap.parse('123'), {
  107.     'NL': [
  108.         123] })
  109. test(ap.parse('NL 123'), {
  110.     'NL': [
  111.         123] })
  112. ap.defaults['NL'] = [
  113.     999]
  114. test(ap.parse('42 42 42'), {
  115.     'NL': [
  116.         42,
  117.         42,
  118.         42] })
  119. test(ap.parse(''), {
  120.     'NL': [
  121.         999] })
  122. print '7) /A'
  123. ap.new('STR/A')
  124. test(ap.defaults, { })
  125. test(ap.parse('string'), {
  126.     'STR': 'string' })
  127. test(ap.parse('STR string'), {
  128.     'STR': 'string' })
  129. print '8) /F'
  130. ap.new('STR/F,SW/S')
  131. test(ap.parse('foo bar   foobar 42'), {
  132.     'STR': 'foo bar   foobar 42',
  133.     'SW': 0 })
  134. test(ap.parse('foo bar SW  foobar 42'), {
  135.     'STR': 'foo bar SW  foobar 42',
  136.     'SW': 0 })
  137. test(ap.parse('SW foo bar SW  foobar 42'), {
  138.     'STR': 'foo bar SW  foobar 42',
  139.     'SW': -1 })
  140. print '9) /K'
  141. ap.new('STR/K')
  142. test(ap.parse('STR string'), {
  143.     'STR': 'string' })
  144. ap.new('STR/K/F')
  145. test(ap.parse('STR string foo bar'), {
  146.     'STR': 'string foo bar' })
  147. print '10) COMBINED'
  148. ap.new('FROM/A/M,TO/A,ALL/S,QUIET/S,BUF=BUFFER/K/N')
  149. test(ap.defaults, {
  150.     'BUF=BUFFER': None,
  151.     'ALL': 0,
  152.     'QUIET': 0 })
  153. test(ap.types, (('FROM', 'A'), ('TO', 'X'), ('ALL', 'S'), ('QUIET', 'S'), ('BUF=BUFFER', 'N')))
  154. ap.defaults['BUF=BUFFER'] = 999
  155. ap.defaults['QUIET'] = 888
  156. test(ap.parse('f1 f2 f3 dest'), {
  157.     'BUF=BUFFER': 999,
  158.     'TO': 'dest',
  159.     'ALL': 0,
  160.     'QUIET': 888,
  161.     'FROM': [
  162.         'f1',
  163.         'f2',
  164.         'f3'] })
  165. test(ap.parse('1 2 3 dest'), {
  166.     'BUF=BUFFER': 999,
  167.     'TO': 'dest',
  168.     'ALL': 0,
  169.     'QUIET': 888,
  170.     'FROM': [
  171.         '1',
  172.         '2',
  173.         '3'] })
  174. test(ap.parse('1 2 3 4'), {
  175.     'BUF=BUFFER': 999,
  176.     'TO': '4',
  177.     'ALL': 0,
  178.     'QUIET': 888,
  179.     'FROM': [
  180.         '1',
  181.         '2',
  182.         '3'] })
  183. test(ap.parse('src dest ALL'), {
  184.     'BUF=BUFFER': 999,
  185.     'TO': 'dest',
  186.     'ALL': -1,
  187.     'QUIET': 888,
  188.     'FROM': [
  189.         'src'] })
  190. test(ap.parse('src dest QUIET'), {
  191.     'BUF=BUFFER': 999,
  192.     'TO': 'dest',
  193.     'ALL': 0,
  194.     'QUIET': -1,
  195.     'FROM': [
  196.         'src'] })
  197. test(ap.parse('src dest QUIET ALL'), {
  198.     'BUF=BUFFER': 999,
  199.     'TO': 'dest',
  200.     'ALL': -1,
  201.     'QUIET': -1,
  202.     'FROM': [
  203.         'src'] })
  204. test(ap.parse('src dest BUF=10'), {
  205.     'BUF=BUFFER': 10,
  206.     'TO': 'dest',
  207.     'ALL': 0,
  208.     'QUIET': 888,
  209.     'FROM': [
  210.         'src'] })
  211.  
  212. def terr(f, a):
  213.     fault = 0
  214.     
  215.     try:
  216.         
  217.         try:
  218.             f(a)
  219.         except (Dos.error, ValueError, SystemError, TypeError):
  220.             fault = 1
  221.  
  222.     finally:
  223.         if not fault:
  224.             raise TestError, 'should have given an error'
  225.         
  226.  
  227.  
  228. print 'TESTING ARGPARSER ERRORS...'
  229. terr(ap.new, '/')
  230. terr(ap.new, 'A/')
  231. terr(ap.new, 'A/A/A')
  232. terr(ap.new, 'A/M/F')
  233. terr(ap.new, 'A/F/M')
  234. terr(ap.new, 'A/M,B/M/N')
  235. terr(ap.new, 'A/M,B\xb3\xb3/\xb1')
  236. terr(ap.new, 'A/M,B/Z')
  237. terr(ap.new, 'A,A')
  238. terr(ap.new, 'A,B,A')
  239. ap.new('FROM/A/M,TO/A,ALL/S,QUIET/S,BUF=BUFFER/K/N')
  240. ap.template = 'KAPUT'
  241. terr(ap.parse, 'foo')
  242. ap.new('A,B')
  243. ap.types = (1, 2)
  244. terr(ap.parse, 'A B')
  245. ap.reset()
  246. test(ap.types, (('A', 'X'), ('B', 'X')))
  247. ap.new('A,B')
  248. ap.types = (('A', 42), ('B', 42))
  249. terr(ap.parse, 'A B')
  250. ap.new('A,B')
  251. ap.types = (('A', 'Z'), ('B', 'Z'))
  252. terr(ap.parse, 'A B')
  253. ap.new('')
  254. terr(ap.parse, 'foo bar')
  255. terr(ap.parse, 'foo')
  256. test(ap.parse(''), { })
  257. del ap
  258. print 'ARGPARSER OK!'
  259.