Open seanballais opened 1 year ago
Not tested: https://crates.io/crates/pty-process
I'll try that one out tomorrow.
You could call functions inside the readline
matches cases and test those on their own:
use std::io::Write;
use rustyline::error::ReadlineError;
use rustyline::Editor;
fn handle_line<W>(mut dest: W, line: String) where W: Write {
write!(&mut dest, "Line: {line}\n").unwrap();
if line == "clear" {
write!(&mut dest, "{esc}[2J{esc}[1;1H", esc = 27 as char).unwrap();
dest.flush().unwrap();
}
}
#[test]
fn line_is_handled() {
let mut dest = Vec::new();
handle_line(&mut dest, "something".to_string());
let actual = String::from_utf8(dest).unwrap();
let expected = "Line: something\n".to_string();
assert_eq!(actual, expected);
}
fn main() {
let mut line_editor = Editor::<()>::new().unwrap();
let mut counter = 0;
let mut dest = std::io::stdout();
loop {
let readline = line_editor.readline("> ");
match readline {
Ok(line) => {
line_editor.add_history_entry(line.clone());
counter += 1;
handle_line(&mut dest, line);
}
Err(ReadlineError::Interrupted) => {
println!("Ctrl+C");
break;
}
Err(ReadlineError::Eof) => {
println!("Ctrl+D | Lines counted: {}", counter);
break;
}
Err(err) => {
println!("Error: {:?}", err);
break;
}
}
}
}
This means that the logic inside main
is not tested, but that is arguably not so bad since it's mostly declarative and already tested by rustyline
. The complex logic will most likely be inside your own logic.
FWIW, here's a working example of end-to-end testing (facilitated via the assert_cmd
crate) for a CLI app that uses rustyline: https://github.com/splitgraph/seafowl/blob/main/tests/cli/basic.rs
I have been figuring out how to test the following basic CLI app that uses
rustyline
usingstd::process::Command
but I could not figure out how to pull it off right.The following is the code for CLI app:
And code for the test:
When I run the test with
cargo test -- --nocapture
, I only get the following output:I was expecting something like:
I believe #504 may be related to this problem. Nevertheless, how can I achieve the second output?