Alex Gaynor | 5951f46 | 2014-11-16 09:08:42 -0800 | [diff] [blame] | 1 | # This file is dual licensed under the terms of the Apache License, Version |
| 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | # for complete details. |
Alex Gaynor | f6d8ccc | 2014-10-21 11:03:31 -0700 | [diff] [blame] | 4 | |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 5 | import abc |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | import six |
| 10 | |
Alex Gaynor | be2eec7 | 2014-10-28 08:32:26 -0700 | [diff] [blame] | 11 | from cryptography.utils import InterfaceNotImplemented, verify_interface |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 12 | |
| 13 | |
| 14 | class TestVerifyInterface(object): |
| 15 | def test_verify_missing_method(self): |
| 16 | @six.add_metaclass(abc.ABCMeta) |
| 17 | class SimpleInterface(object): |
| 18 | @abc.abstractmethod |
| 19 | def method(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 20 | """A simple method""" |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 21 | |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 22 | class NonImplementer(object): |
| 23 | pass |
| 24 | |
| 25 | with pytest.raises(InterfaceNotImplemented): |
| 26 | verify_interface(SimpleInterface, NonImplementer) |
| 27 | |
| 28 | def test_different_arguments(self): |
| 29 | @six.add_metaclass(abc.ABCMeta) |
| 30 | class SimpleInterface(object): |
| 31 | @abc.abstractmethod |
| 32 | def method(self, a): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 33 | """Method with one argument""" |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 34 | |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 35 | class NonImplementer(object): |
| 36 | def method(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 37 | """Method with no arguments""" |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 38 | |
| 39 | with pytest.raises(InterfaceNotImplemented): |
| 40 | verify_interface(SimpleInterface, NonImplementer) |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 41 | |
| 42 | def test_handles_abstract_property(self): |
| 43 | @six.add_metaclass(abc.ABCMeta) |
| 44 | class SimpleInterface(object): |
| 45 | @abc.abstractproperty |
| 46 | def property(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 47 | """An abstract property""" |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 48 | |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 49 | class NonImplementer(object): |
| 50 | @property |
| 51 | def property(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 52 | """A concrete property""" |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 53 | |
| 54 | verify_interface(SimpleInterface, NonImplementer) |