mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
tools,fmt: fix vbin2v.v; extract fmt_bin2v_test.v from fmt_keep_test.v
This commit is contained in:
parent
5e37798560
commit
820f53caa3
4 changed files with 71 additions and 48 deletions
|
@ -36,27 +36,26 @@ fn (context Context) header() string {
|
||||||
header_s += '// v bin2v ${allfiles} ${soptions}\n'
|
header_s += '// v bin2v ${allfiles} ${soptions}\n'
|
||||||
header_s += '// Please, do not edit this file.\n'
|
header_s += '// Please, do not edit this file.\n'
|
||||||
header_s += '// Your changes may be overwritten.\n'
|
header_s += '// Your changes may be overwritten.\n'
|
||||||
header_s += 'const (\n'
|
|
||||||
return header_s
|
return header_s
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (context Context) footer() string {
|
fn (context Context) footer() string {
|
||||||
return ')\n'
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (context Context) file2v(bname string, fbytes []u8, bn_max int) string {
|
fn (context Context) file2v(bname string, fbytes []u8, bn_max int) string {
|
||||||
mut sb := strings.new_builder(1000)
|
mut sb := strings.new_builder(1000)
|
||||||
bn_diff_len := bn_max - bname.len
|
bn_diff_len := bn_max - bname.len
|
||||||
sb.write_string('\t${bname}_len' + ' '.repeat(bn_diff_len - 4) + ' = ${fbytes.len}\n')
|
sb.write_string('const ${bname}_len' + ' = ${fbytes.len}\n')
|
||||||
fbyte := fbytes[0]
|
fbyte := fbytes[0]
|
||||||
bnmae_line := '\t${bname}' + ' '.repeat(bn_diff_len) + ' = [u8(${fbyte}), '
|
bnmae_line := 'const ${bname}' + ' = [u8(${fbyte}), '
|
||||||
sb.write_string(bnmae_line)
|
sb.write_string(bnmae_line)
|
||||||
mut line_len := bnmae_line.len + 3
|
mut line_len := bnmae_line.len + 3
|
||||||
for i := 1; i < fbytes.len; i++ {
|
for i := 1; i < fbytes.len; i++ {
|
||||||
b := int(fbytes[i]).str()
|
b := int(fbytes[i]).str()
|
||||||
if line_len > 94 {
|
if line_len > 98 {
|
||||||
sb.go_back(1)
|
sb.go_back(1)
|
||||||
sb.write_string('\n\t\t')
|
sb.write_string('\n\t')
|
||||||
line_len = 8
|
line_len = 8
|
||||||
}
|
}
|
||||||
if i == fbytes.len - 1 {
|
if i == fbytes.len - 1 {
|
||||||
|
|
66
vlib/v/fmt/fmt_bin2v_test.v
Normal file
66
vlib/v/fmt/fmt_bin2v_test.v
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import os
|
||||||
|
import rand
|
||||||
|
import v.ast
|
||||||
|
import v.parser
|
||||||
|
import v.fmt
|
||||||
|
import v.pref
|
||||||
|
import v.util.diff
|
||||||
|
|
||||||
|
const vexe = os.getenv('VEXE')
|
||||||
|
const tmpfolder = os.join_path(os.temp_dir(), 'fmt_bin2v_test')
|
||||||
|
const b2v_keep_path = os.join_path(tmpfolder, rand.ulid() + '.vv')
|
||||||
|
const ifilename = os.file_name(b2v_keep_path)
|
||||||
|
const fpref = &pref.Preferences{
|
||||||
|
is_fmt: true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_bin2v_formatting() {
|
||||||
|
diff_cmd := diff.find_working_diff_command() or { '' }
|
||||||
|
if diff_cmd == '' {
|
||||||
|
eprintln('>> sorry, but no working "diff" CLI command can be found')
|
||||||
|
exit(0)
|
||||||
|
}
|
||||||
|
os.mkdir_all(tmpfolder)!
|
||||||
|
defer {
|
||||||
|
os.rmdir_all(tmpfolder) or {}
|
||||||
|
}
|
||||||
|
prepare_bin2v_file()
|
||||||
|
|
||||||
|
table := ast.new_table()
|
||||||
|
file_ast := parser.parse_file(b2v_keep_path, table, .parse_comments, fpref)
|
||||||
|
result_ocontent := fmt.fmt(file_ast, table, fpref, false)
|
||||||
|
eprintln('> the file ${b2v_keep_path} can be formatted.')
|
||||||
|
expected_ocontent := os.read_file(b2v_keep_path)!
|
||||||
|
if expected_ocontent != result_ocontent {
|
||||||
|
vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}')
|
||||||
|
os.write_file(vfmt_result_file, result_ocontent) or { panic(err) }
|
||||||
|
eprintln(diff.color_compare_files(diff_cmd, b2v_keep_path, vfmt_result_file))
|
||||||
|
exit(1)
|
||||||
|
} else {
|
||||||
|
assert true
|
||||||
|
}
|
||||||
|
println('> vfmt: `v bin2v file.v` produces fully formatted code.')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prepare_bin2v_file() {
|
||||||
|
write_bin2v_keep_content() or {
|
||||||
|
eprintln('Failed preparing bin2v_keep.vv: ${err.msg()}')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
eprintln('> prepared ${b2v_keep_path} .')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_bin2v_keep_content() ! {
|
||||||
|
// Note: do not put large files here; the goal of this particular test is
|
||||||
|
// just to guarantee that the output of `v bin2v` is invariant to vfmt, not
|
||||||
|
// to stress out bin2v or vfmt...
|
||||||
|
img0 := os.join_path('vlib', 'v', 'embed_file', 'tests', 'v.png')
|
||||||
|
img1 := os.join_path('examples', 'assets', 'logo.png')
|
||||||
|
os.rm(b2v_keep_path) or {}
|
||||||
|
cmd := '${os.quoted_path(vexe)} bin2v -w ${os.quoted_path(b2v_keep_path)} ${os.quoted_path(img0)} ${os.quoted_path(img1)}'
|
||||||
|
eprintln('> running: ${cmd}')
|
||||||
|
res := os.execute(cmd)
|
||||||
|
if res.exit_code != 0 {
|
||||||
|
return error_with_code(res.output.trim_space(), res.exit_code)
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,7 +13,6 @@ import v.util.vtest
|
||||||
|
|
||||||
const error_missing_vexe = 1
|
const error_missing_vexe = 1
|
||||||
const error_failed_tests = 2
|
const error_failed_tests = 2
|
||||||
const b2v_keep_path = os.join_path('vlib', 'v', 'fmt', 'tests', 'bin2v_keep.vv')
|
|
||||||
const fpref = &pref.Preferences{
|
const fpref = &pref.Preferences{
|
||||||
is_fmt: true
|
is_fmt: true
|
||||||
}
|
}
|
||||||
|
@ -40,7 +39,6 @@ fn test_fmt() {
|
||||||
input_files = vtest.filter_vtest_only(input_files, basepath: vroot)
|
input_files = vtest.filter_vtest_only(input_files, basepath: vroot)
|
||||||
input_files.sort()
|
input_files.sort()
|
||||||
fmt_bench.set_total_expected_steps(input_files.len + 1)
|
fmt_bench.set_total_expected_steps(input_files.len + 1)
|
||||||
prepare_bin2v_file(mut fmt_bench)
|
|
||||||
for istep, ipath in input_files {
|
for istep, ipath in input_files {
|
||||||
fmt_bench.cstep = istep + 1
|
fmt_bench.cstep = istep + 1
|
||||||
fmt_bench.step()
|
fmt_bench.step()
|
||||||
|
@ -58,9 +56,6 @@ fn test_fmt() {
|
||||||
if expected_ocontent != result_ocontent {
|
if expected_ocontent != result_ocontent {
|
||||||
fmt_bench.fail()
|
fmt_bench.fail()
|
||||||
eprintln(fmt_bench.step_message_fail('file ${vrelpath} after formatting, does not look as expected.'))
|
eprintln(fmt_bench.step_message_fail('file ${vrelpath} after formatting, does not look as expected.'))
|
||||||
if ipath.ends_with(b2v_keep_path) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if diff_cmd == '' {
|
if diff_cmd == '' {
|
||||||
eprintln('>> sorry, but no working "diff" CLI command can be found')
|
eprintln('>> sorry, but no working "diff" CLI command can be found')
|
||||||
continue
|
continue
|
||||||
|
@ -73,9 +68,6 @@ fn test_fmt() {
|
||||||
fmt_bench.ok()
|
fmt_bench.ok()
|
||||||
eprintln(fmt_bench.step_message_ok(vrelpath))
|
eprintln(fmt_bench.step_message_ok(vrelpath))
|
||||||
}
|
}
|
||||||
restore_bin2v_placeholder() or {
|
|
||||||
eprintln('failed restoring vbin2v_keep.vv placeholder: ${err.msg()}')
|
|
||||||
}
|
|
||||||
fmt_bench.stop()
|
fmt_bench.stop()
|
||||||
eprintln(term.h_divider('-'))
|
eprintln(term.h_divider('-'))
|
||||||
eprintln(fmt_bench.total_message(fmt_message))
|
eprintln(fmt_bench.total_message(fmt_message))
|
||||||
|
@ -83,35 +75,3 @@ fn test_fmt() {
|
||||||
exit(error_failed_tests)
|
exit(error_failed_tests)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_bin2v_file(mut fmt_bench benchmark.Benchmark) {
|
|
||||||
fmt_bench.cstep = 0
|
|
||||||
fmt_bench.step()
|
|
||||||
write_bin2v_keep_content() or {
|
|
||||||
fmt_bench.fail()
|
|
||||||
eprintln(fmt_bench.step_message_fail('Failed preparing bin2v_keep.vv: ${err.msg()}'))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt_bench.ok()
|
|
||||||
eprintln(fmt_bench.step_message_ok('Prepared bin2v_keep.vv'))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_bin2v_keep_content() ! {
|
|
||||||
// Note: do not put large files here; the goal of this particular test is
|
|
||||||
// just to guarantee that the output of `v bin2v` is invariant to vfmt, not
|
|
||||||
// to stress out bin2v or vfmt...
|
|
||||||
img0 := os.join_path('vlib', 'v', 'embed_file', 'tests', 'v.png')
|
|
||||||
img1 := os.join_path('examples', 'assets', 'logo.png')
|
|
||||||
os.rm(b2v_keep_path)!
|
|
||||||
res := os.execute('${os.quoted_path(vexe)} bin2v -w ${os.quoted_path(b2v_keep_path)} ${os.quoted_path(img0)} ${os.quoted_path(img1)}')
|
|
||||||
if res.exit_code != 0 {
|
|
||||||
restore_bin2v_placeholder() or {}
|
|
||||||
return error_with_code(res.output.trim_space(), res.exit_code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn restore_bin2v_placeholder() ! {
|
|
||||||
text := '// This is a placeholder file which will be filled with bin2v output before the test.
|
|
||||||
// HINT: do NOT delete, move or rename this file!\n'
|
|
||||||
os.write_file(b2v_keep_path, text)!
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
// This is a placeholder file which will be filled with bin2v output before the test.
|
|
||||||
// HINT: do NOT delete, move or rename this file!
|
|
Loading…
Add table
Add a link
Reference in a new issue