Package schrodinger :: Package application :: Package msv :: Module test_helpers :: Class RuleBasedStateMachineMetaClass
[hide private]
[frames] | no frames]

Type RuleBasedStateMachineMetaClass

object --+    
         |    
      type --+
             |
            RuleBasedStateMachineMetaClass


A metaclass that makes it easy to create simple rules for a
RuleBasedStateMachine.

'Simple' in this context means that the rule we want to create
    a) calls a method that takes no argument, and then
    b) doesn't check any results

The only thing that we're testing with a rule like this is that calling the
method never results in an unhandled exception. Ideally, we would check the
results of calling methods, but even the lazy route is surprisingly useful
in a state machine test. Advice: begin by adding as many of these simple
rules as possible and then gradually replace them with richer rules and
checks as the tests develop and experience indicates which checks are most
valuable.

Suppose that we are testing self.panel in our state machine and want to call
the methods 'foo' on self.panel.bar and 'gar' on self.panel.mar.  We could
write this:

class PanelMachine(stateful.RuleBaseStateMachine):
    def __init__(self):
        super(PanelMachine, self).__init__()
        self.panel = MyPanel()

    @stateful.rule()
    def foo(self):
        self.panel.bar.foo()

    @stateful.rule()
    def gar(self):
        self.panel.mar.gar()

But this quickly becomes tedious if we want to test a large number of such
methods. Our metaclass will allow us to generate the desired rules for our
state machine by adding strings to the SIMPLE_RULES tuple defined on the
class:

class PanelMachine(stateful.RuleBaseStateMachine):
    __metaclass__ = RuleBasedStateMachineMetaClass
    SIMPLE_RULES = ('panel.bar.foo', 'panel.bar.gar')

    def __init__(self):
        super(PanelMachine, self).__init__()
        self.panel = MyPanel()

Instance Methods [hide private]

Inherited from type: __call__, __delattr__, __eq__, __ge__, __getattribute__, __gt__, __hash__, __init__, __instancecheck__, __le__, __lt__, __ne__, __repr__, __setattr__, __subclasscheck__, __subclasses__, mro

Inherited from object: __format__, __reduce__, __reduce_ex__, __sizeof__, __str__, __subclasshook__

Class Methods [hide private]
function
_makeFunc(cls, component_path)
Returns a test function that simply calls the method specified by component_path
Static Methods [hide private]
a new object with type S, a subtype of T
__new__(cls, name, bases, dct)
Prior to regular class creation, iterates through the SIMPLE_RULES tuple defined on the state machine class and add stateful.rules for each of the items.
Properties [hide private]

Inherited from type: __abstractmethods__, __base__, __bases__, __basicsize__, __dictoffset__, __flags__, __itemsize__, __mro__, __name__, __weakrefoffset__

Inherited from object: __class__

Method Details [hide private]

__new__(cls, name, bases, dct)
Static Method

 

Prior to regular class creation, iterates through the SIMPLE_RULES tuple defined on the state machine class and add stateful.rules for each of the items.

Returns: a new object with type S, a subtype of T
Overrides: object.__new__

_makeFunc(cls, component_path)
Class Method

 

Returns a test function that simply calls the method specified by component_path

Parameters:
  • component_path (list of str) - A list of strings picking out the component
Returns: function
A callback that will call the method specified by component_path