Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kyclark committed Jan 13, 2024
1 parent b5440b6 commit d65e869
Show file tree
Hide file tree
Showing 95 changed files with 514 additions and 371 deletions.
1 change: 1 addition & 0 deletions 01_hello/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ authors = [ "Ken Youens-Clark <[email protected]>" ]

[dev-dependencies]
assert_cmd = "2.0.12"
pretty_assertions = "1.4.0"
6 changes: 5 additions & 1 deletion 01_hello/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use assert_cmd::Command;
use pretty_assertions::assert_eq;

#[test]
fn runs() {
let mut cmd = Command::cargo_bin("hello").unwrap();
cmd.assert().success().stdout("Hello, world!\n");
let output = cmd.output().expect("fail");
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).expect("invalid UTF-8");
assert_eq!(stdout, "Hello, world!\n");
}

#[test]
Expand Down
5 changes: 3 additions & 2 deletions 02_echor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ edition = "2021"
authors = [ "Ken Youens-Clark <[email protected]>" ]

[dependencies]
clap = { version = "4.1.4", features = ["derive"] }
clap = { version = "4.4.16", features = ["derive"] }

[dev-dependencies]
anyhow = "1.0.79"
assert_cmd = "2.0.12"
predicates = "3.0.4"
anyhow = "1.0.79"
pretty_assertions = "1.4.0"
12 changes: 8 additions & 4 deletions 02_echor/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use assert_cmd::Command;
use predicates::prelude::*;
use pretty_assertions::assert_eq;
use std::fs;

// --------------------------------------------------
Expand All @@ -16,11 +17,14 @@ fn dies_no_args() -> Result<()> {
// --------------------------------------------------
fn run(args: &[&str], expected_file: &str) -> Result<()> {
let expected = fs::read_to_string(expected_file)?;
Command::cargo_bin("echor")?
let output = Command::cargo_bin("echor")?
.args(args)
.assert()
.success()
.stdout(expected);
.output()
.expect("fail");

let stdout = String::from_utf8(output.stdout).expect("invalid UTF-8");
assert_eq!(stdout, expected);

Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion 03_catr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ authors = [ "Ken Youens-Clark <[email protected]>" ]

[dependencies]
anyhow = "1.0.79"
clap = { version = "4.1.4", features = ["derive"] }
clap = { version = "4.4.16", features = ["derive"] }

[dev-dependencies]
assert_cmd = "2.0.12"
predicates = "3.0.4"
pretty_assertions = "1.4.0"
rand = "0.8.5"
25 changes: 15 additions & 10 deletions 03_catr/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use assert_cmd::Command;
use predicates::prelude::*;
use pretty_assertions::assert_eq;
use rand::{distributions::Alphanumeric, Rng};
use std::fs;

Expand Down Expand Up @@ -53,11 +54,12 @@ fn skips_bad_file() -> Result<()> {
// --------------------------------------------------
fn run(args: &[&str], expected_file: &str) -> Result<()> {
let expected = fs::read_to_string(expected_file)?;
Command::cargo_bin(PRG)?
.args(args)
.assert()
.success()
.stdout(expected);
let output = Command::cargo_bin(PRG)?.args(args).output().expect("fail");
assert!(output.status.success());

let stdout = String::from_utf8(output.stdout).expect("invalid UTF-8");
assert_eq!(stdout, expected);

Ok(())
}

Expand All @@ -69,12 +71,15 @@ fn run_stdin(
) -> Result<()> {
let input = fs::read_to_string(input_file)?;
let expected = fs::read_to_string(expected_file)?;
Command::cargo_bin(PRG)?
.args(args)
let output = Command::cargo_bin(PRG)?
.write_stdin(input)
.assert()
.success()
.stdout(expected);
.args(args)
.output()
.expect("fail");
assert!(output.status.success());

let stdout = String::from_utf8(output.stdout).expect("invalid UTF-8");
assert_eq!(stdout, expected);
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions 04_headr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ clap = { version = "4.1.4", features = ["derive"] }
[dev-dependencies]
assert_cmd = "2.0.12"
predicates = "3.0.4"
pretty_assertions = "1.4.0"
rand = "0.8.5"
2 changes: 1 addition & 1 deletion 04_headr/mk-outs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ for FILE in $INPUTS/*.txt; do
done

ALL="$INPUTS/empty.txt $INPUTS/one.txt $INPUTS/two.txt $INPUTS/three.txt \
$INPUTS/ten.txt"
$INPUTS/twelve.txt"
head $ALL > $OUT_DIR/all.out
head -n 2 $ALL > $OUT_DIR/all.n2.out
head -n 4 $ALL > $OUT_DIR/all.n4.out
Expand Down
71 changes: 36 additions & 35 deletions 04_headr/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use assert_cmd::Command;
use predicates::prelude::*;
use pretty_assertions::assert_eq;
use rand::{distributions::Alphanumeric, Rng};
use std::fs::{self, File};
use std::io::prelude::*;
Expand All @@ -10,7 +11,7 @@ const EMPTY: &str = "./tests/inputs/empty.txt";
const ONE: &str = "./tests/inputs/one.txt";
const TWO: &str = "./tests/inputs/two.txt";
const THREE: &str = "./tests/inputs/three.txt";
const TEN: &str = "./tests/inputs/ten.txt";
const TWELVE: &str = "./tests/inputs/twelve.txt";

// --------------------------------------------------
fn random_string() -> String {
Expand Down Expand Up @@ -102,11 +103,9 @@ fn run(args: &[&str], expected_file: &str) -> Result<()> {
file.read_to_end(&mut buffer)?;
let expected = String::from_utf8_lossy(&buffer);

Command::cargo_bin(PRG)?
.args(args)
.assert()
.success()
.stdout(predicate::eq(expected.as_bytes() as &[u8]));
let output = Command::cargo_bin(PRG)?.args(args).output().expect("fail");
assert!(output.status.success());
assert_eq!(String::from_utf8_lossy(&output.stdout), expected);

Ok(())
}
Expand All @@ -124,11 +123,13 @@ fn run_stdin(
let expected = String::from_utf8_lossy(&buffer);
let input = fs::read_to_string(input_file)?;

Command::cargo_bin(PRG)?
let output = Command::cargo_bin(PRG)?
.write_stdin(input)
.args(args)
.assert()
.stdout(predicate::eq(expected.as_bytes() as &[u8]));
.output()
.expect("fail");
assert!(output.status.success());
assert_eq!(String::from_utf8_lossy(&output.stdout), expected);

Ok(())
}
Expand Down Expand Up @@ -328,97 +329,97 @@ fn three_c4_stdin() -> Result<()> {

// --------------------------------------------------
#[test]
fn ten() -> Result<()> {
run(&[TEN], "tests/expected/ten.txt.out")
fn twelve() -> Result<()> {
run(&[TWELVE], "tests/expected/twelve.txt.out")
}

#[test]
fn ten_n2() -> Result<()> {
run(&[TEN, "-n", "2"], "tests/expected/ten.txt.n2.out")
fn twelve_n2() -> Result<()> {
run(&[TWELVE, "-n", "2"], "tests/expected/twelve.txt.n2.out")
}

#[test]
fn ten_n4() -> Result<()> {
run(&[TEN, "-n", "4"], "tests/expected/ten.txt.n4.out")
fn twelve_n4() -> Result<()> {
run(&[TWELVE, "-n", "4"], "tests/expected/twelve.txt.n4.out")
}

#[test]
fn ten_c2() -> Result<()> {
run(&[TEN, "-c", "2"], "tests/expected/ten.txt.c2.out")
fn twelve_c2() -> Result<()> {
run(&[TWELVE, "-c", "2"], "tests/expected/twelve.txt.c2.out")
}

#[test]
fn ten_c4() -> Result<()> {
run(&[TEN, "-c", "4"], "tests/expected/ten.txt.c4.out")
fn twelve_c4() -> Result<()> {
run(&[TWELVE, "-c", "4"], "tests/expected/twelve.txt.c4.out")
}

#[test]
fn ten_stdin() -> Result<()> {
run_stdin(&[], TEN, "tests/expected/ten.txt.out")
fn twelve_stdin() -> Result<()> {
run_stdin(&[], TWELVE, "tests/expected/twelve.txt.out")
}

#[test]
fn ten_n2_stdin() -> Result<()> {
run_stdin(&["-n", "2"], TEN, "tests/expected/ten.txt.n2.out")
fn twelve_n2_stdin() -> Result<()> {
run_stdin(&["-n", "2"], TWELVE, "tests/expected/twelve.txt.n2.out")
}

#[test]
fn ten_n4_stdin() -> Result<()> {
run_stdin(&["-n", "4"], TEN, "tests/expected/ten.txt.n4.out")
fn twelve_n4_stdin() -> Result<()> {
run_stdin(&["-n", "4"], TWELVE, "tests/expected/twelve.txt.n4.out")
}

#[test]
fn ten_c2_stdin() -> Result<()> {
run_stdin(&["-c", "2"], TEN, "tests/expected/ten.txt.c2.out")
fn twelve_c2_stdin() -> Result<()> {
run_stdin(&["-c", "2"], TWELVE, "tests/expected/twelve.txt.c2.out")
}

#[test]
fn ten_c4_stdin() -> Result<()> {
run_stdin(&["-c", "4"], TEN, "tests/expected/ten.txt.c4.out")
fn twelve_c4_stdin() -> Result<()> {
run_stdin(&["-c", "4"], TWELVE, "tests/expected/twelve.txt.c4.out")
}

// --------------------------------------------------
#[test]
fn multiple_files() -> Result<()> {
run(&[EMPTY, ONE, TWO, THREE, TEN], "tests/expected/all.out")
run(&[EMPTY, ONE, TWO, THREE, TWELVE], "tests/expected/all.out")
}

#[test]
fn multiple_files_n2() -> Result<()> {
run(
&[EMPTY, ONE, TWO, THREE, TEN, "-n", "2"],
&[EMPTY, ONE, TWO, THREE, TWELVE, "-n", "2"],
"tests/expected/all.n2.out",
)
}

#[test]
fn multiple_files_n4() -> Result<()> {
run(
&["-n", "4", EMPTY, ONE, TWO, THREE, TEN],
&["-n", "4", EMPTY, ONE, TWO, THREE, TWELVE],
"tests/expected/all.n4.out",
)
}

#[test]
fn multiple_files_c1() -> Result<()> {
run(
&[EMPTY, ONE, TWO, THREE, TEN, "-c", "1"],
&[EMPTY, ONE, TWO, THREE, TWELVE, "-c", "1"],
"tests/expected/all.c1.out",
)
}

#[test]
fn multiple_files_c2() -> Result<()> {
run(
&[EMPTY, ONE, TWO, THREE, TEN, "-c", "2"],
&[EMPTY, ONE, TWO, THREE, TWELVE, "-c", "2"],
"tests/expected/all.c2.out",
)
}

#[test]
fn multiple_files_c4() -> Result<()> {
run(
&["-c", "4", EMPTY, ONE, TWO, THREE, TEN],
&["-c", "4", EMPTY, ONE, TWO, THREE, TWELVE],
"tests/expected/all.c4.out",
)
}
2 changes: 1 addition & 1 deletion 04_headr/tests/expected/all.c1.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
T
==> ./tests/inputs/three.txt <==
T
==> ./tests/inputs/ten.txt <==
==> ./tests/inputs/twelve.txt <==
o
2 changes: 1 addition & 1 deletion 04_headr/tests/expected/all.c2.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
Tw
==> ./tests/inputs/three.txt <==
Th
==> ./tests/inputs/ten.txt <==
==> ./tests/inputs/twelve.txt <==
on
2 changes: 1 addition & 1 deletion 04_headr/tests/expected/all.c4.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
Two
==> ./tests/inputs/three.txt <==
Thre
==> ./tests/inputs/ten.txt <==
==> ./tests/inputs/twelve.txt <==
one
2 changes: 1 addition & 1 deletion 04_headr/tests/expected/all.n2.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Four words.
Three
lines,

==> ./tests/inputs/ten.txt <==
==> ./tests/inputs/twelve.txt <==
one
two
2 changes: 1 addition & 1 deletion 04_headr/tests/expected/all.n4.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Three
lines,
four words.

==> ./tests/inputs/ten.txt <==
==> ./tests/inputs/twelve.txt <==
one
two
three
Expand Down
2 changes: 1 addition & 1 deletion 04_headr/tests/expected/all.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Three
lines,
four words.

==> ./tests/inputs/ten.txt <==
==> ./tests/inputs/twelve.txt <==
one
two
three
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ seven
eight
nine
ten
eleven
twelve
3 changes: 2 additions & 1 deletion 05_wcr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ authors = [ "Ken Youens-Clark <[email protected]>" ]

[dependencies]
anyhow = "1.0.79"
clap = { version = "4.1.4", features = ["derive"] }
clap = { version = "4.4.16", features = ["derive"] }

[dev-dependencies]
assert_cmd = "2.0.12"
predicates = "3.0.4"
pretty_assertions = "1.4.0"
rand = "0.8.5"
Loading

0 comments on commit d65e869

Please sign in to comment.