command-line-rust/exercises/ex02/tests/cli.rs

52 lines
1.1 KiB
Rust

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
type TestResult = Result<(), Box<dyn std::error::Error>>;
fn run(args: &[&str], expected_file: &str) -> TestResult {
let expected = fs::read_to_string(format!("{}{}", "tests/expected/", expected_file))?;
Command::cargo_bin("echor")?
.args(args)
.assert()
.success()
.stdout(expected);
Ok(())
}
#[test]
fn error_no_args() -> TestResult {
let mut cmd = Command::cargo_bin("echor")?;
cmd.assert()
.failure()
.stderr(predicate::str::contains("USAGE"));
Ok(())
}
#[test]
fn runs() -> TestResult {
let mut cmd = Command::cargo_bin("echor")?;
cmd.arg("Hola").assert().success();
Ok(())
}
#[test]
fn hello1() -> TestResult {
run(&["Hello there"], "hello1.txt")
}
#[test]
fn hello2() -> TestResult {
run(&["Hello", "there"], "hello2.txt")
}
#[test]
fn hello1_no_newline() -> TestResult {
run(&["Hello there", "-n"], "hello1.n.txt")
}
#[test]
fn hello2_no_newline() -> TestResult {
run(&["-n", "Hello", "there"], "hello2.n.txt")
}