blob: 042245f97706ee82d8a96f6c04e1122133ecc4bd [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# 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 Gaynorf6d8ccc2014-10-21 11:03:31 -07004
Alex Gaynor87112402014-10-21 10:56:33 -07005import abc
6
7import pytest
8
9import six
10
Terry Chia3c35ef12016-08-29 20:21:28 +080011from cryptography.utils import (
Lucia Lic6ba99d2021-11-08 22:06:11 +080012 InterfaceNotImplemented,
13 register_interface_if,
14 verify_interface,
Terry Chia3c35ef12016-08-29 20:21:28 +080015)
16
17
18def 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
30def 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 Gaynor87112402014-10-21 10:56:33 -070040
41
42class 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 Gaynor67a4dd12014-10-21 23:57:04 -070048 """A simple method"""
Alex Gaynor87112402014-10-21 10:56:33 -070049
Alex Gaynor87112402014-10-21 10:56:33 -070050 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 Gaynor67a4dd12014-10-21 23:57:04 -070061 """Method with one argument"""
Alex Gaynor87112402014-10-21 10:56:33 -070062
Alex Gaynor87112402014-10-21 10:56:33 -070063 class NonImplementer(object):
64 def method(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070065 """Method with no arguments"""
Alex Gaynor87112402014-10-21 10:56:33 -070066
Alex Gaynorcc04d672015-07-02 00:06:18 -040067 # Invoke this to ensure the line is covered
Alex Gaynorbe28a242015-07-02 00:05:49 -040068 NonImplementer().method()
Alex Gaynor87112402014-10-21 10:56:33 -070069 with pytest.raises(InterfaceNotImplemented):
70 verify_interface(SimpleInterface, NonImplementer)
Alex Gaynor15dde272014-10-21 11:41:53 -070071
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 Gaynor67a4dd12014-10-21 23:57:04 -070077 """An abstract property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070078
Alex Gaynor15dde272014-10-21 11:41:53 -070079 class NonImplementer(object):
80 @property
81 def property(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070082 """A concrete property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070083
Alex Gaynorcc04d672015-07-02 00:06:18 -040084 # Invoke this to ensure the line is covered
Alex Gaynorbe28a242015-07-02 00:05:49 -040085 NonImplementer().property
Alex Gaynor15dde272014-10-21 11:41:53 -070086 verify_interface(SimpleInterface, NonImplementer)