Beispiel #1
0
    def test_transition_merge(self):
        class Trans1(object):
            pass

        class Trans2(object):
            pass

        trans = mock.Mock(**{
            '__class__': Trans1,
            'priority': 1,
            'merge.return_value': set([12, 13, 14]),
        })
        trans_class = mock.Mock(return_value=trans)
        st_from = states.State()
        st_to = states.State()
        others = set([
            mock.Mock(__class__=Trans1, state_in=st_to),
            mock.Mock(__class__=Trans1, state_in=st_to),
            mock.Mock(__class__=Trans1, state_in=st_to),
        ])
        from_out = set([
            mock.Mock(__class__=Trans1, state_in='st1'),
            mock.Mock(__class__=Trans1, state_in='st2'),
            mock.Mock(__class__=Trans1, state_in='st3'),
            mock.Mock(__class__=Trans2, state_in=st_to),
            mock.Mock(__class__=Trans2, state_in=st_to),
        ])
        st_from._trans_out = {
            0: set([0, 1, 2]),
            1: from_out | others,
            2: set([3, 4, 5]),
        }
        to_in = set([
            mock.Mock(__class__=Trans1),
            mock.Mock(__class__=Trans2),
            mock.Mock(__class__=Trans1),
        ])
        st_to._trans_in = {
            0: set([6, 7, 8]),
            1: to_in | others,
            2: set([9, 10, 11]),
        }

        st_from.transition(trans_class, st_to, a=1, b=2, c=3)

        trans_class.assert_called_once_with(st_from, st_to, a=1, b=2, c=3)
        trans.merge.assert_called_once_with(others)
        self.assertEqual(st_from._trans_out, {
            0: set([0, 1, 2]),
            1: from_out | set([12, 13, 14]),
            2: set([3, 4, 5]),
        })
        self.assertEqual(st_to._trans_in, {
            0: set([6, 7, 8]),
            1: to_in | set([12, 13, 14]),
            2: set([9, 10, 11]),
        })
Beispiel #2
0
    def test_hashable(self):
        obj1 = states.State()
        obj2 = states.State()

        obj_set = set([obj1, obj2])

        self.assertEqual(len(obj_set), 2)
        self.assertIn(obj1, obj_set)
        self.assertIn(obj2, obj_set)
Beispiel #3
0
    def _new_state(self, accepting=False, code=None):
        """
        Construct a new state for the machine.

        :param bool accepting: If ``True``, indicates that the state
                               is an accepting state.
        :param str code: The start code to associate with a node.
                         Defaults to ``None``.

        :returns: The new state.
        :rtype: ``plexgen.states.State``
        """

        # Construct the new state
        new = states.State(accepting, code)

        # Add it to the set of machine states
        self._states.add(new)

        # If it's an accepting state, update the accepting set
        if new.accepting:
            self._accepting.add(new)
            self._final_cache = None  # invalidate cached _final

        return new
Beispiel #4
0
    def test_eps_out_uncached(self, mock_all_eps):
        obj = states.State()
        obj._trans_out = 'out'

        self.assertEqual(obj.eps_out, 'uncached')
        self.assertEqual(obj._eps_out, 'uncached')
        mock_all_eps.assert_called_once_with('out')
Beispiel #5
0
    def test_eps_in_uncached(self, mock_all_eps):
        obj = states.State()
        obj._trans_in = 'in'

        self.assertEqual(obj.eps_in, 'uncached')
        self.assertEqual(obj._eps_in, 'uncached')
        mock_all_eps.assert_called_once_with('in')
Beispiel #6
0
    def test_eps_out_cached(self, mock_all_eps):
        obj = states.State()
        obj._trans_out = 'out'
        obj._eps_out = 'cached'

        self.assertEqual(obj.eps_out, 'cached')
        self.assertEqual(obj._eps_out, 'cached')
        self.assertFalse(mock_all_eps.called)
Beispiel #7
0
    def test_iter_out_prio(self, mock_iter_trans):
        obj = states.State()
        obj._trans_out = 'out'

        result = obj.iter_out(2)

        self.assertEqual(result, mock_iter_trans.return_value)
        mock_iter_trans.assert_called_once_with('out', 2)
Beispiel #8
0
    def test_iter_in_prio(self, mock_iter_trans):
        obj = states.State()
        obj._trans_in = 'in'

        result = obj.iter_in(2)

        self.assertEqual(result, mock_iter_trans.return_value)
        mock_iter_trans.assert_called_once_with('in', 2)
Beispiel #9
0
    def test_reverse(self):
        obj = states.State()
        obj._trans_in = 'in'
        obj._trans_out = 'out'

        obj.reverse()

        self.assertEqual(obj._trans_in, 'out')
        self.assertEqual(obj._trans_out, 'in')
Beispiel #10
0
    def test_init_accepting(self):
        result = states.State('accepting', 'code')

        self.assertIs(result.accepting, True)
        self.assertEqual(result.code, 'code')
        self.assertIsNone(result.name)
        self.assertEqual(result._trans_in, {})
        self.assertEqual(result._trans_out, {})
        self.assertIsNone(result._eps_in)
        self.assertIsNone(result._eps_out)
Beispiel #11
0
    def test_init_base(self):
        result = states.State()

        self.assertIs(result.accepting, False)
        self.assertIsNone(result.code)
        self.assertIsNone(result.name)
        self.assertEqual(result._trans_in, {})
        self.assertEqual(result._trans_out, {})
        self.assertIsNone(result._eps_in)
        self.assertIsNone(result._eps_out)
Beispiel #12
0
    def test_transition_empty(self):
        class Trans1(object):
            pass

        trans = mock.Mock(**{
            '__class__': Trans1,
            'priority': 1,
            'merge.return_value': None,
        })
        trans_class = mock.Mock(return_value=trans)
        st_from = states.State()
        st_to = states.State()

        st_from.transition(trans_class, st_to, a=1, b=2, c=3)

        trans_class.assert_called_once_with(st_from, st_to, a=1, b=2, c=3)
        trans.merge.assert_called_once_with(set())
        self.assertEqual(st_from._trans_out, {1: set([trans])})
        self.assertEqual(st_to._trans_in, {1: set([trans])})