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_arguments_raw(): args = dict( type="Command.CommandType.QUERY", level="Command.NORMAL", description= '''"Return true if autochanger trucks are at ONLINE. " + "This command doesn't read again the sensors."''' ) assert extract_command_arguments(COMMAND_RAW) == args
def test_extract_command_arguments_missing_arguments(): with pytest.raises(ValueError): extract_command_arguments(COMMAND_MISSING_DESCRIPTION)
def test_extract_command_arguments_with_coma(): args = dict(type="Command.CommandType.QUERY", level="Command.ENGINEERING1", description='"Do this and that."') assert extract_command_arguments(COMMAND_DESCRIPTION_WITH_COMA) == args
def test_extract_command_arguments_basic(): args = dict(type="Command.CommandType.ACTION", level="Command.ENGINEERING1", description='"Connect the loader hardware."') assert extract_command_arguments(COMMAND_BASIC) == args