test_tuple.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""
  2. import unittest
  3. class IterInheritsTestCase(unittest.TestCase):
  4. def test_generator(self):
  5. def counter(low, high):
  6. current = low
  7. while current <= high:
  8. yield current
  9. current += 1
  10. l = tuple(counter(1,12))
  11. t = 4 in l
  12. print t, l
  13. self.assertTrue(t)
  14. def test_getitem(self):
  15. class Counter:
  16. def __getitem__(self,idx):
  17. if idx < 13:
  18. return idx
  19. else:
  20. raise StopIteration
  21. l = tuple(Counter())
  22. print l
  23. self.assertTrue(5 in l)
  24. def test_dunderiter(self):
  25. class Counter:
  26. def __init__(self, low, high):
  27. self.current = low
  28. self.high = high
  29. def __iter__(self):
  30. return self
  31. def next(self): # Python 3: def __next__(self)
  32. if self.current > self.high:
  33. raise StopIteration
  34. else:
  35. self.current += 1
  36. return self.current - 1
  37. l = tuple(Counter(1,12))
  38. print l
  39. self.assertTrue(5 in l)
  40. class Foo(Counter):
  41. pass
  42. l = tuple(Foo(100,120))
  43. print l
  44. self.assertTrue(105 in l)
  45. if __name__ == "__main__":
  46. unittest.main()