Beispiel #1
0
def run():
    """This is an example of how contracts work
    """

    print "*"*30
    # Create a stack which can hold 3 elements
    stack = StackImpl(10)
    from logilab.aspects.weaver import weaver
    from logilab.aspects.lib.logger import LoggerAspect
    import sys
    
    weaver.weave_methods(stack, LoggerAspect, sys.stderr)
    print "Tracing stack's method calls"
    stack.is_empty()
    print "-"*20
    stack.push("un élément")
    print "-"*20
    stack.push("un autre élément")
    print "-"*20
    stack.pop()
    print "*"*30

    print "Unweaving Logger Aspect ..."
    weaver.unweave_methods(stack, LoggerAspect)
    print "Now, calling a stack's method shouldn't be traced"
    stack.push("Un autre élement")
    print "Did it work ?"
Beispiel #2
0
def run():
    """This is an example of how contracts work
    """

    print "*" * 30
    # Create a stack which can hold 3 elements
    stack = StackImpl(10)
    from logilab.aspects.weaver import weaver
    from logilab.aspects.lib.logger import LoggerAspect
    import sys

    weaver.weave_methods(stack, LoggerAspect, sys.stderr)
    print "Tracing stack's method calls"
    stack.is_empty()
    print "-" * 20
    stack.push("un élément")
    print "-" * 20
    stack.push("un autre élément")
    print "-" * 20
    stack.pop()
    print "*" * 30

    print "Unweaving Logger Aspect ..."
    weaver.unweave_methods(stack, LoggerAspect)
    print "Now, calling a stack's method shouldn't be traced"
    stack.push("Un autre élement")
    print "Did it work ?"
Beispiel #3
0
 def test_unweave(self):
     """Tests that unweaving cancels dispatch"""
     weaver.unweave_methods(Shape, DispatcherAspect)
     self.assertEquals(self.triangle.combine_shape(self.triangle),
                       NO_DISPATCH)
     self.assertEquals(self.triangle.combine_shape(self.square),
                       NO_DISPATCH)
     self.assertEquals(self.square.combine_shape(self.triangle),
                       NO_DISPATCH)
     self.assertEquals(self.square.combine_shape(self.square), NO_DISPATCH)
 def test_unweave(self):
     """Tests that unweaving cancels dispatch"""
     weaver.unweave_methods(Shape, DispatcherAspect)        
     self.assertEquals(self.triangle.combine_shape(self.triangle),
                       NO_DISPATCH)
     self.assertEquals(self.triangle.combine_shape(self.square),
                       NO_DISPATCH)
     self.assertEquals(self.square.combine_shape(self.triangle),
                       NO_DISPATCH)
     self.assertEquals(self.square.combine_shape(self.square),
                       NO_DISPATCH)
 def test_multiple_weaving(self):
     """Tests if multiple aspects can be (un)weaved
     """
     log_device = open('tests.log', 'w')
     weaver.weave_methods(self.instance, ContractAspect)
     weaver.weave_methods(self.instance, LoggerAspect, log_device)
     self.assert_(len(weaver.get_aspects(self.instance, 'foo')) == 2)
     self.instance.foo(5)
     self.assertRaises(ContractFailedError, self.instance.foo, 15)
     weaver.unweave_methods(self.instance, ContractAspect)
     self.assert_(len(weaver.get_aspects(self.instance, 'foo')) == 1)
     # Tests if the remaining Aspect is not the Contract one.
     self.assertRaises(ValueError, self.instance.foo, 15)
     log_device.close()
 def test_multiple_weaving(self):
     """Tests if multiple aspects can be (un)weaved
     """
     log_device = open('tests.log','w')
     weaver.weave_methods(self.instance, ContractAspect)
     weaver.weave_methods(self.instance, LoggerAspect, log_device)
     self.assert_(len(weaver.get_aspects(self.instance,
                                              'foo')) == 2)
     self.instance.foo(5)
     self.assertRaises(ContractFailedError, self.instance.foo, 15)
     weaver.unweave_methods(self.instance, ContractAspect)
     self.assert_(len(weaver.get_aspects(self.instance,
                                              'foo')) == 1)
     # Tests if the remaining Aspect is not the Contract one.
     self.assertRaises(ValueError, self.instance.foo, 15)
     log_device.close()
 def test_unweave_methods(self):
     """Tests if the weaver unweaves correctly methods
     """
     weaver.weave_methods(self.klass, ContractAspect)
     self.assertRaises(ContractFailedError, self.instance.foo, 15)
     weaver.unweave_methods(self.klass, ContractAspect)
     # Contracts are no more activated : this should pass !
     self.assertRaises(ValueError, self.instance.foo, 15)
     # Test on instances
     another_instance = Test()
     weaver.weave_methods(self.instance, ContractAspect)
     weaver.weave_methods(another_instance, ContractAspect)
     self.assertRaises(ContractFailedError, self.instance.foo, 15)
     self.assertRaises(ContractFailedError, another_instance.foo, 15)
     weaver.unweave_methods(self.instance, ContractAspect)
     # Contracts are no more activated on self.instance: this should pass !
     self.assertRaises(ValueError, self.instance.foo, 15)
     # They should be still activated on another_instance
     self.assertRaises(ContractFailedError, another_instance.foo, 15)
 def test_unweave_methods(self):
     """Tests if the weaver unweaves correctly methods
     """
     weaver.weave_methods(self.klass, ContractAspect)
     self.assertRaises(ContractFailedError, self.instance.foo, 15)
     weaver.unweave_methods(self.klass, ContractAspect)
     # Contracts are no more activated : this should pass !
     self.assertRaises(ValueError, self.instance.foo, 15)
     # Test on instances
     another_instance = Test()
     weaver.weave_methods(self.instance, ContractAspect)
     weaver.weave_methods(another_instance, ContractAspect)
     self.assertRaises(ContractFailedError, self.instance.foo, 15)
     self.assertRaises(ContractFailedError, another_instance.foo, 15)
     weaver.unweave_methods(self.instance, ContractAspect)
     # Contracts are no more activated on self.instance: this should pass !
     self.assertRaises(ValueError, self.instance.foo, 15)
     # They should be still activated on another_instance
     self.assertRaises(ContractFailedError, another_instance.foo, 15)