| 1 | // vtest build: !sanitized_job? |
| 2 | import os |
| 3 | import term |
| 4 | import benchmark |
| 5 | import v.ast |
| 6 | import v.fmt |
| 7 | import v.parser |
| 8 | import v.pref |
| 9 | import v.util.diff |
| 10 | import v.util.vtest |
| 11 | |
| 12 | const vroot = @VEXEROOT |
| 13 | const tdir = os.join_path(vroot, 'vlib/v/fmt') |
| 14 | const fpref = &pref.Preferences{ |
| 15 | is_fmt: true |
| 16 | } |
| 17 | |
| 18 | fn run_fmt(mut input_files []string) { |
| 19 | fmt_message := 'vfmt tests' |
| 20 | eprintln(term.header(fmt_message, '-')) |
| 21 | tmpfolder := os.temp_dir() |
| 22 | assert input_files.len > 0 |
| 23 | input_files = vtest.filter_vtest_only(input_files) |
| 24 | if input_files.len == 0 { |
| 25 | // No need to produce a failing test here. |
| 26 | eprintln('no tests found with VTEST_ONLY filter set to: ' + os.getenv('VTEST_ONLY')) |
| 27 | exit(0) |
| 28 | } |
| 29 | mut fmt_bench := benchmark.new_benchmark() |
| 30 | fmt_bench.set_total_expected_steps(input_files.len) |
| 31 | for istep, ipath in input_files { |
| 32 | fmt_bench.cstep = istep |
| 33 | fmt_bench.step() |
| 34 | opath := ipath.replace('_input.vv', '_expected.vv') |
| 35 | if !os.exists(opath) { |
| 36 | fmt_bench.fail() |
| 37 | eprintln(fmt_bench.step_message_fail('missing file ${opath}')) |
| 38 | continue |
| 39 | } |
| 40 | expected_ocontent := os.read_file(opath) or { |
| 41 | fmt_bench.fail() |
| 42 | eprintln(fmt_bench.step_message_fail('cannot read from ${opath}')) |
| 43 | continue |
| 44 | } |
| 45 | mut table := ast.new_table() |
| 46 | file_ast := parser.parse_file(ipath, mut table, .parse_comments, fpref) |
| 47 | result_ocontent := fmt.fmt(file_ast, mut table, fpref, false) |
| 48 | if expected_ocontent != result_ocontent { |
| 49 | fmt_bench.fail() |
| 50 | eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.')) |
| 51 | vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}') |
| 52 | os.write_file(vfmt_result_file, result_ocontent) or { panic(err) } |
| 53 | println(diff.compare_files(opath, vfmt_result_file) or { err.msg() }) |
| 54 | continue |
| 55 | } |
| 56 | fmt_bench.ok() |
| 57 | eprintln(fmt_bench.step_message_ok(ipath)) |
| 58 | } |
| 59 | fmt_bench.stop() |
| 60 | eprintln(term.h_divider('-')) |
| 61 | eprintln(fmt_bench.total_message(fmt_message)) |
| 62 | assert fmt_bench.nfail == 0 |
| 63 | } |
| 64 | |
| 65 | fn test_fmt() { |
| 66 | mut input_files := os.walk_ext(os.join_path(tdir, 'tests'), '_input.vv') |
| 67 | run_fmt(mut input_files) |
| 68 | |
| 69 | input_files = os.walk_ext(os.join_path(tdir, 'testdata', 'vmodules'), '_input.vv') |
| 70 | run_fmt(mut input_files) |
| 71 | } |
| 72 | |
| 73 | fn test_fmt_vmodules() { |
| 74 | mut vmodules_tdir := os.join_path(tdir, 'tests') |
| 75 | os.setenv('VMODULES', vmodules_tdir, true) |
| 76 | mut input_files := os.walk_ext(vmodules_tdir, '_input.vv') |
| 77 | run_fmt(mut input_files) |
| 78 | |
| 79 | vmodules_tdir = os.join_path(tdir, 'testdata', 'vmodules') |
| 80 | os.setenv('VMODULES', vmodules_tdir, true) |
| 81 | input_files = os.walk_ext(vmodules_tdir, '_input.vv') |
| 82 | run_fmt(mut input_files) |
| 83 | } |
| 84 | |