home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / Python / Source / Amiga_Misc / testset / test_argparser.py < prev    next >
Encoding:
Python Source  |  1998-10-31  |  4.8 KB  |  162 lines

  1. import Dos
  2.  
  3. TestError = 'FAILED --- ArgParser'
  4.  
  5. def test(a,b):
  6.         if a!=b: raise TestError
  7.  
  8.  
  9. print 'TESTING ARGPARSER SETUP...'
  10.  
  11. ap=Dos.ArgParser('ONE')
  12. test(ap.defaults,{'ONE': None})
  13. test(ap.types,(('ONE', 'X'),))
  14. ap.new('ONE,TWO')
  15. test(ap.defaults,{'TWO': None, 'ONE': None})
  16. test(ap.types,(('ONE', 'X'), ('TWO', 'X')))
  17. ap.new('STR,INT/N')
  18. test(ap.defaults,{'STR': None, 'INT': None})
  19. test(ap.types,(('STR', 'X'), ('INT', 'N')))
  20. ap.new('STR/K,INT/N/K')
  21. test(ap.defaults,{'STR': None, 'INT': None})
  22. test(ap.types,(('STR', 'X'), ('INT', 'N')))
  23. ap.new('REQ/A,INTLIST/M/N')
  24. test(ap.defaults,{'INTLIST': []})
  25. test(ap.types,(('REQ', 'X'), ('INTLIST', 'I')))
  26. ap.new('REQ/A/K,STRLIST/M')
  27. test(ap.defaults,{'STRLIST': []})
  28. test(ap.types,(('REQ', 'X'), ('STRLIST', 'A')))
  29. ap.new('REQ/A/K,REST/F,FLAG/S')
  30. test(ap.defaults,{'FLAG': 0, 'REST': None})
  31. test(ap.types,(('REQ', 'X'), ('REST', 'X'), ('FLAG', 'S')))
  32. ap.new('BLA/M,REST/F,FLAG/S/A')
  33. test(ap.defaults,{'BLA':[], 'REST': None})
  34. test(ap.types,(('BLA', 'A'), ('REST', 'X'), ('FLAG', 'S')))
  35. #ap.new('TOGGLE/T')
  36. #test(ap.defaults,{'TOGGLE': 0})
  37. #test(ap.types,(('TOGGLE', 'T'),))
  38.  
  39. print 'TESTING ARGPARSER PARSE...'
  40. print '1) STRING'
  41. ap.new('STR')
  42. test(ap.parse('string'),{'STR':'string'})
  43. test(ap.parse('STR string'),{'STR':'string'})
  44. test(ap.parse(''),{'STR':None})
  45. print '2) NUMBER'
  46. ap.new('NUM/N')
  47. test(ap.parse('1234'),{'NUM':1234})
  48. test(ap.parse('NUM=1234'),{'NUM':1234})
  49. test(ap.parse(''),{'NUM':None})
  50. print '3) TOGGLE'
  51. print '(not yet supported)'
  52. #ap.new('TOG/T')
  53. #test(ap.parse('TOG'),{'TOG':-1})
  54. #ap.defaults['TOG']=999
  55. #test(ap.parse('TOG'),{'TOG':0})
  56. #test(ap.parse(''),{'TOG':999})
  57. print '4) SWITCH'
  58. ap.new('SW/S')
  59. test(ap.parse('SW'),{'SW':-1})
  60. ap.defaults['SW']=999
  61. test(ap.parse('SW'),{'SW':-1})
  62. test(ap.parse(''),{'SW':999})
  63. print '5) STRING LIST'
  64. ap.new('STRL/M')
  65. test(ap.parse('foo bar foobar'),{'STRL':['foo','bar','foobar']})
  66. test(ap.parse('foo'),{'STRL':['foo']})
  67. test(ap.parse('STRL foo'),{'STRL':['foo']})
  68. ap.defaults['STRL']=['dflt']
  69. test(ap.parse('blabla'),{'STRL':['blabla']})
  70. test(ap.parse(''),{'STRL':['dflt']})
  71. print '6) NUMBER LIST'
  72. ap.new('NL/M/N')
  73. test(ap.parse('1 2 3'),{'NL':[1,2,3]})
  74. test(ap.parse('123'),{'NL':[123]})
  75. test(ap.parse('NL 123'),{'NL':[123]})
  76. ap.defaults['NL']=[999]
  77. test(ap.parse('42 42 42'),{'NL':[42,42,42]})
  78. test(ap.parse(''),{'NL':[999]})
  79. print '7) /A'
  80. ap.new('STR/A')
  81. test(ap.defaults,{})
  82. test(ap.parse('string'),{'STR':'string'})
  83. test(ap.parse('STR string'),{'STR':'string'})
  84. print '8) /F'
  85. ap.new('STR/F,SW/S')
  86. test(ap.parse('foo bar   foobar 42'),{'STR':'foo bar   foobar 42', 'SW':0})
  87. test(ap.parse('foo bar SW  foobar 42'),{'STR':'foo bar SW  foobar 42', 'SW':0})
  88. test(ap.parse('SW foo bar SW  foobar 42'),{'STR':'foo bar SW  foobar 42', 'SW':-1})
  89. print '9) /K'
  90. ap.new('STR/K')
  91. test(ap.parse('STR string'),{'STR':'string'})
  92. ap.new('STR/K/F')
  93. test(ap.parse('STR string foo bar'),{'STR':'string foo bar'})
  94.  
  95. print '10) COMBINED'
  96. ap.new('FROM/A/M,TO/A,ALL/S,QUIET/S,BUF=BUFFER/K/N')
  97. test(ap.defaults,{'BUF=BUFFER': None, 'ALL': 0, 'QUIET': 0})
  98. test(ap.types,(('FROM', 'A'), ('TO', 'X'), ('ALL', 'S'), ('QUIET', 'S'), ('BUF=BUFFER', 'N')))
  99.  
  100. # Set our custom defaults:
  101. ap.defaults['BUF=BUFFER']=999
  102. ap.defaults['QUIET']=888
  103. test(ap.parse('f1 f2 f3 dest'),{'BUF=BUFFER': 999, 'TO': 'dest', 'ALL': 0, 'QUIET': 888, 'FROM': ['f1', 'f2', 'f3']})
  104. test(ap.parse('1 2 3 dest'),{'BUF=BUFFER': 999, 'TO': 'dest', 'ALL': 0, 'QUIET': 888, 'FROM': ['1', '2', '3']})
  105. test(ap.parse('1 2 3 4'),{'BUF=BUFFER': 999, 'TO': '4', 'ALL': 0, 'QUIET': 888, 'FROM': ['1', '2', '3']})
  106. test(ap.parse('src dest ALL'),{'BUF=BUFFER': 999, 'TO': 'dest', 'ALL': -1, 'QUIET': 888, 'FROM': ['src']})
  107. test(ap.parse('src dest QUIET'),{'BUF=BUFFER': 999, 'TO': 'dest', 'ALL': 0, 'QUIET': -1, 'FROM': ['src']})
  108. test(ap.parse('src dest QUIET ALL'),{'BUF=BUFFER': 999, 'TO': 'dest', 'ALL': -1, 'QUIET': -1, 'FROM': ['src']})
  109. test(ap.parse('src dest BUF=10'),{'BUF=BUFFER': 10, 'TO': 'dest', 'ALL': 0, 'QUIET': 888, 'FROM': ['src']})
  110.  
  111. def terr(f,a):
  112.  fault=0
  113.  try:
  114.     try:
  115.         f(a)
  116.     except (Dos.error,ValueError,SystemError,TypeError):
  117.         fault=1
  118.  finally:
  119.     if not fault:
  120.         raise TestError,'should have given an error'
  121.  
  122.  
  123. print 'TESTING ARGPARSER ERRORS...'
  124.  
  125. terr(ap.new,'/')
  126. terr(ap.new,'A/')
  127. terr(ap.new,'A/A/A')
  128. terr(ap.new,'A/M/F')
  129. terr(ap.new,'A/F/M')
  130. terr(ap.new,'A/M,B/M/N')
  131. terr(ap.new,'A/M,B³³/±')
  132. terr(ap.new,'A/M,B/Z')
  133. terr(ap.new,'A,A')
  134. terr(ap.new,'A,B,A')
  135.  
  136.  
  137. ap.new('FROM/A/M,TO/A,ALL/S,QUIET/S,BUF=BUFFER/K/N')
  138. ap.template='KAPUT'
  139. terr(ap.parse,'foo')
  140.  
  141. ap.new('A,B')
  142. ap.types=(1,2)
  143. terr(ap.parse,'A B')
  144. ap.reset()
  145. test(ap.types,(('A', 'X'), ('B', 'X')))
  146.  
  147. ap.new('A,B')
  148. ap.types=(('A',42),('B',42))
  149. terr(ap.parse,'A B')
  150.  
  151. ap.new('A,B')
  152. ap.types=(('A','Z'),('B','Z'))
  153. terr(ap.parse,'A B')
  154.  
  155. ap.new('')
  156. terr(ap.parse,'foo bar')
  157. terr(ap.parse,'foo')
  158. test(ap.parse(''),{})
  159.  
  160. del ap
  161. print 'ARGPARSER OK!'
  162.