[librsvg: 3/20] test_svg_reference: new macro to have reftests from two SVG files




commit 41b43c3117f1e3aefdf0ea94ae59429c9d6d72f3
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Oct 19 17:20:05 2021 -0500

    test_svg_reference: new macro to have reftests from two SVG files
    
    Instead of test_compare_render_output, which is slightly awkward
    because one must insert the SVG document as a Rust string.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/616>

 tests/src/reference_utils.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
---
diff --git a/tests/src/reference_utils.rs b/tests/src/reference_utils.rs
index 1d700a4f..0b56f540 100644
--- a/tests/src/reference_utils.rs
+++ b/tests/src/reference_utils.rs
@@ -229,3 +229,78 @@ macro_rules! test_compare_render_output {
         }
     };
 }
+
+/// Render two SVG files and compare them.
+///
+/// This is used to implement reference tests, or reftests.  Use it like this:
+///
+/// ```ignore
+/// test_svg_reference!(test_name, "tests/fixtures/blah/foo.svg", "tests/fixtures/blah/foo-ref.svg");
+/// ```
+///
+/// This will ensure that `foo.svg` and `foo-ref.svg` have exactly the same intrinsic dimensions,
+/// and that they produce the same rendered output.
+#[macro_export]
+macro_rules! test_svg_reference {
+    ($test_name:ident, $test_filename:expr, $reference_filename:expr) => {
+        #[test]
+        fn $test_name() {
+            use crate::utils::setup_font_map;
+            use librsvg::{CairoRenderer, Loader};
+
+            setup_font_map();
+
+            let svg = Loader::new()
+                .read_path($test_filename)
+                .expect("reading SVG test file");
+            let reference = Loader::new()
+                .read_path($reference_filename)
+                .expect("reading reference file");
+
+            let svg_renderer = CairoRenderer::new(&svg);
+            let ref_renderer = CairoRenderer::new(&reference);
+
+            let svg_dim = svg_renderer.intrinsic_dimensions();
+            let ref_dim = ref_renderer.intrinsic_dimensions();
+
+            assert_eq!(
+                svg_dim, ref_dim,
+                "sizes of SVG document and reference file are different"
+            );
+
+            let pixels = svg_renderer
+                .intrinsic_size_in_pixels()
+                .unwrap_or((100.0, 100.0));
+
+            let output_surf = render_document(
+                &svg,
+                SurfaceSize(pixels.0.ceil() as i32, pixels.1.ceil() as i32),
+                |_| (),
+                cairo::Rectangle {
+                    x: 0.0,
+                    y: 0.0,
+                    width: pixels.0,
+                    height: pixels.1,
+                },
+            )
+            .unwrap();
+
+            let reference_surf = render_document(
+                &reference,
+                SurfaceSize(pixels.0.ceil() as i32, pixels.1.ceil() as i32),
+                |_| (),
+                cairo::Rectangle {
+                    x: 0.0,
+                    y: 0.0,
+                    width: pixels.0,
+                    height: pixels.1,
+                },
+            )
+            .unwrap();
+
+            Reference::from_surface(reference_surf.into_image_surface().unwrap())
+                .compare(&output_surf)
+                .evaluate(&output_surf, stringify!($test_name));
+        }
+    };
+}


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