add tests

This commit is contained in:
kbkpbot 2025-09-13 17:39:31 +08:00
parent 98d207fdd0
commit 2a8875e692
7 changed files with 176 additions and 0 deletions

View file

@ -0,0 +1,69 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import os
import term
import benchmark
import v.ast
import v.fmt
import v.parser
import v.pref
import v.util.diff
import v.util.vtest
const vroot = @VEXEROOT
const tdir = os.join_path(vroot, 'vlib', 'v', 'fmt')
const fpref = &pref.Preferences{
is_fmt: true
}
fn run_fmt(mut input_files []string) {
fmt_message := 'vfmt new_int tests'
eprintln(term.header(fmt_message, '-'))
tmpfolder := os.temp_dir()
assert input_files.len > 0
input_files = vtest.filter_vtest_only(input_files)
if input_files.len == 0 {
// No need to produce a failing test here.
eprintln('no tests found with VTEST_ONLY filter set to: ' + os.getenv('VTEST_ONLY'))
exit(0)
}
mut fmt_bench := benchmark.new_benchmark()
fmt_bench.set_total_expected_steps(input_files.len)
for istep, ipath in input_files {
fmt_bench.cstep = istep
fmt_bench.step()
opath := ipath.replace('_input.vv', '_expected_new_int.vv')
if !os.exists(opath) {
// skip not exist files
continue
}
expected_ocontent := os.read_file(opath) or {
fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('cannot read from ${opath}'))
continue
}
mut table := ast.new_table()
file_ast := parser.parse_file(ipath, mut table, .parse_comments, fpref)
result_ocontent := fmt.fmt(file_ast, mut table, fpref, false, new_int: true)
if expected_ocontent != result_ocontent {
fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.'))
vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}')
os.write_file(vfmt_result_file, result_ocontent) or { panic(err) }
println(diff.compare_files(opath, vfmt_result_file) or { err.msg() })
continue
}
fmt_bench.ok()
eprintln(fmt_bench.step_message_ok(ipath))
}
fmt_bench.stop()
eprintln(term.h_divider('-'))
eprintln(fmt_bench.total_message(fmt_message))
assert fmt_bench.nfail == 0
}
fn test_new_int_fmt() {
mut input_files := os.walk_ext(os.join_path(tdir, 'tests'), '_input.vv')
run_fmt(mut input_files)
}

View file

@ -0,0 +1,68 @@
fn C.func(arg i32) i32
fn fn_variadic(arg int, args ...string) {
println('Do nothing')
}
fn fn_with_assign_stmts() {
_, _ := fn_with_multi_return()
}
fn fn_with_multi_return() (int, string) {
return 0, 'test'
}
fn voidfn() {
println('this is a function that does not return anything')
}
fn fn_with_1_arg(arg int) int {
return 0
}
fn fn_with_2a_args(arg1 int, arg2 int) int {
return 0
}
fn fn_with_2_args_to_be_shorten(arg1 int, arg2 int) int {
return 0
}
fn fn_with_2b_args(arg1 string, arg2 int) int {
return 0
}
fn fn_with_3_args(arg1 string, arg2 int, arg3 User) int {
return 0
}
fn (this User) fn_with_receiver() {
println('')
}
fn fn_with_option() ?int {
if true {
return error('true')
}
return 30
}
fn (f Foo) fn_with_option() ?int {
if true {
return error('true')
}
return 40
}
fn mut_array(mut a []int) {
println(1)
}
fn fn_with_ref_return() &Foo {
return &Foo{}
}
@[inline]
fn fn_with_flag() {
println('flag')
}

View file

@ -0,0 +1,5 @@
module main
fn C.abc(a i32, b i32, c charptr) i32
fn abc(a int, b int, c charptr) int

View file

@ -0,0 +1,6 @@
module main
fn C.abc(a int, b int, c charptr) int
fn abc(a int, b int, c charptr) int

View file

@ -0,0 +1,9 @@
@[translated]
module aa
fn C.abc(a int, b int)
fn abc(x int, y int) {
s := int(123)
dump(s)
}

View file

@ -0,0 +1,9 @@
@[translated]
module aa
fn C.abc(a i32, b i32)
fn abc(x i32, y i32) {
s := i32(123)
dump(s)
}

View file

@ -0,0 +1,10 @@
@[translated]
module aa
fn C.abc(a int, b int)
fn abc(x int, y int) {
s := int(123)
dump(s)
}