def test_N_repetition(self):
        edge = Edge('a', 'state_a')
        state = State('state_a')

        edge_out = Edge('a', 'state_a')
        edge_2_out = Edge('a', 'state_a_2')
        state_out = State('state_a', [edge_2_out])
        state_2_out = State('state_a_2')

        in_edges = [edge]
        new_end_states = [state]
        new_states = [state]
        repetition = (2, 2)
        is_optional = False

        in_edges_out = [edge_out]
        new_end_states_out = [state_2_out]
        new_states_out = [state_out, state_2_out]
        expected = [in_edges_out, new_end_states_out, new_states_out]
        actual = repetition_applicator.apply_repetition(
            in_edges, new_end_states, new_states, repetition, is_optional)

        logger.debug(
            f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}'
        )
        logger.debug(f'start_edges')
        self.assertTrue(edges_equal(actual[0], expected[0]))
        logger.debug(f'end_states')
        assert_states_equal(actual[1], expected[1])
        logger.debug(f'states')
        assert_states_equal(actual[2], expected[2])
    def test_any_repetition(self):
        edge_a = Edge('a', 'state_a')
        edge_b = Edge('b', 'state_b')
        state_a = State('state_a')
        state_b = State('state_b')

        edge_out_a = Edge('a', 'state_a')
        edge_out_a_e = Edge('', 'state_a')
        edge_out_b = Edge('b', 'state_b')
        edge_out_b_e = Edge('', 'state_b')
        state_out_a = State('state_a', [edge_out_a, edge_out_b])
        state_out_b = State('state_b', [edge_out_a, edge_out_b])

        in_edges = [edge_a, edge_b]
        new_end_states = [state_a, state_b]
        new_states = [state_a, state_b]
        repetition = (0, float('inf'))
        is_optional = False

        in_edges_out = [edge_out_a, edge_out_b, edge_out_a_e, edge_out_b_e]
        new_end_states_out = [state_out_a, state_out_b]
        new_states_out = [state_out_a, state_out_b]
        expected = [in_edges_out, new_end_states_out, new_states_out]
        actual = repetition_applicator.apply_repetition(
            in_edges, new_end_states, new_states, repetition, is_optional)

        logger.debug(
            f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}'
        )
        logger.debug(f'start_edges')
        self.assertTrue(edges_equal(actual[0], expected[0]))
        logger.debug(f'end_states')
        assert_states_equal(actual[1], expected[1])
        logger.debug(f'states')
        assert_states_equal(actual[2], expected[2])
    def test_N_plus_repetition(self):
        in_edge = Edge('a', 'state_a')
        in_state = State('state_a')

        edge = Edge('a', 'state_a')
        edge2 = Edge('a', 'state_a_2')
        edge3 = Edge('a', 'state_a_3')
        edge3e = Edge('', 'state_a_3')
        state = State('state_a', [edge2])
        state2 = State('state_a_2', [edge3])
        state3 = State('state_a_3', [edge3])

        in_edges = [in_edge]
        new_end_states = [in_state]
        new_states = [in_state]
        repetition = (3, float('inf'))
        is_optional = False

        in_edges_out = [edge]
        new_end_states_out = [state3]
        new_states_out = [state, state2, state3]
        expected = [in_edges_out, new_end_states_out, new_states_out]
        actual = repetition_applicator.apply_repetition(
            in_edges, new_end_states, new_states, repetition, is_optional)

        logger.debug(
            f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}'
        )
        logger.debug(f'start_edges')
        self.assertTrue(edges_equal(actual[0], expected[0]))
        logger.debug(f'end_states')
        assert_states_equal(actual[1], expected[1])
        logger.debug(f'states')
        assert_states_equal(actual[2], expected[2])
示例#4
0
    def test_parenthized_alternative_repeated_alternative_tokens_case(self):
        elements = ['abc', '/', ['2*', ['"def"', '/', '"ghi"']]]
        edge = Edge('abc', 'abc')
        edge2 = Edge('def', 'def')
        edge3 = Edge('ghi', 'ghi')
        edge2_2 = Edge('def', 'def_2')
        edge3_2 = Edge('ghi', 'ghi_2')
        edge2_2 = Edge('def', 'def_2')
        edge2_e = Edge('def', 'def_2')
        edge3_2 = Edge('ghi', 'ghi_2')
        state = State('abc', is_automata=True)
        state2 = State('def', [edge2_2, edge3_2])
        state3 = State('ghi', [edge2_2, edge3_2])
        state2_2 = State('def_2', [edge2_2, edge3_2])
        state3_2 = State('ghi_2', [edge2_2, edge3_2])

        start_edges = [edge, edge2, edge3]
        end_states = [state, state2_2, state3_2]
        states = [state, state2, state3, state2_2, state3_2]
        expected = (start_edges, end_states, states)
        actual = choices_parser.recursive_parse_elements(elements)

        logger.debug(
            f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}'
        )
        logger.debug(f'start_edges')
        self.assertTrue(edges_equal(actual[0], expected[0]))
        logger.debug(f'end_states')
        assert_states_equal(actual[1], expected[1])
        logger.debug(f'states')
        assert_states_equal(actual[2], expected[2])
示例#5
0
    def test_trivial_case(self):
        elements = ['abc']
        edge = Edge('abc', 'abc')
        state = State('abc', is_automata=True)
        start_edges = [edge]
        end_states = [state]
        states = [state]
        expected = (start_edges, end_states, states)
        actual = choices_parser.recursive_parse_elements(elements)

        self.assertTrue(edges_equal(actual[0], expected[0]))
        assert_states_equal(actual[1], expected[1])
        assert_states_equal(actual[2], expected[2])
    def test_N_plus_repetition(self):
        edge_a = Edge('a', 'state_a')
        edge_b = Edge('b', 'state_b')
        state_a = State('state_a', edge_b)
        state_b = State('state_b')

        edge_out_a = Edge('a', 'state_a')
        edge_out_b = Edge('b', 'state_b')
        edge_out_a_2 = Edge('a', 'state_a_2')
        edge_out_b_2 = Edge('b', 'state_b_2')
        edge_out_a_3 = Edge('a', 'state_a_3')
        edge_out_a_3_i = Edge('a', 'state_a_3')
        edge_out_b_3 = Edge('b', 'state_b_3')
        state_out_a = State('state_a', edge_out_b)
        state_out_b = State('state_b', edge_out_a_2)
        state_out_a_2 = State('state_a_2', edge_out_b_2)
        state_out_b_2 = State('state_b_2', edge_out_a_3)
        state_out_a_3 = State('state_a_3', edge_out_b_3)
        state_out_b_3 = State('state_b_3', edge_out_a_3_i)

        in_edges = [edge_a]
        new_end_states = [state_b]
        new_states = [state_a, state_b]
        repetition = (3, float('inf'))
        is_optional = False

        in_edges_out = [edge_out_a]
        new_end_states_out = [state_out_b_3]
        new_states_out = [
            state_out_a, state_out_b, state_out_a_2, state_out_b_2,
            state_out_a_3, state_out_b_3
        ]
        expected = [in_edges_out, new_end_states_out, new_states_out]
        actual = repetition_applicator.apply_repetition(
            in_edges, new_end_states, new_states, repetition, is_optional)

        logger.debug(
            f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}'
        )
        logger.debug(f'start_edges')
        self.assertTrue(edges_equal(actual[0], expected[0]))
        logger.debug(f'end_states')
        assert_states_equal(actual[1], expected[1])
        logger.debug(f'states')
        assert_states_equal(actual[2], expected[2])
示例#7
0
    def test_identical_parenthetized_token_case(self):
        elements = ['abc', ['abc']]
        edge = Edge('abc', 'abc')
        edge2 = Edge('abc', 'abc_#2')
        state = State('abc', edge2, is_automata=True)
        state2 = State('abc_#2', is_automata=True)

        start_edges = [edge]
        end_states = [state2]
        states = [state, state2]
        expected = (start_edges, end_states, states)
        actual = choices_parser.recursive_parse_elements(elements)

        logger.debug(
            f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}'
        )
        logger.debug(f'start_edges')
        self.assertTrue(edges_equal(actual[0], expected[0]))
        logger.debug(f'end_states')
        assert_states_equal(actual[1], expected[1])
        logger.debug(f'states')
        assert_states_equal(actual[2], expected[2])
示例#8
0
    def test_two_alternative_tokens_case(self):
        elements = ['abc', '/', '"def"']
        edge = Edge('abc', 'abc')
        edge2 = Edge('def', 'def')
        state = State('abc', is_automata=True)
        state2 = State('def')

        start_edges = [edge, edge2]
        end_states = [state, state2]
        states = [state, state2]
        expected = (start_edges, end_states, states)
        actual = choices_parser.recursive_parse_elements(elements)

        logger.debug(
            f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}'
        )
        logger.debug(f'start_edges')
        self.assertTrue(edges_equal(actual[0], expected[0]))
        logger.debug(f'end_states')
        assert_states_equal(actual[1], expected[1])
        logger.debug(f'states')
        assert_states_equal(actual[2], expected[2])
示例#9
0
    def test_starting_parenthized_tokens_case(self):
        elements = [['abc', '"def"'], '"ghi"']
        edge = Edge('abc', 'abc')
        edge2 = Edge('def', 'def')
        edge3 = Edge('ghi', 'ghi')
        state = State('abc', edge2, is_automata=True)
        state2 = State('def', edge3)
        state3 = State('ghi')

        start_edges = [edge]
        end_states = [state3]
        states = [state, state2, state3]
        expected = (start_edges, end_states, states)
        actual = choices_parser.recursive_parse_elements(elements)

        logger.debug(
            f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}'
        )
        logger.debug(f'start_edges')
        self.assertTrue(edges_equal(actual[0], expected[0]))
        logger.debug(f'end_states')
        assert_states_equal(actual[1], expected[1])
        logger.debug(f'states')
        assert_states_equal(actual[2], expected[2])