Example #1
0
def test_execute():
    cmd = RenameCommand('rename alias "can you" "could you"')
    assert cmd.command_tokens == [
        "rename", "alias", '"can you"', '"could you"'
    ]
    facade = new_facade()
    cmd.execute()
    with pytest.raises(KeyError):
        AST.get_or_create()[UnitType.alias]["can you"]
    try:
        AST.get_or_create()[UnitType.alias]["could you"]
    except KeyError:
        pytest.fail(
            "Unexpected KeyError exception. Renaming didn't properly work.")

    cmd = RenameCommand('rename ~ "tell me" "a"')
    assert cmd.command_tokens == ["rename", "~", '"tell me"', '"a"']
    facade = new_facade()
    cmd.execute()
    with pytest.raises(KeyError):
        AST.get_or_create()[UnitType.alias]["tell me"]
    try:
        AST.get_or_create()[UnitType.alias]["a"]
    except KeyError:
        pytest.fail(
            "Unexpected KeyError exception. Renaming didn't properly work.")
Example #2
0
def test_execute():
    cmd = RenameCommand('rename alias "can you" "could you"')
    assert cmd.command_tokens == [
        "rename", "alias", '"can you"', '"could you"'
    ]
    facade = new_facade()
    cmd.execute(facade)
    with pytest.raises(KeyError):
        facade.parser.get_definition("can you", UnitType.alias)
    try:
        facade.parser.get_definition("could you", UnitType.alias)
    except KeyError:
        pytest.fail(
            "Unexpected KeyError exception. Renaming didn't properly work.")

    cmd = RenameCommand('rename ~ "tell me" "a"')
    assert cmd.command_tokens == ["rename", "~", '"tell me"', '"a"']
    facade = new_facade()
    cmd.execute(facade)
    with pytest.raises(KeyError):
        facade.parser.get_definition("tell me", UnitType.alias)
    try:
        facade.parser.get_definition("a", UnitType.alias)
    except KeyError:
        pytest.fail(
            "Unexpected KeyError exception. Renaming didn't properly work.")
Example #3
0
def test_abstract_methods():
    new_facade()
    cmd = StatsCommand('stats')
    with pytest.raises(NotImplementedError):
        cmd.execute_on_unit(None, None)
    with pytest.raises(NotImplementedError):
        cmd.finish_execution()
Example #4
0
def test_err(capsys):
    new_facade()

    cmd = DeclareCommand("declare")
    cmd.execute()
    captured = capsys.readouterr()
    assert "[ERROR]\tMissing some arguments\n\tUsage: " + \
           'declare <unit-type> "<unit-name>"' in captured.out

    cmd = DeclareCommand("declare nothing /a/")
    cmd.execute()
    captured = capsys.readouterr()
    assert "[ERROR]\tUnknown unit type: 'nothing'." in captured.out

    cmd = DeclareCommand('declare alias "var#a#b"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "[ERROR]\tUnit identifier couldn't be interpreted. " + \
           "Did you mean to escape some hashtags '#'?" in captured.out

    cmd = DeclareCommand('declare alias "a#var"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "[ERROR]\tVariation name detected, " + \
           "while units cannot be declared with a variation. " + \
           "Did you mean to escape some hashtags '#'?" in captured.out

    cmd = DeclareCommand('declare alias "var"')
    cmd.execute()
    capsys.readouterr()
    assert "Alias 'var' is already defined."
Example #5
0
def test_err(capsys):
    new_facade()
    cmd = HideCommand('hide alias "inexistant"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "Alias 'inexistant' was not defined." in captured.out

    cmd = UnhideCommand("unhide")
    cmd.execute()
    captured = capsys.readouterr()
    assert "[ERROR]\tMissing some arguments\n\tUsage: " + \
           'unhide <unit-type> "<unit-name>"' in captured.out

    cmd = UnhideCommand('unhide alias "inexistant"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "Alias 'inexistant' was not previously hidden." in captured.out

    cmd = UnhideCommand('unhide nothing "a"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "Unknown unit type: 'nothing'." in captured.out

    cmd = UnhideCommand('unhide alias "var#a#b"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "Unit identifier couldn't be interpreted. " + \
           "Did you mean to escape some hashtags '#'?" in captured.out

    cmd = UnhideCommand("unhide alias /unmatchable/")
    cmd.execute()
    captured = capsys.readouterr()
    assert "No alias matched." in captured.out
Example #6
0
def test_err(capsys):
    new_facade()

    cmd = ParseCommand("error")
    assert cmd.command_tokens == ["error"]
    cmd.execute()
    captured = capsys.readouterr()
    assert "[ERROR]\tMissing template file path\n" + \
           "\tUsage: 'parse <file_path>'" in captured.out
Example #7
0
def test_obj():
    new_facade()
    cmd = StatsCommand("")
    assert isinstance(cmd, CommandStrategy)
    assert cmd.command_tokens == []

    cmd = StatsCommand("stats")
    assert isinstance(cmd, CommandStrategy)
    assert cmd.command_tokens == ["stats"]
Example #8
0
def test_variations(capsys):
    cmd = HideCommand('hide alias "var#one"')
    facade = new_facade()
    cmd.execute()
    try:
        unit = AST.get_or_create()[UnitType.alias]["var"]
        assert "one" not in unit._variation_rules
    except KeyError:
        pytest.fail(
            "Unexpected KeyError. Alias 'var' doesn't exist in the parser.")

    cmd = UnhideCommand('unhide alias "var#nothing"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "Variation 'nothing' of alias 'var' " + \
           "was not previously hidden." in captured.out

    cmd = UnhideCommand('unhide ~ "var#one"')
    cmd.execute()
    try:
        unit = AST.get_or_create()[UnitType.alias]["var"]
        assert "one" in unit._variation_rules
    except KeyError:
        pytest.fail("Unexpected KeyError. Alias 'var' doesn't exist " + \
                    "in the parser.")

    new_facade()
    cmd = HideCommand('hide ~ "var#nothing"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "Couldn't find variation 'nothing' in alias 'var'." in captured.out

    cmd = UnhideCommand('unhide ~ "nothing#var"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "Alias 'nothing' is not defined." in captured.out

    # Hide a variation and try to restore it twice
    cmd = HideCommand('hide ~ "var#one"')
    facade = new_facade()
    cmd.execute()

    cmd = HideCommand('hide ~ "var#one"')
    other_facade = new_facade()
    cmd.execute()

    cmd = UnhideCommand('unhide ~ "var#one"')
    assert cmd.command_tokens == ["unhide", "~", '"var#one"']
    cmd.execute()

    cmd.execute()
    captured = capsys.readouterr()
    assert "[ERROR]\tVariation 'one' is " + \
           "already defined for alias 'var'." in captured.out
Example #9
0
def test_execute(capsys):
    new_facade()
    cmd = StatsCommand('stats')
    assert cmd.command_tokens == ["stats"]
    cmd.execute()
    captured = capsys.readouterr()
    assert "Statistics:" in captured.out
    assert "Parsed files: 1" in captured.out
    assert "Declared units: 7 (9 variations)" in captured.out
    assert "Declared slots: 1 (2 variations)" in captured.out
    assert "Declared aliases: 5 (6 variations)" in captured.out
    assert "Parsed rules: 25" in captured.out
Example #10
0
def test_execute(capsys):
    facade = new_facade()
    cmd = HideCommand('hide alias "tell me"')
    try:
        AST.get_or_create()[UnitType.alias]["tell me"]
    except KeyError:
        pytest.fail(
            "Unexpected KeyError. Alias 'tell me' doesn't exist " + \
            "in the parser."
        )
    cmd.execute()
    with pytest.raises(KeyError):
        AST.get_or_create()[UnitType.alias]["tell me"]
    captured = capsys.readouterr()
    assert "Alias 'tell me' was successfully hidden." in captured.out

    cmd = UnhideCommand('unhide alias "tell me"')
    cmd.execute()
    try:
        AST.get_or_create()[UnitType.alias]["tell me"]
    except KeyError:
        pytest.fail("Unexpected KeyError. Alias 'tell me' wasn't restored.")
    captured = capsys.readouterr()
    assert "Alias 'tell me' was successfully restored." in captured.out

    cmd = HideCommand("hide ~ /./")
    cmd.execute()
    _ = capsys.readouterr()
    cmd = UnhideCommand("unhide ~ /./")
    cmd.execute()
    captured = capsys.readouterr()
    assert "Alias 'tell me' was successfully restored." in captured.out
    assert "Alias 'can you' was successfully restored." in captured.out

    # Hide a unit and try to restore it twice
    cmd = HideCommand('hide ~ "can you"')
    facade = new_facade()
    cmd.execute()

    cmd = UnhideCommand('unhide alias "can you"')
    other_facade = new_facade()
    cmd.execute()

    captured = capsys.readouterr()
    assert "Alias 'can you' is already defined in the parser." in captured.out
def test_execute(capsys):
    cmd = DeleteCommand('delete alias "inexistant"')
    cmd.execute(new_facade())
    captured = capsys.readouterr()
    assert "Alias 'inexistant' was not defined." in captured.out

    cmd = DeleteCommand('delete alias "tell me"')
    facade = new_facade()
    try:
        facade.parser.get_definition("tell me", UnitType.alias)
    except KeyError:
        pytest.fail("Unexpected KeyError. Alias 'tell me' doesn't exist " + \
                    "in the parser.")
    cmd.execute(facade)
    with pytest.raises(KeyError):
        facade.parser.get_definition("tell me", UnitType.alias)
    captured = capsys.readouterr()
    assert "Alias 'tell me' was successfully deleted." in captured.out
Example #12
0
def test_execute(capsys):
    print("AAAAAAAAAAAA")
    facade = new_facade()
    cmd = DeclareCommand('declare alias "machin"')
    cmd.execute()
    try:
        unit = AST.get_or_create()[UnitType.alias]["machin"]
        assert len(unit._variation_rules) == 0
        assert len(unit._all_rules) == 0
    except (KeyError, ValueError):
        pytest.fail("Unexpected error, 'declare' command didn't work.")
    
    captured = capsys.readouterr()
    assert "Alias 'machin' was successfully declared." in captured.out

    print("AAAAAAAAAAAA")
    facade = new_facade()
    cmd = DeclareCommand('declare slot "machin"')
    cmd.execute()
    try:
        unit = AST.get_or_create()[UnitType.slot]["machin"]
        assert len(unit._variation_rules) == 0
        assert len(unit._all_rules) == 0
    except (KeyError, ValueError):
        pytest.fail("Unexpected error, 'declare' command didn't work.")
    
    captured = capsys.readouterr()
    assert "Slot 'machin' was successfully declared." in captured.out

    print("AAAAAAAAAAAA")
    facade = new_facade()
    cmd = DeclareCommand('declare intent "machin"')
    cmd.execute()
    try:
        unit = AST.get_or_create()[UnitType.intent]["machin"]
        assert len(unit._variation_rules) == 0
        assert len(unit._all_rules) == 0
    except (KeyError, ValueError):
        pytest.fail("Unexpected error, 'declare' command didn't work.")
    
    captured = capsys.readouterr()
    assert "Intent 'machin' was successfully declared." in captured.out
Example #13
0
def test_execute(capsys):
    new_facade()

    cmd = DeleteCommand('delete alias "inexistant"')
    cmd.execute()
    captured = capsys.readouterr()
    assert "Alias 'inexistant' was not defined." in captured.out

    cmd = DeleteCommand('delete alias "tell me"')
    new_facade()
    try:
        AST.get_or_create()[UnitType.alias]["tell me"]
    except KeyError:
        pytest.fail("Unexpected KeyError. Alias 'tell me' doesn't exist " + \
                    "in the parser.")
    cmd.execute()
    with pytest.raises(KeyError):
        AST.get_or_create()[UnitType.alias]["tell me"]
    captured = capsys.readouterr()
    assert "Alias 'tell me' was successfully deleted." in captured.out
Example #14
0
def test_execute(capsys):
    cmd = ParseCommand(
        "parse tests/unit-testing/cli/interactive_commands/toilets.chatette")
    assert cmd.command_tokens == \
        ["parse",
         "tests/unit-testing/cli/interactive_commands/toilets.chatette"]
    cmd.execute(new_facade())
    captured = capsys.readouterr()
    assert "[DBG] Parsing master file: " + \
           "tests/unit-testing/cli/interactive_commands/toilets.chatette\n" + \
           "[DBG] Parsing finished!" in captured.out
def test_execute(capsys):
    cmd = DeclareCommand('declare alias "machin"')
    facade = new_facade()
    cmd.execute(facade)
    try:
        unit = facade.parser.get_definition("machin", UnitType.alias)
        assert len(unit.variations) == 0
        assert len(unit.rules) == 0
    except (KeyError, ValueError):
        pytest.fail("Unexpected error, 'declare' command didn't work.")
    
    captured = capsys.readouterr()
    assert "Alias 'machin' was successfully declared." in captured.out

    cmd = DeclareCommand('declare slot "machin"')
    facade = new_facade()
    cmd.execute(facade)
    try:
        unit = facade.parser.get_definition("machin", UnitType.slot)
        assert len(unit.variations) == 0
        assert len(unit.rules) == 0
    except (KeyError, ValueError):
        pytest.fail("Unexpected error, 'declare' command didn't work.")
    
    captured = capsys.readouterr()
    assert "Slot 'machin' was successfully declared." in captured.out

    cmd = DeclareCommand('declare intent "machin"')
    facade = new_facade()
    cmd.execute(facade)
    try:
        unit = facade.parser.get_definition("machin", UnitType.intent)
        assert len(unit.variations) == 0
        assert len(unit.rules) == 0
    except (KeyError, ValueError):
        pytest.fail("Unexpected error, 'declare' command didn't work.")
    
    captured = capsys.readouterr()
    assert "Intent 'machin' was successfully declared." in captured.out
Example #16
0
def test_execute(capsys):
    new_facade()
    cmd = ShowCommand('show alias "sorry"')
    assert cmd.command_tokens == ["show", "alias", '"sorry"']
    cmd.execute()
    captured = capsys.readouterr()
    assert \
        "alias 'sorry'\nNo modifiers\n0 variation(s)" + \
        "\nTemplate rules:\n~[sorry]\n	sorry\n	excuse me\n" in captured.out

    cmd = ShowCommand('show ~ /o/g')
    assert cmd.command_tokens == ["show", "~", "/o/g"]
    cmd.execute()
    captured = capsys.readouterr()
    assert "alias 'can you'\nNo modifiers\n0 variation(s)" in captured.out
    assert \
        "Template rules:\n~[lots of rules]\n\trule 0\n\trule 1\n\trule 2" + \
        "\n\trule 3\n\trule 4\n\trule 5\n\trule 6\n\trule 7\n\trule 8" + \
        "\n\trule 9\n\trule 10\n\trule 11\n\trule 12" + \
        "\n\trule 13\n" in captured.out
    assert \
        "alias 'sorry'\nNo modifiers\n0 variation(s)" + \
        "\nTemplate rules:\n~[sorry]\n	sorry\n	excuse me\n" in captured.out

    cmd = ShowCommand('show slot "INEXISTANT"')
    assert cmd.command_tokens == ["show", "slot", '"INEXISTANT"']
    cmd.execute()
    captured = capsys.readouterr()
    assert "Slot 'INEXISTANT' is not defined." in captured.out

    cmd = ShowCommand('show ~ "lots of rules"')
    assert cmd.command_tokens == ["show", "~", '"lots of rules"']
    cmd.execute()
    captured = capsys.readouterr()
    assert "alias 'lots of rules'\nNo modifiers\n0 variation(s)" in captured.out
    assert "rule 0" in captured.out
    assert "rule 11" in captured.out
    assert "rule 12" in captured.out