[librsvg: 5/15] tests: Add tests for rsvg-convert output size options



commit d9b7a6fa072d132fa2d4ee6ea26bf98afb7f4779
Author: Sven Neumann <sven svenfoo org>
Date:   Tue Feb 4 18:05:34 2020 +0100

    tests: Add tests for rsvg-convert output size options

 tests/src/cmdline/png_predicate.rs |   6 +-
 tests/src/cmdline/rsvg_convert.rs  | 149 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 151 insertions(+), 4 deletions(-)
---
diff --git a/tests/src/cmdline/png_predicate.rs b/tests/src/cmdline/png_predicate.rs
index 6bd8ad10..83621877 100644
--- a/tests/src/cmdline/png_predicate.rs
+++ b/tests/src/cmdline/png_predicate.rs
@@ -60,6 +60,10 @@ 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)
+        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 a7d9fb73..87c329c1 100644
--- a/tests/src/cmdline/rsvg_convert.rs
+++ b/tests/src/cmdline/rsvg_convert.rs
@@ -7,6 +7,17 @@ use assert_cmd::Command;
 use predicates::prelude::*;
 use std::path::Path;
 
+// What should be tested here?
+// The goal is to test the code in rsvg-convert, not the entire library.
+//
+//  - all command-line options are accepted
+//  - size and resolution of the output (should be sufficient to do that for PNG)
+//  - limit on output size (32767 pixels)
+//  - handling of SOURCE_DATA_EPOCH environment variable for PDF output
+//  - handling of background color option
+//  - error handling for export lookup ID
+//  - error handling for invalid input
+
 struct RsvgConvert {}
 
 impl RsvgConvert {
@@ -33,12 +44,12 @@ impl RsvgConvert {
 
 #[test]
 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");
+    let starts_with = predicate::str::starts_with("Error reading SVG");
+    let ends_with = predicate::str::ends_with("Input file is too short");
     RsvgConvert::new()
         .assert()
         .failure()
-        .stderr(start.and(end).trim());
+        .stderr(starts_with.and(ends_with).trim());
 }
 
 #[test]
@@ -50,6 +61,138 @@ fn reads_from_stdin() {
         .stdout(png_predicate::has_size(200, 100));
 }
 
+#[test]
+fn width_option() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("--width=300")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(300, 150));
+}
+
+#[test]
+fn height_option() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("--height=200")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(400, 200));
+}
+
+#[test]
+fn width_and_height_options() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("--width=300")
+        .arg("--height=200")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(300, 200));
+}
+
+#[test]
+fn zoom_factor() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("--zoom=0.8")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(160, 80));
+}
+
+// TODO: Is this a bug in rsvg-convert or the desired behavior ?
+#[test]
+fn zoom_factor_and_width_conflicts() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("--width=400")
+        .arg("--zoom=1.5")
+        .assert()
+        .failure()
+        .stderr(predicate::str::ends_with("Could not render file stdin").trim());
+}
+
+#[test]
+fn zoom_factor_and_larger_size() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("--width=400")
+        .arg("--height=200")
+        .arg("--zoom=1.5")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(300, 150));
+}
+
+#[test]
+fn zoom_factor_and_smaller_size() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("--width=400")
+        .arg("--height=200")
+        .arg("--zoom=3.5")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(400, 200));
+}
+
+#[test]
+fn x_zoom_option() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("--x-zoom=2")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(400, 100));
+}
+
+#[test]
+fn x_short_option() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("-x")
+        .arg("2.0")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(400, 100));
+}
+
+#[test]
+fn y_zoom_option() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("--y-zoom=2.0")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(200, 200));
+}
+
+#[test]
+fn y_short_option() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    RsvgConvert::new_with_input(input)
+        .arg("-y")
+        .arg("2")
+        .assert()
+        .success()
+        .stdout(png_predicate::has_size(200, 200));
+}
+
+#[test]
+fn huge_zoom_yields_error() {
+    let input = Path::new("fixtures/dimensions/521-with-viewbox.svg");
+    let starts_with =
+        predicate::str::starts_with("The resulting image would be larger than 32767 pixels");
+    let ends_with = predicate::str::ends_with("Please specify a smaller size.");
+    RsvgConvert::new_with_input(input)
+        .arg("--zoom=1000")
+        .assert()
+        .failure()
+        .stderr(starts_with.and(ends_with).trim());
+}
+
 #[test]
 fn version_option() {
     let out = predicate::str::starts_with("rsvg-convert version ");


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