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 | |
Alex Gaynor | be2eec7 | 2014-10-28 08:32:26 -0700 | [diff] [blame^] | 20 | from cryptography.utils import InterfaceNotImplemented, verify_interface |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class TestVerifyInterface(object): |
| 24 | def test_verify_missing_method(self): |
| 25 | @six.add_metaclass(abc.ABCMeta) |
| 26 | class SimpleInterface(object): |
| 27 | @abc.abstractmethod |
| 28 | def method(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 29 | """A simple method""" |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 30 | |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 31 | class NonImplementer(object): |
| 32 | pass |
| 33 | |
| 34 | with pytest.raises(InterfaceNotImplemented): |
| 35 | verify_interface(SimpleInterface, NonImplementer) |
| 36 | |
| 37 | def test_different_arguments(self): |
| 38 | @six.add_metaclass(abc.ABCMeta) |
| 39 | class SimpleInterface(object): |
| 40 | @abc.abstractmethod |
| 41 | def method(self, a): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 42 | """Method with one argument""" |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 43 | |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 44 | class NonImplementer(object): |
| 45 | def method(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 46 | """Method with no arguments""" |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 47 | |
| 48 | with pytest.raises(InterfaceNotImplemented): |
| 49 | verify_interface(SimpleInterface, NonImplementer) |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 50 | |
| 51 | def test_handles_abstract_property(self): |
| 52 | @six.add_metaclass(abc.ABCMeta) |
| 53 | class SimpleInterface(object): |
| 54 | @abc.abstractproperty |
| 55 | def property(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 56 | """An abstract property""" |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 57 | |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 58 | class NonImplementer(object): |
| 59 | @property |
| 60 | def property(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 61 | """A concrete property""" |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 62 | |
| 63 | verify_interface(SimpleInterface, NonImplementer) |