v/vlib/io/multi_writer_test.v

67 lines
1.2 KiB
V
Raw Normal View History

2021-05-08 13:21:53 +02:00
module io
fn test_multi_writer_write_successful() {
w0 := TestWriter{}
w1 := TestWriter{}
mut mw := new_multi_writer(w0, w1)
2021-05-08 13:21:53 +02:00
n := mw.write('0123456789'.bytes()) or {
assert false
return
}
assert n == 10
assert w0.bytes == '0123456789'.bytes()
assert w1.bytes == '0123456789'.bytes()
}
fn test_multi_writer_write_incomplete() {
w0 := TestWriter{}
w1 := TestIncompleteWriter{}
mut mw := new_multi_writer(w0, w1)
2021-05-08 13:21:53 +02:00
n := mw.write('0123456789'.bytes()) or {
assert w0.bytes == '0123456789'.bytes()
assert w1.bytes == '012345678'.bytes()
return
}
assert false
}
fn test_multi_writer_write_error() {
w0 := TestWriter{}
w1 := TestErrorWriter{}
w2 := TestWriter{}
mut mw := new_multi_writer(w0, w1, w2)
2021-05-08 13:21:53 +02:00
n := mw.write('0123456789'.bytes()) or {
assert w0.bytes == '0123456789'.bytes()
assert w2.bytes == []
return
}
assert false
}
struct TestWriter {
pub mut:
2022-04-15 14:35:35 +02:00
bytes []u8
2021-05-08 13:21:53 +02:00
}
2022-04-15 14:35:35 +02:00
fn (mut w TestWriter) write(buf []u8) ?int {
2021-05-08 13:21:53 +02:00
w.bytes << buf
return buf.len
}
struct TestIncompleteWriter {
pub mut:
2022-04-15 14:35:35 +02:00
bytes []u8
2021-05-08 13:21:53 +02:00
}
2022-04-15 14:35:35 +02:00
fn (mut w TestIncompleteWriter) write(buf []u8) ?int {
2021-05-08 13:21:53 +02:00
b := buf[..buf.len - 1]
w.bytes << b
return b.len
}
struct TestErrorWriter {}
2022-04-15 14:35:35 +02:00
fn (mut w TestErrorWriter) write(buf []u8) ?int {
2021-05-08 13:21:53 +02:00
return error('error writer errored')
}