Lines
100 %
Functions
75 %
Branches
use std::io::{self, BufRead, Write};
/// Prints a prompt and reads a line of input.
pub fn prompt<R>(prompt_text: &str, mut reader: R) -> String
where
R: BufRead,
{
print!("{}", prompt_text);
io::stdout().flush().expect("Failed to flush stdout.");
let mut input = String::new();
reader.read_line(&mut input).expect("Failed to read input.");
input.trim().to_lowercase()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_prompt() {
// GIVEN a prompt with expected text
let prompt_text = "Enter some text: ";
let expected = "hello";
let reader = std::io::Cursor::new(format!("{}\n", expected));
// WHEN the user enters text
let actual = prompt(prompt_text, reader);
// THEN it should match the expected one
assert_eq!(actual, expected);