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 | |
Terry Chia | 3c35ef1 | 2016-08-29 20:21:28 +0800 | [diff] [blame] | 11 | from cryptography.utils import ( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 12 | InterfaceNotImplemented, |
| 13 | register_interface_if, |
| 14 | verify_interface, |
Terry Chia | 3c35ef1 | 2016-08-29 20:21:28 +0800 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | |
| 18 | def test_register_interface_if_true(): |
| 19 | @six.add_metaclass(abc.ABCMeta) |
| 20 | class SimpleInterface(object): |
| 21 | pass |
| 22 | |
| 23 | @register_interface_if(1 == 1, SimpleInterface) |
| 24 | class SimpleClass(object): |
| 25 | pass |
| 26 | |
| 27 | assert issubclass(SimpleClass, SimpleInterface) is True |
| 28 | |
| 29 | |
| 30 | def test_register_interface_if_false(): |
| 31 | @six.add_metaclass(abc.ABCMeta) |
| 32 | class SimpleInterface(object): |
| 33 | pass |
| 34 | |
| 35 | @register_interface_if(1 == 2, SimpleInterface) |
| 36 | class SimpleClass(object): |
| 37 | pass |
| 38 | |
| 39 | assert issubclass(SimpleClass, SimpleInterface) is False |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 40 | |
| 41 | |
| 42 | class TestVerifyInterface(object): |
| 43 | def test_verify_missing_method(self): |
| 44 | @six.add_metaclass(abc.ABCMeta) |
| 45 | class SimpleInterface(object): |
| 46 | @abc.abstractmethod |
| 47 | def method(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 48 | """A simple method""" |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 49 | |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 50 | class NonImplementer(object): |
| 51 | pass |
| 52 | |
| 53 | with pytest.raises(InterfaceNotImplemented): |
| 54 | verify_interface(SimpleInterface, NonImplementer) |
| 55 | |
| 56 | def test_different_arguments(self): |
| 57 | @six.add_metaclass(abc.ABCMeta) |
| 58 | class SimpleInterface(object): |
| 59 | @abc.abstractmethod |
| 60 | def method(self, a): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 61 | """Method with one argument""" |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 62 | |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 63 | class NonImplementer(object): |
| 64 | def method(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 65 | """Method with no arguments""" |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 66 | |
Alex Gaynor | cc04d67 | 2015-07-02 00:06:18 -0400 | [diff] [blame] | 67 | # Invoke this to ensure the line is covered |
Alex Gaynor | be28a24 | 2015-07-02 00:05:49 -0400 | [diff] [blame] | 68 | NonImplementer().method() |
Alex Gaynor | 8711240 | 2014-10-21 10:56:33 -0700 | [diff] [blame] | 69 | with pytest.raises(InterfaceNotImplemented): |
| 70 | verify_interface(SimpleInterface, NonImplementer) |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 71 | |
| 72 | def test_handles_abstract_property(self): |
| 73 | @six.add_metaclass(abc.ABCMeta) |
| 74 | class SimpleInterface(object): |
| 75 | @abc.abstractproperty |
| 76 | def property(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 77 | """An abstract property""" |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 78 | |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 79 | class NonImplementer(object): |
| 80 | @property |
| 81 | def property(self): |
Alex Gaynor | 67a4dd1 | 2014-10-21 23:57:04 -0700 | [diff] [blame] | 82 | """A concrete property""" |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 83 | |
Alex Gaynor | cc04d67 | 2015-07-02 00:06:18 -0400 | [diff] [blame] | 84 | # Invoke this to ensure the line is covered |
Alex Gaynor | be28a24 | 2015-07-02 00:05:49 -0400 | [diff] [blame] | 85 | NonImplementer().property |
Alex Gaynor | 15dde27 | 2014-10-21 11:41:53 -0700 | [diff] [blame] | 86 | verify_interface(SimpleInterface, NonImplementer) |