pattern: ported to pytest (#4015)

This commit is contained in:
Massimiliano Culpo 2017-04-27 16:45:04 +02:00 committed by Todd Gamblin
parent 91b32f67cc
commit a6986312ba

View file

@ -23,85 +23,88 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import unittest
import pytest
import spack.util.pattern as pattern
class CompositeTest(unittest.TestCase):
@pytest.fixture()
def interface():
"""Returns the interface class for the composite."""
class Base:
counter = 0
def setUp(self):
class Base:
counter = 0
def add(self):
raise NotImplemented('add not implemented')
def subtract(self):
raise NotImplemented('subtract not implemented')
return Base
@pytest.fixture()
def implementation(interface):
"""Returns an implementation of the interface"""
class Implementation(interface):
def __init__(self, value):
self.value = value
def add(self):
raise NotImplemented('add not implemented')
interface.counter += self.value
def subtract(self):
raise NotImplemented('subtract not implemented')
interface.counter -= self.value
class One(Base):
return Implementation
def add(self):
Base.counter += 1
def subtract(self):
Base.counter -= 1
class Two(Base):
def add(self):
Base.counter += 2
def subtract(self):
Base.counter -= 2
self.Base = Base
self.One = One
self.Two = Two
def test_composite_from_method_list(self):
@pattern.composite(method_list=['add', 'subtract'])
class CompositeFromMethodList:
@pytest.fixture(params=[
'interface',
'method_list'
])
def composite(interface, implementation, request):
"""Returns a composite that contains an instance of `implementation(1)`
and one of `implementation(2)`.
"""
if request.param == 'interface':
@pattern.composite(interface=interface)
class Composite:
pass
composite = CompositeFromMethodList()
composite.append(self.One())
composite.append(self.Two())
composite.add()
self.assertEqual(self.Base.counter, 3)
composite.pop()
composite.subtract()
self.assertEqual(self.Base.counter, 2)
else:
@pattern.composite(method_list=['add', 'subtract'])
class Composite:
pass
def test_composite_from_interface(self):
c = Composite()
c.append(implementation(1))
c.append(implementation(2))
@pattern.composite(interface=self.Base)
return c
def test_composite_interface_calls(interface, composite):
composite.add()
assert interface.counter == 3
composite.pop()
composite.subtract()
assert interface.counter == 2
def test_composite_wrong_container(interface):
with pytest.raises(TypeError):
@pattern.composite(interface=interface, container=2)
class CompositeFromInterface:
pass
composite = CompositeFromInterface()
composite.append(self.One())
composite.append(self.Two())
composite.add()
self.assertEqual(self.Base.counter, 3)
composite.pop()
composite.subtract()
self.assertEqual(self.Base.counter, 2)
def test_composite_no_methods():
def test_error_conditions(self):
def wrong_container():
@pattern.composite(interface=self.Base, container=2)
class CompositeFromInterface:
pass
def no_methods():
@pattern.composite()
class CompositeFromInterface:
pass
self.assertRaises(TypeError, wrong_container)
self.assertRaises(TypeError, no_methods)
with pytest.raises(TypeError):
@pattern.composite()
class CompositeFromInterface:
pass