Feat(testing and infra): skeleton for catr library
This commit is contained in:
200
tests/cli.rs
Normal file
200
tests/cli.rs
Normal file
@@ -0,0 +1,200 @@
|
||||
use assert_cmd::Command;
|
||||
use predicates::prelude::*;
|
||||
use rand::{distributions::Alphanumeric, Rng};
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
|
||||
type TestResult = Result<(), Box<dyn Error>>;
|
||||
|
||||
const PRG: &str = "catr";
|
||||
const EMPTY: &str = "tests/inputs/empty.txt";
|
||||
const FOX: &str = "tests/inputs/fox.txt";
|
||||
const SPIDERS: &str = "tests/inputs/spiders.txt";
|
||||
const BUSTLE: &str = "tests/inputs/the-bustle.txt";
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn usage() -> TestResult {
|
||||
for flag in &["-h", "--help"] {
|
||||
Command::cargo_bin(PRG)?
|
||||
.arg(flag)
|
||||
.assert()
|
||||
.stdout(predicate::str::contains("USAGE"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
fn gen_bad_file() -> String {
|
||||
loop {
|
||||
let filename: String = rand::thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(7)
|
||||
.map(char::from)
|
||||
.collect();
|
||||
|
||||
if fs::metadata(&filename).is_err() {
|
||||
return filename;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn skips_bad_file() -> TestResult {
|
||||
let bad = gen_bad_file();
|
||||
let expected = format!("{}: .* [(]os error 2[)]", bad);
|
||||
Command::cargo_bin(PRG)?
|
||||
.arg(&bad)
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(predicate::str::is_match(expected)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
fn run(args: &[&str], expected_file: &str) -> TestResult {
|
||||
let expected = fs::read_to_string(expected_file)?;
|
||||
Command::cargo_bin(PRG)?
|
||||
.args(args)
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
fn run_stdin(
|
||||
input_file: &str,
|
||||
args: &[&str],
|
||||
expected_file: &str,
|
||||
) -> TestResult {
|
||||
let input = fs::read_to_string(input_file)?;
|
||||
let expected = fs::read_to_string(expected_file)?;
|
||||
Command::cargo_bin(PRG)?
|
||||
.args(args)
|
||||
.write_stdin(input)
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn bustle_stdin() -> TestResult {
|
||||
run_stdin(BUSTLE, &["-"], "tests/expected/the-bustle.txt.stdin.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn bustle_stdin_n() -> TestResult {
|
||||
run_stdin(
|
||||
BUSTLE,
|
||||
&["-n", "-"],
|
||||
"tests/expected/the-bustle.txt.n.stdin.out",
|
||||
)
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn bustle_stdin_b() -> TestResult {
|
||||
run_stdin(
|
||||
BUSTLE,
|
||||
&["-b", "-"],
|
||||
"tests/expected/the-bustle.txt.b.stdin.out",
|
||||
)
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn empty() -> TestResult {
|
||||
run(&[EMPTY], "tests/expected/empty.txt.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn empty_n() -> TestResult {
|
||||
run(&["-n", EMPTY], "tests/expected/empty.txt.n.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn empty_b() -> TestResult {
|
||||
run(&["-b", EMPTY], "tests/expected/empty.txt.b.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn fox() -> TestResult {
|
||||
run(&[FOX], "tests/expected/fox.txt.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn fox_n() -> TestResult {
|
||||
run(&["-n", FOX], "tests/expected/fox.txt.n.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn fox_b() -> TestResult {
|
||||
run(&["-b", FOX], "tests/expected/fox.txt.b.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn spiders() -> TestResult {
|
||||
run(&[SPIDERS], "tests/expected/spiders.txt.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn spiders_n() -> TestResult {
|
||||
run(&["--number", SPIDERS], "tests/expected/spiders.txt.n.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn spiders_b() -> TestResult {
|
||||
run(
|
||||
&["--number-nonblank", SPIDERS],
|
||||
"tests/expected/spiders.txt.b.out",
|
||||
)
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn bustle() -> TestResult {
|
||||
run(&[BUSTLE], "tests/expected/the-bustle.txt.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn bustle_n() -> TestResult {
|
||||
run(&["-n", BUSTLE], "tests/expected/the-bustle.txt.n.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn bustle_b() -> TestResult {
|
||||
run(&["-b", BUSTLE], "tests/expected/the-bustle.txt.b.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn all() -> TestResult {
|
||||
run(&[FOX, SPIDERS, BUSTLE], "tests/expected/all.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn all_n() -> TestResult {
|
||||
run(&[FOX, SPIDERS, BUSTLE, "-n"], "tests/expected/all.n.out")
|
||||
}
|
||||
|
||||
// --------------------------------------------------
|
||||
#[test]
|
||||
fn all_b() -> TestResult {
|
||||
run(&[FOX, SPIDERS, BUSTLE, "-b"], "tests/expected/all.b.out")
|
||||
}
|
||||
13
tests/expected/all.b.out
Normal file
13
tests/expected/all.b.out
Normal file
@@ -0,0 +1,13 @@
|
||||
1 The quick brown fox jumps over the lazy dog.
|
||||
1 Don't worry, spiders,
|
||||
2 I keep house
|
||||
3 casually.
|
||||
1 The bustle in a house
|
||||
2 The morning after death
|
||||
3 Is solemnest of industries
|
||||
4 Enacted upon earth,—
|
||||
|
||||
5 The sweeping up the heart,
|
||||
6 And putting love away
|
||||
7 We shall not want to use again
|
||||
8 Until eternity.
|
||||
13
tests/expected/all.n.out
Normal file
13
tests/expected/all.n.out
Normal file
@@ -0,0 +1,13 @@
|
||||
1 The quick brown fox jumps over the lazy dog.
|
||||
1 Don't worry, spiders,
|
||||
2 I keep house
|
||||
3 casually.
|
||||
1 The bustle in a house
|
||||
2 The morning after death
|
||||
3 Is solemnest of industries
|
||||
4 Enacted upon earth,—
|
||||
5
|
||||
6 The sweeping up the heart,
|
||||
7 And putting love away
|
||||
8 We shall not want to use again
|
||||
9 Until eternity.
|
||||
13
tests/expected/all.out
Normal file
13
tests/expected/all.out
Normal file
@@ -0,0 +1,13 @@
|
||||
The quick brown fox jumps over the lazy dog.
|
||||
Don't worry, spiders,
|
||||
I keep house
|
||||
casually.
|
||||
The bustle in a house
|
||||
The morning after death
|
||||
Is solemnest of industries
|
||||
Enacted upon earth,—
|
||||
|
||||
The sweeping up the heart,
|
||||
And putting love away
|
||||
We shall not want to use again
|
||||
Until eternity.
|
||||
0
tests/expected/empty.txt.b.out
Normal file
0
tests/expected/empty.txt.b.out
Normal file
0
tests/expected/empty.txt.n.out
Normal file
0
tests/expected/empty.txt.n.out
Normal file
0
tests/expected/empty.txt.out
Normal file
0
tests/expected/empty.txt.out
Normal file
1
tests/expected/fox.txt.b.out
Normal file
1
tests/expected/fox.txt.b.out
Normal file
@@ -0,0 +1 @@
|
||||
1 The quick brown fox jumps over the lazy dog.
|
||||
1
tests/expected/fox.txt.n.out
Normal file
1
tests/expected/fox.txt.n.out
Normal file
@@ -0,0 +1 @@
|
||||
1 The quick brown fox jumps over the lazy dog.
|
||||
1
tests/expected/fox.txt.out
Normal file
1
tests/expected/fox.txt.out
Normal file
@@ -0,0 +1 @@
|
||||
The quick brown fox jumps over the lazy dog.
|
||||
3
tests/expected/spiders.txt.b.out
Normal file
3
tests/expected/spiders.txt.b.out
Normal file
@@ -0,0 +1,3 @@
|
||||
1 Don't worry, spiders,
|
||||
2 I keep house
|
||||
3 casually.
|
||||
3
tests/expected/spiders.txt.n.out
Normal file
3
tests/expected/spiders.txt.n.out
Normal file
@@ -0,0 +1,3 @@
|
||||
1 Don't worry, spiders,
|
||||
2 I keep house
|
||||
3 casually.
|
||||
3
tests/expected/spiders.txt.out
Normal file
3
tests/expected/spiders.txt.out
Normal file
@@ -0,0 +1,3 @@
|
||||
Don't worry, spiders,
|
||||
I keep house
|
||||
casually.
|
||||
9
tests/expected/the-bustle.txt.b.out
Normal file
9
tests/expected/the-bustle.txt.b.out
Normal file
@@ -0,0 +1,9 @@
|
||||
1 The bustle in a house
|
||||
2 The morning after death
|
||||
3 Is solemnest of industries
|
||||
4 Enacted upon earth,—
|
||||
|
||||
5 The sweeping up the heart,
|
||||
6 And putting love away
|
||||
7 We shall not want to use again
|
||||
8 Until eternity.
|
||||
9
tests/expected/the-bustle.txt.b.stdin.out
Normal file
9
tests/expected/the-bustle.txt.b.stdin.out
Normal file
@@ -0,0 +1,9 @@
|
||||
1 The bustle in a house
|
||||
2 The morning after death
|
||||
3 Is solemnest of industries
|
||||
4 Enacted upon earth,—
|
||||
|
||||
5 The sweeping up the heart,
|
||||
6 And putting love away
|
||||
7 We shall not want to use again
|
||||
8 Until eternity.
|
||||
9
tests/expected/the-bustle.txt.n.out
Normal file
9
tests/expected/the-bustle.txt.n.out
Normal file
@@ -0,0 +1,9 @@
|
||||
1 The bustle in a house
|
||||
2 The morning after death
|
||||
3 Is solemnest of industries
|
||||
4 Enacted upon earth,—
|
||||
5
|
||||
6 The sweeping up the heart,
|
||||
7 And putting love away
|
||||
8 We shall not want to use again
|
||||
9 Until eternity.
|
||||
9
tests/expected/the-bustle.txt.n.stdin.out
Normal file
9
tests/expected/the-bustle.txt.n.stdin.out
Normal file
@@ -0,0 +1,9 @@
|
||||
1 The bustle in a house
|
||||
2 The morning after death
|
||||
3 Is solemnest of industries
|
||||
4 Enacted upon earth,—
|
||||
5
|
||||
6 The sweeping up the heart,
|
||||
7 And putting love away
|
||||
8 We shall not want to use again
|
||||
9 Until eternity.
|
||||
9
tests/expected/the-bustle.txt.out
Normal file
9
tests/expected/the-bustle.txt.out
Normal file
@@ -0,0 +1,9 @@
|
||||
The bustle in a house
|
||||
The morning after death
|
||||
Is solemnest of industries
|
||||
Enacted upon earth,—
|
||||
|
||||
The sweeping up the heart,
|
||||
And putting love away
|
||||
We shall not want to use again
|
||||
Until eternity.
|
||||
9
tests/expected/the-bustle.txt.stdin.out
Normal file
9
tests/expected/the-bustle.txt.stdin.out
Normal file
@@ -0,0 +1,9 @@
|
||||
The bustle in a house
|
||||
The morning after death
|
||||
Is solemnest of industries
|
||||
Enacted upon earth,—
|
||||
|
||||
The sweeping up the heart,
|
||||
And putting love away
|
||||
We shall not want to use again
|
||||
Until eternity.
|
||||
Reference in New Issue
Block a user