mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
tests: make error handling the same as the main function (#15825)
This commit is contained in:
@ -60,7 +60,7 @@ const (
|
||||
// This test simulates reading a larger text file step by step into a buffer and
|
||||
// returning on each newline, even before the buffer is full, and reaching EOF before
|
||||
// the buffer is completely filled.
|
||||
fn test_read_bytes_into_newline_text() ? {
|
||||
fn test_read_bytes_into_newline_text() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
f.write_string('Hello World!\nGood\r morning.')?
|
||||
f.close()
|
||||
@ -86,7 +86,7 @@ fn test_read_bytes_into_newline_text() ? {
|
||||
// test_read_bytes_into_newline_binary tests reading a binary file with NUL bytes.
|
||||
// This test simulates the scenario when a byte stream is read and a newline byte
|
||||
// appears in that stream and an EOF occurs before the buffer is full.
|
||||
fn test_read_bytes_into_newline_binary() ? {
|
||||
fn test_read_bytes_into_newline_binary() {
|
||||
os.rm(tfile) or {} // FIXME This is a workaround for macos, because the file isn't truncated when open with 'w'
|
||||
mut bw := []u8{len: 15}
|
||||
bw[9] = 0xff
|
||||
@ -121,7 +121,7 @@ fn test_read_bytes_into_newline_binary() ? {
|
||||
// the end-of-file is detected and results in a none error being returned. This
|
||||
// test simulates file reading where the end-of-file is reached inside an fread
|
||||
// containing data.
|
||||
fn test_read_eof_last_read_partial_buffer_fill() ? {
|
||||
fn test_read_eof_last_read_partial_buffer_fill() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
bw := []u8{len: 199, init: 5}
|
||||
f.write(bw)?
|
||||
@ -130,11 +130,11 @@ fn test_read_eof_last_read_partial_buffer_fill() ? {
|
||||
f = os.open_file(tfile, 'r')?
|
||||
mut br := []u8{len: 100}
|
||||
// Read first 100 bytes of 199 byte file, should fill buffer with no error.
|
||||
n0 := f.read(mut br) or { return error('failed to read 100 bytes') }
|
||||
n0 := f.read(mut br)!
|
||||
assert n0 == 100
|
||||
// Read remaining 99 bytes of 199 byte file, should fill buffer with no
|
||||
// error, even though end-of-file was reached.
|
||||
n1 := f.read(mut br) or { return error('failed to read 100 bytes') }
|
||||
n1 := f.read(mut br)!
|
||||
assert n1 == 99
|
||||
// Read again, end-of-file was previously reached so should return none
|
||||
// error.
|
||||
@ -153,7 +153,7 @@ fn test_read_eof_last_read_partial_buffer_fill() ? {
|
||||
// end-of-file is detected and results in a none error being returned. This test
|
||||
// simulates file reading where the end-of-file is reached at the beinning of an
|
||||
// fread that returns no data.
|
||||
fn test_read_eof_last_read_full_buffer_fill() ? {
|
||||
fn test_read_eof_last_read_full_buffer_fill() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
bw := []u8{len: 200, init: 5}
|
||||
f.write(bw)?
|
||||
@ -162,11 +162,11 @@ fn test_read_eof_last_read_full_buffer_fill() ? {
|
||||
f = os.open_file(tfile, 'r')?
|
||||
mut br := []u8{len: 100}
|
||||
// Read first 100 bytes of 200 byte file, should fill buffer with no error.
|
||||
n0 := f.read(mut br) or { return error('failed to read 100 bytes') }
|
||||
n0 := f.read(mut br)!
|
||||
assert n0 == 100
|
||||
// Read remaining 100 bytes of 200 byte file, should fill buffer with no
|
||||
// error. The end-of-file isn't reached yet, but there is no more data.
|
||||
n1 := f.read(mut br) or { return error('failed to read 100 bytes') }
|
||||
n1 := f.read(mut br)!
|
||||
assert n1 == 100
|
||||
// Read again, end-of-file was previously reached so should return none
|
||||
// error.
|
||||
@ -181,7 +181,7 @@ fn test_read_eof_last_read_full_buffer_fill() ? {
|
||||
f.close()
|
||||
}
|
||||
|
||||
fn test_write_struct() ? {
|
||||
fn test_write_struct() {
|
||||
os.rm(tfile) or {} // FIXME This is a workaround for macos, because the file isn't truncated when open with 'w'
|
||||
size_of_point := int(sizeof(Point))
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
@ -197,7 +197,7 @@ fn test_write_struct() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_write_struct_at() ? {
|
||||
fn test_write_struct_at() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
f.write_struct(extended_point)?
|
||||
f.write_struct_at(another_point, 3)?
|
||||
@ -210,7 +210,7 @@ fn test_write_struct_at() ? {
|
||||
assert p == another_point
|
||||
}
|
||||
|
||||
fn test_read_struct() ? {
|
||||
fn test_read_struct() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
f.write_struct(another_point)?
|
||||
f.close()
|
||||
@ -223,7 +223,7 @@ fn test_read_struct() ? {
|
||||
assert p == another_point
|
||||
}
|
||||
|
||||
fn test_read_struct_at() ? {
|
||||
fn test_read_struct_at() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
f.write([u8(1), 2, 3])?
|
||||
f.write_struct(another_point)?
|
||||
@ -236,7 +236,7 @@ fn test_read_struct_at() ? {
|
||||
assert p == another_point
|
||||
}
|
||||
|
||||
fn test_write_raw() ? {
|
||||
fn test_write_raw() {
|
||||
os.rm(tfile) or {} // FIXME This is a workaround for macos, because the file isn't truncated when open with 'w'
|
||||
size_of_point := int(sizeof(Point))
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
@ -252,7 +252,7 @@ fn test_write_raw() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_write_raw_at() ? {
|
||||
fn test_write_raw_at() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
f.write_raw(extended_point)?
|
||||
f.write_raw_at(another_point, 3)?
|
||||
@ -265,7 +265,7 @@ fn test_write_raw_at() ? {
|
||||
assert p == another_point
|
||||
}
|
||||
|
||||
fn test_write_raw_at_negative_pos() ? {
|
||||
fn test_write_raw_at_negative_pos() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
if _ := f.write_raw_at(another_point, -1) {
|
||||
assert false
|
||||
@ -274,7 +274,7 @@ fn test_write_raw_at_negative_pos() ? {
|
||||
f.close()
|
||||
}
|
||||
|
||||
fn test_read_raw() ? {
|
||||
fn test_read_raw() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
f.write_raw(another_point)?
|
||||
f.write_raw(another_byte)?
|
||||
@ -294,7 +294,7 @@ fn test_read_raw() ? {
|
||||
assert x == another_permission
|
||||
}
|
||||
|
||||
fn test_read_raw_at() ? {
|
||||
fn test_read_raw_at() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
f.write([u8(1), 2, 3])?
|
||||
f.write_raw(another_point)?
|
||||
@ -320,7 +320,7 @@ fn test_read_raw_at() ? {
|
||||
assert x == another_permission
|
||||
}
|
||||
|
||||
fn test_read_raw_at_negative_pos() ? {
|
||||
fn test_read_raw_at_negative_pos() {
|
||||
mut f := os.open_file(tfile, 'r')?
|
||||
if _ := f.read_raw_at<Point>(-1) {
|
||||
assert false
|
||||
@ -329,7 +329,7 @@ fn test_read_raw_at_negative_pos() ? {
|
||||
f.close()
|
||||
}
|
||||
|
||||
fn test_seek() ? {
|
||||
fn test_seek() {
|
||||
mut f := os.open_file(tfile, 'w')?
|
||||
f.write_raw(another_point)?
|
||||
f.write_raw(another_byte)?
|
||||
@ -352,7 +352,7 @@ fn test_seek() ? {
|
||||
f.close()
|
||||
}
|
||||
|
||||
fn test_tell() ? {
|
||||
fn test_tell() {
|
||||
for size in 10 .. 30 {
|
||||
s := 'x'.repeat(size)
|
||||
os.write_file(tfile, s)?
|
||||
@ -368,7 +368,7 @@ fn test_tell() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_reopen() ? {
|
||||
fn test_reopen() {
|
||||
tfile1 := os.join_path_single(tfolder, 'tfile1')
|
||||
tfile2 := os.join_path_single(tfolder, 'tfile2')
|
||||
os.write_file(tfile1, 'Hello World!\nGood\r morning.\nBye 1.')?
|
||||
@ -397,7 +397,7 @@ fn test_reopen() ? {
|
||||
assert content.ends_with('Bye 1.')
|
||||
}
|
||||
|
||||
fn test_eof() ? {
|
||||
fn test_eof() {
|
||||
os.write_file(tfile, 'Hello World!\n')?
|
||||
|
||||
mut f := os.open(tfile)?
|
||||
@ -407,7 +407,7 @@ fn test_eof() ? {
|
||||
assert f.eof()
|
||||
}
|
||||
|
||||
fn test_open_file_wb_ab() ? {
|
||||
fn test_open_file_wb_ab() {
|
||||
os.rm(tfile) or {}
|
||||
mut wfile := os.open_file('text.txt', 'wb', 0o666)?
|
||||
wfile.write_string('hello')?
|
||||
|
@ -1,6 +1,6 @@
|
||||
module os
|
||||
|
||||
fn test_find_abs_path_of_executable() ? {
|
||||
fn test_find_abs_path_of_executable() {
|
||||
tfolder := join_path(temp_dir(), 'v', 'tests', 'filepath_test')
|
||||
rmdir_all(tfolder) or {}
|
||||
assert !is_dir(tfolder)
|
||||
|
@ -30,7 +30,7 @@ fn redeep_glob() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_glob_can_find_v_files_3_levels_deep() ? {
|
||||
fn test_glob_can_find_v_files_3_levels_deep() {
|
||||
$if !windows {
|
||||
deep_glob()?
|
||||
redeep_glob()?
|
||||
@ -38,7 +38,7 @@ fn test_glob_can_find_v_files_3_levels_deep() ? {
|
||||
assert true
|
||||
}
|
||||
|
||||
fn test_glob_can_find_files_in_current_folder() ? {
|
||||
fn test_glob_can_find_files_in_current_folder() {
|
||||
os.chdir(@VMODROOT)?
|
||||
matches := os.glob('*')?
|
||||
assert '.gitignore' in matches
|
||||
@ -52,7 +52,7 @@ fn test_glob_can_find_files_in_current_folder() ? {
|
||||
assert 'thirdparty/' in matches
|
||||
}
|
||||
|
||||
fn test_glob_can_be_used_with_multiple_patterns() ? {
|
||||
fn test_glob_can_be_used_with_multiple_patterns() {
|
||||
os.chdir(@VMODROOT)?
|
||||
matches := os.glob('*', 'cmd/tools/*')?
|
||||
assert 'README.md' in matches
|
||||
@ -65,14 +65,14 @@ fn test_glob_can_be_used_with_multiple_patterns() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_glob_star() ? {
|
||||
fn test_glob_star() {
|
||||
os.chdir(@VMODROOT)?
|
||||
matches := os.glob('*ake*')?
|
||||
assert 'Makefile' in matches
|
||||
assert 'make.bat' in matches
|
||||
}
|
||||
|
||||
fn test_glob_not_found() ? {
|
||||
fn test_glob_not_found() {
|
||||
os.glob('an_unknown_folder/*.v') or {
|
||||
assert true
|
||||
return
|
||||
|
@ -15,7 +15,7 @@ fn make_pipe() ?(int, int) {
|
||||
return -1, -1
|
||||
}
|
||||
|
||||
fn test_level_trigger() ? {
|
||||
fn test_level_trigger() {
|
||||
// currently only linux is supported
|
||||
$if linux {
|
||||
mut notifier := notify.new()?
|
||||
@ -36,7 +36,7 @@ fn test_level_trigger() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_edge_trigger() ? {
|
||||
fn test_edge_trigger() {
|
||||
// currently only linux is supported
|
||||
$if linux {
|
||||
mut notifier := notify.new()?
|
||||
@ -64,7 +64,7 @@ fn test_edge_trigger() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_one_shot() ? {
|
||||
fn test_one_shot() {
|
||||
$if linux {
|
||||
mut notifier := notify.new()?
|
||||
reader, writer := make_pipe()?
|
||||
@ -89,7 +89,7 @@ fn test_one_shot() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_hangup() ? {
|
||||
fn test_hangup() {
|
||||
$if linux {
|
||||
mut notifier := notify.new()?
|
||||
reader, writer := make_pipe()?
|
||||
@ -111,7 +111,7 @@ fn test_hangup() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_write() ? {
|
||||
fn test_write() {
|
||||
$if linux {
|
||||
mut notifier := notify.new()?
|
||||
reader, writer := make_pipe()?
|
||||
@ -132,7 +132,7 @@ fn test_write() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_remove() ? {
|
||||
fn test_remove() {
|
||||
$if linux {
|
||||
mut notifier := notify.new()?
|
||||
reader, writer := make_pipe()?
|
||||
|
@ -96,7 +96,7 @@ fn create_and_write_to_file(fpath string, content string) ? {
|
||||
f.close()
|
||||
}
|
||||
|
||||
fn test_create_file() ? {
|
||||
fn test_create_file() {
|
||||
filename := './test1.txt'
|
||||
hello := 'hello world!'
|
||||
create_and_write_to_file(filename, hello)?
|
||||
@ -226,7 +226,7 @@ fn normalise_paths(paths []string) []string {
|
||||
return res
|
||||
}
|
||||
|
||||
fn test_walk_ext() ? {
|
||||
fn test_walk_ext() {
|
||||
create_tree()?
|
||||
defer {
|
||||
remove_tree()
|
||||
@ -442,7 +442,7 @@ fn test_realpath_does_not_absolutize_non_existing_relative_paths() {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_realpath_absolutepath_symlink() ? {
|
||||
fn test_realpath_absolutepath_symlink() {
|
||||
file_name := 'tolink_file.txt'
|
||||
symlink_name := 'symlink.txt'
|
||||
create_file(file_name)?
|
||||
@ -482,7 +482,7 @@ fn test_make_symlink_check_is_link_and_remove_symlink() {
|
||||
assert symlink_exists == false
|
||||
}
|
||||
|
||||
fn test_make_symlink_check_is_link_and_remove_symlink_with_file() ? {
|
||||
fn test_make_symlink_check_is_link_and_remove_symlink_with_file() {
|
||||
file := 'tfile'
|
||||
symlink := 'tsymlink'
|
||||
os.rm(symlink) or {}
|
||||
@ -496,7 +496,7 @@ fn test_make_symlink_check_is_link_and_remove_symlink_with_file() ? {
|
||||
assert symlink_exists == false
|
||||
}
|
||||
|
||||
fn test_make_hardlink_check_is_link_and_remove_hardlink_with_file() ? {
|
||||
fn test_make_hardlink_check_is_link_and_remove_hardlink_with_file() {
|
||||
file := 'tfile'
|
||||
symlink := 'tsymlink'
|
||||
os.rm(symlink) or {}
|
||||
@ -544,7 +544,7 @@ fn test_symlink() {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_is_executable_writable_readable() ? {
|
||||
fn test_is_executable_writable_readable() {
|
||||
file_name := 'rwxfile.exe'
|
||||
create_file(file_name)?
|
||||
$if !windows {
|
||||
@ -704,7 +704,7 @@ cmd.close()
|
||||
*/
|
||||
}
|
||||
|
||||
fn test_posix_set_bit() ? {
|
||||
fn test_posix_set_bit() {
|
||||
$if windows {
|
||||
assert true
|
||||
} $else {
|
||||
@ -764,7 +764,7 @@ fn test_exists_in_system_path() {
|
||||
assert os.exists_in_system_path('ls')
|
||||
}
|
||||
|
||||
fn test_truncate() ? {
|
||||
fn test_truncate() {
|
||||
filename := './test_trunc.txt'
|
||||
hello := 'hello world!'
|
||||
mut f := os.create(filename)?
|
||||
@ -781,7 +781,7 @@ fn test_hostname() {
|
||||
assert os.hostname().len > 2
|
||||
}
|
||||
|
||||
fn test_glob() ? {
|
||||
fn test_glob() {
|
||||
os.mkdir('test_dir') or { panic(err) }
|
||||
for i in 0 .. 4 {
|
||||
if i == 3 {
|
||||
@ -818,7 +818,7 @@ fn test_utime() {
|
||||
assert os.file_last_mod_unix(filename) == mtime
|
||||
}
|
||||
|
||||
fn test_execute() ? {
|
||||
fn test_execute() {
|
||||
print0script := os.join_path_single(tfolder, 'print0.v')
|
||||
// The output of the next command contains a 0 byte in the middle.
|
||||
// Nevertheless, the execute function *should* return a string that
|
||||
|
Reference in New Issue
Block a user