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