v: skip asserts in -prod mode inside non _test.v files
parent
84fe2d8c6e
commit
68c3eccec5
|
@ -548,6 +548,7 @@ pub:
|
||||||
bytes int // number of processed source code bytes
|
bytes int // number of processed source code bytes
|
||||||
mod Module // the module of the source file (from `module xyz` at the top)
|
mod Module // the module of the source file (from `module xyz` at the top)
|
||||||
global_scope &Scope
|
global_scope &Scope
|
||||||
|
is_test bool // true for _test.v files
|
||||||
pub mut:
|
pub mut:
|
||||||
scope &Scope
|
scope &Scope
|
||||||
stmts []Stmt // all the statements in the source file
|
stmts []Stmt // all the statements in the source file
|
||||||
|
@ -1245,7 +1246,8 @@ pub struct AssertStmt {
|
||||||
pub:
|
pub:
|
||||||
pos token.Position
|
pos token.Position
|
||||||
pub mut:
|
pub mut:
|
||||||
expr Expr
|
expr Expr
|
||||||
|
is_used bool // asserts are used in _test.v files, as well as in non -prod builds of all files
|
||||||
}
|
}
|
||||||
|
|
||||||
// `if [x := opt()] {`
|
// `if [x := opt()] {`
|
||||||
|
|
|
@ -6,6 +6,9 @@ module c
|
||||||
import v.ast
|
import v.ast
|
||||||
|
|
||||||
fn (mut g Gen) gen_assert_stmt(original_assert_statement ast.AssertStmt) {
|
fn (mut g Gen) gen_assert_stmt(original_assert_statement ast.AssertStmt) {
|
||||||
|
if !original_assert_statement.is_used {
|
||||||
|
return
|
||||||
|
}
|
||||||
mut node := original_assert_statement
|
mut node := original_assert_statement
|
||||||
g.writeln('// assert')
|
g.writeln('// assert')
|
||||||
if mut node.expr is ast.InfixExpr {
|
if mut node.expr is ast.InfixExpr {
|
||||||
|
|
|
@ -613,6 +613,9 @@ fn (mut g JsGen) expr(node ast.Expr) {
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
fn (mut g JsGen) gen_assert_stmt(a ast.AssertStmt) {
|
fn (mut g JsGen) gen_assert_stmt(a ast.AssertStmt) {
|
||||||
|
if !a.is_used {
|
||||||
|
return
|
||||||
|
}
|
||||||
g.writeln('// assert')
|
g.writeln('// assert')
|
||||||
g.write('if( ')
|
g.write('if( ')
|
||||||
g.expr(a.expr)
|
g.expr(a.expr)
|
||||||
|
|
|
@ -54,8 +54,10 @@ pub fn (mut w Walker) stmt(node ast.Stmt) {
|
||||||
w.asm_io(node.input)
|
w.asm_io(node.input)
|
||||||
}
|
}
|
||||||
ast.AssertStmt {
|
ast.AssertStmt {
|
||||||
w.expr(node.expr)
|
if node.is_used {
|
||||||
w.n_asserts++
|
w.expr(node.expr)
|
||||||
|
w.n_asserts++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ast.AssignStmt {
|
ast.AssignStmt {
|
||||||
w.exprs(node.left)
|
w.exprs(node.left)
|
||||||
|
|
|
@ -31,6 +31,7 @@ mut:
|
||||||
peek_tok token.Token
|
peek_tok token.Token
|
||||||
table &ast.Table
|
table &ast.Table
|
||||||
language ast.Language
|
language ast.Language
|
||||||
|
inside_test_file bool // when inside _test.v or _test.vv file
|
||||||
inside_if bool
|
inside_if bool
|
||||||
inside_if_expr bool
|
inside_if_expr bool
|
||||||
inside_ct_if_expr bool
|
inside_ct_if_expr bool
|
||||||
|
@ -79,6 +80,7 @@ mut:
|
||||||
pub fn parse_stmt(text string, table &ast.Table, scope &ast.Scope) ast.Stmt {
|
pub fn parse_stmt(text string, table &ast.Table, scope &ast.Scope) ast.Stmt {
|
||||||
mut p := Parser{
|
mut p := Parser{
|
||||||
scanner: scanner.new_scanner(text, .skip_comments, &pref.Preferences{})
|
scanner: scanner.new_scanner(text, .skip_comments, &pref.Preferences{})
|
||||||
|
inside_test_file: true
|
||||||
table: table
|
table: table
|
||||||
pref: &pref.Preferences{}
|
pref: &pref.Preferences{}
|
||||||
scope: scope
|
scope: scope
|
||||||
|
@ -138,6 +140,9 @@ pub fn (mut p Parser) set_path(path string) {
|
||||||
p.file_name = path
|
p.file_name = path
|
||||||
p.file_base = os.base(path)
|
p.file_base = os.base(path)
|
||||||
p.file_name_dir = os.dir(path)
|
p.file_name_dir = os.dir(path)
|
||||||
|
if p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv') {
|
||||||
|
p.inside_test_file = true
|
||||||
|
}
|
||||||
before_dot_v := path.before('.v') // also works for .vv and .vsh
|
before_dot_v := path.before('.v') // also works for .vv and .vsh
|
||||||
language := before_dot_v.all_after_last('.')
|
language := before_dot_v.all_after_last('.')
|
||||||
langauge_with_underscore := before_dot_v.all_after_last('_')
|
langauge_with_underscore := before_dot_v.all_after_last('_')
|
||||||
|
@ -276,6 +281,7 @@ pub fn (mut p Parser) parse() ast.File {
|
||||||
return ast.File{
|
return ast.File{
|
||||||
path: p.file_name
|
path: p.file_name
|
||||||
path_base: p.file_base
|
path_base: p.file_base
|
||||||
|
is_test: p.inside_test_file
|
||||||
lines: p.scanner.line_nr
|
lines: p.scanner.line_nr
|
||||||
bytes: p.scanner.text.len
|
bytes: p.scanner.text.len
|
||||||
mod: module_decl
|
mod: module_decl
|
||||||
|
@ -686,6 +692,7 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
|
||||||
return ast.AssertStmt{
|
return ast.AssertStmt{
|
||||||
expr: expr
|
expr: expr
|
||||||
pos: pos
|
pos: pos
|
||||||
|
is_used: p.inside_test_file || !p.pref.is_prod
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.key_for {
|
.key_for {
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() {
|
||||||
|
assert 1 > 2
|
||||||
|
println('finished')
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
finished
|
|
@ -41,7 +41,7 @@ fn diff_files(file_result string, file_expected string) string {
|
||||||
|
|
||||||
pub fn run_repl_file(wd string, vexec string, file string) ?string {
|
pub fn run_repl_file(wd string, vexec string, file string) ?string {
|
||||||
vexec_folder := os.dir(vexec) + os.path_separator
|
vexec_folder := os.dir(vexec) + os.path_separator
|
||||||
fcontent := os.read_file(file) or { return error('Could not read file $file') }
|
fcontent := os.read_file(file) or { return error('Could not read repl file $file') }
|
||||||
content := fcontent.replace('\r', '')
|
content := fcontent.replace('\r', '')
|
||||||
input := content.all_before('===output===\n')
|
input := content.all_before('===output===\n')
|
||||||
output := content.all_after('===output===\n').trim_right('\n\r')
|
output := content.all_after('===output===\n').trim_right('\n\r')
|
||||||
|
@ -81,7 +81,7 @@ $diff
|
||||||
pub fn run_prod_file(wd string, vexec string, file string) ?string {
|
pub fn run_prod_file(wd string, vexec string, file string) ?string {
|
||||||
file_expected := '${file}.expected.txt'
|
file_expected := '${file}.expected.txt'
|
||||||
f_expected_content := os.read_file(file_expected) or {
|
f_expected_content := os.read_file(file_expected) or {
|
||||||
return error('Could not read file $file')
|
return error('Could not read expected prod file $file_expected')
|
||||||
}
|
}
|
||||||
expected_content := f_expected_content.replace('\r', '')
|
expected_content := f_expected_content.replace('\r', '')
|
||||||
cmd := '"$vexec" -prod run "$file"'
|
cmd := '"$vexec" -prod run "$file"'
|
||||||
|
|
Loading…
Reference in New Issue