oxedyne/fe2o3/fe2o3_core/tests/term_out.rs
2.8 KiB, 1 run
created by r1870400018:77101, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! `term::out::Out` over a buffer and over a writer whose reader has gone, and `Error::hung_up` |
| 2 | //! telling that from other faults. |
| 3 | |
| 4 | use oxedyne_fe2o3_core::prelude::*; |
| 5 | use oxedyne_fe2o3_core::term::out::Out; |
| 6 | |
| 7 | use std::io::{ |
| 8 | self, |
| 9 | ErrorKind, |
| 10 | Write, |
| 11 | }; |
| 12 | |
| 13 | |
| 14 | // A writer whose reader has gone, or which fails some other way. |
| 15 | struct Gone(ErrorKind); |
| 16 | |
| 17 | impl Write for Gone { |
| 18 | fn write(&mut self, _: &[u8]) -> io::Result<usize> { |
| 19 | Err(io::Error::new(self.0, "gone")) |
| 20 | } |
| 21 | fn flush(&mut self) -> io::Result<()> { |
| 22 | Ok(()) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn io_err(kind: ErrorKind) -> io::Result<()> { |
| 27 | Err(io::Error::new(kind, "test")) |
| 28 | } |
| 29 | |
| 30 | fn tagged_output(kind: ErrorKind) -> Outcome<()> { |
| 31 | res!(io_err(kind), IO, Output, Write); |
| 32 | Ok(()) |
| 33 | } |
| 34 | |
| 35 | fn tagged_network(kind: ErrorKind) -> Outcome<()> { |
| 36 | res!(io_err(kind), IO, Network, Write); |
| 37 | Ok(()) |
| 38 | } |
| 39 | |
| 40 | // A `res!` frame over the fault, as a caller's `main` sees it. |
| 41 | fn wrapped(kind: ErrorKind) -> Outcome<()> { |
| 42 | res!(tagged_output(kind)); |
| 43 | Ok(()) |
| 44 | } |
| 45 | |
| 46 | #[test] |
| 47 | fn test_hung_up_only_broken_pipe_on_output() -> Outcome<()> { |
| 48 | let cases: [(Outcome<()>, bool, &str); 4] = [ |
| 49 | (tagged_output(ErrorKind::BrokenPipe), true, "broken pipe on output"), |
| 50 | (wrapped(ErrorKind::BrokenPipe), true, "broken pipe on output, wrapped"), |
| 51 | (tagged_network(ErrorKind::BrokenPipe), false, "broken pipe on a socket"), |
| 52 | (tagged_output(ErrorKind::PermissionDenied),false, "another io kind on output"), |
| 53 | ]; |
| 54 | for (r, want, what) in cases { |
| 55 | match r { |
| 56 | Err(e) if e.hung_up() == want => (), |
| 57 | Err(e) => return Err(err!("{}: hung_up was {}, expected {}: {}", what, !want, want, e; Test, Mismatch)), |
| 58 | Ok(()) => return Err(err!("{}: no error", what; Test, Missing)), |
| 59 | } |
| 60 | } |
| 61 | Ok(()) |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn test_out_writes_text_and_lines() -> Outcome<()> { |
| 66 | let mut out = Out::new(Vec::new()); |
| 67 | res!(out.put("a ")); |
| 68 | res!(out.line("b")); |
| 69 | res!(out.line("")); |
| 70 | let got = res!(String::from_utf8(out.into_inner())); |
| 71 | if got != "a b\n\n" { |
| 72 | return Err(err!("Wrote {:?}.", got; Test, Mismatch)); |
| 73 | } |
| 74 | Ok(()) |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn test_out_reader_gone_is_hung_up() -> Outcome<()> { |
| 79 | let mut out = Out::new(Gone(ErrorKind::BrokenPipe)); |
| 80 | match out.line("x") { |
| 81 | Err(e) if e.hung_up() => (), |
| 82 | Err(e) => return Err(err!("A broken pipe was not seen as hung up: {}", e; Test, Mismatch)), |
| 83 | Ok(()) => return Err(err!("A write to a gone reader succeeded."; Test, Missing)), |
| 84 | } |
| 85 | let mut out = Out::new(Gone(ErrorKind::Other)); |
| 86 | match out.put("x") { |
| 87 | Err(e) if !e.hung_up() && e.tags().contains(&ErrTag::Write) => Ok(()), |
| 88 | Err(e) => Err(err!("Another write fault was misread: {}", e; Test, Mismatch)), |
| 89 | Ok(()) => Err(err!("A failing write succeeded."; Test, Missing)), |
| 90 | } |
| 91 | } |