Пример #1
0
def test_comparison(interface: AltInterface, input_file: str, source: str):
    """

    Basic comparison test. Takes input and compares it with itself.

    :param interface: `pytest fixture` returning :class:`~backend.python_interface.AltInterface` instance
    :param input_file: path to XML file containing input
    :param source: type of input

    """
    xml_input = read_input(input_file)

    res = interface.comparison(xml_input, source, xml_input, source)
    assert res is True
Пример #2
0
def test_algorithm_result(interface: AltInterface, input_file: str,
                          algorithm: str, expected_file: str,
                          optional_param: str):
    """

    Testing given algorithm on given input and comparing the result with expected output.

    .. note::

        Bare on mind that this function is heavily testing comparison. If this test fails, don't forget to check also
        comparison function and wrapper!

    :param interface: `pytest fixture` returning :class:`~backend.python_interface.AltInterface` instance
    :param input_file: path to XML file containing input
    :param algorithm: algorithm to be run
    :param expected_file: path to XML file containing expected output
    :param optional_param: extra parameter for algorithms that need it (currently only regexp derivation and CYK)

    """
    xml_input = read_input(input_file)
    expected_output = read_input(expected_file)

    res = interface.algorithms(xml_input, algorithm, optional_param)

    if 'automaton' in algorithm:
        input_type = 'fa'
    elif 'grammar' in algorithm:
        input_type = 'cfg'
    elif 'regexp' in algorithm:
        input_type = 're'
    else:
        pytest.fail('Invalid algorithm passed as argument!!')
        return

    # When comparing step tables of minimization, just compare two strings
    if (algorithm == AlgorithmTypes.AUTOMATON_MINIMIZATION
            or algorithm == AlgorithmTypes.GRAMMAR_CYK
            or algorithm == AlgorithmTypes.GRAMMAR_CYK_NO_VERBOSE):
        assert res == expected_output
    else:
        # Also testing comparison. If this test fails, don't forget to check also that!
        res = interface.comparison(res, input_type, expected_output,
                                   input_type)
        assert res is True
Пример #3
0
def test_conversion(interface: AltInterface, input_file: str, source: str,
                    target: str):
    """

    Simple conversion test. Converts input to `target`, checks the output type, converts it back to `source` and compare
    it with original input.

    .. note::

        Bare on mind that this function is also slightly testing comparison. If this test fails, don't forget to check
        also comparison function and wrapper!

    :param interface: `pytest fixture` returning :class:`~backend.python_interface.AltInterface` instance
    :param input_file: path to XML file containing input
    :param source: type of input
    :param target: type of conversion output

    """
    if target == 'fa':
        result_type = '</NFA>'
    elif target == 'rg':
        result_type = '</RightRG>'
    elif target == 're':
        result_type = '</UnboundedRegExp>'
    else:
        pytest.fail('Invalid target passed as argument!!')
        return

    xml_input = read_input(input_file)
    res = interface.conversion(xml_input, source, target)
    assert res.endswith(result_type + '\n')

    res = interface.conversion(res, target, source)

    res = interface.comparison(xml_input, source, res, source)
    assert res is True
Пример #4
0
def test_fails(interface: AltInterface):
    """

    Testing invalid requests for :class:`~backend.python_interface.AltInterface` that should always fail.

    :param interface: `pytest fixture` returning :class:`~backend.python_interface.AltInterface` instance

    """

    # Passing file as parameter
    with pytest.raises(AltInterfaceException, match='Cannot parse the XML'):
        interface.algorithms(REGEXPS + '/regexp.xml',
                             AlgorithmTypes.REGEXP_TRIM)

    # Omitting the optional parameter which is mandatory for some algorithms OR giving optional parameter when it's not
    # wanted
    xml_input = read_input(REGEXPS + '/regexp.xml')
    with pytest.raises(AltInterfaceException,
                       match='No string to differentiate by was given!'):
        interface.algorithms(xml_input, AlgorithmTypes.REGEXP_DERIVATION)
    with pytest.raises(
            AltInterfaceException,
            match='Optional parameter was given even though it can\'t be used!'
    ):
        interface.algorithms(xml_input, AlgorithmTypes.REGEXP_TRIM,
                             'ThisShouldNotBeHere')

    # Passing invalid algorithm
    xml_input = read_input(AUTOMATA + '/NFSM1.xml')
    with pytest.raises(AltInterfaceException,
                       match='Unknown algorithm passed as parameter!'):
        interface.algorithms(xml_input, 'determinization')
    with pytest.raises(AltInterfaceException,
                       match='Unknown algorithm passed as parameter!'):
        interface.algorithms(xml_input, 'automaton_determinize')

    # Passing invalid input type
    with pytest.raises(AltInterfaceException, match='Entry overload'):
        interface.algorithms(xml_input, AlgorithmTypes.AUTOMATON_MINIMIZATION)

    # Passing invalid source or target type to conversion
    with pytest.raises(
            AltInterfaceException,
            match='Unknown \'from\' parameter passed as parameter!'):
        interface.conversion(xml_input, 'fsm', 'rg')
    with pytest.raises(AltInterfaceException,
                       match='Unknown \'to\' parameter passed as parameter'):
        interface.conversion(xml_input, 'fa', 'cfg')
    with pytest.raises(AltInterfaceException,
                       match='Unknown \'from\' parameter passed as parameter'):
        interface.conversion(xml_input, 'pda', 'cfg')

    # Trying to convert ENFA to RG
    xml_input = read_input(AUTOMATA + '/ENFA1.EPSILON.xml')
    with pytest.raises(AltInterfaceException, match='Entry overload'):
        interface.conversion(xml_input, 'fa', 'rg')

    # Trying to compare FA and CFG
    xml_input2 = read_input(GRAMMARS + '/CFG1.UNIT.xml')
    with pytest.raises(AltInterfaceException, match='Entry overload'):
        interface.comparison(xml_input, 'fa', xml_input2, 'cfg')