示例#1
0
def test_main_preprocess_non_existent_file(capsys):
    with pytest.raises(SystemExit):
        main(["preprocess", "unicorn.hera"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == 'Error: file "unicorn.hera" does not exist.\n'
示例#2
0
def test_main_with_multiple_positional_args(capsys):
    with pytest.raises(SystemExit):
        main(["a.hera", "b.hera"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == "Too many file paths supplied.\n"
示例#3
0
def test_main_with_short_quiet_flag(capsys):
    with patch("sys.stdin", StringIO("SET(R1, 42)")):
        main(["-q", "-"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == "\n"
示例#4
0
def test_main_with_long_version_flag(capsys):
    with pytest.raises(SystemExit):
        main(["--version"])

    captured = capsys.readouterr()
    assert captured.err == ""
    assert re.match(r"^hera-py [0-9.]+ for HERA version [0-9.]+$", captured.out)
示例#5
0
def test_disassemble_another_branch(capsys):
    with patch("sys.stdin", StringIO("0011")):
        main(["disassemble", "-"])

    captured = capsys.readouterr()
    assert captured.err == ""
    assert captured.out == "\nBRR(17)\n"
示例#6
0
def test_credits_flag(capsys):
    with pytest.raises(SystemExit):
        main(["--credits"])

    captured = capsys.readouterr()
    assert captured.err == ""
    assert "Ian Fisher" in captured.out
示例#7
0
def test_disassemble_from_stdin(capsys):
    with patch("sys.stdin", StringIO("e1ff")):
        main(["disassemble", "-"])

    captured = capsys.readouterr()
    assert captured.err == ""
    assert captured.out == "\nSETLO(R1, 255)\n"
示例#8
0
def test_main_with_init_flag_withg_different_syntax(capsys):
    with patch("sys.stdin", StringIO("NOP()")):
        main(["-", "--init", "r1=4, r2=5"])

    captured = capsys.readouterr().err
    assert "R1  = 0x0004 = 4" in captured
    assert "R2  = 0x0005 = 5" in captured
示例#9
0
def test_main_with_no_positional_args(capsys):
    with pytest.raises(SystemExit):
        main([])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == "No file path supplied.\n"
示例#10
0
def test_main_with_quiet_and_verbose_flags(capsys):
    with pytest.raises(SystemExit):
        main(["--quiet", "--verbose", "main.hera"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == "--quiet and --verbose are incompatible.\n"
示例#11
0
def test_main_with_verbose_flag(capsys):
    with patch("sys.stdin", StringIO("SET(R1, 42)")):
        main(["--verbose", "-"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert (
        captured.err
        == """\


Virtual machine state after execution (HERA v2.4.0):
    R1  = 0x002a = 42 = '*'
    R2  = 0x0000 = 0
    R3  = 0x0000 = 0
    R4  = 0x0000 = 0
    R5  = 0x0000 = 0
    R6  = 0x0000 = 0
    R7  = 0x0000 = 0
    R8  = 0x0000 = 0
    R9  = 0x0000 = 0
    R10 = 0x0000 = 0
    R11 = 0x0000 = 0
    R12 = 0x0000 = 0
    R13 = 0x0000 = 0
    R14 = 0x0000 = 0
    R15 = 0x0000 = 0

    Carry-block flag is OFF
    Carry flag is OFF
    Overflow flag is OFF
    Zero flag is OFF
    Sign flag is OFF
"""
    )
示例#12
0
def test_mega_error(capsys):
    with pytest.raises(SystemExit):
        main(["test/assets/error/mega_error.hera"])

    captured = capsys.readouterr().err
    assert (
        captured
        == """\
Warning: consider using "0o" prefix for octal numbers, line 11 col 9 of test/assets/error/mega_error.hera

  SET(R2, 01)
          ^

Warning: unrecognized backslash escape, line 17 col 13 of test/assets/error/mega_error.hera

  LP_STRING("\\y")
              ^

Error: expected comma or right parenthesis, line 2 col 8 of test/assets/error/mega_error.hera

  SET(R1 40)
         ^

Error: over-long character literal, line 14 col 11 of test/assets/error/mega_error.hera

  SETLO(R7, 'ab')
            ^

Error: unclosed string literal, line 20 col 9 of test/assets/error/mega_error.hera

  SET(R1, "
          ^

Error: expected integer, line 5 col 6 of test/assets/error/mega_error.hera

  FOFF(R1)
       ^

Error: too few args to ADD (expected 3), line 8 col 1 of test/assets/error/mega_error.hera

  ADD('c', "abc")
  ^

Error: expected register, line 8 col 5 of test/assets/error/mega_error.hera

  ADD('c', "abc")
      ^

Error: expected register, line 8 col 10 of test/assets/error/mega_error.hera

  ADD('c', "abc")
           ^

Error: data statement after code, line 17 col 1 of test/assets/error/mega_error.hera

  LP_STRING("\\y")
  ^

"""
    )
示例#13
0
def test_main_preprocess_with_warn_return_off_flag(capsys):
    with pytest.raises(SystemExit):
        main(["--warn-return-off", "preprocess", "main.hera"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == "--warn-return-off is not compatible with the chosen mode.\n"
示例#14
0
def test_main_preprocess_and_debug_with_big_stack_flag(capsys):
    with pytest.raises(SystemExit):
        main(["--big-stack", "preprocess", "main.hera"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == "--big-stack is not compatible with the chosen mode.\n"
示例#15
0
def test_main_with_help_flag_and_other_flags(capsys):
    with pytest.raises(SystemExit):
        main(["--help", "main.hera"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == "--help may not be combined with other flags or commands.\n"
示例#16
0
def test_main_with_data_flag(capsys):
    with pytest.raises(SystemExit):
        main(["--data", "whatever.hera"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err == "--data is not compatible with the chosen mode.\n"
示例#17
0
def test_main_with_big_stack_flag(capsys):
    program = "DLABEL(X)\nSET(R1, X)"
    with patch("sys.stdin", StringIO(program)):
        main(["--big-stack", "-"])

    captured = capsys.readouterr()
    assert captured.out == ""
    assert "R1  = 0xc167" in captured.err
示例#18
0
def test_main_debug(capsys):
    # Just making sure we can invoke the debugger from the command-line, not testing
    # any of the debugger's functionality.
    with patch("sys.stdin", StringIO("quit")):
        main(["debug", "test/assets/cs240/factorial.hera"])

    captured = capsys.readouterr()
    assert captured.err == ""
示例#19
0
def test_recursive_program(capsys):
    with pytest.raises(SystemExit):
        main(["test/assets/include/recursive.hera"])

    captured = capsys.readouterr()
    assert "recursive include" in captured.err
    assert '#include "recursive.hera"' in captured.err
    assert "line 1 col 10 of test/assets/include/recursive.hera" in captured.err
示例#20
0
def test_error_message_from_include(capsys):
    with pytest.raises(SystemExit):
        main(["test/assets/error/from_include.hera"])

    captured = capsys.readouterr()
    assert "SET(R1, R2)  // error!" in captured.err
    assert "ADD(R1, R2)  // error!" in captured.err
    assert "test/assets/error/from_include.hera" in captured.err
    assert "test/assets/error/included.hera" in captured.err
示例#21
0
def test_main_with_dash_to_separate_arguments(capsys):
    with pytest.raises(SystemExit):
        main(["debug", "--", "--version"])

    captured = capsys.readouterr()
    assert captured.out == ""
    # Make sure we get a "file not found" error and not a "cannot use --version with
    # debug subcommand" error, which would indicate that the dash separator was not
    # being interpreted correctly.
    assert 'file "--version" does not exist' in captured.err
示例#22
0
def test_disassemble_set_inc(capsys):
    main(["disassemble", "test/assets/asm/set_inc.hera.lcode"])

    captured = capsys.readouterr()
    assert captured.err == ""
    assert (captured.out == """\
SETLO(R1, 255)
SETHI(R3, 42)
INC(R10, 1)
DEC(R1, 20)
""")
示例#23
0
def test_disassemble_misc(capsys):
    main(["disassemble", "test/assets/asm/misc.hera.lcode"])

    captured = capsys.readouterr()
    assert captured.err == ""
    assert (captured.out == """\
LOAD(R1, 10, R14)
STORE(R2, 0, R14)
SWI(10)
RTI()
""")
示例#24
0
def test_disassemble_flag(capsys):
    main(["disassemble", "test/assets/asm/flag.hera.lcode"])

    captured = capsys.readouterr()
    assert captured.err == ""
    assert (captured.out == """\
SAVEF(R5)
RSTRF(R5)
FON(4)
FOFF(5)
FSET5(3)
FSET4(2)
""")
示例#25
0
def test_disassemble_binary_op(capsys):
    main(["disassemble", "test/assets/asm/binary_op.hera.lcode"])

    captured = capsys.readouterr()
    assert captured.err == ""
    assert (captured.out == """\
AND(R1, R7, R12)
OR(R0, R4, R2)
ADD(R3, R5, R8)
SUB(R9, R13, R15)
MUL(R1, R2, R3)
XOR(R8, R7, R4)
""")
示例#26
0
def test_disassemble_shift(capsys):
    main(["disassemble", "test/assets/asm/shift.hera.lcode"])

    captured = capsys.readouterr()
    assert captured.err == ""
    assert (captured.out == """\
LSL(R3, R4)
LSR(R5, R6)
LSL8(R7, R8)
LSR8(R8, R9)
ASL(R1, R2)
ASR(R2, R3)
""")
示例#27
0
def test_stdlib_reg_getchar(capsys):
    with patch("sys.stdin", StringIO("abc\n")):
        vm = main(["test/assets/cs350/getchar_reg.hera"])

    addr = vm.registers[1]
    assert vm.memory[addr] == 1
    assert vm.memory[addr + 1] == ord("a")
示例#28
0
def test_main_with_no_debug_flag(capsys):
    with pytest.raises(SystemExit):
        program = "print_reg(R1)"
        with patch("sys.stdin", StringIO(program)):
            main(["--no-debug-ops", "-"])

    captured = capsys.readouterr()
    assert (
        captured.err
        == """\

Error: debugging instructions disallowed with --no-debug-ops flag, line 1 col 1 of <stdin>

  print_reg(R1)
  ^

"""
    )
示例#29
0
def test_no_ANSI_color_when_stderr_is_not_tty():
    buf = StringIO()
    with patch("sys.stderr", buf):
        with patch("sys.stdin", StringIO(")")):
            with pytest.raises(SystemExit):
                main(["-"])

    assert (
        buf.getvalue()
        == """\

Error: expected HERA operation or #include, line 1 col 1 of <stdin>

  )
  ^

"""
    )
示例#30
0
def test_stdlib_reg_getline():
    with patch("sys.stdin", StringIO("hello\n")):
        vm = main(["test/assets/cs350/getline_reg.hera"])

    addr = vm.registers[1]
    assert vm.memory[addr] == 5
    assert vm.memory[addr + 1] == ord("h")
    assert vm.memory[addr + 2] == ord("e")
    assert vm.memory[addr + 3] == ord("l")
    assert vm.memory[addr + 4] == ord("l")
    assert vm.memory[addr + 5] == ord("o")