2014-11-16 17:08:42 +00:00
|
|
|
# This file is dual licensed under the terms of the Apache License, Version
|
|
|
|
|
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
|
|
|
|
|
# for complete details.
|
2014-10-21 18:03:31 +00:00
|
|
|
|
2014-10-21 17:56:33 +00:00
|
|
|
import abc
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
import six
|
|
|
|
|
|
2016-08-29 12:21:28 +00:00
|
|
|
from cryptography.utils import (
|
2020-07-20 18:06:29 +00:00
|
|
|
InterfaceNotImplemented,
|
|
|
|
|
register_interface_if,
|
|
|
|
|
verify_interface,
|
2016-08-29 12:21:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_interface_if_true():
|
|
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
|
|
|
class SimpleInterface(object):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@register_interface_if(1 == 1, SimpleInterface)
|
|
|
|
|
class SimpleClass(object):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
assert issubclass(SimpleClass, SimpleInterface) is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_interface_if_false():
|
|
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
|
|
|
class SimpleInterface(object):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@register_interface_if(1 == 2, SimpleInterface)
|
|
|
|
|
class SimpleClass(object):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
assert issubclass(SimpleClass, SimpleInterface) is False
|
2014-10-21 17:56:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestVerifyInterface(object):
|
|
|
|
|
def test_verify_missing_method(self):
|
|
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
|
|
|
class SimpleInterface(object):
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def method(self):
|
2014-10-22 06:57:04 +00:00
|
|
|
"""A simple method"""
|
2014-10-21 17:56:33 +00:00
|
|
|
|
|
|
|
|
class NonImplementer(object):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
with pytest.raises(InterfaceNotImplemented):
|
|
|
|
|
verify_interface(SimpleInterface, NonImplementer)
|
|
|
|
|
|
|
|
|
|
def test_different_arguments(self):
|
|
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
|
|
|
class SimpleInterface(object):
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def method(self, a):
|
2014-10-22 06:57:04 +00:00
|
|
|
"""Method with one argument"""
|
2014-10-21 17:56:33 +00:00
|
|
|
|
|
|
|
|
class NonImplementer(object):
|
|
|
|
|
def method(self):
|
2014-10-22 06:57:04 +00:00
|
|
|
"""Method with no arguments"""
|
2014-10-21 17:56:33 +00:00
|
|
|
|
2015-07-02 04:06:18 +00:00
|
|
|
# Invoke this to ensure the line is covered
|
2015-07-02 04:05:49 +00:00
|
|
|
NonImplementer().method()
|
2014-10-21 17:56:33 +00:00
|
|
|
with pytest.raises(InterfaceNotImplemented):
|
|
|
|
|
verify_interface(SimpleInterface, NonImplementer)
|
2014-10-21 18:41:53 +00:00
|
|
|
|
|
|
|
|
def test_handles_abstract_property(self):
|
|
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
|
|
|
class SimpleInterface(object):
|
|
|
|
|
@abc.abstractproperty
|
|
|
|
|
def property(self):
|
2014-10-22 06:57:04 +00:00
|
|
|
"""An abstract property"""
|
2014-10-21 18:41:53 +00:00
|
|
|
|
|
|
|
|
class NonImplementer(object):
|
|
|
|
|
@property
|
|
|
|
|
def property(self):
|
2014-10-22 06:57:04 +00:00
|
|
|
"""A concrete property"""
|
2014-10-21 18:41:53 +00:00
|
|
|
|
2015-07-02 04:06:18 +00:00
|
|
|
# Invoke this to ensure the line is covered
|
2015-07-02 04:05:49 +00:00
|
|
|
NonImplementer().property
|
2014-10-21 18:41:53 +00:00
|
|
|
verify_interface(SimpleInterface, NonImplementer)
|