util.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright (C) Jean-Paul Calderone
  2. # Copyright (C) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Helpers for the OpenSSL test suite, largely copied from
  6. U{Twisted<http://twistedmatrix.com/>}.
  7. """
  8. from six import PY2
  9. # This is the UTF-8 encoding of the SNOWMAN unicode code point.
  10. NON_ASCII = b"\xe2\x98\x83".decode("utf-8")
  11. def is_consistent_type(theType, name, *constructionArgs):
  12. """
  13. Perform various assertions about *theType* to ensure that it is a
  14. well-defined type. This is useful for extension types, where it's
  15. pretty easy to do something wacky. If something about the type is
  16. unusual, an exception will be raised.
  17. :param theType: The type object about which to make assertions.
  18. :param name: A string giving the name of the type.
  19. :param constructionArgs: Positional arguments to use with
  20. *theType* to create an instance of it.
  21. """
  22. assert theType.__name__ == name
  23. assert isinstance(theType, type)
  24. instance = theType(*constructionArgs)
  25. assert type(instance) is theType
  26. return True
  27. class EqualityTestsMixin(object):
  28. """
  29. A mixin defining tests for the standard implementation of C{==} and C{!=}.
  30. """
  31. def anInstance(self):
  32. """
  33. Return an instance of the class under test. Each call to this method
  34. must return a different object. All objects returned must be equal to
  35. each other.
  36. """
  37. raise NotImplementedError()
  38. def anotherInstance(self):
  39. """
  40. Return an instance of the class under test. Each call to this method
  41. must return a different object. The objects must not be equal to the
  42. objects returned by C{anInstance}. They may or may not be equal to
  43. each other (they will not be compared against each other).
  44. """
  45. raise NotImplementedError()
  46. def test_identicalEq(self):
  47. """
  48. An object compares equal to itself using the C{==} operator.
  49. """
  50. o = self.anInstance()
  51. assert o == o
  52. def test_identicalNe(self):
  53. """
  54. An object doesn't compare not equal to itself using the C{!=} operator.
  55. """
  56. o = self.anInstance()
  57. assert not (o != o)
  58. def test_sameEq(self):
  59. """
  60. Two objects that are equal to each other compare equal to each other
  61. using the C{==} operator.
  62. """
  63. a = self.anInstance()
  64. b = self.anInstance()
  65. assert a == b
  66. def test_sameNe(self):
  67. """
  68. Two objects that are equal to each other do not compare not equal to
  69. each other using the C{!=} operator.
  70. """
  71. a = self.anInstance()
  72. b = self.anInstance()
  73. assert not (a != b)
  74. def test_differentEq(self):
  75. """
  76. Two objects that are not equal to each other do not compare equal to
  77. each other using the C{==} operator.
  78. """
  79. a = self.anInstance()
  80. b = self.anotherInstance()
  81. assert not (a == b)
  82. def test_differentNe(self):
  83. """
  84. Two objects that are not equal to each other compare not equal to each
  85. other using the C{!=} operator.
  86. """
  87. a = self.anInstance()
  88. b = self.anotherInstance()
  89. assert a != b
  90. def test_anotherTypeEq(self):
  91. """
  92. The object does not compare equal to an object of an unrelated type
  93. (which does not implement the comparison) using the C{==} operator.
  94. """
  95. a = self.anInstance()
  96. b = object()
  97. assert not (a == b)
  98. def test_anotherTypeNe(self):
  99. """
  100. The object compares not equal to an object of an unrelated type (which
  101. does not implement the comparison) using the C{!=} operator.
  102. """
  103. a = self.anInstance()
  104. b = object()
  105. assert a != b
  106. def test_delegatedEq(self):
  107. """
  108. The result of comparison using C{==} is delegated to the right-hand
  109. operand if it is of an unrelated type.
  110. """
  111. class Delegate(object):
  112. def __eq__(self, other):
  113. # Do something crazy and obvious.
  114. return [self]
  115. a = self.anInstance()
  116. b = Delegate()
  117. assert (a == b) == [b]
  118. def test_delegateNe(self):
  119. """
  120. The result of comparison using C{!=} is delegated to the right-hand
  121. operand if it is of an unrelated type.
  122. """
  123. class Delegate(object):
  124. def __ne__(self, other):
  125. # Do something crazy and obvious.
  126. return [self]
  127. a = self.anInstance()
  128. b = Delegate()
  129. assert (a != b) == [b]
  130. # The type name expected in warnings about using the wrong string type.
  131. if PY2:
  132. WARNING_TYPE_EXPECTED = "unicode"
  133. else:
  134. WARNING_TYPE_EXPECTED = "str"