|
| 1 | +use async_std::fs::File; |
| 2 | +use async_std::io::prelude::*; |
| 3 | +use async_std::io::{self, SeekFrom}; |
| 4 | +use async_std::sync::Arc; |
| 5 | +use async_std::task::{Context, Poll}; |
| 6 | +use std::pin::Pin; |
| 7 | +use std::sync::Mutex; |
| 8 | + |
| 9 | +#[derive(Clone)] |
| 10 | +pub struct TestCase { |
| 11 | + reader: Arc<Mutex<File>>, |
| 12 | + writer: Arc<Mutex<File>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl TestCase { |
| 16 | + /// Create a new instance. |
| 17 | + pub async fn new(reader: &str, writer: &str) -> TestCase { |
| 18 | + use std::io::Write; |
| 19 | + |
| 20 | + let mut temp = tempfile::tempfile().expect("Failed writer create tempfile"); |
| 21 | + temp.write(reader.as_bytes()) |
| 22 | + .expect("Could not write writer dest file"); |
| 23 | + let mut file: File = temp.into(); |
| 24 | + file.seek(SeekFrom::Start(0)).await.unwrap(); |
| 25 | + let reader = Arc::new(Mutex::new(file.into())); |
| 26 | + |
| 27 | + let mut temp = tempfile::tempfile().expect("Failed writer create tempfile"); |
| 28 | + temp.write(writer.as_bytes()) |
| 29 | + .expect("Could not write writer dest file"); |
| 30 | + let mut file: File = temp.into(); |
| 31 | + file.seek(SeekFrom::Start(0)).await.unwrap(); |
| 32 | + let writer = Arc::new(Mutex::new(file.into())); |
| 33 | + |
| 34 | + TestCase { reader, writer } |
| 35 | + } |
| 36 | + |
| 37 | + /// Get the value of the "writer" string. |
| 38 | + pub async fn writer(&self) -> String { |
| 39 | + let mut writer = String::new(); |
| 40 | + let mut file = self.writer.lock().unwrap(); |
| 41 | + file.seek(SeekFrom::Start(0)).await.unwrap(); |
| 42 | + file.read_to_string(&mut writer).await.unwrap(); |
| 43 | + writer |
| 44 | + } |
| 45 | + |
| 46 | + /// Assert the reader. |
| 47 | + pub async fn assert_reader(&self, rhs: &str) { |
| 48 | + let mut lhs = self.reader().await; |
| 49 | + pretty_assertions::assert_eq!(lhs, rhs); |
| 50 | + } |
| 51 | + |
| 52 | + /// Assert the reader but pass it through a closure first.. |
| 53 | + pub async fn assert_reader_with(&self, mut rhs: &str, f: impl Fn(&mut String, &mut String)) { |
| 54 | + let mut lhs = self.reader().await; |
| 55 | + let mut rhs = String::from(rhs); |
| 56 | + f(&mut lhs, &mut rhs); |
| 57 | + pretty_assertions::assert_eq!(lhs, rhs); |
| 58 | + } |
| 59 | + |
| 60 | + /// Assert the writer. |
| 61 | + pub async fn assert_writer(&self, rhs: &str) { |
| 62 | + let mut lhs = self.writer().await; |
| 63 | + pretty_assertions::assert_eq!(lhs, rhs); |
| 64 | + } |
| 65 | + |
| 66 | + /// Assert the reader but pass it through a closure first.. |
| 67 | + pub async fn assert_writer_with(&self, mut rhs: &str, f: impl Fn(&mut String, &mut String)) { |
| 68 | + let mut lhs = self.writer().await; |
| 69 | + let mut rhs = String::from(rhs); |
| 70 | + f(&mut lhs, &mut rhs); |
| 71 | + pretty_assertions::assert_eq!(lhs, rhs); |
| 72 | + } |
| 73 | + |
| 74 | + /// Get the value of the "reader" string. |
| 75 | + pub async fn reader(&self) -> String { |
| 76 | + let mut reader = String::new(); |
| 77 | + let mut file = self.reader.lock().unwrap(); |
| 78 | + file.seek(SeekFrom::Start(0)).await.unwrap(); |
| 79 | + file.read_to_string(&mut reader).await.unwrap(); |
| 80 | + reader |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl Read for TestCase { |
| 85 | + fn poll_read( |
| 86 | + self: Pin<&mut Self>, |
| 87 | + cx: &mut Context, |
| 88 | + buf: &mut [u8], |
| 89 | + ) -> Poll<io::Result<usize>> { |
| 90 | + Pin::new(&mut &*self.reader.lock().unwrap()).poll_read(cx, buf) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl Write for TestCase { |
| 95 | + fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> { |
| 96 | + Pin::new(&mut &*self.writer.lock().unwrap()).poll_write(cx, buf) |
| 97 | + } |
| 98 | + |
| 99 | + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> { |
| 100 | + Pin::new(&mut &*self.writer.lock().unwrap()).poll_flush(cx) |
| 101 | + } |
| 102 | + |
| 103 | + fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> { |
| 104 | + Pin::new(&mut &*self.writer.lock().unwrap()).poll_close(cx) |
| 105 | + } |
| 106 | +} |
0 commit comments