diff --git a/src/lib.rs b/src/lib.rs index 26d488c..bb425a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -241,9 +241,9 @@ //! //! Some higher-level functions are provided to write data sequence with default format to output: //! -//! - [WriteInto::write()] (or [WriteInto::try_write()]) writes to [standard output](std::io::Stdout) with default format. -//! - [WriteInto::write_into()] (or [WriteInto::try_write_into()]) writes to given buffer that implements [std::io::Write] with default format. -//! - [WriteInto::write_into_string()] (or [WriteInto::try_write_into_string()]) writes to a new string with default format. +//! - [WriteInto::try_write()] writes to [standard output](std::io::Stdout) with default format. +//! - [WriteInto::try_write_into()] writes to given buffer that implements [std::io::Write] with default format. +//! - [WriteInto::try_write_into_string()] writes to a new string with default format. //! //! The default format is defined as follows: //! @@ -258,12 +258,6 @@ //! [Display]: std::fmt::Display //! [Display::fmt]: std::fmt::Display::fmt //! -//! ## [WriteOneInto] -//! -//! Some lower-level functions are provided to write a single data item to output: -//! -//! - [WriteOneInto::write_one_into()] (or [WriteOneInto::try_write_one_into()]) writes to given buffer that implements [std::io::Write]. -//! //! [NonZeroU8]: std::num::NonZeroU8 //! [NonZeroU16]: std::num::NonZeroU16 //! [NonZeroU32]: std::num::NonZeroU32 diff --git a/src/write/mod.rs b/src/write/mod.rs index df1af2e..bc56eaa 100644 --- a/src/write/mod.rs +++ b/src/write/mod.rs @@ -34,7 +34,7 @@ pub trait WriteInto: Rank { where Self: GetDefaultSeparator, { - self.try_write_into_with_sep(s, &Self::DEFAULT_SEPARATOR) + self.try_write_into_with_sep(s, Self::DEFAULT_SEPARATOR) } /// Write into a string with given separator. #[inline] @@ -50,7 +50,7 @@ pub trait WriteInto: Rank { where Self: GetDefaultSeparator, { - self.try_write_into_string_with_sep(&Self::DEFAULT_SEPARATOR) + self.try_write_into_string_with_sep(Self::DEFAULT_SEPARATOR) } /// Write into [std::io::Stdout] with given separator. #[inline] @@ -63,7 +63,7 @@ pub trait WriteInto: Rank { where Self: GetDefaultSeparator, { - self.try_write_with_sep(&Self::DEFAULT_SEPARATOR) + self.try_write_with_sep(Self::DEFAULT_SEPARATOR) } } diff --git a/src/write/separator.rs b/src/write/separator.rs index 5e8bc08..47d4440 100644 --- a/src/write/separator.rs +++ b/src/write/separator.rs @@ -56,3 +56,24 @@ impl GetDefaultSeparator for T { type Separator = &'static str; const DEFAULT_SEPARATOR: &'static [&'static str] = get_rank(T::RANK, T::SPACE); } + +#[cfg(test)] +mod tests { + use super::*; + use crate::Mat; + + fn check(separator: &[&str]) { + assert_eq!(::DEFAULT_SEPARATOR, separator); + assert_eq!(get_rank(T::RANK, T::SPACE), separator); + } + + #[test] + fn check_separator() { + check::(&[""; 0]); + check::>(&[""]); + check::>(&["\n", ""]); + check::(&[""; 0]); + check::>(&[" "]); + check::>(&["\n", " "]); + } +} diff --git a/tests/ill_buffer.rs b/tests/ill_buffer.rs index 20b494b..305fc53 100644 --- a/tests/ill_buffer.rs +++ b/tests/ill_buffer.rs @@ -1,5 +1,5 @@ -use iof::{unwrap, BufReadExt, InputStream, ReadInto, ReadOneInto, WriteInto}; -use std::io; +use iof::{show, unwrap, BufReadExt, InputStream, ReadInto, ReadOneInto, WriteInto}; +use std::io::{self, Cursor}; struct IllBuffer; @@ -113,10 +113,38 @@ fn try_write() { #[test] #[should_panic = "ill buffer"] -fn write_into() { +fn write_error_error() { + use std::io::Write; + let mut buf = InputStream::new(Cursor::new("-1 -2 -3".as_bytes())); + let vec: Result<[u32; 3], iof::ReadError<_>> = buf.try_read(); + let err = vec.unwrap_err(); + unwrap!(write!(&mut IllBuffer, "{}", err)); +} + +#[test] +#[should_panic = "ill buffer"] +fn write_array() { unwrap!([1, 2, 3].try_write_into(&mut IllBuffer)); } +#[test] +#[should_panic = "ill buffer"] +fn write_tuple() { + unwrap!((1, 2, 3).try_write_into(&mut IllBuffer)); +} + +#[test] +#[should_panic = "ill buffer"] +fn show_array() { + show!([1, 2, 3] => IllBuffer); +} + +#[test] +#[should_panic = "ill buffer"] +fn show_tuple() { + show!((1, 2, 3), sep = [", "] => IllBuffer); +} + #[test] fn write() { let mut buf = IllBuffer; diff --git a/tests/tuple.rs b/tests/tuple.rs index fcad648..5f5bafe 100644 --- a/tests/tuple.rs +++ b/tests/tuple.rs @@ -64,13 +64,55 @@ fn read_tuple_12() { assert!(::try_read_from(&mut reader).is_err()); } +#[test] +fn write() { + let mut buf = Vec::new(); + let vec = (1, 2, 3); + unwrap!(vec.try_write_into(&mut buf)); + assert_eq!(String::from_utf8(buf).unwrap(), "1 2 3"); + + let mut buf = Vec::new(); + let vec = (1,); + unwrap!(vec.try_write_into(&mut buf)); + assert_eq!(String::from_utf8(buf).unwrap(), "1"); + + let mut buf = Vec::new(); + let vec = ((1, 2), (3, 4)); + unwrap!(vec.try_write_into(&mut buf)); + assert_eq!(String::from_utf8(buf).unwrap(), "1 2\n3 4"); + + let mut buf = Vec::new(); + let vec = (); + unwrap!(vec.try_write_into(&mut buf)); + assert_eq!(String::from_utf8(buf).unwrap(), ""); + + let mut buf = Vec::new(); + let vec = ((1, 2, 3), (4, 5, 6), (7, 8, 9)); + unwrap!(vec.try_write_into(&mut buf)); + assert_eq!(String::from_utf8(buf).unwrap(), "1 2 3\n4 5 6\n7 8 9"); +} + #[test] fn show() { - show!((1, 2, 3)); - show!(((1, 2), (3, 4))); - show!(()); - show!(((1, 2, 3), (4, 5, 6), (7, 8, 9))); + use std::str::from_utf8; let mut buf = Vec::new(); + buf.clear(); + show!((1, 2, 3) => buf); + assert_eq!(unwrap!(from_utf8(&buf)), "1 2 3\n"); + + buf.clear(); + show!(((1, 2), (3, 4)) => buf); + assert_eq!(unwrap!(from_utf8(&buf)), "1 2\n3 4\n"); + + buf.clear(); + show!(() => buf); + assert_eq!(unwrap!(from_utf8(&buf)), "\n"); + + buf.clear(); + show!(((1, 2, 3), (4, 5, 6), (7, 8, 9)) => buf); + assert_eq!(unwrap!(from_utf8(&buf)), "1 2 3\n4 5 6\n7 8 9\n"); + + buf.clear(); show!(((1, 2, 3), (4, 5, 6), (7, 8, 9)) => buf); - assert_eq!(String::from_utf8(buf).unwrap(), "1 2 3\n4 5 6\n7 8 9\n"); + assert_eq!(unwrap!(from_utf8(&buf)), "1 2 3\n4 5 6\n7 8 9\n"); }