blob: b988abee8febd7f6ed66652dc230c51e86ef2637 [file] [log] [blame]
Alex Gaynorf6d8ccc2014-10-21 11:03:31 -07001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
Alex Gaynor87112402014-10-21 10:56:33 -070014import abc
15
16import pytest
17
18import six
19
Alex Gaynorbe2eec72014-10-28 08:32:26 -070020from cryptography.utils import InterfaceNotImplemented, verify_interface
Alex Gaynor87112402014-10-21 10:56:33 -070021
22
23class TestVerifyInterface(object):
24 def test_verify_missing_method(self):
25 @six.add_metaclass(abc.ABCMeta)
26 class SimpleInterface(object):
27 @abc.abstractmethod
28 def method(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070029 """A simple method"""
Alex Gaynor87112402014-10-21 10:56:33 -070030
Alex Gaynor87112402014-10-21 10:56:33 -070031 class NonImplementer(object):
32 pass
33
34 with pytest.raises(InterfaceNotImplemented):
35 verify_interface(SimpleInterface, NonImplementer)
36
37 def test_different_arguments(self):
38 @six.add_metaclass(abc.ABCMeta)
39 class SimpleInterface(object):
40 @abc.abstractmethod
41 def method(self, a):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070042 """Method with one argument"""
Alex Gaynor87112402014-10-21 10:56:33 -070043
Alex Gaynor87112402014-10-21 10:56:33 -070044 class NonImplementer(object):
45 def method(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070046 """Method with no arguments"""
Alex Gaynor87112402014-10-21 10:56:33 -070047
48 with pytest.raises(InterfaceNotImplemented):
49 verify_interface(SimpleInterface, NonImplementer)
Alex Gaynor15dde272014-10-21 11:41:53 -070050
51 def test_handles_abstract_property(self):
52 @six.add_metaclass(abc.ABCMeta)
53 class SimpleInterface(object):
54 @abc.abstractproperty
55 def property(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070056 """An abstract property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070057
Alex Gaynor15dde272014-10-21 11:41:53 -070058 class NonImplementer(object):
59 @property
60 def property(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070061 """A concrete property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070062
63 verify_interface(SimpleInterface, NonImplementer)