blob: 4d571ea6ab0708d257036d0203578a9f05f0a26f [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
Alex Gaynorbe2eec72014-10-28 08:32:26 -070011from cryptography.utils import InterfaceNotImplemented, verify_interface
Alex Gaynor87112402014-10-21 10:56:33 -070012
13
14class 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 Gaynor67a4dd12014-10-21 23:57:04 -070020 """A simple method"""
Alex Gaynor87112402014-10-21 10:56:33 -070021
Alex Gaynor87112402014-10-21 10:56:33 -070022 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 Gaynor67a4dd12014-10-21 23:57:04 -070033 """Method with one argument"""
Alex Gaynor87112402014-10-21 10:56:33 -070034
Alex Gaynor87112402014-10-21 10:56:33 -070035 class NonImplementer(object):
36 def method(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070037 """Method with no arguments"""
Alex Gaynor87112402014-10-21 10:56:33 -070038
39 with pytest.raises(InterfaceNotImplemented):
40 verify_interface(SimpleInterface, NonImplementer)
Alex Gaynor15dde272014-10-21 11:41:53 -070041
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 Gaynor67a4dd12014-10-21 23:57:04 -070047 """An abstract property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070048
Alex Gaynor15dde272014-10-21 11:41:53 -070049 class NonImplementer(object):
50 @property
51 def property(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070052 """A concrete property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070053
54 verify_interface(SimpleInterface, NonImplementer)