file io
This commit is contained in:
+116
-2
@@ -2266,6 +2266,120 @@ milestone_33_injects_explicit_io_provider_and_runs_std_io :: proc(t: ^testing.T)
|
||||
)
|
||||
}
|
||||
|
||||
@(test)
|
||||
std_io_opens_existing_files_through_the_captured_provider :: proc(t: ^testing.T) {
|
||||
directory := "/tmp/brolang-test-file-io"
|
||||
main_path := "/tmp/brolang-test-file-io/main.bro"
|
||||
data_path := "/tmp/brolang-test-file-io/data"
|
||||
output := "/tmp/brolang-test-file-io/app"
|
||||
seed := "xxxxx"
|
||||
text := `io :: import "@std/io"
|
||||
process :: import "@std/process"
|
||||
|
||||
missing_fails func(system io.Io) bool {
|
||||
_ = io.open(system, "/tmp/brolang-test-file-io/missing", .read_only) catch |err| {
|
||||
return err == .open_failed
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
wrong_read_fails func(file io.File) bool {
|
||||
buffer [1]mut u8 = [0]
|
||||
_ = io.read(io.reader(file), buffer[..]) catch |err| {
|
||||
return err == .not_open_for_reading
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
wrong_write_fails func(file io.File) bool {
|
||||
_ = io.write(io.writer(file), "x") catch |err| {
|
||||
return err == .not_open_for_writing
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
main func(init process.Init) i32 {
|
||||
if !missing_fails(init.io) {
|
||||
return 1
|
||||
}
|
||||
|
||||
read_file io.File :: io.open(init.io, "/tmp/brolang-test-file-io/data", .read_only) catch |_| {
|
||||
return 2
|
||||
}
|
||||
buffer [5]mut u8 = [0, 0, 0, 0, 0]
|
||||
count usize = io.read(io.reader(read_file), buffer[..]) catch |_| {
|
||||
return 4
|
||||
}
|
||||
if count != 5 or buffer[0] != 'x' or !wrong_write_fails(read_file) {
|
||||
return 5
|
||||
}
|
||||
io.close(read_file) catch |_| {
|
||||
return 6
|
||||
}
|
||||
|
||||
write_file io.File :: io.open(init.io, "/tmp/brolang-test-file-io/data", .write_only) catch |_| {
|
||||
return 7
|
||||
}
|
||||
if !wrong_read_fails(write_file) {
|
||||
return 8
|
||||
}
|
||||
io.write_all(io.writer(write_file), "bro") catch |_| {
|
||||
return 9
|
||||
}
|
||||
io.close(write_file) catch |_| {
|
||||
return 10
|
||||
}
|
||||
|
||||
read_write_file io.File :: io.open(init.io, "/tmp/brolang-test-file-io/data", .read_write) catch |_| {
|
||||
return 11
|
||||
}
|
||||
count = io.read(io.reader(read_write_file), buffer[..]) catch |_| {
|
||||
return 12
|
||||
}
|
||||
if count != 5 or buffer[0] != 'b' or buffer[1] != 'r' or buffer[2] != 'o' {
|
||||
return 13
|
||||
}
|
||||
io.write_all(io.writer(read_write_file), "!") catch |_| {
|
||||
return 14
|
||||
}
|
||||
io.close(read_write_file) catch |_| {
|
||||
return 15
|
||||
}
|
||||
return 0
|
||||
}
|
||||
`
|
||||
|
||||
_ = os2.remove_all(directory)
|
||||
defer _ = os2.remove_all(directory)
|
||||
testing.expect(t, os.make_directory(directory) == nil)
|
||||
testing.expect(t, os.write_entire_file(main_path, transmute([]byte)text))
|
||||
testing.expect(t, os.write_entire_file(data_path, transmute([]byte)seed))
|
||||
|
||||
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, 0)
|
||||
testing.expect_value(t, len(stdout), 0)
|
||||
testing.expect_value(t, len(stderr), 0)
|
||||
|
||||
data, ok := os.read_entire_file(data_path)
|
||||
defer delete(data)
|
||||
testing.expect(t, ok)
|
||||
testing.expect_value(t, string(data), "broxx!")
|
||||
}
|
||||
|
||||
@(test)
|
||||
milestone_33_rejects_non_io_and_extra_main_parameters :: proc(t: ^testing.T) {
|
||||
cases := [?]string{
|
||||
@@ -2549,7 +2663,7 @@ milestone_37_format_errors_are_reported_at_comptime :: proc(t: ^testing.T) {
|
||||
process :: import "@std/process"
|
||||
|
||||
main func(init process.Init) void {
|
||||
writer io.Writer :: io.Writer {impl = init.io, stream = .stdout}
|
||||
writer io.Writer :: io.stdout(init.io)
|
||||
stored :: typeinfo!(i32)
|
||||
_ = stored
|
||||
io.print(writer, "", 1) catch |_| {}
|
||||
@@ -4582,7 +4696,7 @@ allocator_contract_c_allocator_global_lowers :: proc(t: ^testing.T) {
|
||||
found_anyopaque_context = pointer_ok &&
|
||||
pointer_item.kind == .Pointer &&
|
||||
pointer_item.mutable &&
|
||||
pointer_item.many &&
|
||||
!pointer_item.many &&
|
||||
pointer_item.child == types.ANYOPAQUE
|
||||
}
|
||||
} else if name == "vtable" {
|
||||
|
||||
Reference in New Issue
Block a user