Alex Gaynor | f6d8ccc | 2014-10-21 11:03:31 -0700 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | # you may not use this file except in compliance with the License. |
| 3 | # You may obtain a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 10 | # implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 14 | import abc |
| 15 | |
| 16 | import pytest |
| 17 | |
| 18 | import six |
| 19 | |
| 20 | from cryptography.utils import ( |
| 21 | InterfaceNotImplemented, register_interface, verify_interface |
| 22 | ) |
| 23 | |
| 24 | |
| 25 | class TestVerifyInterface(object): |
| 26 | def test_verify_missing_method(self): |
| 27 | @six.add_metaclass(abc.ABCMeta) |
| 28 | class SimpleInterface(object): |
| 29 | @abc.abstractmethod |
| 30 | def method(self): |
| 31 | pass |
| 32 | |
| 33 | @register_interface(SimpleInterface) |
| 34 | class NonImplementer(object): |
| 35 | pass |
| 36 | |
| 37 | with pytest.raises(InterfaceNotImplemented): |
| 38 | verify_interface(SimpleInterface, NonImplementer) |
| 39 | |
| 40 | def test_different_arguments(self): |
| 41 | @six.add_metaclass(abc.ABCMeta) |
| 42 | class SimpleInterface(object): |
| 43 | @abc.abstractmethod |
| 44 | def method(self, a): |
| 45 | pass |
| 46 | |
| 47 | @register_interface(SimpleInterface) |
| 48 | class NonImplementer(object): |
| 49 | def method(self): |
| 50 | pass |
| 51 | |
| 52 | with pytest.raises(InterfaceNotImplemented): |
| 53 | verify_interface(SimpleInterface, NonImplementer) |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame^] | 54 | |
| 55 | def test_handles_abstract_property(self): |
| 56 | @six.add_metaclass(abc.ABCMeta) |
| 57 | class SimpleInterface(object): |
| 58 | @abc.abstractproperty |
| 59 | def property(self): |
| 60 | pass |
| 61 | |
| 62 | @register_interface(SimpleInterface) |
| 63 | class NonImplementer(object): |
| 64 | @property |
| 65 | def property(self): |
| 66 | pass |
| 67 | |
| 68 | verify_interface(SimpleInterface, NonImplementer) |