Esempio n. 1
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
Esempio n. 2
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')