blob: 0c72ad335e9253f6a846d9c44a80680943a4467d [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
20from cryptography.utils import (
21 InterfaceNotImplemented, register_interface, verify_interface
22)
23
24
25class TestVerifyInterface(object):
26 def test_verify_missing_method(self):
27 @six.add_metaclass(abc.ABCMeta)
28 class SimpleInterface(object):
29 @abc.abstractmethod
30 def method(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070031 """A simple method"""
Alex Gaynor87112402014-10-21 10:56:33 -070032
33 @register_interface(SimpleInterface)
34 class NonImplementer(object):
35 pass
36
37 with pytest.raises(InterfaceNotImplemented):
38 verify_interface(SimpleInterface, NonImplementer)
39
40 def test_different_arguments(self):
41 @six.add_metaclass(abc.ABCMeta)
42 class SimpleInterface(object):
43 @abc.abstractmethod
44 def method(self, a):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070045 """Method with one argument"""
Alex Gaynor87112402014-10-21 10:56:33 -070046
47 @register_interface(SimpleInterface)
48 class NonImplementer(object):
49 def method(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070050 """Method with no arguments"""
Alex Gaynor87112402014-10-21 10:56:33 -070051
52 with pytest.raises(InterfaceNotImplemented):
53 verify_interface(SimpleInterface, NonImplementer)
Alex Gaynor15dde272014-10-21 11:41:53 -070054
55 def test_handles_abstract_property(self):
56 @six.add_metaclass(abc.ABCMeta)
57 class SimpleInterface(object):
58 @abc.abstractproperty
59 def property(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070060 """An abstract property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070061
62 @register_interface(SimpleInterface)
63 class NonImplementer(object):
64 @property
65 def property(self):
Alex Gaynor67a4dd12014-10-21 23:57:04 -070066 """A concrete property"""
Alex Gaynor15dde272014-10-21 11:41:53 -070067
68 verify_interface(SimpleInterface, NonImplementer)