[librsvg: 3/17] rsvg-convert.rs: add tests for ResizeStrategy




commit a772cf3d116014820c449f6a57a8a600876c5843
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Feb 21 12:32:41 2022 -0600

    rsvg-convert.rs: add tests for ResizeStrategy
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/669>

 src/bin/rsvg-convert.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)
---
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index 963a054d7..219daab32 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -83,7 +83,7 @@ impl Scale {
     }
 }
 
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone, Copy, Debug, PartialEq)]
 struct Size {
     pub w: f64,
     pub h: f64,
@@ -1157,3 +1157,53 @@ mod color_tests {
         );
     }
 }
+
+#[cfg(test)]
+mod sizing_tests {
+    use super::*;
+
+    #[test]
+    fn detects_empty_size() {
+        let strategy = ResizeStrategy::Scale(Scale { x: 42.0, y: 42.0 });
+        assert!(strategy.apply(&Size::new(0.0, 0.0), true).is_none());
+    }
+
+    #[test]
+    fn scale() {
+        let strategy = ResizeStrategy::Scale(Scale { x: 2.0, y: 3.0 });
+        assert_eq!(
+            strategy.apply(&Size::new(1.0, 2.0), false).unwrap(),
+            Size::new(2.0, 6.0),
+        );
+    }
+
+    #[test]
+    fn fit_wider_than_tall() {
+        let strategy = ResizeStrategy::Fit(40.0, 10.0);
+
+        assert_eq!(
+            strategy.apply(&Size::new(2.0, 1.0), false).unwrap(), // don't keep_aspect_ratio
+            Size::new(40.0, 10.0),
+        );
+
+        assert_eq!(
+            strategy.apply(&Size::new(2.0, 1.0), true).unwrap(), // keep_aspect_ratio
+            Size::new(20.0, 10.0),
+        );
+    }
+
+    #[test]
+    fn fit_taller_than_wide() {
+        let strategy = ResizeStrategy::Fit(100.0, 50.0);
+
+        assert_eq!(
+            strategy.apply(&Size::new(1.0, 2.0), false).unwrap(), // don't keep_aspect_ratio
+            Size::new(100.0, 50.0),
+        );
+
+        assert_eq!(
+            strategy.apply(&Size::new(1.0, 2.0), true).unwrap(), // keep_aspect_ratio
+            Size::new(25.0, 50.0),
+        );
+    }
+}


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