fallible main
This commit is contained in:
@@ -3129,6 +3129,79 @@ bodyless_root_main_recovers_as_a_trap_definition :: proc(t: ^testing.T) {
|
||||
testing.expect(t, !strings.contains(llvm_text, "declare i32 @main()"))
|
||||
}
|
||||
|
||||
@(test)
|
||||
fallible_main_maps_success_and_error_channels_to_exit_codes :: proc(t: ^testing.T) {
|
||||
Case :: struct {
|
||||
name: string,
|
||||
source: string,
|
||||
exit_code: int,
|
||||
}
|
||||
cases := [?]Case{
|
||||
{
|
||||
name="void-success",
|
||||
source=`Error :: enum { failed }
|
||||
succeed func() void ! Error {}
|
||||
main func() void ! Error { try succeed() }
|
||||
`,
|
||||
exit_code=0,
|
||||
},
|
||||
{
|
||||
name="void-error",
|
||||
source=`Error :: enum { failed }
|
||||
fail func() void ! Error { return .failed }
|
||||
main func() void ! Error { try fail() }
|
||||
`,
|
||||
exit_code=1,
|
||||
},
|
||||
{
|
||||
name="int-success",
|
||||
source=`Error :: enum { failed }
|
||||
value func() i32 ! Error { return 7 }
|
||||
main func() int ! Error { return try value() }
|
||||
`,
|
||||
exit_code=7,
|
||||
},
|
||||
{
|
||||
name="process-error",
|
||||
source=`process :: import "@std/process"
|
||||
Error :: enum { failed }
|
||||
fail func() void ! Error { return .failed }
|
||||
main func(_ process.Init) void ! Error { try fail() }
|
||||
`,
|
||||
exit_code=1,
|
||||
},
|
||||
}
|
||||
for test_case in cases {
|
||||
directory := fmt.tprintf("/tmp/brolang-test-fallible-main-%s", test_case.name)
|
||||
output := fmt.tprintf("%s/app", directory)
|
||||
_ = os2.remove_all(directory)
|
||||
defer _ = os2.remove_all(directory)
|
||||
testing.expect(t, os.make_directory(directory) == nil)
|
||||
testing.expect(t, os.write_entire_file(
|
||||
fmt.tprintf("%s/main.bro", directory),
|
||||
transmute([]byte)test_case.source,
|
||||
))
|
||||
status := compiler_core.compile_package(
|
||||
directory,
|
||||
output,
|
||||
nil,
|
||||
target.DEFAULT,
|
||||
cimport.Options{},
|
||||
".",
|
||||
)
|
||||
testing.expect_value(t, status, 0)
|
||||
state, stdout, stderr, _ := os2.process_exec(
|
||||
os2.Process_Desc{command=[]string{output}},
|
||||
context.allocator,
|
||||
)
|
||||
defer delete(stdout)
|
||||
defer delete(stderr)
|
||||
testing.expect_value(t, state.exit_code, test_case.exit_code)
|
||||
testing.expect_value(t, len(stdout), 0)
|
||||
testing.expect_value(t, len(stderr), 0)
|
||||
}
|
||||
}
|
||||
|
||||
@(test)
|
||||
milestone_33_injects_explicit_io_provider_and_runs_std_io :: proc(t: ^testing.T) {
|
||||
sources := source.init_store()
|
||||
|
||||
Reference in New Issue
Block a user