1# 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. 4 5import abc 6 7import pytest 8 9import six 10 11from cryptography.utils import ( 12 InterfaceNotImplemented, 13 register_interface_if, 14 verify_interface, 15) 16 17 18def test_register_interface_if_true(): 19 @six.add_metaclass(abc.ABCMeta) 20 class SimpleInterface(object): 21 pass 22 23 @register_interface_if(1 == 1, SimpleInterface) 24 class SimpleClass(object): 25 pass 26 27 assert issubclass(SimpleClass, SimpleInterface) is True 28 29 30def test_register_interface_if_false(): 31 @six.add_metaclass(abc.ABCMeta) 32 class SimpleInterface(object): 33 pass 34 35 @register_interface_if(1 == 2, SimpleInterface) 36 class SimpleClass(object): 37 pass 38 39 assert issubclass(SimpleClass, SimpleInterface) is False 40 41 42class TestVerifyInterface(object): 43 def test_verify_missing_method(self): 44 @six.add_metaclass(abc.ABCMeta) 45 class SimpleInterface(object): 46 @abc.abstractmethod 47 def method(self): 48 """A simple method""" 49 50 class NonImplementer(object): 51 pass 52 53 with pytest.raises(InterfaceNotImplemented): 54 verify_interface(SimpleInterface, NonImplementer) 55 56 def test_different_arguments(self): 57 @six.add_metaclass(abc.ABCMeta) 58 class SimpleInterface(object): 59 @abc.abstractmethod 60 def method(self, a): 61 """Method with one argument""" 62 63 class NonImplementer(object): 64 def method(self): 65 """Method with no arguments""" 66 67 # Invoke this to ensure the line is covered 68 NonImplementer().method() 69 with pytest.raises(InterfaceNotImplemented): 70 verify_interface(SimpleInterface, NonImplementer) 71 72 def test_handles_abstract_property(self): 73 @six.add_metaclass(abc.ABCMeta) 74 class SimpleInterface(object): 75 @abc.abstractproperty 76 def property(self): 77 """An abstract property""" 78 79 class NonImplementer(object): 80 @property 81 def property(self): 82 """A concrete property""" 83 84 # Invoke this to ensure the line is covered 85 NonImplementer().property 86 verify_interface(SimpleInterface, NonImplementer) 87