Jean-Paul Calderone | 8671c85 | 2011-03-02 19:26:20 -0500 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone |
| 2 | # Copyright (C) Twisted Matrix Laboratories. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 3 | # See LICENSE for details. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 4 | """ |
| 5 | Helpers for the OpenSSL test suite, largely copied from |
| 6 | U{Twisted<http://twistedmatrix.com/>}. |
| 7 | """ |
| 8 | |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 9 | from six import PY3 |
| 10 | |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 11 | |
| 12 | # This is the UTF-8 encoding of the SNOWMAN unicode code point. |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 13 | NON_ASCII = b"\xe2\x98\x83".decode("utf-8") |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 14 | |
| 15 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 16 | def 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 Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 33 | |
| 34 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 35 | class EqualityTestsMixin(object): |
| 36 | """ |
| 37 | A mixin defining tests for the standard implementation of C{==} and C{!=}. |
| 38 | """ |
Alex Gaynor | c88f628 | 2015-09-05 15:32:39 -0400 | [diff] [blame] | 39 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 40 | 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 Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 48 | 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 Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 57 | def test_identicalEq(self): |
| 58 | """ |
| 59 | An object compares equal to itself using the C{==} operator. |
| 60 | """ |
| 61 | o = self.anInstance() |
Alex Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 62 | assert (o == o) |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 63 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 64 | 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 Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 69 | assert not (o != o) |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 70 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 71 | 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 Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 78 | assert (a == b) |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 79 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 80 | 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 Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 87 | assert not (a != b) |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 88 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 89 | 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 Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 96 | assert not (a == b) |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 97 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 98 | 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 Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 105 | assert (a != b) |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 106 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 107 | 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 Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 114 | assert not (a == b) |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 115 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 116 | 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 Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 123 | assert (a != b) |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 124 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 125 | 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 Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 137 | assert (a == b) == [b] |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 138 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 139 | 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 Chan | 63ef9bc | 2016-12-19 12:02:06 +0000 | [diff] [blame] | 151 | assert (a != b) == [b] |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 152 | |
| 153 | |
| 154 | # The type name expected in warnings about using the wrong string type. |
| 155 | if PY3: |
| 156 | WARNING_TYPE_EXPECTED = "str" |
| 157 | else: |
| 158 | WARNING_TYPE_EXPECTED = "unicode" |