Skip to content

Commit 799c95d

Browse files
committed
tests: filter test_ fns with params from the list of automatically run test functions (fix #13443)
1 parent 11a0df5 commit 799c95d

File tree

8 files changed

+36
-13
lines changed

8 files changed

+36
-13
lines changed

vlib/v/ast/ast.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ pub:
460460
is_noreturn bool // true, when [noreturn] is used on a fn
461461
is_manualfree bool // true, when [manualfree] is used on a fn
462462
is_main bool // true for `fn main()`
463-
is_test bool // true for `fn test_abcde`
463+
is_test bool // true for `fn test_abcde() {}`, false for `fn test_abc(x int) {}`, or for fns that do not start with test_
464464
is_conditional bool // true for `[if abc] fn abc(){}`
465465
is_exported bool // true for `[export: 'exact_C_name']`
466466
is_keep_alive bool // passed memory must not be freed (by GC) before function returns

vlib/v/checker/fn.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
270270
}
271271
}
272272
// TODO c.pref.is_vet
273-
if node.language == .v && !node.is_method && node.is_test {
273+
if c.file.is_test && (!node.is_method && (node.short_name.starts_with('test_')
274+
|| node.short_name.starts_with('testsuite_'))) {
274275
if !c.pref.is_test {
275276
// simple heuristic
276277
for st in node.stmts {
@@ -285,6 +286,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
285286
if node.params.len != 0 {
286287
c.error('test functions should take 0 parameters', node.pos)
287288
}
289+
288290
if node.return_type != ast.void_type_idx
289291
&& node.return_type.clear_flag(.optional) != ast.void_type_idx {
290292
c.error('test functions should either return nothing at all, or be marked to return `?`',

vlib/v/gen/c/cgen.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6304,6 +6304,9 @@ fn (g &Gen) get_all_test_function_names() []string {
63046304
mut tsuite_begin := ''
63056305
mut tsuite_end := ''
63066306
for _, f in g.table.fns {
6307+
if !f.is_test {
6308+
continue
6309+
}
63076310
if f.name.ends_with('.testsuite_begin') {
63086311
tsuite_begin = f.name
63096312
continue

vlib/v/gen/js/js.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ fn (g &JsGen) get_all_test_function_names() []string {
383383
mut tsuite_begin := ''
384384
mut tsuite_end := ''
385385
for _, f in g.table.fns {
386+
if !f.is_test {
387+
continue
388+
}
386389
if f.name.ends_with('.testsuite_begin') {
387390
tsuite_begin = f.name
388391
continue

vlib/v/parser/fn.v

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
361361
end_pos := p.prev_tok.pos()
362362
short_fn_name := name
363363
is_main := short_fn_name == 'main' && p.mod == 'main'
364-
mut is_test := (short_fn_name.starts_with('test_') || short_fn_name.starts_with('testsuite_'))
365-
&& (p.file_base.ends_with('_test.v')
366-
|| p.file_base.all_before_last('.v').all_before_last('.').ends_with('_test'))
367-
364+
is_test := (!is_method && params.len == 0) && p.inside_test_file
365+
&& (short_fn_name.starts_with('test_') || short_fn_name.starts_with('testsuite_'))
368366
file_mode := p.file_backend_mode
369367
// Register
370368
if is_method {

vlib/v/parser/parser.v

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,18 @@ pub fn (mut p Parser) set_path(path string) {
162162
p.file_name = path
163163
p.file_base = os.base(path)
164164
p.file_name_dir = os.dir(path)
165-
if p.file_name_dir.contains('vlib') {
166-
p.inside_vlib_file = true
167-
}
165+
p.inside_vlib_file = p.file_name_dir.contains('vlib')
166+
p.inside_test_file = p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv')
167+
|| p.file_base.all_before_last('.v').all_before_last('.').ends_with('_test')
168+
168169
hash := fnv1a.sum64_string(path)
169170
p.unique_prefix = hash.hex_full()
170-
if p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv') {
171-
p.inside_test_file = true
172-
}
171+
172+
p.file_backend_mode = .v
173173
before_dot_v := path.all_before_last('.v') // also works for .vv and .vsh
174174
language := before_dot_v.all_after_last('.')
175175
language_with_underscore := before_dot_v.all_after_last('_')
176176
if language == before_dot_v && language_with_underscore == before_dot_v {
177-
p.file_backend_mode = .v
178177
return
179178
}
180179
actual_language := if language == before_dot_v { language_with_underscore } else { language }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module main
2+
3+
fn test_any_name() {
4+
println(@FN)
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
println(@FN)
3+
println('OK')
4+
}
5+
6+
fn test_fn_in_normal_v_file() {
7+
println(@FN)
8+
}
9+
10+
fn test_any(any_name int) int {
11+
println(@FN)
12+
return 0
13+
}

0 commit comments

Comments
 (0)