PyXR

c:\python24\lib \ test \ test_unpack.py



0001 doctests = """
0002 
0003 Unpack tuple
0004 
0005     >>> t = (1, 2, 3)
0006     >>> a, b, c = t
0007     >>> a == 1 and b == 2 and c == 3
0008     True
0009 
0010 Unpack list
0011 
0012     >>> l = [4, 5, 6]
0013     >>> a, b, c = l
0014     >>> a == 4 and b == 5 and c == 6
0015     True
0016 
0017 Unpack implied tuple
0018 
0019     >>> a, b, c = 7, 8, 9
0020     >>> a == 7 and b == 8 and c == 9
0021     True
0022 
0023 Unpack string... fun!
0024 
0025     >>> a, b, c = 'one'
0026     >>> a == 'o' and b == 'n' and c == 'e'
0027     True
0028 
0029 Unpack generic sequence
0030 
0031     >>> class Seq:
0032     ...     def __getitem__(self, i):
0033     ...         if i >= 0 and i < 3: return i
0034     ...         raise IndexError
0035     ...
0036     >>> a, b, c = Seq()
0037     >>> a == 0 and b == 1 and c == 2
0038     True
0039 
0040 Single element unpacking, with extra syntax
0041 
0042     >>> st = (99,)
0043     >>> sl = [100]
0044     >>> a, = st
0045     >>> a
0046     99
0047     >>> b, = sl
0048     >>> b
0049     100
0050 
0051 Now for some failures
0052 
0053 Unpacking non-sequence
0054 
0055     >>> a, b, c = 7
0056     Traceback (most recent call last):
0057       ...
0058     TypeError: unpack non-sequence
0059 
0060 Unpacking tuple of wrong size
0061 
0062     >>> a, b = t
0063     Traceback (most recent call last):
0064       ...
0065     ValueError: too many values to unpack
0066 
0067 Unpacking tuple of wrong size
0068 
0069     >>> a, b = l
0070     Traceback (most recent call last):
0071       ...
0072     ValueError: too many values to unpack
0073 
0074 Unpacking sequence too short
0075 
0076     >>> a, b, c, d = Seq()
0077     Traceback (most recent call last):
0078       ...
0079     ValueError: need more than 3 values to unpack
0080 
0081 Unpacking sequence too long
0082 
0083     >>> a, b = Seq()
0084     Traceback (most recent call last):
0085       ...
0086     ValueError: too many values to unpack
0087 
0088 Unpacking a sequence where the test for too long raises a different kind of
0089 error
0090 
0091     >>> class BozoError(Exception):
0092     ...     pass
0093     ...
0094     >>> class BadSeq:
0095     ...     def __getitem__(self, i):
0096     ...         if i >= 0 and i < 3:
0097     ...             return i
0098     ...         elif i == 3:
0099     ...             raise BozoError
0100     ...         else:
0101     ...             raise IndexError
0102     ...
0103 
0104 Trigger code while not expecting an IndexError (unpack sequence too long, wrong
0105 error)
0106 
0107     >>> a, b, c, d, e = BadSeq()
0108     Traceback (most recent call last):
0109       ...
0110     BozoError
0111 
0112 Trigger code while expecting an IndexError (unpack sequence too short, wrong
0113 error)
0114 
0115     >>> a, b, c = BadSeq()
0116     Traceback (most recent call last):
0117       ...
0118     BozoError
0119 
0120 """
0121 
0122 __test__ = {'doctests' : doctests}
0123 
0124 def test_main(verbose=False):
0125     import sys
0126     from test import test_support
0127     from test import test_unpack
0128     test_support.run_doctest(test_unpack, verbose)
0129 
0130 if __name__ == "__main__":
0131     test_main(verbose=True)
0132 

Generated by PyXR 0.9.4
SourceForge.net Logo