-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//! Modifiers related to the [`Read`](std::io::Read) trait. | ||
mod extensions; | ||
mod modifiers; | ||
|
||
pub use extensions::*; | ||
pub use modifiers::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::io::Read; | ||
|
||
use crate::assertions::AssertionBuilder; | ||
|
||
use super::WhenReadAsBytesModifier; | ||
|
||
/// Modifiers for types that implement [`Read`]. | ||
pub trait ReadExtensions<T, M> | ||
where | ||
T: Read, | ||
{ | ||
/// Reads the subject into a buffer, then executes the assertion on it. | ||
/// | ||
/// ``` | ||
/// # use expecters::prelude::*; | ||
/// use std::io::Cursor; | ||
/// expect!( | ||
/// Cursor::new("Hello, world!"), | ||
/// when_read, | ||
/// as_utf8, | ||
/// to_equal("Hello, world!"), | ||
/// ); | ||
/// ``` | ||
/// | ||
/// The assertion fails if reading the subject fails: | ||
/// | ||
/// ```should_panic | ||
/// # use expecters::prelude::*; | ||
/// use std::io::{Error, ErrorKind, Read}; | ||
/// | ||
/// struct MyReader; | ||
/// | ||
/// impl Read for MyReader { | ||
/// fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> { | ||
/// Err(Error::new(ErrorKind::Other, "always fail")) | ||
/// } | ||
/// } | ||
/// | ||
/// expect!( | ||
/// MyReader, | ||
/// when_read, | ||
/// count, | ||
/// to_be_greater_than_or_equal_to(0), | ||
/// ); | ||
/// ``` | ||
fn when_read(self) -> AssertionBuilder<Vec<u8>, WhenReadAsBytesModifier<M>>; | ||
} | ||
|
||
impl<T, M> ReadExtensions<T, M> for AssertionBuilder<T, M> | ||
where | ||
T: Read, | ||
{ | ||
#[inline] | ||
fn when_read(self) -> AssertionBuilder<Vec<u8>, WhenReadAsBytesModifier<M>> { | ||
AssertionBuilder::modify(self, WhenReadAsBytesModifier::new) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod as_bytes; | ||
|
||
pub use as_bytes::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::io::Read; | ||
|
||
use crate::assertions::{ | ||
general::IntoInitializableOutput, Assertion, AssertionContext, AssertionContextBuilder, | ||
AssertionModifier, | ||
}; | ||
|
||
/// Reads a subject into a buffer. | ||
#[derive(Clone, Debug)] | ||
pub struct WhenReadAsBytesModifier<M> { | ||
prev: M, | ||
} | ||
|
||
impl<M> WhenReadAsBytesModifier<M> { | ||
#[inline] | ||
pub(crate) fn new(prev: M) -> Self { | ||
WhenReadAsBytesModifier { prev } | ||
} | ||
} | ||
|
||
impl<M, A> AssertionModifier<A> for WhenReadAsBytesModifier<M> | ||
where | ||
M: AssertionModifier<WhenReadAsBytesAssertion<A>>, | ||
{ | ||
type Output = M::Output; | ||
|
||
fn apply(self, cx: AssertionContextBuilder, next: A) -> Self::Output { | ||
self.prev.apply(cx, WhenReadAsBytesAssertion { next }) | ||
} | ||
} | ||
|
||
/// Reads the subject into a buffer and executes the inner assertion on it. | ||
#[derive(Clone, Debug)] | ||
pub struct WhenReadAsBytesAssertion<A> { | ||
next: A, | ||
} | ||
|
||
impl<T, A> Assertion<T> for WhenReadAsBytesAssertion<A> | ||
where | ||
T: Read, | ||
A: Assertion<Vec<u8>, Output: IntoInitializableOutput>, | ||
{ | ||
type Output = <A::Output as IntoInitializableOutput>::Initialized; | ||
|
||
fn execute(self, mut cx: AssertionContext, subject: T) -> Self::Output { | ||
let bytes = match subject.bytes().collect::<Result<Vec<_>, _>>() { | ||
Ok(bytes) => bytes, | ||
Err(error) => { | ||
cx.annotate("error", &error); | ||
return cx.fail("failed to read"); | ||
} | ||
}; | ||
|
||
self.next.execute(cx, bytes).into_initialized() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters