js: draft support for compiling `-b js x_test.v`
							parent
							
								
									90b9b9d755
								
							
						
					
					
						commit
						b0a721b2ec
					
				|  | @ -323,6 +323,14 @@ pub fn (s string) to_lower() string { | ||||||
| 	return result | 	return result | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // TODO: check if that behaves the same as V's own string.replace(old_sub,new_sub):
 | ||||||
|  | pub fn (s string) replace(old_sub string, new_sub string) string { | ||||||
|  | 	mut result := '' | ||||||
|  | 	#result = new string( s.str.replaceAll(old_sub.str, new_sub.str) ) | ||||||
|  | 
 | ||||||
|  | 	return result | ||||||
|  | } | ||||||
|  | 
 | ||||||
| pub fn (s string) to_upper() string { | pub fn (s string) to_upper() string { | ||||||
| 	mut result := '' | 	mut result := '' | ||||||
| 	#let str = s.str.toUpperCase() | 	#let str = s.str.toUpperCase() | ||||||
|  |  | ||||||
|  | @ -237,7 +237,10 @@ pub fn (v &Builder) get_user_files() []string { | ||||||
| 	mut user_files := []string{} | 	mut user_files := []string{} | ||||||
| 	// See cmd/tools/preludes/README.md for more info about what preludes are
 | 	// See cmd/tools/preludes/README.md for more info about what preludes are
 | ||||||
| 	vroot := os.dir(pref.vexe_path()) | 	vroot := os.dir(pref.vexe_path()) | ||||||
| 	preludes_path := os.join_path(vroot, 'vlib', 'v', 'preludes') | 	mut preludes_path := os.join_path(vroot, 'vlib', 'v', 'preludes') | ||||||
|  | 	if v.pref.backend == .js_node { | ||||||
|  | 		preludes_path = os.join_path(vroot, 'vlib', 'v', 'preludes_js') | ||||||
|  | 	} | ||||||
| 	if v.pref.is_livemain || v.pref.is_liveshared { | 	if v.pref.is_livemain || v.pref.is_liveshared { | ||||||
| 		user_files << os.join_path(preludes_path, 'live.v') | 		user_files << os.join_path(preludes_path, 'live.v') | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -0,0 +1,87 @@ | ||||||
|  | module main | ||||||
|  | 
 | ||||||
|  | import os_js | ||||||
|  | 
 | ||||||
|  | // VAssertMetaInfo is used during assertions. An instance of it
 | ||||||
|  | // is filled in by compile time generated code, when an assertion fails.
 | ||||||
|  | pub struct VAssertMetaInfo { | ||||||
|  | pub: | ||||||
|  | 	fpath   string // the source file path of the assertion
 | ||||||
|  | 	line_nr int    // the line number of the assertion
 | ||||||
|  | 	fn_name string // the function name in which the assertion is
 | ||||||
|  | 	src     string // the actual source line of the assertion
 | ||||||
|  | 	op      string // the operation of the assertion, i.e. '==', '<', 'call', etc ...
 | ||||||
|  | 	llabel  string // the left side of the infix expressions as source
 | ||||||
|  | 	rlabel  string // the right side of the infix expressions as source
 | ||||||
|  | 	lvalue  string // the stringified *actual value* of the left side of a failed assertion
 | ||||||
|  | 	rvalue  string // the stringified *actual value* of the right side of a failed assertion
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | const use_relative_paths = can_use_relative_paths() | ||||||
|  | 
 | ||||||
|  | fn can_use_relative_paths() bool { | ||||||
|  | 	return match os_js.getenv('VERROR_PATHS') { | ||||||
|  | 		'absolute' { false } | ||||||
|  | 		else { true } | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn myeprintln(s string) { | ||||||
|  | 	println(s) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // //////////////////////////////////////////////////////////////////
 | ||||||
|  | // / This file will get compiled as part of the main program,
 | ||||||
|  | // / for a _test.v file.
 | ||||||
|  | // / The methods defined here are called back by the test program's
 | ||||||
|  | // / assert statements, on each success/fail. The goal is to make
 | ||||||
|  | // / customizing the look & feel of the assertions results easier,
 | ||||||
|  | // / since it is done in normal V code, instead of in embedded C ...
 | ||||||
|  | // //////////////////////////////////////////////////////////////////
 | ||||||
|  | // TODO copy pasta builtin.v fn ___print_assert_failure
 | ||||||
|  | fn cb_assertion_failed(i VAssertMetaInfo) { | ||||||
|  | 	filepath := if use_relative_paths { i.fpath } else { os_js.real_path(i.fpath) } | ||||||
|  | 	mut final_filepath := filepath + ':${i.line_nr + 1}:' | ||||||
|  | 	mut final_funcname := 'fn ' + i.fn_name.replace('main.', '').replace('__', '.') | ||||||
|  | 	final_src := 'assert ' + i.src | ||||||
|  | 	myeprintln('$final_filepath $final_funcname') | ||||||
|  | 	if i.op.len > 0 && i.op != 'call' { | ||||||
|  | 		mut lvtitle := '    Left value:' | ||||||
|  | 		mut rvtitle := '    Right value:' | ||||||
|  | 		mut slvalue := '$i.lvalue' | ||||||
|  | 		mut srvalue := '$i.rvalue' | ||||||
|  | 		cutoff_limit := 30 | ||||||
|  | 		if slvalue.len > cutoff_limit || srvalue.len > cutoff_limit { | ||||||
|  | 			myeprintln('  > $final_src') | ||||||
|  | 			myeprintln(lvtitle) | ||||||
|  | 			myeprintln('      $slvalue') | ||||||
|  | 			myeprintln(rvtitle) | ||||||
|  | 			myeprintln('      $srvalue') | ||||||
|  | 		} else { | ||||||
|  | 			myeprintln('   > $final_src') | ||||||
|  | 			myeprintln(' $lvtitle $slvalue') | ||||||
|  | 			myeprintln('$rvtitle $srvalue') | ||||||
|  | 		} | ||||||
|  | 	} else { | ||||||
|  | 		myeprintln('    $final_src') | ||||||
|  | 	} | ||||||
|  | 	myeprintln('') | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn cb_assertion_ok(i &VAssertMetaInfo) { | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn cb_propagate_test_error(line_nr int, file string, mod string, fn_name string, errmsg string) { | ||||||
|  | 	filepath := if use_relative_paths { file } else { os_js.real_path(file) } | ||||||
|  | 	mut final_filepath := filepath + ':$line_nr:' | ||||||
|  | 	mut final_funcname := 'fn ' + fn_name.replace('main.', '').replace('__', '.') | ||||||
|  | 	final_msg := errmsg | ||||||
|  | 	myeprintln('$final_filepath $final_funcname failed propagation with error: $final_msg') | ||||||
|  | 	// TODO: implement os_js.is_file and os_js.read_lines:
 | ||||||
|  | 	/* | ||||||
|  | 	if os_js.is_file(file) { | ||||||
|  | 		source_lines := os_js.read_lines(file) or { []string{len: line_nr + 1} } | ||||||
|  | 		myeprintln('${line_nr:5} | ${source_lines[line_nr - 1]}') | ||||||
|  | 	} | ||||||
|  | 	*/ | ||||||
|  | } | ||||||
|  | @ -0,0 +1,80 @@ | ||||||
|  | module main | ||||||
|  | 
 | ||||||
|  | // /////////////////////////////////////////////////////////////////////
 | ||||||
|  | // / This file will get compiled as a part of the same module,
 | ||||||
|  | // / in which a given _test.v file is, when v is given -stats argument
 | ||||||
|  | // / The methods defined here are called back by the test program's
 | ||||||
|  | // / main function, so that customizing the look & feel of the results
 | ||||||
|  | // / is easy, since it is done in normal V code, instead of in embedded C ...
 | ||||||
|  | // /////////////////////////////////////////////////////////////////////
 | ||||||
|  | const inner_indent = '     ' | ||||||
|  | 
 | ||||||
|  | struct BenchedTests { | ||||||
|  | mut: | ||||||
|  | 	oks                   int | ||||||
|  | 	fails                 int | ||||||
|  | 	test_suit_file        string | ||||||
|  | 	step_func_name        string | ||||||
|  | 	total_number_of_tests int | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // ///////////////////////////////////////////////////////////////////
 | ||||||
|  | // Called at the start of the test program produced by `v -stats file_test.v`
 | ||||||
|  | fn start_testing(total_number_of_tests int, vfilename string) BenchedTests { | ||||||
|  | 	mut benched_tests_res := BenchedTests{} | ||||||
|  | 	benched_tests_res.total_number_of_tests = total_number_of_tests | ||||||
|  | 	benched_tests_res.test_suit_file = vfilename | ||||||
|  | 	println('running tests in: $benched_tests_res.test_suit_file') | ||||||
|  | 	return benched_tests_res | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Called before each test_ function, defined in file_test.v
 | ||||||
|  | fn (mut b BenchedTests) testing_step_start(stepfunc string) { | ||||||
|  | 	b.step_func_name = stepfunc.replace('main.', '').replace('__', '.') | ||||||
|  | 	b.oks = C.g_test_oks | ||||||
|  | 	b.fails = C.g_test_fails | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Called after each test_ function, defined in file_test.v
 | ||||||
|  | fn (mut b BenchedTests) testing_step_end() { | ||||||
|  | 	ok_diff := C.g_test_oks - b.oks | ||||||
|  | 	fail_diff := C.g_test_fails - b.fails | ||||||
|  | 	// ////////////////////////////////////////////////////////////////
 | ||||||
|  | 	if ok_diff == 0 && fail_diff == 0 { | ||||||
|  | 		println(inner_indent + '   NO asserts | ' + b.fn_name()) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	// ////////////////////////////////////////////////////////////////
 | ||||||
|  | 	if ok_diff > 0 { | ||||||
|  | 		// b.bench.ok_many(ok_diff)
 | ||||||
|  | 	} | ||||||
|  | 	if fail_diff > 0 { | ||||||
|  | 		// b.bench.fail_many(fail_diff)
 | ||||||
|  | 	} | ||||||
|  | 	// ////////////////////////////////////////////////////////////////
 | ||||||
|  | 	if ok_diff > 0 && fail_diff == 0 { | ||||||
|  | 		println(inner_indent + nasserts(ok_diff) + b.fn_name()) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	if fail_diff > 0 { | ||||||
|  | 		println(inner_indent + nasserts(fail_diff) + b.fn_name()) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn (b &BenchedTests) fn_name() string { | ||||||
|  | 	return b.step_func_name + '()' | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Called at the end of the test program produced by `v -stats file_test.v`
 | ||||||
|  | fn (mut b BenchedTests) end_testing() { | ||||||
|  | 	println(inner_indent + 'running V tests in "' + b.test_suit_file + '"') | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // ///////////////////////////////////////////////////////////////////
 | ||||||
|  | fn nasserts(n int) string { | ||||||
|  | 	if n == 1 { | ||||||
|  | 		return '${n:5d} assert  | ' | ||||||
|  | 	} | ||||||
|  | 	return '${n:5d} asserts | ' | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue