Ejemplo n.º 1
0
def test_parse_warrior_skip_comment_lines():
    data = """  ; This is comment line
                ADD.AB #4, 3
                ; Another comment
                 MOV.I  2, @2
    """
    file_handle = StringIO(data)
    warrior = parse_warrior(file_handle)
    instructions = warrior.instructions()
    assert len(instructions) == 2
Ejemplo n.º 2
0
def test_parse_warrior_skip_blank_lines():
    data = """
                    ADD.AB #4, 3
                    
                    MOV.I  2, @2
    """
    file_handle = StringIO(data)
    warrior = parse_warrior(file_handle)
    instructions = warrior.instructions()
    assert len(instructions) == 2
Ejemplo n.º 3
0
def test_parse_instruction_data():
    data = "ADD.AB #-4, $3"
    file_handle = StringIO(data)
    warrior = parse_warrior(file_handle)
    first_instruction = warrior.instructions()[0]
    assert first_instruction.modifier() == Modifier.AB
    assert first_instruction.a_mode() == Mode.IMMEDIATE
    assert first_instruction.a_value() == -4
    assert first_instruction.b_mode() == Mode.DIRECT
    assert first_instruction.b_value() == 3
Ejemplo n.º 4
0
def test_parse_warrior_instructions_classes():
    data = """
                ADD.AB #4, 3
                MOV.I  2, @2
                JMP    -2 ; Useless comment for parsing test
                DAT    #0, #0
            """
    file_handle = StringIO(data)
    warrior = parse_warrior(file_handle)
    instructions = warrior.instructions()
    assert len(instructions) == 4
    assert type(warrior.instructions()[0]) is ADD
    assert type(warrior.instructions()[1]) is MOV
    assert type(warrior.instructions()[2]) is JMP
    assert type(warrior.instructions()[3]) is DAT
Ejemplo n.º 5
0
def get_warrior_list(warrior_files):
    """
    Parse warriors from specified files or use default warriors directory
    :param warrior_files: List of warrior file paths
    :return: Warrior objects list
    """
    if warrior_files:
        # Load warriors from specified files
        paths = warrior_files
    else:
        # Load random warriors from default directory
        warrior_files = get_warrior_files()
        paths = random.sample(warrior_files, DEFAULT_WARRIOR_COUNT)

    warriors = []
    for path in paths:
        if os.path.isfile(path):
            with open(path, 'r') as file_handle:
                warrior = parse_warrior(file_handle)
                warriors.append(warrior)
    return warriors
Ejemplo n.º 6
0
def test_parse_warrior_invalid_instruction_syntax():
    data = "INVALID.AB ERR"
    file_handle = StringIO(data)
    with pytest.raises(InvalidInstructionSyntaxException):
        parse_warrior(file_handle)
Ejemplo n.º 7
0
def test_parse_warrior_invalid_instruction_code():
    data = "XYZ.AB #4, 3"
    file_handle = StringIO(data)
    with pytest.raises(InvalidInstructionCodeException):
        parse_warrior(file_handle)
Ejemplo n.º 8
0
def test_parse_warrior_name():
    data = ";name Imp"
    file_handle = StringIO(data)
    warrior = parse_warrior(file_handle)
    assert warrior.warrior_info().name() == "Imp"