Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show data as text when an tokio_test::io assert fails #6690

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tokio-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async-stream = "0.3.3"

bytes = "1.0.0"
futures-core = "0.3.0"
bstr = "1.6.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MSRV failure is because you are adding this dependency.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll take a look if it is possible to downgrade bstr. Otherwise i'll just copy paste* the relevant formatting code or cobble something together myself.

That said, taking a step back, do you think this change is a good idea in the first place?

  • Checking the license first, of course

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not add a new dependency for this. But you can put together something simple yourself.


[dev-dependencies]
tokio = { version = "1.2.0", path = "../tokio", features = ["full"] }
Expand Down
36 changes: 33 additions & 3 deletions tokio-test/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//! [`AsyncRead`]: tokio::io::AsyncRead
//! [`AsyncWrite`]: tokio::io::AsyncWrite

use bstr::BStr;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing! thanks

use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::sync::mpsc;
use tokio::time::{self, Duration, Instant, Sleep};
Expand Down Expand Up @@ -256,7 +257,7 @@ impl Inner {
Action::Write(ref mut expect) => {
let n = cmp::min(src.len(), expect.len());

assert_eq!(&src[..n], &expect[..n]);
assert_eq!(ShowBytes(src, n), ShowBytes(expect, n));

// Drop data that was matched
expect.drain(..n);
Expand Down Expand Up @@ -475,8 +476,16 @@ impl Drop for Mock {
}

self.inner.actions.iter().for_each(|a| match a {
Action::Read(data) => assert!(data.is_empty(), "There is still data left to read."),
Action::Write(data) => assert!(data.is_empty(), "There is still data left to write."),
Action::Read(data) => assert!(
data.is_empty(),
"There is still data left to read: {:?}",
ShowBytes(data, 10)
),
Action::Write(data) => assert!(
data.is_empty(),
"There is still data left to write: {:?}",
ShowBytes(data, 10)
),
_ => (),
});
}
Expand Down Expand Up @@ -508,3 +517,24 @@ impl fmt::Debug for Inner {
write!(f, "Inner {{...}}")
}
}

struct ShowBytes<'a>(&'a [u8], usize);

impl<'a> fmt::Debug for ShowBytes<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let n = self.0.len();
let limit = self.1;
if self.0.len() <= limit {
write!(f, "{:?}", BStr::new(self.0))
} else {
let bytes = BStr::new(&self.0[..limit]);
write!(f, "{bytes:?} plus {} more bytes", n - limit)
}
}
}

impl<'a> PartialEq for ShowBytes<'a> {
fn eq(&self, other: &Self) -> bool {
self.0[..self.1] == other.0[..other.1]
}
}
Loading