예제 #1
0
 def test_bad_state(self):
     S1 = State("S1")
     S2 = State("S2")
     S1.add_transition(Transition("S1->S2", None, S2, 100))
     S1.add_transition(Transition("S1->S2", None, S2, 50))
     self.assertRaises(BadState, S1.build_pdf)
예제 #2
0
    def test_simple_state_machine(self):
        # This state machine will generate even length strings of the form
        # (AB|AC)* or odd length strings of the form (AB|AC)* | (A|)
        S1 = State("S1")
        S2 = State("S2")

        regex = re.compile("^(AB|AC|)+(A|)$")

        self._str = ""

        def appendA():
            self._str += "A"

        def appendB():
            self._str += "B"

        def appendC():
            self._str += "C"

        S1.add_transition(Transition("S1->S2", appendA, S2, 100))
        S2.add_transition(Transition("S2->S1", appendB, S1, 50))
        S2.add_transition(Transition("S2->S1", appendC, S1, 50))
        S1.build_pdf()
        S2.build_pdf()

        generator = StateMachine("Gen", S1)
        for x in xrange(1000):
            generator.transition()

        # Verify that the State machine generates a correct regex
        assert_that(regex.match(self._str), not_none())
        # Verify that there isnt a transition from B to C
        assert_that("BC" in self._str, is_(False))
        assert_that("CB" in self._str, is_(False))