Add a test that you can choose the number of colours
- ID
ab68e59- date
2021-11-27 08:03:37+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
0dd5aed- message
Add a test that you can choose the number of colours- changed files
2 files, 34 additions, 5 deletions
Changed files
src/main.rs (5867) → src/main.rs (6750)
diff --git a/src/main.rs b/src/main.rs
index f9fc051..37ceff4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -102,8 +102,7 @@ mod tests {
use assert_cmd::Command;
// Note: for the purposes of these tests, I mostly trust the k-means code
- // provided by the external library. The test images are blocks of solid colour
- // that should give somewhat deterministic output.
+ // provided by the external library.
#[test]
fn it_prints_the_color_with_ansi_escape_codes() {
@@ -132,7 +131,7 @@ mod tests {
fn it_omits_the_escape_codes_with_no_palette() {
let mut cmd = Command::cargo_bin("dominant_colours").unwrap();
let output = cmd
- .args(&["./src/tests/red.png", "--count=1", "--no-palette"])
+ .args(&["./src/tests/red.png", "--count=1"])
.unwrap()
.assert()
.success()
@@ -143,8 +142,8 @@ mod tests {
let stdout = str::from_utf8(&output.stdout).unwrap();
assert!(
- stdout == "#ff0000\n" ||
- stdout == "#fe0000\n",
+ stdout == "\u{1b}[38;2;255;0;0m▇ #ff0000\u{1b}[0m\n" ||
+ stdout == "\u{1b}[38;2;254;0;0m▇ #fe0000\u{1b}[0m\n",
"stdout = {:?}", stdout
);
@@ -152,6 +151,36 @@ mod tests {
}
#[test]
+ fn it_defaults_to_five_colours() {
+ let mut cmd = Command::cargo_bin("dominant_colours").unwrap();
+ let output = cmd
+ .args(&["./src/tests/noise.jpg"])
+ .unwrap()
+ .assert()
+ .success()
+ .get_output()
+ .to_owned();
+
+ let stdout = str::from_utf8(&output.stdout).unwrap();
+ assert_eq!(stdout.matches("\n").count(), 5, "stdout = {:?}", stdout);
+ }
+
+ #[test]
+ fn it_lets_you_choose_the_count() {
+ let mut cmd = Command::cargo_bin("dominant_colours").unwrap();
+ let output = cmd
+ .args(&["./src/tests/noise.jpg", "--count=8"])
+ .unwrap()
+ .assert()
+ .success()
+ .get_output()
+ .to_owned();
+
+ let stdout = str::from_utf8(&output.stdout).unwrap();
+ assert_eq!(stdout.matches("\n").count(), 8, "stdout = {:?}", stdout);
+ }
+
+ #[test]
fn it_fails_if_you_pass_an_invalid_count() {
let mut cmd = Command::cargo_bin("dominant_colours").unwrap();
let output = cmd
src/tests/noise.jpg (0) → src/tests/noise.jpg (25175)
diff --git a/src/tests/noise.jpg b/src/tests/noise.jpg
new file mode 100644
index 0000000..0998e82
Binary files /dev/null and b/src/tests/noise.jpg differ