Commit 4bfe6a41 authored by RazrFalcon's avatar RazrFalcon

(usvg) Tests rewritten.

parent 7058c08f
This diff is collapsed.
......@@ -31,7 +31,7 @@ travis-ci = { repository = "RazrFalcon/resvg" }
log = "0.4.5"
rgb = "0.8.9"
usvg = { path = "usvg" }
unicode-segmentation = "1.2.1"
unicode-segmentation = "1.2.1" # TODO: reexport?
# cairo backend
cairo-rs = { version = "0.5", features = ["png"], optional = true }
......
......@@ -150,12 +150,13 @@ if 'RESVG_CAIRO_BACKEND' in os.environ:
if 'USVG_TESTING' in os.environ:
with cd('tools/usvg'):
run(['cargo', 'build'], check=True)
with cd('usvg'):
run(['cargo', 'test'], check=True)
# usvg/testing_tools/regression.py uses tools/usvg
with cd('tools/usvg'):
run(['cargo', 'build'], check=True)
with cd('usvg/testing_tools'):
run(['./regression.py', '--ci-mode', '../../target/resvg-test-suite/svg',
'../../target/test-suite-temp'], check=True)
......@@ -23,11 +23,4 @@ svgdom = { git = "https://github.com/RazrFalcon/svgdom", rev = "f69fc2a" }
unicode-segmentation = "1.2.1"
[dev-dependencies]
assert_cli = "0.6"
pretty_assertions = "0.5.1"
rustc-test = "0.3"
rustc_version = "0.2.2"
serde = "1.0.34"
serde_derive = "1.0.34"
tempdir = "0.3"
toml = "0.4.5"
......@@ -694,3 +694,14 @@ pub struct FeTile {
/// `in` in the SVG.
pub input: FilterInput,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn node_kind_size() {
assert!(std::mem::size_of::<NodeKind>() <= 256);
}
}
......@@ -13,7 +13,7 @@ from subprocess import run
# List of files that should be skipped.
CRASH_ALLOWED = [
'e-svg-007.svg'
'e-svg-007.svg', # non-UTF8 encoding
]
CACHE_FILENAME = 'cache.csv'
......
extern crate usvg;
extern crate rustc_version;
use std::mem;
use rustc_version::{Version, version_meta};
#[test]
fn node_kind_size_1() {
let size = if version_meta().unwrap().semver == Version::parse("1.22.0").unwrap() {
264
} else {
// Newer rust versions has a better enum packing.
256
};
assert!(mem::size_of::<usvg::NodeKind>() <= size);
}
extern crate assert_cli;
extern crate tempdir;
#[macro_use] extern crate pretty_assertions;
use std::fmt;
use tempdir::TempDir;
const APP_PATH: &str = "../target/debug/usvg";
#[test]
fn file_to_file() {
let dir = TempDir::new("usvg").unwrap();
let file_out = dir.path().join("test1.svg");
let file_out = file_out.to_str().unwrap();
let args = &[
APP_PATH,
"--indent=2",
"--attrs-indent=3",
"tests/images/test1-in.svg",
file_out,
];
assert_cli::Assert::command(args)
.stdout().is("")
.stderr().is("")
.unwrap();
cmp_files("tests/images/test1-out.svg", file_out);
}
#[test]
fn file_to_stdout() {
let args = &[
APP_PATH,
"-c",
"--indent=2",
"--attrs-indent=3",
"tests/images/test1-in.svg",
];
assert_cli::Assert::command(args)
.stdout().is(load_file("tests/images/test1-out.svg").as_str())
.stderr().is("")
.unwrap();
}
#[test]
fn stdin_to_file() {
let dir = TempDir::new("usvg").unwrap();
let file_out = dir.path().join("test1.svg");
let file_out = file_out.to_str().unwrap();
let args = &[
APP_PATH,
"--indent=2",
"--attrs-indent=3",
file_out,
"-",
];
assert_cli::Assert::command(args)
.stdin(load_file("tests/images/test1-out.svg").as_str())
.stdout().is("")
.stderr().is("")
.unwrap();
cmp_files("tests/images/test1-out.svg", file_out);
}
#[test]
fn stdin_to_stdout() {
let args = &[
APP_PATH,
"-c",
"--indent=2",
"--attrs-indent=3",
"-",
];
let data = load_file("tests/images/test1-out.svg");
assert_cli::Assert::command(args)
.stdin(data.as_str())
.stdout().is(data.as_str())
.stderr().is("")
.unwrap();
}
#[derive(Clone, Copy, PartialEq)]
struct MStr<'a>(&'a str);
impl<'a> fmt::Debug for MStr<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
fn cmp_files(path1: &str, path2: &str) {
assert_eq!(MStr(&load_file(path1)), MStr(&load_file(path2)));
}
fn load_file(path: &str) -> String {
use std::fs;
use std::io::Read;
let mut file = fs::File::open(path).unwrap();
let length = file.metadata().unwrap().len() as usize;
let mut s = String::with_capacity(length + 1);
file.read_to_string(&mut s).unwrap();
s
}
// TODO: we need log without line numbers, somehow
//#[test]
//fn svgdom_error_msg_1() {
// let args = &[
// APP_PATH,
// "-c",
// "tests/images/crosslink-err.svg",
// ];
//
// assert_cli::Assert::command(args)
// .stdout().is(load_file("tests/images/default.svg"))
// .stderr().is("Warning (in usvg:80): Failed to parse an SVG data cause element crosslink.\n\
// Warning (in usvg::preproc:99): Invalid SVG structure. The Document will be cleared.\n\
// Warning (in usvg::convert:63): Invalid SVG structure. An empty tree will be produced.\n")
// .unwrap();
//}
//#[test]
//fn warn_msg_1() {
// let args = &[
// APP_PATH,
// "-c",
// "tests/images/invalid-attr-value-in.svg",
// ];
//
// assert_cli::Assert::command(args)
// .stdout().is(load_file("tests/images/invalid-attr-value-out.svg"))
// .stderr().is("Warning (in svgdom::parser:459): Attribute 'fill' has an invalid value: 'qwe'.\n")
// .unwrap();
//}
We are using `xml` extension and not `toml` to enable automatic highlight detection by text editors.
And it works, even through those files is not valid XML.
As TOML:
```toml
title = "Minimal"
input = '''
<svg viewBox="0 0 1 1">
<rect width="10" height="10"/>
</svg>
'''
output = '''
<svg
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.1.0">
<defs/>
<path
fill="#000000"
fill-opacity="1"
fill-rule="evenodd"
stroke="none"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</svg>
'''
```
As XML:
```xml
title = "Minimal"
input = '''
<svg viewBox="0 0 1 1">
<rect width="10" height="10"/>
</svg>
'''
output = '''
<svg
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
<path
fill="#000000"
fill-opacity="1"
fill-rule="evenodd"
stroke="none"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</svg>
'''
```
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<clipPath id="clip1">
<rect/>
</clipPath>
<rect clip-path="url(#clip1)" width="10" height="10"/>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
</svg>
'''
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<clipPath id="clip1">
<rect/>
<line/>
<polyline/>
<polygon/>
<circle/>
<ellipse/>
<path/>
</clipPath>
<rect clip-path="url(#clip1)" width="10" height="10"/>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
</svg>
'''
# clipPath requires a new canvas so we have to indicate this by adding a new group.
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<clipPath id="clip1">
<rect width="10" height="10"/>
</clipPath>
<rect clip-path="url(#clip1)" width="10" height="10"/>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs>
<clipPath
id="clip1"
clipPathUnits="userSpaceOnUse">
<path
fill="#000000"
fill-opacity="1"
stroke="none"
clip-rule="nonzero"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</clipPath>
</defs>
<g
clip-path="url(#clip1)">
<path
fill="#000000"
fill-opacity="1"
fill-rule="nonzero"
stroke="none"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</g>
</svg>
'''
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<g>
<g>
<rect width="10" height="10"/>
</g>
</g>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
<path
fill="#000000"
fill-opacity="1"
fill-rule="nonzero"
stroke="none"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</svg>
'''
# No need to keep empty groups even if `keep_named_groups` is enabled.
keep_named_groups = true
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<g id="some_group"/>
<rect width="10" height="10"/>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
<path
fill="#000000"
fill-opacity="1"
fill-rule="nonzero"
stroke="none"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</svg>
'''
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<g id="some_group">
<rect width="10" height="10"/>
</g>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
<path
id="some_group"
fill="#000000"
fill-opacity="1"
fill-rule="nonzero"
stroke="none"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</svg>
'''
keep_named_groups = true
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<g id="some_group">
<rect width="10" height="10"/>
</g>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
<g
id="some_group">
<path
fill="#000000"
fill-opacity="1"
fill-rule="nonzero"
stroke="none"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</g>
</svg>
'''
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<g opacity="0.5">
<g>
<rect width="10" height="10"/>
</g>
</g>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
<g
opacity="0.5">
<path
fill="#000000"
fill-opacity="1"
fill-rule="nonzero"
stroke="none"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</g>
</svg>
'''
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<rect width="10" height="10"/>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
<path
fill="#000000"
fill-opacity="1"
fill-rule="nonzero"
stroke="none"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</svg>
'''
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<pattern id="patt1">
<rect/>
</pattern>
<rect fill="url(#patt1)" width="10" height="10"/>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
</svg>
'''
input = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<pattern id="patt1" patternUnits="userSpaceOnUse" width="20" height="40"/>
<rect fill="url(#patt1)" width="10" height="10"/>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs/>
</svg>
'''
# All supported elements should be listed.
# We keep id's even if `keep_named_groups` is disabled.
# ID on `svg`, `defs`, `stop` and `tspan` is ignored because they can't be rendered directly.
input = '''
<svg id="svg1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">
<defs id="defs1">
<linearGradient id="lg1">
<stop id="stop1" offset="0" stop-color="white"/>
<stop offset="1" stop-color="black"/>
</linearGradient>
<radialGradient id="rg1">
<stop offset="0" stop-color="white"/>
<stop offset="1" stop-color="black"/>
</radialGradient>
<clipPath id="clip1">
<rect id="rect2" width="10" height="10"/>
</clipPath>
<pattern id="patt1" width="1" height="1">
<rect width="10" height="10"/>
</pattern>
</defs>
<rect id="rect1" fill="url(#lg1)" stroke="url(#rg1)" clip-path="url(#clip1)" width="10" height="10"/>
<path id="path1" fill="url(#patt1)" d="M 10 20 30 40"/>
<text id="text1">Some text</text>
<text id="text2"><tspan id="tspan2">Some text</tspan></text>
<image id="image1" width="1" height="1" xlink:href="data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAB3RJTUUH4gMLDwAjrsLbtwAAAAlw
SFlzAAAuIwAALiMBeKU/dgAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAG
UExURQAA/xjQP14JpdQAAAABYktHRACIBR1IAAAAFklEQVR42mMAgvp/IJTAhgdB1ADVAgDvdAnx
N1Ib1gAAAABJRU5ErkJggg=="/>
</svg>
'''
output = '''
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="1"
height="1"
viewBox="0 0 1 1"
preserveAspectRatio="xMidYMid"
xmlns:usvg="https://github.com/RazrFalcon/usvg"
usvg:version="0.4.0">
<defs>
<linearGradient
id="lg1"
x1="0"
y1="0"
x2="1"
y2="0"
gradientUnits="objectBoundingBox"
spreadMethod="pad">
<stop
stop-color="#ffffff"
stop-opacity="1"
offset="0"/>
<stop
stop-color="#000000"
stop-opacity="1"
offset="1"/>
</linearGradient>
<radialGradient
id="rg1"
cx="0.5"
cy="0.5"
r="0.5"
fx="0.5"
fy="0.5"
gradientUnits="objectBoundingBox"
spreadMethod="pad">
<stop
stop-color="#ffffff"
stop-opacity="1"
offset="0"/>
<stop
stop-color="#000000"
stop-opacity="1"
offset="1"/>
</radialGradient>
<clipPath
id="clip1"
clipPathUnits="userSpaceOnUse">
<path
id="rect2"
fill="#000000"
fill-opacity="1"
stroke="none"
clip-rule="nonzero"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</clipPath>
<pattern
id="patt1"
x="0"
y="0"
width="1"
height="1"
patternUnits="objectBoundingBox"
patternContentUnits="userSpaceOnUse">
<path
fill="#000000"
fill-opacity="1"
fill-rule="nonzero"
stroke="none"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</pattern>
</defs>
<g
clip-path="url(#clip1)">
<path
id="rect1"
fill="url(#lg1)"
fill-opacity="1"
fill-rule="nonzero"
stroke="url(#rg1)"
stroke-dasharray="none"
stroke-dashoffset="0"
stroke-linecap="butt"
stroke-linejoin="miter"
stroke-miterlimit="4"
stroke-opacity="1"
stroke-width="1"
visibility="visible"
d="M 0 0 L 10 0 L 10 10 L 0 10 Z"/>
</g>
<path
id="path1"
fill="url(#patt1)"
fill-opacity="1"
fill-rule="nonzero"
stroke="none"
visibility="visible"
d="M 10 20 L 30 40"/>
<text
id="text1"><tspan><tspan