Skip to content

Commit

Permalink
Fix doc links and test and make clippy happy.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVeryDarkness committed Oct 6, 2024
1 parent 45ef6ef commit 0312577
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 20 deletions.
12 changes: 3 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//!
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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)
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/write/separator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,24 @@ impl<T: Rank + ?Sized> 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<T: Rank + ?Sized>(separator: &[&str]) {
assert_eq!(<T as GetDefaultSeparator>::DEFAULT_SEPARATOR, separator);
assert_eq!(get_rank(T::RANK, T::SPACE), separator);
}

#[test]
fn check_separator() {
check::<char>(&[""; 0]);
check::<Vec<char>>(&[""]);
check::<Mat<char>>(&["\n", ""]);
check::<usize>(&[""; 0]);
check::<Vec<usize>>(&[" "]);
check::<Mat<usize>>(&["\n", " "]);
}
}
34 changes: 31 additions & 3 deletions tests/ill_buffer.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand Down
52 changes: 47 additions & 5 deletions tests/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,55 @@ fn read_tuple_12() {
assert!(<u32>::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");
}

0 comments on commit 0312577

Please sign in to comment.