Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVeryDarkness committed Sep 3, 2024
1 parent 4bf54d0 commit 3f15f3b
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 25 deletions.
9 changes: 1 addition & 8 deletions src/read/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt::{self, Debug, Display};

use crate::stream::{self, MSG_EOF, MSG_EOL};
use std::fmt::{self, Debug, Display};

/// Error during using [ReadInto] or [ReadOneFrom].
///
Expand Down Expand Up @@ -43,12 +42,6 @@ where

impl<E> std::error::Error for ReadError<E> where E: std::error::Error {}

impl<E> From<std::io::Error> for ReadError<E> {
fn from(error: std::io::Error) -> Self {
Self::IOError(error)
}
}

impl<E> From<stream::error::StreamError> for ReadError<E> {
fn from(error: stream::error::StreamError) -> Self {
match error {
Expand Down
10 changes: 10 additions & 0 deletions src/stream/input_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ impl<B: BufRead> BufReadExt for InputStream<B> {
}

fn try_skip_eol(&mut self) -> Result<(), StreamError> {
self.fill_buf_if_eol()?;
let remaining = as_slice_from(&self.line_buf, self.cursor);
for c in remaining.chars() {
if is_eol(c) {
self.cursor += c.len_utf8();
debug_assert!(self.line_buf.is_char_boundary(self.cursor));
} else {
break;
}
Expand Down Expand Up @@ -227,6 +229,10 @@ mod tests {
"{:?}",
stream.try_get_string_some(),
);
assert_eq!(
stream.try_get().unwrap_err().to_string(),
StreamError::Eof.to_string(),
);
}

#[test]
Expand All @@ -242,5 +248,9 @@ mod tests {
"{:?}",
stream.try_get_string_some()
);
assert_eq!(
stream.try_get().unwrap_err().to_string(),
StreamError::Eof.to_string(),
);
}
}
11 changes: 11 additions & 0 deletions src/stream/line_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,15 @@ mod tests {
stream.try_get_string_some()
);
}

#[test]
fn try_get_until_in_line() {
let s = "Hello, world!";
let mut stream = LineBuf::new(s);
assert_eq!(stream.try_get_until_in_line(&[',']).unwrap(), "Hello");
assert_eq!(stream.try_get_until_in_line(&['!']).unwrap(), ", world");
assert_eq!(stream.try_get_until_in_line(&['!']).unwrap(), "");
assert_eq!(stream.try_get_until_in_line(&[]).unwrap(), "!");
assert_eq!(stream.try_get_until_in_line(&[]).unwrap(), "");
}
}
2 changes: 2 additions & 0 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use error::StreamError;
pub(super) mod error;
pub(super) mod input_stream;
pub(super) mod line_buf;
#[cfg(test)]
mod tests;
pub(super) mod traits;

/// ASCII white space characters.
Expand Down
48 changes: 48 additions & 0 deletions src/stream/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::line_buf::LineBuf;
use crate::{unwrap, BufReadExt, InputStream};
use std::io::Cursor;

fn all(stream: &mut impl BufReadExt) {
let c = unwrap!(stream.try_get());
assert_eq!(c, 'H');
let c = unwrap!(stream.try_peek());
assert_eq!(c, 'e');
let c = unwrap!(stream.try_get_if(&['H', 'e']));
assert_eq!(c, Some('e'));
let c = unwrap!(stream.try_skip_eol());
assert_eq!(c, ());
let c = unwrap!(stream.try_skip_all(&['l', 'o']));
assert_eq!(c, 3);
let s = unwrap!(stream.try_get_until_in_line(&['!']));
assert_eq!(s, ", world");
let s = unwrap!(stream.try_get_string_some());
assert_eq!(s, "!");
}

#[test]
fn line_buf() {
let s = "Hello, world!";
let mut stream = LineBuf::new(s);
all(&mut stream);
}

#[test]
fn ref_line_buf() {
let s = "Hello, world!";
let mut stream = LineBuf::new(s);
all(&mut &mut stream);
}

#[test]
fn input_stream() {
let s = "Hello, world!";
let mut stream = InputStream::new(Cursor::new(s));
all(&mut stream);
}

#[test]
fn ref_input_stream() {
let s = "Hello, world!";
let mut stream = InputStream::new(Cursor::new(s));
all(&mut &mut stream);
}
2 changes: 2 additions & 0 deletions src/stream/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub trait BufReadExt {
}

/// Go to the next line if the remaining part are end of line characters.
///
/// Only skip the first end of line character.
fn try_skip_eol(&mut self) -> Result<(), StreamError>;

/// Skip all characters in `skipped`.
Expand Down
21 changes: 12 additions & 9 deletions src/write/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ pub struct Writer<'sep, 'end, 'buf> {
buf: Option<&'buf mut dyn Write>,
}

impl<'sep, 'end, 'buf> Writer<'sep, 'end, 'buf> {
/// Create a new writer.
pub fn new() -> Self {
let sep = " ";
let end = "\n";
let buf = None;
impl<'sep, 'end, 'buf> Default for Writer<'sep, 'end, 'buf> {
fn default() -> Self {
Self {
index: 0,
sep,
end,
buf,
sep: " ",
end: "\n",
buf: None,
}
}
}

impl<'sep, 'end, 'buf> Writer<'sep, 'end, 'buf> {
/// Create a new writer.
pub fn new() -> Self {
Self::default()
}
/// Set the separator.
pub fn sep(&mut self, sep: &'sep str) -> &mut Self {
self.sep = sep;
Expand Down
29 changes: 29 additions & 0 deletions tests/char.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use iof::{InputStream, ReadInto, ReadOneInto};
use std::io::Cursor;

#[test]
#[should_panic = "expect more characters before EOF"]
fn from_empty() {
let reader = Cursor::new("".as_bytes());
let mut reader = InputStream::new(reader);

let _: char = reader.read();
}

#[test]
#[should_panic = "expect more characters before EOF"]
fn from_all_spaces() {
let reader = Cursor::new(" \n\r\n \t \r\n".as_bytes());
let mut reader = InputStream::new(reader);

let _: char = reader.read();
}

#[test]
#[should_panic = "Error during converting a string \"abc\" to a value of `char`: too many characters in string"]
fn from_multiple() {
let reader = Cursor::new("abc".as_bytes());
let mut reader = InputStream::new(reader);

let _: char = reader.read_in_line_some_trimmed();
}
93 changes: 86 additions & 7 deletions tests/ill_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iof::{InputStream, ReadInto, ReadOneInto, WriteInto, WriteOneInto};
use iof::{BufReadExt, InputStream, ReadInto, ReadOneInto, WriteInto, WriteOneInto};
use std::io;

struct IllBuffer;
Expand All @@ -24,43 +24,122 @@ impl io::Write for IllBuffer {
}

#[test]
fn try_read_ill() {
fn try_get_string_some() {
let mut buf = InputStream::new(IllBuffer);
let res: Result<&str, _> = buf.try_get_string_some();
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");
}

#[test]
fn try_get_line_some_trimmed() {
let mut buf = InputStream::new(IllBuffer);
let res: Result<&str, _> = buf.try_get_line_some_trimmed();
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");
}

#[test]
fn try_read() {
let mut buf = InputStream::new(IllBuffer);
let res: Result<u32, _> = buf.try_read();
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");
}

#[test]
fn try_read_some_in_line() {
let mut buf = InputStream::new(IllBuffer);
let res: Result<Vec<u32>, _> = buf.try_read_some_in_line();
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");
}

#[test]
fn try_read_any_in_line() {
let mut buf = InputStream::new(IllBuffer);
let res: Result<Vec<u32>, _> = buf.try_read_any_in_line();
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");
}

#[test]
fn try_read_line_ill() {
fn try_read_in_line_some_trimmed() {
let mut buf = InputStream::new(IllBuffer);

let res: Result<u32, _> = buf.try_read_in_line_some_trimmed();
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");
}

#[test]
fn try_read_remained_line_ill() {
fn try_read_in_line_trimmed() {
let mut buf = InputStream::new(IllBuffer);

let res: Result<u32, _> = buf.try_read_in_line_trimmed();
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");
}

#[test]
fn try_write_ill() {
fn try_write() {
let mut buf = IllBuffer;

let res: Result<(), _> = [1, 2, 3].try_write_into(&mut buf);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");

let res: Result<(), _> = ["", "", ""].try_write_into(&mut buf);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");

let res: Result<(), _> = ().try_write_into(&mut buf);
assert!(res.is_ok());

let res: Result<(), _> = ("\n", 2, ()).try_write_into(&mut buf);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.to_string(), "ill buffer");
}

#[test]
#[should_panic = "ill buffer"]
fn write_into_ill() {
fn write_into() {
[1, 2, 3].write_into(&mut IllBuffer);
}

#[test]
#[should_panic = "ill buffer"]
fn write_one_into_ill() {
fn write_one_into() {
42_usize.write_one_into(&mut IllBuffer);
}

#[test]
fn write() {
let mut buf = IllBuffer;
let mut buf = &mut buf;
let mut buf = &mut buf;
let buf = &mut buf;

assert_eq!(
' '.try_write_into(buf).unwrap_err().to_string(),
"ill buffer",
);
assert_eq!(
" ".try_write_into(buf).unwrap_err().to_string(),
"ill buffer",
);
assert_eq!(
1_usize.try_write_into(buf).unwrap_err().to_string(),
"ill buffer",
);
}
6 changes: 6 additions & 0 deletions tests/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ fn read_array_0() {
assert!(s1.is_empty());
assert!(s2.is_empty());
}

#[test]
fn read_empty_tuple() {
let () = read();
let () = try_read().unwrap();
}
18 changes: 17 additions & 1 deletion tests/stdin_lock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iof::{stdin, ReadInto};
use iof::{get_line, get_line_some, stdin, ReadInto};
use ntest::timeout;

#[test]
Expand All @@ -13,3 +13,19 @@ fn reentry_stdin() {
let _: usize = a.read();
let _: usize = b.read();
}

#[test]
#[cfg_attr(miri, ignore)]
#[timeout(100)]
#[should_panic]
fn stdin_get_line() {
let _ = get_line();
}

#[test]
#[cfg_attr(miri, ignore)]
#[timeout(100)]
#[should_panic]
fn stdin_get_line_some() {
let _ = get_line_some();
}
9 changes: 9 additions & 0 deletions tests/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ fn read_tuple_3() {
assert!(<u32>::try_read_from(&mut reader).is_err());
}

#[test]
#[should_panic = "expect more characters before EOF"]
fn try_read_tuple_3_insufficient_err() {
let reader = Cursor::new("1".as_bytes());
let mut reader = InputStream::new(reader);

let _: (i32, i8, u32) = reader.read();
}

#[test]
fn try_read_tuple_3_from_str_err() {
let reader = Cursor::new("1 2 -3".as_bytes());
Expand Down
Loading

0 comments on commit 3f15f3b

Please sign in to comment.