| 1 | // Copyright (c) 2025 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | import os |
| 5 | import benchmark |
| 6 | import v.ast |
| 7 | import v.fmt |
| 8 | import v.parser |
| 9 | import v.pref |
| 10 | import v.util.diff |
| 11 | import v.util.vtest |
| 12 | |
| 13 | const tmpfolder = os.temp_dir() |
| 14 | const fpref = &pref.Preferences{ |
| 15 | is_fmt: true |
| 16 | } |
| 17 | |
| 18 | fn test_new_int_fmt() { |
| 19 | suffix_for_expected := '_expected_new_int.vv' |
| 20 | suffix_for_input := '_input.vv' |
| 21 | mut input_files := os.walk_ext(os.join_path(@VEXEROOT, 'vlib/v/fmt/tests'), suffix_for_expected) |
| 22 | input_files = input_files.map(it.replace(suffix_for_expected, suffix_for_input)) |
| 23 | input_files = vtest.filter_vtest_only(input_files) |
| 24 | if input_files.len == 0 { |
| 25 | eprintln('no tests found with VTEST_ONLY filter set to: ' + os.getenv('VTEST_ONLY')) |
| 26 | exit(0) |
| 27 | } |
| 28 | mut fmt_bench := benchmark.new_benchmark() |
| 29 | fmt_bench.set_total_expected_steps(input_files.len) |
| 30 | for istep, ipath in input_files { |
| 31 | fmt_bench.cstep = istep |
| 32 | fmt_bench.step() |
| 33 | opath := ipath.replace('_input.vv', '_expected_new_int.vv') |
| 34 | expected_ocontent := os.read_file(opath) or { |
| 35 | fmt_bench.fail() |
| 36 | eprintln(fmt_bench.step_message_fail('cannot read from ${opath}')) |
| 37 | continue |
| 38 | } |
| 39 | mut table := ast.new_table() |
| 40 | file_ast := parser.parse_file(ipath, mut table, .parse_comments, fpref) |
| 41 | table.new_int = true |
| 42 | result_ocontent := fmt.fmt(file_ast, mut table, fpref, false) |
| 43 | if expected_ocontent != result_ocontent { |
| 44 | fmt_bench.fail() |
| 45 | eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected ${opath}.')) |
| 46 | vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}') |
| 47 | os.write_file(vfmt_result_file, result_ocontent) or { panic(err) } |
| 48 | println(diff.compare_files(opath, vfmt_result_file) or { err.msg() }) |
| 49 | continue |
| 50 | } |
| 51 | fmt_bench.ok() |
| 52 | eprintln(fmt_bench.step_message_ok(ipath)) |
| 53 | } |
| 54 | fmt_bench.stop() |
| 55 | println(fmt_bench.total_message('vfmt new_int tests')) |
| 56 | assert fmt_bench.nfail == 0 |
| 57 | } |
| 58 | |