| 1 | import os |
| 2 | |
| 3 | const vexe = @VEXE |
| 4 | const vtmp_folder = os.join_path(os.vtmp_dir(), 'division_by_zero_runtime_tests') |
| 5 | |
| 6 | @[markused] |
| 7 | const turn_off_vcolors = os.setenv('VCOLORS', 'never', true) |
| 8 | |
| 9 | fn test_indirect_integer_division_by_zero_panics_in_v() { |
| 10 | output := |
| 11 | compile_and_run_program('fn divide(a int, b int) int {\n\treturn a / b\n}\n\nfn main() {\n\tprintln(divide(1, 0))\n}\n') |
| 12 | assert_valid_panic_output(output, 'division by zero') |
| 13 | } |
| 14 | |
| 15 | fn test_indirect_integer_modulo_by_zero_panics_in_v() { |
| 16 | output := |
| 17 | compile_and_run_program('fn modulo(a int, b int) int {\n\treturn a % b\n}\n\nfn main() {\n\tprintln(modulo(3, 0))\n}\n') |
| 18 | assert_valid_panic_output(output, 'modulo by zero') |
| 19 | } |
| 20 | |
| 21 | fn assert_valid_panic_output(output string, message string) { |
| 22 | normalized := output.replace('\r', '') |
| 23 | assert normalized.contains('V panic: ${message}'), normalized |
| 24 | assert !normalized.to_lower().contains('runtime error:'), normalized |
| 25 | } |
| 26 | |
| 27 | fn compile_and_run_program(source string) string { |
| 28 | os.mkdir_all(vtmp_folder) or {} |
| 29 | defer { |
| 30 | os.rmdir_all(vtmp_folder) or {} |
| 31 | } |
| 32 | source_path := os.join_path(vtmp_folder, 'division_by_zero_program.v') |
| 33 | exe_path := os.join_path(vtmp_folder, 'division_by_zero_test${exe_suffix()}') |
| 34 | os.write_file(source_path, source) or { panic(err) } |
| 35 | compile_cmd := '${os.quoted_path(vexe)} -o ${os.quoted_path(exe_path)} ${os.quoted_path(source_path)}' |
| 36 | compilation := os.execute(compile_cmd) |
| 37 | assert compilation.exit_code == 0, 'compilation failed: ${compilation.output}' |
| 38 | result := os.execute(os.quoted_path(exe_path)) |
| 39 | assert result.exit_code != 0, 'program unexpectedly succeeded' |
| 40 | return result.output |
| 41 | } |
| 42 | |
| 43 | fn exe_suffix() string { |
| 44 | return if os.user_os() == 'windows' { '.exe' } else { '' } |
| 45 | } |
| 46 | |