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

compiler: move prelude files to tools/preludes/

This commit is contained in:
Delyan Angelov
2020-01-08 18:57:41 +02:00
committed by Alexander Medvednikov
parent 56421beb8a
commit 0d93eeb3fe
8 changed files with 38 additions and 6 deletions

28
tools/preludes/README.md Normal file
View File

@@ -0,0 +1,28 @@
# V preludes:
The tools/preludes/ contains small v code snippets, that V uses when
compiling certain v programs. V adds the files below automatically itself.
Each file is used in different situations (see below).
NB: preludes are *NOT* intended to be used by user programs/modules.
The folder tools/preludes/ is *NOT* a v module.
## Details:
### tools/preludes/live_main.v
Used when compiling live programs. This file is used by the main executable
live program, that starts the file change monitoring thread. Each live program
needs module `os` and module `time`, in order for the background file change
monitoring thread to work properly.
### tools/preludes/live_shared.v
Used when compiling live programs, for the shared library portion of the live
programs, that is reloaded each time the code is changed.
### tools/preludes/tests_assertions.v
Used when compiling `_test.v` programs.
It specifies how failed assertions will look.
### tools/preludes/tests_with_stats.v
Used when compiling `_test.v` programs with -stats option.
It specifies how the result will appear ('assert' vs 'asserts' and so on).

View File

@@ -0,0 +1,9 @@
module main
import os
import time
const (
os_used = os.MAX_PATH
time_used = time.now()
)

View File

@@ -0,0 +1,9 @@
module main
import os
import time
const (
os_used = os.MAX_PATH
time_used = time.now()
)

View File

@@ -0,0 +1,36 @@
module main
import os
import term
// //////////////////////////////////////////////////////////////////
// / 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 ...
// //////////////////////////////////////////////////////////////////
fn cb_assertion_failed(filename string, line int, sourceline string, funcname string) {
color_on := term.can_show_color_on_stderr()
use_relative_paths := match os.getenv('VERROR_PATHS') {
'absolute'{
false
}
else {
true}
}
final_filename := if use_relative_paths { filename } else { os.realpath(filename) }
final_funcname := funcname.replace('main__', '').replace('__', '.')
mut fail_message := 'FAILED assertion'
if color_on {
fail_message = term.bold(term.red(fail_message))
}
eprintln('$final_filename:$line: $fail_message')
eprintln('Function: $final_funcname')
eprintln('Source : $sourceline')
}
fn cb_assertion_ok(filename string, line int, sourceline string, funcname string) {
// do nothing for now on an OK assertion
// println('OK ${line:5d}|$sourceline ')
}

View File

@@ -0,0 +1,104 @@
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, generated by compiler/main.v so that customizing the
// / look & feel of the results is easy, since it is done in normal V
// / code, instead of in embedded C ...
// /////////////////////////////////////////////////////////////////////
import (
filepath
benchmark
)
const (
INNER_INDENT = ' '
)
struct BenchedTests {
mut:
oks int
fails int
test_suit_file string
step_func_name string
bench benchmark.Benchmark
}
// ///////////////////////////////////////////////////////////////////
// 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 b := BenchedTests{
bench: benchmark.new_benchmark()
}
b.bench.set_total_expected_steps(total_number_of_tests)
b.test_suit_file = vfilename
println('running tests in: $b.test_suit_file')
return b
}
// Called before each test_ function, defined in file_test.v
fn (b mut 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
b.bench.step()
}
// Called after each test_ function, defined in file_test.v
fn (b mut 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 {
b.bench.neither_fail_nor_ok()
println(INNER_INDENT + b.bench.step_message_ok('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 + b.bench.step_message_ok(nasserts(ok_diff)) + b.fn_name())
return
}
if fail_diff > 0 {
println(INNER_INDENT + b.bench.step_message_fail(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 (b mut BenchedTests) end_testing() {
b.bench.stop()
println(INNER_INDENT + b.bench.total_message('running V tests in "' + filepath.filename(b.test_suit_file) + '"'))
}
// ///////////////////////////////////////////////////////////////////
fn nasserts(n int) string {
if n == 0 {
return '${n:2d} asserts | '
}
if n == 1 {
return '${n:2d} assert | '
}
if n < 10 {
return '${n:2d} asserts | '
}
if n < 100 {
return '${n:3d} asserts | '
}
if n < 1000 {
return '${n:4d} asserts | '
}
return '${n:5d} asserts | '
}