def test_command_simple():
    cmd = Command(name=NAME,
                  cmdtype=TYPE,
                  level=LEVEL,
                  description=DESCRIPTION)

    assert repr(cmd) == f"{NAME}: {DESCRIPTION}"
    assert str(
        cmd
    ) == f"Command[name={NAME}, type={TYPE}, level={LEVEL}, desc='{DESCRIPTION}']"
    assert cmd.to_csv("Toto") == f"Toto,{NAME},{TYPE},{LEVEL},{DESCRIPTION},\n"
def test_command_with_arguments():
    cmd = Command(name=NAME,
                  cmdtype=TYPE,
                  level=LEVEL,
                  description=DESCRIPTION,
                  args=[ARGUMENT])

    assert repr(cmd) == f"{NAME}: {DESCRIPTION}"
    assert str(
        cmd
    ) == f"Command[name={NAME}, type={TYPE}, level={LEVEL}, desc='{DESCRIPTION}', args=({repr(ARGUMENT)})]"
    assert cmd.to_csv(
        "Toto"
    ) == f"Toto,{NAME},{TYPE},{LEVEL},{DESCRIPTION},{repr(ARGUMENT)}\n"
Exemple #3
0
def extract_command_info(lines: List[str], idx: int) -> Command:
    """Create a Command instance from text"""
    # Command decorator
    cmd_decorator = lines[idx]
    while not cmd_decorator.endswith(")"):
        idx += 1
        cmd_decorator += lines[idx]

    command_dict = extract_command_arguments(cmd_decorator)

    # Method definition
    method_id = idx + 1
    method = lines[method_id]
    while method.startswith("@Override") or method.startswith("//"):
        method_id += 1
        method = lines[method_id]

    command_name = extract_command_name(method)
    argument_dict = extract_method_arguments(method)
    arguments = [
        Argument(name, type_)
        for name, type_ in argument_dict.items()
    ]

    return Command(
        name=command_name,
        cmdtype=command_dict.get("type", ""),
        level=command_dict.get("level", ""),
        description=command_dict.get("description", ""),
        args=arguments,
    )
def test_extract_command_info_basic():
    input_ = [COMMAND_BASIC, "public void myCommand() {"]
    cmd_position = 0
    result = extract_command_info(input_, cmd_position)
    expected = Command(
        name="myCommand", cmdtype="ACTION", level="ENGINEERING1",
        description="Connect the loader hardware.", args=[]
    )
    assert result == expected
def test_extract_command_info_real():
    input_ = SPACED_TEXT_CLEANED
    cmd_position = 5
    result = extract_command_info(input_, cmd_position)
    expected = Command(
        name="setFilter", cmdtype="ACTION", level="NORMAL",
        description="Set filter.", args=[Argument(name="filterId", ptype="int")]
    )
    assert result == expected
def test_extract_command_info_multiline():
    input_ = COMMAND_TWO_LINES + ["public void myCommand() {"]
    cmd_position = 0
    result = extract_command_info(input_, cmd_position)
    expected = Command(
        name="myCommand", cmdtype="QUERY", level="NORMAL",
        description="First line. Second line.", args=[]
    )
    assert result == expected
def test_parse_raw_text():
    expected_commands = [
        Command(
            name="setFilter", cmdtype="ACTION", level="NORMAL",
            description="Set filter.", args=[Argument(name="filterId", ptype="int")]
        )
    ]
    expected_parameters = [
        ConfigurationParameter(name="deltaPosition", ptype="int", description="Position margin to terminate movement.", category="UNKNOWN")
    ]
    res_commands, res_parameters = parse_raw_text(SPACED_TEXT_RAW)

    for exp_cmd, res_cmd in zip(expected_commands, res_commands):
        assert exp_cmd == res_cmd

    for exp_param, res_param in zip(expected_parameters, res_parameters):
        assert exp_param == res_param