[librsvg: 4/15] tests: Add predicate to check the size of the generated PNG



commit 249d9f4996d8352cc7d3a551802acb258cecbad3
Author: Sven Neumann <sven svenfoo org>
Date:   Tue Feb 4 11:42:30 2020 +0100

    tests: Add predicate to check the size of the generated PNG

 Cargo.lock                         | 47 +++++++++++++++++++++
 tests/Cargo.toml                   |  1 +
 tests/Makefile.am                  | 11 ++---
 tests/src/cmdline/mod.rs           |  1 +
 tests/src/cmdline/png_predicate.rs | 65 +++++++++++++++++++++++++++++
 tests/src/cmdline/rsvg_convert.rs  | 84 +++++++++++++++++++++++++++-----------
 6 files changed, 180 insertions(+), 29 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index a270a30d..117d3e04 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,5 +1,10 @@
 # This file is automatically @generated by Cargo.
 # It is not intended for manual editing.
+[[package]]
+name = "adler32"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "aho-corasick"
 version = "0.7.7"
@@ -147,6 +152,14 @@ dependencies = [
  "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "crc32fast"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "criterion"
 version = "0.2.11"
@@ -277,6 +290,15 @@ dependencies = [
  "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "deflate"
+version = "0.7.20"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "derive_more"
 version = "0.99.2"
@@ -611,6 +633,14 @@ dependencies = [
  "unicode-normalization 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "inflate"
+version = "0.4.5"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "itertools"
 version = "0.8.2"
@@ -660,6 +690,7 @@ name = "librsvg-tests"
 version = "0.1.0"
 dependencies = [
  "assert_cmd 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "png 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "predicates 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -1002,6 +1033,17 @@ name = "pkg-config"
 version = "0.3.17"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
+[[package]]
+name = "png"
+version = "0.15.3"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "deflate 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)",
+ "inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "ppv-lite86"
 version = "0.2.6"
@@ -1553,6 +1595,7 @@ version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
 [metadata]
+"checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2"
 "checksum aho-corasick 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)" = 
"5f56c476256dc249def911d6f7580b5fc7e875895b5d7ee88f5d602208035744"
 "checksum alga 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"658f9468113d34781f6ca9d014d174c74b73de870f1e0e3ad32079bbab253b19"
 "checksum approx 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"f0e60b75072ecd4168020818c0107f2857bb6c4e64252d8d3983f6263b40a5c3"
@@ -1571,6 +1614,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
 "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
 "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
+"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
 "checksum criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = 
"0363053954f3e679645fc443321ca128b7b950a6fe288cf5f9335cc22ee58394"
 "checksum criterion-plot 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"76f9212ddf2f4a9eb2d401635190600656a1f88a932ef53d06e7fa4c7e02fb8e"
 "checksum crossbeam-deque 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c3aa945d63861bfe624b55d153a39684da1e8c0bc8fba932f7ee3a3c16cea3ca"
@@ -1582,6 +1626,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum csv 1.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"00affe7f6ab566df61b4be3ce8cf16bc2576bca0963ceb0955e45d514bf9a279"
 "checksum csv-core 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = 
"9b5cadb6b25c77aeff80ba701712494213f4a8418fcda2ee11b6560c3ad0bf4c"
 "checksum data-url 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d33fe99ccedd6e84bc035f1931bb2e6be79739d6242bd895e7311c886c50dc9c"
+"checksum deflate 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)" = 
"707b6a7b384888a70c8d2e8650b3e60170dfc6a67bb4aa67b6dfca57af4bedb4"
 "checksum derive_more 0.99.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"2159be042979966de68315bce7034bb000c775f22e3e834e1c52ff78f041cae8"
 "checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
 "checksum doc-comment 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"923dea538cea0aa3025e8685b20d6ee21ef99c4f77e954a30febbaac5ec73a97"
@@ -1620,6 +1665,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum gobject-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"31d1a804f62034eccf370006ccaef3708a71c31d561fee88564abe71177553d9"
 "checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = 
"eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772"
 "checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9"
+"checksum inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff"
 "checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
 "checksum itoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
 "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
@@ -1662,6 +1708,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum phf_shared 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7"
 "checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
 "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = 
"05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
+"checksum png 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ef859a23054bbfee7811284275ae522f0434a3c8e7f4b74bd4a35ae7e1c4a283"
 "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = 
"74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
 "checksum precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
 "checksum predicates 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a9bfe52247e5cc9b2f943682a85a5549fb9662245caf094504e69a2f03fe64d4"
diff --git a/tests/Cargo.toml b/tests/Cargo.toml
index 84707ecd..1dfa5293 100644
--- a/tests/Cargo.toml
+++ b/tests/Cargo.toml
@@ -7,4 +7,5 @@ edition = "2018"
 
 [dev-dependencies]
 assert_cmd = "0.12"
+png = "0.15.3"
 predicates = "1.0.2"
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e5e6581e..5167de3e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,10 +1,11 @@
 include $(top_srcdir)/glib-tap.mk
 
-test_sources =                         \
-       Cargo.toml              \
-       src/main.rs             \
-       src/cmdline/mod.rs      \
-       src/cmdline/rsvg_convert.rs
+test_sources =                         \
+       Cargo.toml                      \
+       src/main.rs                     \
+       src/cmdline/mod.rs              \
+       src/cmdline/rsvg_convert.rs     \
+       src/cmdline/png_predicate.rs
 
 EXTRA_DIST +=          \
        $(test_sources) \
diff --git a/tests/src/cmdline/mod.rs b/tests/src/cmdline/mod.rs
index c694bf2e..1647c326 100644
--- a/tests/src/cmdline/mod.rs
+++ b/tests/src/cmdline/mod.rs
@@ -1 +1,2 @@
+mod png_predicate;
 mod rsvg_convert;
diff --git a/tests/src/cmdline/png_predicate.rs b/tests/src/cmdline/png_predicate.rs
new file mode 100644
index 00000000..6bd8ad10
--- /dev/null
+++ b/tests/src/cmdline/png_predicate.rs
@@ -0,0 +1,65 @@
+extern crate png;
+extern crate predicates;
+
+use predicates::reflection::{Case, PredicateReflection, Product};
+use std::fmt;
+
+#[derive(Debug)]
+pub struct PngSizePredicate {
+    width: u32,
+    height: u32,
+}
+
+pub fn has_size(width: u32, height: u32) -> PngSizePredicate {
+    PngSizePredicate { width, height }
+}
+
+impl PngSizePredicate {
+    fn eval_info(&self, info: &png::OutputInfo) -> bool {
+        info.width == self.width && info.height == self.height
+    }
+
+    fn find_case_for_info<'a>(
+        &'a self,
+        expected: bool,
+        info: &png::OutputInfo,
+    ) -> Option<Case<'a>> {
+        if self.eval_info(info) == expected {
+            let product = self.product_for_info(info);
+            Some(Case::new(Some(self), false).add_product(product))
+        } else {
+            None
+        }
+    }
+
+    fn product_for_info(&self, info: &png::OutputInfo) -> Product {
+        let actual_size = format!("{} x {}", info.width, info.height);
+        Product::new("actual size", actual_size)
+    }
+}
+
+impl predicates::prelude::Predicate<[u8]> for PngSizePredicate {
+    fn eval(&self, data: &[u8]) -> bool {
+        let decoder = png::Decoder::new(data);
+        match decoder.read_info() {
+            Ok((info, _)) => self.eval_info(&info),
+            _ => false,
+        }
+    }
+
+    fn find_case<'a>(&'a self, expected: bool, data: &[u8]) -> Option<Case<'a>> {
+        let decoder = png::Decoder::new(data);
+        match decoder.read_info() {
+            Ok((info, _)) => self.find_case_for_info(expected, &info),
+            Err(e) => Some(Case::new(Some(self), false).add_product(Product::new("Error", e))),
+        }
+    }
+}
+
+impl PredicateReflection for PngSizePredicate {}
+
+impl fmt::Display for PngSizePredicate {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "check that it's a PNG with size {} x {}", self.width, self.height)
+    }
+}
diff --git a/tests/src/cmdline/rsvg_convert.rs b/tests/src/cmdline/rsvg_convert.rs
index 89ffad29..a7d9fb73 100644
--- a/tests/src/cmdline/rsvg_convert.rs
+++ b/tests/src/cmdline/rsvg_convert.rs
@@ -1,45 +1,81 @@
 extern crate assert_cmd;
 extern crate predicates;
 
-use assert_cmd::assert::Assert;
+use crate::cmdline::png_predicate;
+
 use assert_cmd::Command;
 use predicates::prelude::*;
 use std::path::Path;
 
-struct RsvgConvert {
-    cmd: Command,
-}
-
-fn location() -> &'static Path {
-    match option_env!("LIBRSVG_BUILD_DIR") {
-        Some(dir) => Path::new(dir),
-        None => Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap(),
-    }
-}
+struct RsvgConvert {}
 
 impl RsvgConvert {
-    fn new() -> Self {
-        let path = location().join("rsvg-convert");
-        RsvgConvert {
-            cmd: Command::new(path),
+    fn binary_location() -> &'static Path {
+        match option_env!("LIBRSVG_BUILD_DIR") {
+            Some(dir) => Path::new(dir),
+            None => Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap(),
         }
     }
 
-    fn assert_arg(self: &mut Self, arg: &str) -> Assert {
-        self.cmd.arg(arg).assert().success()
+    fn new() -> Command {
+        let path = Self::binary_location().join("rsvg-convert");
+        Command::new(path)
+    }
+
+    fn new_with_input(input: &Path) -> Command {
+        let mut command = RsvgConvert::new();
+        match command.pipe_stdin(input) {
+            Ok(_) => command,
+            Err(e) => panic!("Error opening file '{}': {}", input.display(), e),
+        }
     }
 }
 
 #[test]
-fn help_option() {
-    let expected = predicate::str::starts_with("Usage:");
-    RsvgConvert::new().assert_arg("-?").stdout(expected.clone());
-    RsvgConvert::new().assert_arg("--help").stdout(expected);
+fn empty_input_yields_error() {
+    let start = predicate::str::starts_with("Error reading SVG");
+    let end = predicate::str::ends_with("Input file is too short");
+    RsvgConvert::new()
+        .assert()
+        .failure()
+        .stderr(start.and(end).trim());
+}
+
+#[test]
+fn reads_from_stdin() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(200, 100));
 }
 
 #[test]
 fn version_option() {
-    let expected = predicate::str::starts_with("rsvg-convert version ");
-    RsvgConvert::new().assert_arg("-v").stdout(expected.clone());
-    RsvgConvert::new().assert_arg("--version").stdout(expected);
+    let out = predicate::str::starts_with("rsvg-convert version ");
+    RsvgConvert::new()
+        .arg("-v")
+        .assert()
+        .success()
+        .stdout(out.clone());
+    RsvgConvert::new()
+        .arg("--version")
+        .assert()
+        .success()
+        .stdout(out);
+}
+
+#[test]
+fn help_option() {
+    let out = predicate::str::starts_with("Usage:");
+    RsvgConvert::new()
+        .arg("-?")
+        .assert()
+        .success()
+        .stdout(out.clone());
+    RsvgConvert::new()
+        .arg("--help")
+        .assert()
+        .success()
+        .stdout(out);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]