v icon indicating copy to clipboard operation
v copied to clipboard

tests: make error handling the same as the main function

Open yuyi98 opened this issue 3 years ago • 2 comments

This PR make error handling the same as the main function.

  • Make error handling the same as the main function.
  • Modify all the related tests.

e.g.

fn test_write_struct_at() {
	mut f := os.open_file(tfile, 'w')?
	f.write_struct(extended_point)?
	f.write_struct_at(another_point, 3)?
	f.close()
	f = os.open_file(tfile, 'r')?
	mut p := Point{}
	f.read_struct_at(mut p, 3)?
	f.close()

	assert p == another_point
}

yuyi98 avatar Sep 19 '22 23:09 yuyi98

On one hand, in my mind, the way tests worked before, by requiring ?, if the user wanted to propagate the error further, just like ordinary functions, was more consistent, and at times, I even wanted main to require ?, although that had the complication of script mode, and would have required more care, because there is not an explicit main, and it would have to be generated in 2 ways, depending on what the user's source.

On the other hand, this PR, by making tests behave just like main (without needing ?), makes for a much better/smoother experience while writing them - it is annoying, to have to go back, and add ?, just because in the middle of a test, you happened to have written os.chdir(some_dir)?, or to have to use os.chdir(some_dir) or { panic(err) }, and that had happened to me multiple times already.

In any case, good work @yuyi98 !

@medvednikov what do you think?

spytheman avatar Sep 20 '22 05:09 spytheman

Thanks !!! This treatment does not affect the original treatment, but can be omitted returning ? or !, and if you return a mixture of optional and result, this is no problem.

yuyi98 avatar Sep 20 '22 05:09 yuyi98