Skip to main content

Add a couple of tests with a simple red image

ID
9a357f7
date
2021-11-27 07:43:16+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
7086119
message
Add a couple of tests with a simple red image
changed files
4 files, 158 additions

Changed files

Cargo.lock (9947) → Cargo.lock (13035)

diff --git a/Cargo.lock b/Cargo.lock
index 72ec702..efc6aee 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -27,6 +27,20 @@ dependencies = [
 ]
 
 [[package]]
+name = "assert_cmd"
+version = "2.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e996dc7940838b7ef1096b882e29ec30a3149a3a443cdc8dba19ed382eca1fe2"
+dependencies = [
+ "bstr",
+ "doc-comment",
+ "predicates",
+ "predicates-core",
+ "predicates-tree",
+ "wait-timeout",
+]
+
+[[package]]
 name = "atty"
 version = "0.2.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -50,6 +64,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
 
 [[package]]
+name = "bstr"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
+dependencies = [
+ "lazy_static",
+ "memchr",
+ "regex-automata",
+]
+
+[[package]]
 name = "bytemuck"
 version = "1.7.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -108,9 +133,22 @@ dependencies = [
 ]
 
 [[package]]
+name = "difflib"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
+
+[[package]]
+name = "doc-comment"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
+
+[[package]]
 name = "dominant_colours"
 version = "1.0.0"
 dependencies = [
+ "assert_cmd",
  "clap",
  "image",
  "kmeans_colors",
@@ -118,6 +156,12 @@ dependencies = [
 ]
 
 [[package]]
+name = "either"
+version = "1.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
+
+[[package]]
 name = "getrandom"
 version = "0.2.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -154,6 +198,15 @@ dependencies = [
 ]
 
 [[package]]
+name = "itertools"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
+dependencies = [
+ "either",
+]
+
+[[package]]
 name = "jpeg-decoder"
 version = "0.1.22"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -171,12 +224,24 @@ dependencies = [
 ]
 
 [[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
 name = "libc"
 version = "0.2.108"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"
 
 [[package]]
+name = "memchr"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
+
+[[package]]
 name = "miniz_oxide"
 version = "0.3.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -267,6 +332,33 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
 
 [[package]]
+name = "predicates"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95e5a7689e456ab905c22c2b48225bb921aba7c8dfa58440d68ba13f6222a715"
+dependencies = [
+ "difflib",
+ "itertools",
+ "predicates-core",
+]
+
+[[package]]
+name = "predicates-core"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "57e35a3326b75e49aa85f5dc6ec15b41108cf5aee58eabb1f274dd18b73c2451"
+
+[[package]]
+name = "predicates-tree"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "338c7be2905b732ae3984a2f40032b5e94fd8f52505b186c7d4d68d193445df7"
+dependencies = [
+ "predicates-core",
+ "termtree",
+]
+
+[[package]]
 name = "proc-macro2"
 version = "1.0.32"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -315,6 +407,12 @@ dependencies = [
 ]
 
 [[package]]
+name = "regex-automata"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
+
+[[package]]
 name = "strsim"
 version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -332,6 +430,12 @@ dependencies = [
 ]
 
 [[package]]
+name = "termtree"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "13a4ec180a2de59b57434704ccfad967f789b12737738798fa08798cd5824c16"
+
+[[package]]
 name = "textwrap"
 version = "0.11.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -359,6 +463,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
 
 [[package]]
+name = "wait-timeout"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
+dependencies = [
+ "libc",
+]
+
+[[package]]
 name = "wasi"
 version = "0.10.2+wasi-snapshot-preview1"
 source = "registry+https://github.com/rust-lang/crates.io-index"

Cargo.toml (378) → Cargo.toml (399)

diff --git a/Cargo.toml b/Cargo.toml
index 7c8791b..d35a835 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,6 +4,7 @@ version = "1.0.0"
 edition = "2018"
 
 [dependencies]
+assert_cmd = "2.0.2"
 clap = "2.33"
 
 [dependencies.kmeans_colors]

src/main.rs (3550) → src/main.rs (5005)

diff --git a/src/main.rs b/src/main.rs
index 395e4fe..40a0f60 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -93,3 +93,47 @@ fn main() {
         }
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use std::str;
+
+    use assert_cmd::assert::OutputAssertExt;
+    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 deterministic output.
+
+    #[test]
+    fn it_prints_the_color_with_ansi_escape_codes() {
+        let mut cmd = Command::cargo_bin("dominant_colours").unwrap();
+        let output = cmd
+            .args(&["./src/tests/red.png", "--count=1"])
+            .unwrap()
+            .assert()
+            .success()
+            .get_output()
+            .to_owned();
+
+        assert_eq!(output.status.code().unwrap(), 0);
+        assert_eq!(str::from_utf8(&output.stdout).unwrap(), "\u{1b}[38;2;255;0;0m▇ #ff0000\u{1b}[0m\n");
+        assert_eq!(str::from_utf8(&output.stderr).unwrap(), "");
+    }
+
+    #[test]
+    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"])
+            .unwrap()
+            .assert()
+            .success()
+            .get_output()
+            .to_owned();
+
+        assert_eq!(output.status.code().unwrap(), 0);
+        assert_eq!(str::from_utf8(&output.stdout).unwrap(), "#ff0000\n");
+        assert_eq!(str::from_utf8(&output.stderr).unwrap(), "");
+    }
+}

src/tests/red.png (0) → src/tests/red.png (725)

diff --git a/src/tests/red.png b/src/tests/red.png
new file mode 100644
index 0000000..80aea6e
Binary files /dev/null and b/src/tests/red.png differ