1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tests: filter test_ fns with params from the list of automatically run test functions (fix #13443)

This commit is contained in:
Delyan Angelov 2022-02-12 15:06:09 +02:00
parent 11a0df5bee
commit 799c95dc4e
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
8 changed files with 36 additions and 13 deletions

View File

@ -460,7 +460,7 @@ pub:
is_noreturn bool // true, when [noreturn] is used on a fn is_noreturn bool // true, when [noreturn] is used on a fn
is_manualfree bool // true, when [manualfree] is used on a fn is_manualfree bool // true, when [manualfree] is used on a fn
is_main bool // true for `fn main()` is_main bool // true for `fn main()`
is_test bool // true for `fn test_abcde` is_test bool // true for `fn test_abcde() {}`, false for `fn test_abc(x int) {}`, or for fns that do not start with test_
is_conditional bool // true for `[if abc] fn abc(){}` is_conditional bool // true for `[if abc] fn abc(){}`
is_exported bool // true for `[export: 'exact_C_name']` is_exported bool // true for `[export: 'exact_C_name']`
is_keep_alive bool // passed memory must not be freed (by GC) before function returns is_keep_alive bool // passed memory must not be freed (by GC) before function returns

View File

@ -270,7 +270,8 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
} }
} }
// TODO c.pref.is_vet // TODO c.pref.is_vet
if node.language == .v && !node.is_method && node.is_test { if c.file.is_test && (!node.is_method && (node.short_name.starts_with('test_')
|| node.short_name.starts_with('testsuite_'))) {
if !c.pref.is_test { if !c.pref.is_test {
// simple heuristic // simple heuristic
for st in node.stmts { for st in node.stmts {
@ -285,6 +286,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
if node.params.len != 0 { if node.params.len != 0 {
c.error('test functions should take 0 parameters', node.pos) c.error('test functions should take 0 parameters', node.pos)
} }
if node.return_type != ast.void_type_idx if node.return_type != ast.void_type_idx
&& node.return_type.clear_flag(.optional) != ast.void_type_idx { && node.return_type.clear_flag(.optional) != ast.void_type_idx {
c.error('test functions should either return nothing at all, or be marked to return `?`', c.error('test functions should either return nothing at all, or be marked to return `?`',

View File

@ -6304,6 +6304,9 @@ fn (g &Gen) get_all_test_function_names() []string {
mut tsuite_begin := '' mut tsuite_begin := ''
mut tsuite_end := '' mut tsuite_end := ''
for _, f in g.table.fns { for _, f in g.table.fns {
if !f.is_test {
continue
}
if f.name.ends_with('.testsuite_begin') { if f.name.ends_with('.testsuite_begin') {
tsuite_begin = f.name tsuite_begin = f.name
continue continue

View File

@ -383,6 +383,9 @@ fn (g &JsGen) get_all_test_function_names() []string {
mut tsuite_begin := '' mut tsuite_begin := ''
mut tsuite_end := '' mut tsuite_end := ''
for _, f in g.table.fns { for _, f in g.table.fns {
if !f.is_test {
continue
}
if f.name.ends_with('.testsuite_begin') { if f.name.ends_with('.testsuite_begin') {
tsuite_begin = f.name tsuite_begin = f.name
continue continue

View File

@ -361,10 +361,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
end_pos := p.prev_tok.pos() end_pos := p.prev_tok.pos()
short_fn_name := name short_fn_name := name
is_main := short_fn_name == 'main' && p.mod == 'main' is_main := short_fn_name == 'main' && p.mod == 'main'
mut is_test := (short_fn_name.starts_with('test_') || short_fn_name.starts_with('testsuite_')) is_test := (!is_method && params.len == 0) && p.inside_test_file
&& (p.file_base.ends_with('_test.v') && (short_fn_name.starts_with('test_') || short_fn_name.starts_with('testsuite_'))
|| p.file_base.all_before_last('.v').all_before_last('.').ends_with('_test'))
file_mode := p.file_backend_mode file_mode := p.file_backend_mode
// Register // Register
if is_method { if is_method {

View File

@ -162,19 +162,18 @@ pub fn (mut p Parser) set_path(path string) {
p.file_name = path p.file_name = path
p.file_base = os.base(path) p.file_base = os.base(path)
p.file_name_dir = os.dir(path) p.file_name_dir = os.dir(path)
if p.file_name_dir.contains('vlib') { p.inside_vlib_file = p.file_name_dir.contains('vlib')
p.inside_vlib_file = true p.inside_test_file = p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv')
} || p.file_base.all_before_last('.v').all_before_last('.').ends_with('_test')
hash := fnv1a.sum64_string(path) hash := fnv1a.sum64_string(path)
p.unique_prefix = hash.hex_full() p.unique_prefix = hash.hex_full()
if p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv') {
p.inside_test_file = true p.file_backend_mode = .v
}
before_dot_v := path.all_before_last('.v') // also works for .vv and .vsh before_dot_v := path.all_before_last('.v') // also works for .vv and .vsh
language := before_dot_v.all_after_last('.') language := before_dot_v.all_after_last('.')
language_with_underscore := before_dot_v.all_after_last('_') language_with_underscore := before_dot_v.all_after_last('_')
if language == before_dot_v && language_with_underscore == before_dot_v { if language == before_dot_v && language_with_underscore == before_dot_v {
p.file_backend_mode = .v
return return
} }
actual_language := if language == before_dot_v { language_with_underscore } else { language } actual_language := if language == before_dot_v { language_with_underscore } else { language }

View File

@ -0,0 +1,5 @@
module main
fn test_any_name() {
println(@FN)
}

View File

@ -0,0 +1,13 @@
fn main() {
println(@FN)
println('OK')
}
fn test_fn_in_normal_v_file() {
println(@FN)
}
fn test_any(any_name int) int {
println(@FN)
return 0
}