test_dict.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import unittest
  2. '''
  3. source: https://bitbucket.org/pypy/pypy/src/d7777f2ccd3f0c1a4720d5e6fc3c6b8da4547ea7/lib-python/2.7/test/test_dict.py?at=default
  4. '''
  5. class DictTest(unittest.TestCase):
  6. def test_constructor(self):
  7. # calling built-in types without argument must return empty
  8. self.assertEqual(dict(), {})
  9. self.assertIsNot(dict(), {})
  10. def test_bool(self):
  11. self.assertIs(not {}, True)
  12. self.assertTrue({1: 2})
  13. self.assertIs(bool({}), False)
  14. self.assertIs(bool({1: 2}), True)
  15. def test_keys(self):
  16. d = {}
  17. self.assertEqual(d.keys(), [])
  18. d = {'a': 1, 'b': 2}
  19. k = d.keys()
  20. self.assertEqual(set(k), {'a', 'b'})
  21. self.assertIn('a', k)
  22. self.assertIn('b', k)
  23. self.assertTrue(d.has_key('a'))
  24. self.assertTrue(d.has_key('b'))
  25. self.assertRaises(TypeError, d.keys, None)
  26. def test_values(self):
  27. d = {}
  28. self.assertEqual(d.values(), [])
  29. d = {1:2}
  30. self.assertEqual(d.values(), [2])
  31. self.assertRaises(TypeError, d.values, None)
  32. def test_items(self):
  33. d = {}
  34. self.assertEqual(d.items(), [])
  35. d = {1:2}
  36. self.assertEqual(d.items(), [(1, 2)])
  37. self.assertRaises(TypeError, d.items, None)
  38. def test_has_key(self):
  39. d = {}
  40. self.assertFalse(d.has_key('a'))
  41. d = {'a': 1, 'b': 2}
  42. k = d.keys()
  43. k.sort()
  44. self.assertEqual(k, ['a', 'b'])
  45. self.assertRaises(TypeError, d.has_key)
  46. def test_contains(self):
  47. d = {}
  48. self.assertNotIn('a', d)
  49. self.assertFalse('a' in d)
  50. self.assertTrue('a' not in d)
  51. d = {'a': 1, 'b': 2}
  52. self.assertIn('a', d)
  53. self.assertIn('b', d)
  54. self.assertNotIn('c', d)
  55. '''
  56. The direct call of __contains__ on a dict is currently not working
  57. in skulpt
  58. '''
  59. self.assertRaises(TypeError, d.__contains__)
  60. def test_len(self):
  61. d = {}
  62. self.assertEqual(len(d), 0)
  63. d = {'a': 1, 'b': 2}
  64. self.assertEqual(len(d), 2)
  65. def test_clear(self):
  66. d = {1:1, 2:2, 3:3}
  67. d.clear()
  68. self.assertEqual(d, {})
  69. self.assertRaises(TypeError, d.clear, None)
  70. def test_update(self):
  71. d = {}
  72. d.update({1:100})
  73. d.update({2:20})
  74. d.update({1:1, 2:2, 3:3})
  75. self.assertEqual(d, {1:1, 2:2, 3:3})
  76. d.update()
  77. self.assertEqual(d, {1:1, 2:2, 3:3})
  78. self.assertRaises(TypeError, d.update, None)
  79. class SimpleUserDict:
  80. def __init__(self):
  81. self.d = {1:1, 2:2, 3:3}
  82. def keys(self):
  83. return self.d.keys()
  84. def __getitem__(self, i):
  85. return self.d[i]
  86. d.clear()
  87. d.update(SimpleUserDict())
  88. self.assertEqual(d, {1:1, 2:2, 3:3})
  89. d.clear()
  90. class FailingUserDict:
  91. def keys(self):
  92. raise KeyError
  93. self.assertRaises(KeyError, d.update, FailingUserDict())
  94. # skulpt seems not to support this kind of behavior
  95. # especially defining this inner iter
  96. # hence test is disabled
  97. class FailingUserDict:
  98. def keys(self):
  99. class BogonIter:
  100. def __init__(self):
  101. self.i = 1
  102. def __iter__(self):
  103. return self
  104. def next(self):
  105. if self.i:
  106. self.i = 0
  107. return 'a'
  108. raise Exception
  109. return BogonIter()
  110. def __getitem__(self, key):
  111. return key
  112. self.assertRaises(Exception, d.update, FailingUserDict())
  113. class FailingUserDict:
  114. def keys(self):
  115. class BogonIter:
  116. def __init__(self):
  117. self.i = ord('a')
  118. def __iter__(self):
  119. return self
  120. def next(self):
  121. if self.i <= ord('z'):
  122. rtn = chr(self.i)
  123. self.i += 1
  124. return rtn
  125. raise StopIteration
  126. return BogonIter()
  127. def __getitem__(self, key):
  128. raise Exception
  129. self.assertRaises(Exception, d.update, FailingUserDict())
  130. class badseq(object):
  131. def __iter__(self):
  132. return self
  133. def next(self):
  134. raise Exception()
  135. self.assertRaises(Exception, {}.update, badseq())
  136. self.assertRaises(ValueError, {}.update, [(1, 2, 3)])
  137. def test_repr(self):
  138. d = {}
  139. self.assertEqual(repr(d), '{}')
  140. d[1] = 2
  141. self.assertEqual(repr(d), '{1: 2}')
  142. d = {}
  143. d[1] = d
  144. self.assertEqual(repr(d), '{1: {...}}')
  145. # we cannot subclass Exceptions -.-
  146. '''
  147. class Exc(Exception): pass
  148. class BadRepr(object):
  149. def __repr__(self):
  150. raise Exc()
  151. d = {1: BadRepr()}
  152. #self.assertRaises(Exc, repr, d)
  153. '''
  154. def test_get(self):
  155. d = {}
  156. self.assertIs(d.get('c'), None)
  157. self.assertEqual(d.get('c', 3), 3)
  158. d = {'a': 1, 'b': 2}
  159. self.assertIs(d.get('c'), None)
  160. self.assertEqual(d.get('c', 3), 3)
  161. self.assertEqual(d.get('a'), 1)
  162. self.assertEqual(d.get('a', 3), 1)
  163. self.assertRaises(TypeError, d.get)
  164. self.assertRaises(TypeError, d.get, None, None, None)
  165. def test_attrib(self):
  166. d = {}
  167. def do_set():
  168. d.x = 42
  169. self.assertRaises(AttributeError, do_set)
  170. self.assertRaises(AttributeError, lambda: d.x)
  171. if __name__ == '__main__':
  172. unittest.main()