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 | |
Alex Gaynor | cc04d67 | 2015-07-02 00:06:18 -0400 | [diff] [blame^] | 39 | # Invoke this to ensure the line is covered |
Alex Gaynor | be28a24 | 2015-07-02 00:05:49 -0400 | [diff] [blame] | 40 | NonImplementer().method() |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 41 | with pytest.raises(InterfaceNotImplemented): |
| 42 | verify_interface(SimpleInterface, NonImplementer) |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 43 | |
| 44 | def test_handles_abstract_property(self): |
| 45 | @six.add_metaclass(abc.ABCMeta) |
| 46 | class SimpleInterface(object): |
| 47 | @abc.abstractproperty |
| 48 | def property(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 49 | """An abstract property""" |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 50 | |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 51 | class NonImplementer(object): |
| 52 | @property |
| 53 | def property(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 54 | """A concrete property""" |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 55 | |
Alex Gaynor | cc04d67 | 2015-07-02 00:06:18 -0400 | [diff] [blame^] | 56 | # Invoke this to ensure the line is covered |
Alex Gaynor | be28a24 | 2015-07-02 00:05:49 -0400 | [diff] [blame] | 57 | NonImplementer().property |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 58 | verify_interface(SimpleInterface, NonImplementer) |