You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basemapping.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2001, 2002 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """Base Mapping tests
  15. """
  16. from operator import __getitem__
  17. def testIReadMapping(self, inst, state, absent):
  18. for key in state:
  19. self.assertEqual(inst[key], state[key])
  20. self.assertEqual(inst.get(key, None), state[key])
  21. self.assertTrue(key in inst)
  22. for key in absent:
  23. self.assertEqual(inst.get(key, None), None)
  24. self.assertEqual(inst.get(key), None)
  25. self.assertEqual(inst.get(key, self), self)
  26. self.assertRaises(KeyError, __getitem__, inst, key)
  27. def test_keys(self, inst, state):
  28. # Return the keys of the mapping object
  29. inst_keys = list(inst.keys()); inst_keys.sort()
  30. state_keys = list(state.keys()) ; state_keys.sort()
  31. self.assertEqual(inst_keys, state_keys)
  32. def test_iter(self, inst, state):
  33. # Return the keys of the mapping object
  34. inst_keys = list(inst); inst_keys.sort()
  35. state_keys = list(state.keys()) ; state_keys.sort()
  36. self.assertEqual(inst_keys, state_keys)
  37. def test_values(self, inst, state):
  38. # Return the values of the mapping object
  39. inst_values = list(inst.values()); inst_values.sort()
  40. state_values = list(state.values()) ; state_values.sort()
  41. self.assertEqual(inst_values, state_values)
  42. def test_items(self, inst, state):
  43. # Return the items of the mapping object
  44. inst_items = list(inst.items()); inst_items.sort()
  45. state_items = list(state.items()) ; state_items.sort()
  46. self.assertEqual(inst_items, state_items)
  47. def test___len__(self, inst, state):
  48. # Return the number of items
  49. self.assertEqual(len(inst), len(state))
  50. def testIEnumerableMapping(self, inst, state):
  51. test_keys(self, inst, state)
  52. test_items(self, inst, state)
  53. test_values(self, inst, state)
  54. test___len__(self, inst, state)
  55. class BaseTestIReadMapping(object):
  56. def testIReadMapping(self):
  57. inst = self._IReadMapping__sample()
  58. state = self._IReadMapping__stateDict()
  59. absent = self._IReadMapping__absentKeys()
  60. testIReadMapping(self, inst, state, absent)
  61. class BaseTestIEnumerableMapping(BaseTestIReadMapping):
  62. # Mapping objects whose items can be enumerated
  63. def test_keys(self):
  64. # Return the keys of the mapping object
  65. inst = self._IEnumerableMapping__sample()
  66. state = self._IEnumerableMapping__stateDict()
  67. test_keys(self, inst, state)
  68. def test_values(self):
  69. # Return the values of the mapping object
  70. inst = self._IEnumerableMapping__sample()
  71. state = self._IEnumerableMapping__stateDict()
  72. test_values(self, inst, state)
  73. def test_items(self):
  74. # Return the items of the mapping object
  75. inst = self._IEnumerableMapping__sample()
  76. state = self._IEnumerableMapping__stateDict()
  77. test_items(self, inst, state)
  78. def test___len__(self):
  79. # Return the number of items
  80. inst = self._IEnumerableMapping__sample()
  81. state = self._IEnumerableMapping__stateDict()
  82. test___len__(self, inst, state)
  83. def _IReadMapping__stateDict(self):
  84. return self._IEnumerableMapping__stateDict()
  85. def _IReadMapping__sample(self):
  86. return self._IEnumerableMapping__sample()
  87. def _IReadMapping__absentKeys(self):
  88. return self._IEnumerableMapping__absentKeys()