blob: 44643790641211d3c5703f583939a949c6050fa4 [file] [log] [blame]
Jean-Paul Calderone8671c852011-03-02 19:26:20 -05001# Copyright (C) Jean-Paul Calderone
2# Copyright (C) Twisted Matrix Laboratories.
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -04003# See LICENSE for details.
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -04004"""
5Helpers for the OpenSSL test suite, largely copied from
6U{Twisted<http://twistedmatrix.com/>}.
7"""
8
Jean-Paul Calderone6462b072015-03-29 07:03:11 -04009from six import PY3
10
Jean-Paul Calderone210c0f32015-04-12 09:20:31 -040011
12# This is the UTF-8 encoding of the SNOWMAN unicode code point.
Alex Gaynore7f51982016-09-11 11:48:14 -040013NON_ASCII = b"\xe2\x98\x83".decode("utf-8")
Jean-Paul Calderone210c0f32015-04-12 09:20:31 -040014
15
Alex Chanc6077062016-11-18 13:53:39 +000016def is_consistent_type(theType, name, *constructionArgs):
17 """
18 Perform various assertions about *theType* to ensure that it is a
19 well-defined type. This is useful for extension types, where it's
20 pretty easy to do something wacky. If something about the type is
21 unusual, an exception will be raised.
22
23 :param theType: The type object about which to make assertions.
24 :param name: A string giving the name of the type.
25 :param constructionArgs: Positional arguments to use with
26 *theType* to create an instance of it.
27 """
28 assert theType.__name__ == name
29 assert isinstance(theType, type)
30 instance = theType(*constructionArgs)
31 assert type(instance) is theType
32 return True
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040033
34
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040035class EqualityTestsMixin(object):
36 """
37 A mixin defining tests for the standard implementation of C{==} and C{!=}.
38 """
Alex Gaynorc88f6282015-09-05 15:32:39 -040039
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040040 def anInstance(self):
41 """
42 Return an instance of the class under test. Each call to this method
43 must return a different object. All objects returned must be equal to
44 each other.
45 """
46 raise NotImplementedError()
47
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040048 def anotherInstance(self):
49 """
50 Return an instance of the class under test. Each call to this method
51 must return a different object. The objects must not be equal to the
52 objects returned by C{anInstance}. They may or may not be equal to
53 each other (they will not be compared against each other).
54 """
55 raise NotImplementedError()
56
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040057 def test_identicalEq(self):
58 """
59 An object compares equal to itself using the C{==} operator.
60 """
61 o = self.anInstance()
Alex Chan63ef9bc2016-12-19 12:02:06 +000062 assert (o == o)
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040063
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040064 def test_identicalNe(self):
65 """
66 An object doesn't compare not equal to itself using the C{!=} operator.
67 """
68 o = self.anInstance()
Alex Chan63ef9bc2016-12-19 12:02:06 +000069 assert not (o != o)
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040070
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040071 def test_sameEq(self):
72 """
73 Two objects that are equal to each other compare equal to each other
74 using the C{==} operator.
75 """
76 a = self.anInstance()
77 b = self.anInstance()
Alex Chan63ef9bc2016-12-19 12:02:06 +000078 assert (a == b)
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040079
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040080 def test_sameNe(self):
81 """
82 Two objects that are equal to each other do not compare not equal to
83 each other using the C{!=} operator.
84 """
85 a = self.anInstance()
86 b = self.anInstance()
Alex Chan63ef9bc2016-12-19 12:02:06 +000087 assert not (a != b)
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040088
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040089 def test_differentEq(self):
90 """
91 Two objects that are not equal to each other do not compare equal to
92 each other using the C{==} operator.
93 """
94 a = self.anInstance()
95 b = self.anotherInstance()
Alex Chan63ef9bc2016-12-19 12:02:06 +000096 assert not (a == b)
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040097
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -040098 def test_differentNe(self):
99 """
100 Two objects that are not equal to each other compare not equal to each
101 other using the C{!=} operator.
102 """
103 a = self.anInstance()
104 b = self.anotherInstance()
Alex Chan63ef9bc2016-12-19 12:02:06 +0000105 assert (a != b)
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -0400106
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -0400107 def test_anotherTypeEq(self):
108 """
109 The object does not compare equal to an object of an unrelated type
110 (which does not implement the comparison) using the C{==} operator.
111 """
112 a = self.anInstance()
113 b = object()
Alex Chan63ef9bc2016-12-19 12:02:06 +0000114 assert not (a == b)
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -0400115
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -0400116 def test_anotherTypeNe(self):
117 """
118 The object compares not equal to an object of an unrelated type (which
119 does not implement the comparison) using the C{!=} operator.
120 """
121 a = self.anInstance()
122 b = object()
Alex Chan63ef9bc2016-12-19 12:02:06 +0000123 assert (a != b)
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -0400124
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -0400125 def test_delegatedEq(self):
126 """
127 The result of comparison using C{==} is delegated to the right-hand
128 operand if it is of an unrelated type.
129 """
130 class Delegate(object):
131 def __eq__(self, other):
132 # Do something crazy and obvious.
133 return [self]
134
135 a = self.anInstance()
136 b = Delegate()
Alex Chan63ef9bc2016-12-19 12:02:06 +0000137 assert (a == b) == [b]
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -0400138
Jean-Paul Calderone9c7f0692014-04-30 18:17:19 -0400139 def test_delegateNe(self):
140 """
141 The result of comparison using C{!=} is delegated to the right-hand
142 operand if it is of an unrelated type.
143 """
144 class Delegate(object):
145 def __ne__(self, other):
146 # Do something crazy and obvious.
147 return [self]
148
149 a = self.anInstance()
150 b = Delegate()
Alex Chan63ef9bc2016-12-19 12:02:06 +0000151 assert (a != b) == [b]
Jean-Paul Calderone6462b072015-03-29 07:03:11 -0400152
153
154# The type name expected in warnings about using the wrong string type.
155if PY3:
156 WARNING_TYPE_EXPECTED = "str"
157else:
158 WARNING_TYPE_EXPECTED = "unicode"