cgen: fix comptime if attributes for `test_` functions (#14756)

master
Leo Developer 2022-06-13 20:22:25 +02:00 committed by GitHub
parent 5efa67906c
commit 67716b5b59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 11 deletions

View File

@ -210,6 +210,7 @@ mut:
generated_free_methods map[int]bool generated_free_methods map[int]bool
autofree_scope_stmts []string autofree_scope_stmts []string
use_segfault_handler bool = true use_segfault_handler bool = true
test_function_names []string
} }
// global or const variable definition string // global or const variable definition string
@ -368,6 +369,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string {
global_g.pcs << g.pcs global_g.pcs << g.pcs
global_g.json_types << g.json_types global_g.json_types << g.json_types
global_g.hotcode_fn_names << g.hotcode_fn_names global_g.hotcode_fn_names << g.hotcode_fn_names
global_g.test_function_names << g.test_function_names
unsafe { g.free_builders() } unsafe { g.free_builders() }
for k, v in g.autofree_methods { for k, v in g.autofree_methods {
global_g.autofree_methods[k] = v global_g.autofree_methods[k] = v
@ -5538,24 +5540,21 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
} }
} }
fn (g &Gen) get_all_test_function_names() []string { fn (g Gen) get_all_test_function_names() []string {
mut tfuncs := []string{} mut tfuncs := []string{}
mut tsuite_begin := '' mut tsuite_begin := ''
mut tsuite_end := '' mut tsuite_end := ''
for _, f in g.table.fns { for name in g.test_function_names {
if !f.is_test { if name.ends_with('.testsuite_begin') {
tsuite_begin = name
continue continue
} }
if f.name.ends_with('.testsuite_begin') { if name.contains('.test_') {
tsuite_begin = f.name tfuncs << name
continue continue
} }
if f.name.contains('.test_') { if name.ends_with('.testsuite_end') {
tfuncs << f.name tsuite_end = name
continue
}
if f.name.ends_with('.testsuite_end') {
tsuite_end = f.name
continue continue
} }
} }

View File

@ -32,6 +32,9 @@ fn (mut g Gen) fn_decl(node ast.FnDecl) {
if node.should_be_skipped { if node.should_be_skipped {
return return
} }
if node.is_test {
g.test_function_names << node.name
}
if node.ninstances == 0 && node.generic_names.len > 0 { if node.ninstances == 0 && node.generic_names.len > 0 {
$if trace_generics ? { $if trace_generics ? {
eprintln('skipping generic fn with no concrete instances: $node.mod $node.name') eprintln('skipping generic fn with no concrete instances: $node.mod $node.name')

View File

@ -0,0 +1,4 @@
[if customflag ?]
fn test_abcdef() {
println('this should not be in the c code')
}

View File

@ -0,0 +1 @@
println(_SLIT("this should be in the c code"));

View File

@ -0,0 +1,5 @@
// vtest vflags: -d customflag
[if customflag ?]
fn test_abcdef() {
println('this should be in the c code')
}