blob: bdb4a94d2ac8295301509d73de05d3431a03114c [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
Alex Gaynorcc04d672015-07-02 00:06:18 -040039 # Invoke this to ensure the line is covered
Alex Gaynorbe28a242015-07-02 00:05:49 -040040 NonImplementer().method()
Alex Gaynor87112402014-10-21 10:56:33 -070041 with pytest.raises(InterfaceNotImplemented):
42 verify_interface(SimpleInterface, NonImplementer)
Alex Gaynor15dde272014-10-21 11:41:53 -070043
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 Gaynor67a4dd12014-10-21 23:57:04 -070049 """An abstract property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070050
Alex Gaynor15dde272014-10-21 11:41:53 -070051 class NonImplementer(object):
52 @property
53 def property(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070054 """A concrete property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070055
Alex Gaynorcc04d672015-07-02 00:06:18 -040056 # Invoke this to ensure the line is covered
Alex Gaynorbe28a242015-07-02 00:05:49 -040057 NonImplementer().property
Alex Gaynor15dde272014-10-21 11:41:53 -070058 verify_interface(SimpleInterface, NonImplementer)