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

cgen: allow -printfn main for _test.v files too

This commit is contained in:
Delyan Angelov 2020-07-03 17:53:48 +03:00
parent 652380c5fd
commit 8adaf4efd4
2 changed files with 44 additions and 40 deletions

View File

@ -144,7 +144,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
if g.file.path.ends_with('_test.v') {
g.is_test = is_test
}
if g.file.path == '' || is_test || !g.pref.autofree {
if g.file.path == '' || !g.pref.autofree {
// cgen test or building V
// println('autofree=false')
g.autofree = false
@ -3650,44 +3650,6 @@ fn (g Gen) type_default(typ table.Type) string {
*/
}
pub fn (mut g Gen) write_tests_main() {
g.includes.writeln('#include <setjmp.h> // write_tests_main')
g.definitions.writeln('int g_test_oks = 0;')
g.definitions.writeln('int g_test_fails = 0;')
g.definitions.writeln('jmp_buf g_jump_buffer;')
$if windows {
g.writeln('int wmain() {')
} $else {
g.writeln('int main() {')
}
g.writeln('\t_vinit();')
g.writeln('')
all_tfuncs := g.get_all_test_function_names()
if g.pref.is_stats {
g.writeln('\tmain__BenchedTests bt = main__start_testing($all_tfuncs.len, tos_lit("$g.pref.path"));')
}
for t in all_tfuncs {
g.writeln('')
if g.pref.is_stats {
g.writeln('\tmain__BenchedTests_testing_step_start(&bt, tos_lit("$t"));')
}
g.writeln('\tif (!setjmp(g_jump_buffer)) ${t}();')
if g.pref.is_stats {
g.writeln('\tmain__BenchedTests_testing_step_end(&bt);')
}
}
g.writeln('')
if g.pref.is_stats {
g.writeln('\tmain__BenchedTests_end_testing(&bt);')
}
g.writeln('')
if g.autofree {
g.writeln('\t_vcleanup();')
}
g.writeln('\treturn g_test_fails > 0;')
g.writeln('}')
}
fn (g Gen) get_all_test_function_names() []string {
mut tfuncs := []string{}
mut tsuite_begin := ''

View File

@ -14,7 +14,7 @@ pub fn (mut g Gen) gen_c_main() {
g.gen_c_main_footer()
if g.pref.printfn_list.len > 0 && 'main' in g.pref.printfn_list {
println(g.out.after(main_fn_start_pos))
}
}
}
fn (mut g Gen) gen_c_main_header() {
@ -67,3 +67,45 @@ pub fn (mut g Gen) gen_c_main_footer() {
g.writeln('\treturn 0;')
g.writeln('}')
}
pub fn (mut g Gen) write_tests_main() {
g.includes.writeln('#include <setjmp.h> // write_tests_main')
g.definitions.writeln('int g_test_oks = 0;')
g.definitions.writeln('int g_test_fails = 0;')
g.definitions.writeln('jmp_buf g_jump_buffer;')
main_fn_start_pos := g.out.len
$if windows {
g.writeln('int wmain() {')
} $else {
g.writeln('int main() {')
}
g.writeln('\t_vinit();')
g.writeln('')
all_tfuncs := g.get_all_test_function_names()
if g.pref.is_stats {
g.writeln('\tmain__BenchedTests bt = main__start_testing($all_tfuncs.len, tos_lit("$g.pref.path"));')
}
for t in all_tfuncs {
g.writeln('')
if g.pref.is_stats {
g.writeln('\tmain__BenchedTests_testing_step_start(&bt, tos_lit("$t"));')
}
g.writeln('\tif (!setjmp(g_jump_buffer)) ${t}();')
if g.pref.is_stats {
g.writeln('\tmain__BenchedTests_testing_step_end(&bt);')
}
}
g.writeln('')
if g.pref.is_stats {
g.writeln('\tmain__BenchedTests_end_testing(&bt);')
}
g.writeln('')
if g.autofree {
g.writeln('\t_vcleanup();')
}
g.writeln('\treturn g_test_fails > 0;')
g.writeln('}')
if g.pref.printfn_list.len > 0 && 'main' in g.pref.printfn_list {
println(g.out.after(main_fn_start_pos))
}
}