diff --git a/mk-outs.sh b/mk-outs.sh new file mode 100755 index 0000000..eb2c76e --- /dev/null +++ b/mk-outs.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +OUTDIR="tests/expected" +[[ ! -d "$OUTDIR" ]] && mkdir -p "$OUTDIR" + +echo "Hello there" > $OUTDIR/hello1.txt +echo "Hello" "there" > $OUTDIR/hello2.txt +echo -n "Hello there" > $OUTDIR/hello1.n.txt +echo -n "Hello" "there" > $OUTDIR/hello2.n.txt diff --git a/tests/cli.rs b/tests/cli.rs index bb23235..6f0e292 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,18 +1,43 @@ use assert_cmd::Command; use predicates::prelude::*; +use std::fs; + +type TestResult = Result<(), Box>; #[test] -fn dies_no_args() { - let mut cmd = Command::cargo_bin("echor").expect("cargo binary error"); +fn dies_no_args() -> TestResult { + let mut cmd = Command::cargo_bin("echor")?; cmd.assert() .failure() .stderr(predicate::str::contains("Usage")); + + Ok(()) +} + +fn run(args: &[&str], expected_file: &str) -> TestResult { + let expected = fs::read_to_string(expected_file)?; + Command::cargo_bin("echor")? + .args(args) + .assert() + .success() + .stdout(expected); + Ok(()) } #[test] -fn runs() { - let mut cmd = Command::cargo_bin("echor").expect("cargo binary error"); - - cmd.arg("hello").assert().success(); +fn hello1() -> TestResult { + run(&["Hello there"], "tests/expected/hello1.txt") +} +#[test] +fn hello2() -> TestResult { + run(&["Hello", "there"], "tests/expected/hello2.txt") +} +#[test] +fn hello1_no_newline() -> TestResult { + run(&["Hello there", "-n"], "tests/expected/hello1.n.txt") +} +#[test] +fn hello2_no_newline() -> TestResult { + run(&["-n", "Hello", "there"], "tests/expected/hello2.n.txt") } diff --git a/tests/expected/hello1.n.txt b/tests/expected/hello1.n.txt new file mode 100644 index 0000000..b1011d0 --- /dev/null +++ b/tests/expected/hello1.n.txt @@ -0,0 +1 @@ +Hello there \ No newline at end of file diff --git a/tests/expected/hello1.txt b/tests/expected/hello1.txt new file mode 100644 index 0000000..d6613f5 --- /dev/null +++ b/tests/expected/hello1.txt @@ -0,0 +1 @@ +Hello there diff --git a/tests/expected/hello2.n.txt b/tests/expected/hello2.n.txt new file mode 100644 index 0000000..466c153 --- /dev/null +++ b/tests/expected/hello2.n.txt @@ -0,0 +1 @@ +Hello there \ No newline at end of file diff --git a/tests/expected/hello2.txt b/tests/expected/hello2.txt new file mode 100644 index 0000000..d6613f5 --- /dev/null +++ b/tests/expected/hello2.txt @@ -0,0 +1 @@ +Hello there