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'
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"
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"
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)
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"
def test_credits_flag(capsys): with pytest.raises(SystemExit): main(["--credits"]) captured = capsys.readouterr() assert captured.err == "" assert "Ian Fisher" in captured.out
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"
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
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"
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"
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 """ )
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") ^ """ )
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"
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"
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"
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"
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
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 == ""
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
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
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
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) """)
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() """)
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) """)
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) """)
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) """)
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")
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) ^ """ )
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> ) ^ """ )
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")