blob: 97df45a3d892d11c608076d811d5267217f1f86f [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 (
12 InterfaceNotImplemented, register_interface_if, verify_interface
13)
14
15
16def test_register_interface_if_true():
17 @six.add_metaclass(abc.ABCMeta)
18 class SimpleInterface(object):
19 pass
20
21 @register_interface_if(1 == 1, SimpleInterface)
22 class SimpleClass(object):
23 pass
24
25 assert issubclass(SimpleClass, SimpleInterface) is True
26
27
28def test_register_interface_if_false():
29 @six.add_metaclass(abc.ABCMeta)
30 class SimpleInterface(object):
31 pass
32
33 @register_interface_if(1 == 2, SimpleInterface)
34 class SimpleClass(object):
35 pass
36
37 assert issubclass(SimpleClass, SimpleInterface) is False
Alex Gaynor87112402014-10-21 10:56:33 -070038
39
40class TestVerifyInterface(object):
41 def test_verify_missing_method(self):
42 @six.add_metaclass(abc.ABCMeta)
43 class SimpleInterface(object):
44 @abc.abstractmethod
45 def method(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070046 """A simple method"""
Alex Gaynor87112402014-10-21 10:56:33 -070047
Alex Gaynor87112402014-10-21 10:56:33 -070048 class NonImplementer(object):
49 pass
50
51 with pytest.raises(InterfaceNotImplemented):
52 verify_interface(SimpleInterface, NonImplementer)
53
54 def test_different_arguments(self):
55 @six.add_metaclass(abc.ABCMeta)
56 class SimpleInterface(object):
57 @abc.abstractmethod
58 def method(self, a):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070059 """Method with one argument"""
Alex Gaynor87112402014-10-21 10:56:33 -070060
Alex Gaynor87112402014-10-21 10:56:33 -070061 class NonImplementer(object):
62 def method(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070063 """Method with no arguments"""
Alex Gaynor87112402014-10-21 10:56:33 -070064
Alex Gaynorcc04d672015-07-02 00:06:18 -040065 # Invoke this to ensure the line is covered
Alex Gaynorbe28a242015-07-02 00:05:49 -040066 NonImplementer().method()
Alex Gaynor87112402014-10-21 10:56:33 -070067 with pytest.raises(InterfaceNotImplemented):
68 verify_interface(SimpleInterface, NonImplementer)
Alex Gaynor15dde272014-10-21 11:41:53 -070069
70 def test_handles_abstract_property(self):
71 @six.add_metaclass(abc.ABCMeta)
72 class SimpleInterface(object):
73 @abc.abstractproperty
74 def property(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070075 """An abstract property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070076
Alex Gaynor15dde272014-10-21 11:41:53 -070077 class NonImplementer(object):
78 @property
79 def property(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070080 """A concrete property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070081
Alex Gaynorcc04d672015-07-02 00:06:18 -040082 # Invoke this to ensure the line is covered
Alex Gaynorbe28a242015-07-02 00:05:49 -040083 NonImplementer().property
Alex Gaynor15dde272014-10-21 11:41:53 -070084 verify_interface(SimpleInterface, NonImplementer)