diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/test/pattern.py | 117 |
1 files changed, 60 insertions, 57 deletions
diff --git a/lib/spack/spack/test/pattern.py b/lib/spack/spack/test/pattern.py index b76f88e670..bd75206563 100644 --- a/lib/spack/spack/test/pattern.py +++ b/lib/spack/spack/test/pattern.py @@ -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 add(self): - raise NotImplemented('add not implemented') + def subtract(self): + raise NotImplemented('subtract not implemented') - def subtract(self): - raise NotImplemented('subtract not implemented') + return Base - class One(Base): - def add(self): - Base.counter += 1 +@pytest.fixture() +def implementation(interface): + """Returns an implementation of the interface""" + class Implementation(interface): - def subtract(self): - Base.counter -= 1 - - class Two(Base): + def __init__(self, value): + self.value = value def add(self): - Base.counter += 2 + interface.counter += self.value def subtract(self): - Base.counter -= 2 - - self.Base = Base - self.One = One - self.Two = Two - - def test_composite_from_method_list(self): + interface.counter -= self.value + + return Implementation + + +@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 + else: @pattern.composite(method_list=['add', 'subtract']) - class CompositeFromMethodList: + 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) + c = Composite() + c.append(implementation(1)) + c.append(implementation(2)) - def test_composite_from_interface(self): + return c - @pattern.composite(interface=self.Base) - class CompositeFromInterface: - pass - composite = CompositeFromInterface() - composite.append(self.One()) - composite.append(self.Two()) - composite.add() - self.assertEqual(self.Base.counter, 3) +def test_composite_interface_calls(interface, composite): + + composite.add() + assert interface.counter == 3 - composite.pop() - composite.subtract() - self.assertEqual(self.Base.counter, 2) + composite.pop() + composite.subtract() + assert interface.counter == 2 - def test_error_conditions(self): - def wrong_container(): - @pattern.composite(interface=self.Base, container=2) - class CompositeFromInterface: - pass +def test_composite_wrong_container(interface): - def no_methods(): - @pattern.composite() - class CompositeFromInterface: - pass + with pytest.raises(TypeError): + @pattern.composite(interface=interface, container=2) + class CompositeFromInterface: + pass - self.assertRaises(TypeError, wrong_container) - self.assertRaises(TypeError, no_methods) + +def test_composite_no_methods(): + + with pytest.raises(TypeError): + @pattern.composite() + class CompositeFromInterface: + pass |