home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / test_unpack.py < prev    next >
Text File  |  1997-08-25  |  2KB  |  132 lines

  1. from test_support import *
  2.  
  3. t = (1, 2, 3)
  4. l = [4, 5, 6]
  5.  
  6. class Seq:
  7.     def __getitem__(self, i):
  8.     if i >= 0 and i < 3: return i
  9.     raise IndexError
  10.  
  11. a = -1
  12. b = -1
  13. c = -1
  14.  
  15. # unpack tuple
  16. if verbose:
  17.     print 'unpack tuple'
  18. a, b, c = t
  19. if a <> 1 or b <> 2 or c <> 3:
  20.     raise TestFailed
  21.  
  22. # unpack list
  23. if verbose:
  24.     print 'unpack list'
  25. a, b, c = l
  26. if a <> 4 or b <> 5 or c <> 6:
  27.     raise TestFailed
  28.  
  29. # unpack implied tuple
  30. if verbose:
  31.     print 'unpack implied tuple'
  32. a, b, c = 7, 8, 9
  33. if a <> 7 or b <> 8 or c <> 9:
  34.     raise TestFailed
  35.  
  36. # unpack string... fun!
  37. if verbose:
  38.     print 'unpack string'
  39. a, b, c = 'one'
  40. if a <> 'o' or b <> 'n' or c <> 'e':
  41.     raise TestFailed
  42.  
  43. # unpack generic sequence
  44. if verbose:
  45.     print 'unpack sequence'
  46. a, b, c = Seq()
  47. if a <> 0 or b <> 1 or c <> 2:
  48.     raise TestFailed
  49.  
  50. # now for some failures
  51.  
  52. # unpacking non-sequence
  53. if verbose:
  54.     print 'unpack non-sequence'
  55. try:
  56.     a, b, c = 7
  57.     raise TestFailed
  58. except TypeError:
  59.     pass
  60.  
  61.  
  62. # unpacking tuple of wrong size
  63. if verbose:
  64.     print 'unpack tuple wrong size'
  65. try:
  66.     a, b = t
  67.     raise TestFailed
  68. except ValueError:
  69.     pass
  70.  
  71. # unpacking list of wrong size
  72. if verbose:
  73.     print 'unpack list wrong size'
  74. try:
  75.     a, b = l
  76.     raise TestFailed
  77. except ValueError:
  78.     pass
  79.  
  80.  
  81. # unpacking sequence too short
  82. if verbose:
  83.     print 'unpack sequence too short'
  84. try:
  85.     a, b, c, d = Seq()
  86.     raise TestFailed
  87. except ValueError:
  88.     pass
  89.  
  90.  
  91. # unpacking sequence too long
  92. if verbose:
  93.     print 'unpack sequence too long'
  94. try:
  95.     a, b = Seq()
  96.     raise TestFailed
  97. except ValueError:
  98.     pass
  99.  
  100.  
  101. # unpacking a sequence where the test for too long raises a different
  102. # kind of error
  103. BozoError = 'BozoError'
  104.  
  105. class BadSeq:
  106.     def __getitem__(self, i):
  107.     if i >= 0 and i < 3:
  108.         return i
  109.     elif i == 3:
  110.         raise BozoError
  111.     else:
  112.         raise IndexError
  113.  
  114.  
  115. # trigger code while not expecting an IndexError
  116. if verbose:
  117.     print 'unpack sequence too long, wrong error'
  118. try:
  119.     a, b, c, d, e = BadSeq()
  120.     raise TestFailed
  121. except BozoError:
  122.     pass
  123.  
  124. # trigger code while expecting an IndexError
  125. if verbose:
  126.     print 'unpack sequence too short, wrong error'
  127. try:
  128.     a, b, c = BadSeq()
  129.     raise TestFailed
  130. except BozoError:
  131.     pass
  132.