[librsvg: 2/5] New test_utils module in rsvg_internals
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 2/5] New test_utils module in rsvg_internals
- Date: Sat, 24 Oct 2020 01:17:54 +0000 (UTC)
commit beee25fd435abe48f4379ac9c55124263b169596
Author: Federico Mena Quintero <federico gnome org>
Date: Fri Oct 23 19:28:09 2020 -0500
New test_utils module in rsvg_internals
Move the generic test utilities from librsvg_crate to here.
Makefile.am | 1 +
librsvg_crate/tests/standalone/api.rs | 3 +-
librsvg_crate/tests/standalone/bugs.rs | 3 +-
.../tests/standalone/intrinsic_dimensions.rs | 3 +-
librsvg_crate/tests/standalone/primitives.rs | 3 +-
librsvg_crate/tests/standalone/utils.rs | 102 ---------------------
rsvg_internals/Cargo.toml | 2 +-
rsvg_internals/src/lib.rs | 1 +
rsvg_internals/src/test_utils.rs | 101 ++++++++++++++++++++
9 files changed, 112 insertions(+), 107 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 814c6b9d..7c5e8cfa 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -98,6 +98,7 @@ LIBRSVG_INTERNALS_SRC = \
rsvg_internals/src/space.rs \
rsvg_internals/src/structure.rs \
rsvg_internals/src/style.rs \
+ rsvg_internals/src/test_utils.rs \
rsvg_internals/src/text.rs \
rsvg_internals/src/transform.rs \
rsvg_internals/src/unit_interval.rs \
diff --git a/librsvg_crate/tests/standalone/api.rs b/librsvg_crate/tests/standalone/api.rs
index 256e6813..ed7d4ee1 100644
--- a/librsvg_crate/tests/standalone/api.rs
+++ b/librsvg_crate/tests/standalone/api.rs
@@ -2,8 +2,9 @@ use cairo;
use librsvg::{CairoRenderer, DefsLookupErrorKind, HrefError, RenderingError};
use rsvg_internals::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
+use rsvg_internals::test_utils::compare_to_surface;
-use crate::utils::{compare_to_surface, load_svg};
+use crate::utils::load_svg;
#[test]
fn has_element_with_id_works() {
diff --git a/librsvg_crate/tests/standalone/bugs.rs b/librsvg_crate/tests/standalone/bugs.rs
index 1debc7e6..d07085cd 100644
--- a/librsvg_crate/tests/standalone/bugs.rs
+++ b/librsvg_crate/tests/standalone/bugs.rs
@@ -2,8 +2,9 @@ use cairo;
use librsvg::{LoadingError, SvgHandle};
use matches::matches;
use rsvg_internals::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
+use rsvg_internals::test_utils::compare_to_surface;
-use crate::utils::{compare_to_surface, load_svg, render_document, SurfaceSize};
+use crate::utils::{load_svg, render_document, SurfaceSize};
// https://gitlab.gnome.org/GNOME/librsvg/issues/335
#[test]
diff --git a/librsvg_crate/tests/standalone/intrinsic_dimensions.rs
b/librsvg_crate/tests/standalone/intrinsic_dimensions.rs
index 85ed952d..aa8a50b2 100644
--- a/librsvg_crate/tests/standalone/intrinsic_dimensions.rs
+++ b/librsvg_crate/tests/standalone/intrinsic_dimensions.rs
@@ -1,13 +1,14 @@
use cairo;
use rsvg_internals::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
+use rsvg_internals::test_utils::compare_to_surface;
use librsvg::{
CairoRenderer, DefsLookupErrorKind, HrefError, IntrinsicDimensions, Length, LengthUnit,
RenderingError,
};
-use crate::utils::{compare_to_surface, load_svg, render_document, SurfaceSize};
+use crate::utils::{load_svg, render_document, SurfaceSize};
#[test]
fn no_intrinsic_dimensions() {
diff --git a/librsvg_crate/tests/standalone/primitives.rs b/librsvg_crate/tests/standalone/primitives.rs
index d9959dad..6d035e45 100644
--- a/librsvg_crate/tests/standalone/primitives.rs
+++ b/librsvg_crate/tests/standalone/primitives.rs
@@ -1,8 +1,9 @@
use cairo;
use rsvg_internals::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
+use rsvg_internals::test_utils::compare_to_surface;
-use crate::utils::{compare_to_surface, load_svg, render_document, SurfaceSize};
+use crate::utils::{load_svg, render_document, SurfaceSize};
#[test]
fn simple_opacity_with_transform() {
diff --git a/librsvg_crate/tests/standalone/utils.rs b/librsvg_crate/tests/standalone/utils.rs
index 30776189..ec2df9fe 100644
--- a/librsvg_crate/tests/standalone/utils.rs
+++ b/librsvg_crate/tests/standalone/utils.rs
@@ -7,15 +7,6 @@ use librsvg::{CairoRenderer, Loader, LoadingError, RenderingError, SvgHandle};
use rsvg_internals::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
-use rsvg_internals::{compare_surfaces, BufferDiff, Diff};
-
-use std::convert::TryFrom;
-use std::env;
-use std::fs::{self, File};
-use std::io::BufReader;
-use std::path::PathBuf;
-use std::sync::Once;
-
pub fn load_svg(input: &'static [u8]) -> Result<SvgHandle, LoadingError> {
let bytes = glib::Bytes::from_static(input);
let stream = gio::MemoryInputStream::new_from_bytes(&bytes);
@@ -46,96 +37,3 @@ pub fn render_document<F: FnOnce(&cairo::Context)>(
res.and_then(|_| Ok(SharedImageSurface::wrap(output, SurfaceType::SRgb)?))
}
-
-fn tolerable_difference() -> u8 {
- static mut TOLERANCE: u8 = 2;
-
- static ONCE: Once = Once::new();
- ONCE.call_once(|| unsafe {
- if let Ok(str) = env::var("RSVG_TEST_TOLERANCE") {
- let value: usize = str
- .parse()
- .expect("Can not parse RSVG_TEST_TOLERANCE as a number");
- TOLERANCE =
- u8::try_from(value).expect("RSVG_TEST_TOLERANCE should be between 0 and 255");
- }
- });
-
- unsafe { TOLERANCE }
-}
-
-trait Deviation {
- fn distinguishable(&self) -> bool;
- fn inacceptable(&self) -> bool;
-}
-
-impl Deviation for Diff {
- fn distinguishable(&self) -> bool {
- self.max_diff > 2
- }
-
- fn inacceptable(&self) -> bool {
- self.max_diff > tolerable_difference()
- }
-}
-
-pub fn output_dir() -> PathBuf {
- let path = PathBuf::from(
- env::var_os("OUT_DIR")
- .expect(r#"OUT_DIR is not set, please set it or run under "cargo test""#),
- );
-
- fs::create_dir_all(&path).expect("could not create output directory for tests");
-
- path
-}
-
-pub fn compare_to_surface(
- output_surf: &SharedImageSurface,
- reference_surf: &SharedImageSurface,
- output_base_name: &str,
-) {
- let output_path = output_dir().join(&format!("{}-out.png", output_base_name));
-
- println!("output: {}", output_path.to_string_lossy());
-
- let mut output_file = File::create(output_path).unwrap();
- output_surf
- .clone()
- .into_image_surface()
- .unwrap()
- .write_to_png(&mut output_file)
- .unwrap();
-
- let diff = compare_surfaces(output_surf, reference_surf).unwrap();
- evaluate_diff(&diff, output_base_name);
-}
-
-fn evaluate_diff(diff: &BufferDiff, output_base_name: &str) {
- match diff {
- BufferDiff::DifferentSizes => unreachable!("surfaces should be of the same size"),
-
- BufferDiff::Diff(diff) => {
- if diff.distinguishable() {
- println!(
- "{}: {} pixels changed with maximum difference of {}",
- output_base_name, diff.num_pixels_changed, diff.max_diff,
- );
- save_diff(diff, output_base_name);
-
- if diff.inacceptable() {
- panic!("surfaces are too different");
- }
- }
- }
- }
-
- fn save_diff(diff: &Diff, output_base_name: &str) {
- let surf = diff.surface.clone().into_image_surface().unwrap();
- let diff_path = output_dir().join(&format!("{}-diff.png", output_base_name));
- println!("diff: {}", diff_path.to_string_lossy());
-
- let mut output_file = File::create(diff_path).unwrap();
- surf.write_to_png(&mut output_file).unwrap();
- }
-}
diff --git a/rsvg_internals/Cargo.toml b/rsvg_internals/Cargo.toml
index 27f947b9..e6d90e6c 100644
--- a/rsvg_internals/Cargo.toml
+++ b/rsvg_internals/Cargo.toml
@@ -10,7 +10,7 @@ edition = "2018"
# Keep these in sync with respect to the cairo-rs version:
# librsvg_crate/Cargo.toml
# librsvg_crate/src/lib.rs - toplevel example in the docs
-cairo-rs = { version="0.8.0", features=["v1_16"] }
+cairo-rs = { version = "0.8.0", features = ["v1_16", "png", "pdf", "svg"] }
cairo-sys-rs = "0.9.0"
cast = "0.2.3"
cssparser = "0.27.1"
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index e43835bd..2eaa88b3 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -141,6 +141,7 @@ mod space;
mod structure;
mod style;
pub mod surface_utils;
+pub mod test_utils;
mod text;
mod transform;
mod unit_interval;
diff --git a/rsvg_internals/src/test_utils.rs b/rsvg_internals/src/test_utils.rs
new file mode 100644
index 00000000..d47dc730
--- /dev/null
+++ b/rsvg_internals/src/test_utils.rs
@@ -0,0 +1,101 @@
+use crate::surface_utils::shared_surface::SharedImageSurface;
+use crate::surface_utils::compare_surfaces::{compare_surfaces, BufferDiff, Diff};
+
+use std::convert::TryFrom;
+use std::env;
+use std::fs::{self, File};
+use std::path::PathBuf;
+use std::sync::Once;
+
+fn tolerable_difference() -> u8 {
+ static mut TOLERANCE: u8 = 2;
+
+ static ONCE: Once = Once::new();
+ ONCE.call_once(|| unsafe {
+ if let Ok(str) = env::var("RSVG_TEST_TOLERANCE") {
+ let value: usize = str
+ .parse()
+ .expect("Can not parse RSVG_TEST_TOLERANCE as a number");
+ TOLERANCE =
+ u8::try_from(value).expect("RSVG_TEST_TOLERANCE should be between 0 and 255");
+ }
+ });
+
+ unsafe { TOLERANCE }
+}
+
+trait Deviation {
+ fn distinguishable(&self) -> bool;
+ fn inacceptable(&self) -> bool;
+}
+
+impl Deviation for Diff {
+ fn distinguishable(&self) -> bool {
+ self.max_diff > 2
+ }
+
+ fn inacceptable(&self) -> bool {
+ self.max_diff > tolerable_difference()
+ }
+}
+
+pub fn output_dir() -> PathBuf {
+ let path = PathBuf::from(
+ env::var_os("OUT_DIR")
+ .expect(r#"OUT_DIR is not set, please set it or run under "cargo test""#),
+ );
+
+ fs::create_dir_all(&path).expect("could not create output directory for tests");
+
+ path
+}
+
+pub fn compare_to_surface(
+ output_surf: &SharedImageSurface,
+ reference_surf: &SharedImageSurface,
+ output_base_name: &str,
+) {
+ let output_path = output_dir().join(&format!("{}-out.png", output_base_name));
+
+ println!("output: {}", output_path.to_string_lossy());
+
+ let mut output_file = File::create(output_path).unwrap();
+ output_surf
+ .clone()
+ .into_image_surface()
+ .unwrap()
+ .write_to_png(&mut output_file)
+ .unwrap();
+
+ let diff = compare_surfaces(output_surf, reference_surf).unwrap();
+ evaluate_diff(&diff, output_base_name);
+}
+
+fn evaluate_diff(diff: &BufferDiff, output_base_name: &str) {
+ match diff {
+ BufferDiff::DifferentSizes => unreachable!("surfaces should be of the same size"),
+
+ BufferDiff::Diff(diff) => {
+ if diff.distinguishable() {
+ println!(
+ "{}: {} pixels changed with maximum difference of {}",
+ output_base_name, diff.num_pixels_changed, diff.max_diff,
+ );
+ save_diff(diff, output_base_name);
+
+ if diff.inacceptable() {
+ panic!("surfaces are too different");
+ }
+ }
+ }
+ }
+
+ fn save_diff(diff: &Diff, output_base_name: &str) {
+ let surf = diff.surface.clone().into_image_surface().unwrap();
+ let diff_path = output_dir().join(&format!("{}-diff.png", output_base_name));
+ println!("diff: {}", diff_path.to_string_lossy());
+
+ let mut output_file = File::create(diff_path).unwrap();
+ surf.write_to_png(&mut output_file).unwrap();
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]