pub struct VioFakeBuilder { /* private fields */ }
Expand description
Create a new FakeVio
to be used for testing.
The builder first lets you specify the expected IO messages, then lets you
create the FakeVio
.
use virtual_io::VirtualIo;
fn get_name_base(vio: &mut impl VirtualIo) -> String {
vio.print("What is your name? ");
let name = vio.read_line();
vio.println(format!("Hello, {}!", name));
name
}
#[cfg(test)]
mod test {
use super::*;
use virtual_io::VioFakeBuilder;
#[test]
fn test_get_name() {
// Create a fake version of vio that we can inject into the base
// function.
let mut vio = VioFakeBuilder::new()
// Add the expected io calls.
.expect_stdout("What is your name? ")
.provide_stdin("John")
.expect_stdout("Hello, John!\n")
// Build the fake vio.
.build();
// Assert that the return value is correct.
assert_eq!(get_name_base(&mut vio), "John");
// Assert that all io operations were identical to what was expected.
assert_eq!(vio.get_actual(), vio.get_expected());
}
}
Implementations§
Source§impl VioFakeBuilder
impl VioFakeBuilder
pub const fn new() -> Self
pub fn expect_stdout<S: Into<String>>(&mut self, message: S) -> &mut Self
pub fn provide_stdin<S: Into<String>>(&mut self, message: S) -> &mut Self
pub fn set_environment_var<S: Into<String>>( &mut self, name: S, value: S, ) -> &mut Self
pub fn build(&self) -> VioFake
Trait Implementations§
Source§impl Debug for VioFakeBuilder
impl Debug for VioFakeBuilder
Source§impl Default for VioFakeBuilder
impl Default for VioFakeBuilder
Source§fn default() -> VioFakeBuilder
fn default() -> VioFakeBuilder
Returns the “default value” for a type. Read more
Source§impl PartialEq for VioFakeBuilder
impl PartialEq for VioFakeBuilder
impl Eq for VioFakeBuilder
impl StructuralPartialEq for VioFakeBuilder
Auto Trait Implementations§
impl Freeze for VioFakeBuilder
impl RefUnwindSafe for VioFakeBuilder
impl Send for VioFakeBuilder
impl Sync for VioFakeBuilder
impl Unpin for VioFakeBuilder
impl UnwindSafe for VioFakeBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more