[librsvg: 10/15] tests: Improve PDF tests for rsvg-convert using the lopdf crate



commit c9b7c1cd3c87cbfafb177f6c2a481d87d2ac32e3
Author: Sven Neumann <sven svenfoo org>
Date:   Mon Feb 10 21:32:28 2020 +0100

    tests: Improve PDF tests for rsvg-convert using the lopdf crate
    
    This allows to check that multiple SVG files are rendered into a
    multi-page PDF document.

 Cargo.lock                        | 114 ++++++++++++++++++++++++++++++++++++++
 tests/Cargo.toml                  |   1 +
 tests/src/cmdline/predicates.rs   |  87 +++++++++++++++++++++++------
 tests/src/cmdline/rsvg_convert.rs |  14 ++---
 4 files changed, 191 insertions(+), 25 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 117d3e04..14b11e89 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -134,6 +134,15 @@ name = "cfg-if"
 version = "0.1.10"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
+[[package]]
+name = "chrono"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
+ "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "clap"
 version = "2.33.0"
@@ -410,6 +419,17 @@ dependencies = [
  "serde_json 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "flate2"
+version = "1.0.13"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
+ "crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "miniz_oxide 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "float-cmp"
 version = "0.5.3"
@@ -649,6 +669,11 @@ dependencies = [
  "either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "itoa"
+version = "0.3.4"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "itoa"
 version = "0.4.5"
@@ -690,6 +715,7 @@ name = "librsvg-tests"
 version = "0.1.0"
 dependencies = [
  "assert_cmd 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lopdf 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "png 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "predicates 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
@@ -713,6 +739,11 @@ dependencies = [
  "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "linked-hash-map"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "locale_config"
 version = "0.3.0"
@@ -733,6 +764,29 @@ dependencies = [
  "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "lopdf"
+version = "0.23.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "dtoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
+ "flate2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
+ "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pom 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "lzw"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "mac"
 version = "0.1.1"
@@ -791,6 +845,14 @@ dependencies = [
  "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "miniz_oxide"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "nalgebra"
 version = "0.19.0"
@@ -823,6 +885,16 @@ name = "normalize-line-endings"
 version = "0.2.2"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
+[[package]]
+name = "num"
+version = "0.1.42"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-iter 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "num-complex"
 version = "0.2.4"
@@ -841,6 +913,16 @@ dependencies = [
  "num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "num-iter"
+version = "0.1.40"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "num-rational"
 version = "0.2.3"
@@ -1044,6 +1126,11 @@ dependencies = [
  "inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "pom"
+version = "3.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "ppv-lite86"
 version = "0.2.6"
@@ -1240,6 +1327,11 @@ dependencies = [
  "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "redox_syscall"
+version = "0.1.56"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "regex"
 version = "1.3.4"
@@ -1487,6 +1579,16 @@ dependencies = [
  "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "time"
+version = "0.1.42"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "tinytemplate"
 version = "1.0.3"
@@ -1612,6 +1714,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum cairo-sys-rs 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ff65ba02cac715be836f63429ab00a767d48336efc5497c5637afb53b4f14d63"
 "checksum cast 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4b9434b9a5aa1450faa3f9cb14ea0e8c53bb5d2b3c1bfd1ab4fc03e9f33fbfb0"
 "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+"checksum chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"158b0bd7d75cbb6bf9c25967a48a2e9f77da95876b858eadfabaa99cd069de6e"
 "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
 "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
 "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
@@ -1642,6 +1745,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18"
 "checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
 "checksum escargot 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"74cf96bec282dcdb07099f7e31d9fed323bca9435a09aba7b6d99b7617bca96d"
+"checksum flate2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = 
"6bd6d6f4752952feb71363cffc9ebac9411b75b87c6ab6058c40c8900cf43c0f"
 "checksum float-cmp 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"75224bec9bfe1a65e2d34132933f2de7fe79900c96a0174307554244ece8150e"
 "checksum float-cmp 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"da62c4f1b81918835a8c6a484a397775fff5953fe83529afd51b05f5c6a6617d"
 "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
@@ -1667,13 +1771,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9"
 "checksum inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff"
 "checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
+"checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c"
 "checksum itoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
 "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
 "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
 "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
 "checksum libm 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a"
+"checksum linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd"
 "checksum locale_config 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"08d2c35b16f4483f6c26f0e4e9550717a2f6575bcd6f12a53ff0c490a94a6934"
 "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = 
"14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
+"checksum lopdf 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"fcaa03113adb9d334594b44a91356b8c8b8e55dd06d63372c7e6bf298bb28e11"
+"checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084"
 "checksum mac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
 "checksum malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = 
"62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
 "checksum markup5ever 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"aae38d669396ca9b707bfc3db254bc382ddb94f57cc5c235f34623a669a01dab"
@@ -1681,12 +1789,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum matrixmultiply 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d4f7ec66360130972f34830bfad9ef05c6610a43938a467bcc9ab9369ab3478f"
 "checksum memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"3197e20c7edb283f87c071ddfc7a2cca8f8e0b888c242959846a6fce03c72223"
 "checksum memoffset 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"75189eb85871ea5c2e2c15abbdd541185f63b408415e5051f5cac122d8c774b9"
+"checksum miniz_oxide 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = 
"aa679ff6578b1cddee93d7e82e263b94a575e0bfced07284eb0c037c1d2416a5"
 "checksum nalgebra 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"0abb021006c01b126a936a8dd1351e0720d83995f4fc942d0d426c654f990745"
 "checksum new_debug_unreachable 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
 "checksum nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = 
"72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
 "checksum normalize-line-endings 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"2e0a1a39eab95caf4f5556da9289b9e68f0aafac901b2ce80daaf020d3b733a8"
+"checksum num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"
 "checksum num-complex 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95"
 "checksum num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = 
"3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba"
+"checksum num-iter 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = 
"dfb0800a0291891dd9f4fe7bd9c19384f98f7fbe0cd0f39a2c6b88b9868bbc00"
 "checksum num-rational 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"da4dc79f9e6c81bef96148c8f6b8e72ad4541caa4a24373e900a36da07de03a3"
 "checksum num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
 "checksum num_cpus 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"46203554f085ff89c235cd12f7075f3233af9b11ed7c9e16dfe2560d03313ce6"
@@ -1709,6 +1820,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
 "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = 
"05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
 "checksum png 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ef859a23054bbfee7811284275ae522f0434a3c8e7f4b74bd4a35ae7e1c4a283"
+"checksum pom 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ef5cf7f52c12da93c26b63ee0d9f012bc82fb071851c546c030dc6ecb5f2994b"
 "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = 
"74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
 "checksum precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
 "checksum predicates 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a9bfe52247e5cc9b2f943682a85a5549fb9662245caf094504e69a2f03fe64d4"
@@ -1733,6 +1845,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum rayon-core 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"08a89b46efaf957e52b18062fb2f4660f8b8a4dde1807ca002690868ef2c85a9"
 "checksum rctree 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"be9e29cb19c8fe84169fcb07f8f11e66bc9e6e0280efd4715c54818296f8a4a8"
 "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
+"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = 
"2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
 "checksum regex 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"322cf97724bea3ee221b78fe25ac9c46114ebb51747ad5babd51a2fc6a8235a8"
 "checksum regex-automata 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = 
"92b73c2a1770c255c240eaa4ee600df1704a38dc3feaa6e949e7fcd4f8dc09f9"
 "checksum regex-syntax 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)" = 
"b28dfe3fe9badec5dbf0a79a9cccad2cfc2ab5484bdb3e44cbd1ae8b3ba2be06"
@@ -1759,6 +1872,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
 "checksum thin-slice 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c"
 "checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
+"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = 
"db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
 "checksum tinytemplate 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"57a3c6667d3e65eb1bc3aed6fd14011c6cbc3a0665218ab7f5daf040b9ec371a"
 "checksum treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41"
 "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9"
diff --git a/tests/Cargo.toml b/tests/Cargo.toml
index 1dfa5293..1643d410 100644
--- a/tests/Cargo.toml
+++ b/tests/Cargo.toml
@@ -7,5 +7,6 @@ edition = "2018"
 
 [dev-dependencies]
 assert_cmd = "0.12"
+lopdf = "0.23.0"
 png = "0.15.3"
 predicates = "1.0.2"
diff --git a/tests/src/cmdline/predicates.rs b/tests/src/cmdline/predicates.rs
index 5a685e23..b0505a9b 100644
--- a/tests/src/cmdline/predicates.rs
+++ b/tests/src/cmdline/predicates.rs
@@ -1,3 +1,4 @@
+extern crate lopdf;
 extern crate png;
 extern crate predicates;
 
@@ -10,35 +11,25 @@ pub mod file {
 
     use std::fmt;
 
-    /// Checks that the variable of type [u8] looks like a PDF file.
-    /// Actually it only looks at the very first bytes.
+    /// Checks that the variable of type [u8] can be parsed as a PDF file.
     #[derive(Debug)]
     pub struct PdfPredicate {}
 
     impl PdfPredicate {
-        fn not_a_pdf<'a>(&'a self, reason: &'static str) -> Option<Case<'a>> {
-            Some(Case::new(Some(self), false).add_product(Product::new("not a PDF", reason)))
+        pub fn with_page_count(self: Self, num_pages: usize) -> PageCountPredicate<Self> {
+            PageCountPredicate::<Self> { p: self, n: num_pages }
         }
     }
 
     impl Predicate<[u8]> for PdfPredicate {
         fn eval(&self, data: &[u8]) -> bool {
-            match data.get(0..5) {
-                Some(head) => head == b"%PDF-",
-                None => false,
-            }
+            lopdf::Document::load_mem(data).is_ok()
         }
 
         fn find_case<'a>(&'a self, _expected: bool, data: &[u8]) -> Option<Case<'a>> {
-            match data.get(0..5) {
-                Some(head) => {
-                    if head == b"%PDF-" {
-                        None
-                    } else {
-                        self.not_a_pdf("header mismatch")
-                    }
-                }
-                None => self.not_a_pdf("too short"),
+            match lopdf::Document::load_mem(data) {
+                Ok(_) => None,
+                Err(e) => Some(Case::new(Some(self), false).add_product(Product::new("Error", e)))
             }
         }
     }
@@ -47,7 +38,67 @@ pub mod file {
 
     impl fmt::Display for PdfPredicate {
         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-            write!(f, "has PDF header")
+            write!(f, "is a PDF")
+        }
+    }
+
+    /// Extends a PdfPredicate by a check for a given number of pages.
+    #[derive(Debug)]
+    pub struct PageCountPredicate<PdfPredicate> {
+        p: PdfPredicate,
+        n: usize
+    }
+
+    impl PageCountPredicate<PdfPredicate> {
+        fn eval_doc(&self, doc: &lopdf::Document) -> bool {
+            doc.get_pages().len() == self.n
+        }
+
+        fn find_case_for_doc<'a>(
+            &'a self,
+            expected: bool,
+            doc: &lopdf::Document,
+        ) -> Option<Case<'a>> {
+            if self.eval_doc(doc) == expected {
+                let product = self.product_for_doc(doc);
+                Some(Case::new(Some(self), false).add_product(product))
+            } else {
+                None
+            }
+        }
+
+        fn product_for_doc(&self, doc: &lopdf::Document) -> Product {
+            let actual_count = format!("{} page(s)", doc.get_pages().len());
+            Product::new("actual page count", actual_count)
+        }
+    }
+
+    impl Predicate<[u8]> for PageCountPredicate<PdfPredicate> {
+        fn eval(&self, data: &[u8]) -> bool {
+            match lopdf::Document::load_mem(data) {
+                Ok(doc) => self.eval_doc(&doc),
+                _ => false,
+            }
+        }
+
+        fn find_case<'a>(&'a self, expected: bool, data: &[u8]) -> Option<Case<'a>> {
+            match lopdf::Document::load_mem(data) {
+                Ok(doc) => self.find_case_for_doc(expected, &doc),
+                Err(e) => Some(Case::new(Some(self), false).add_product(Product::new("Error", e))),
+            }
+        }
+    }
+
+    impl PredicateReflection for PageCountPredicate<PdfPredicate> {
+        fn children<'a>(&'a self) -> Box<dyn Iterator<Item = Child<'a>> + 'a> {
+            let params = vec![Child::new("predicate", &self.p)];
+            Box::new(params.into_iter())
+        }
+    }
+
+    impl fmt::Display for PageCountPredicate<PdfPredicate> {
+        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+            write!(f, "is a PDF with {} page(s)", self.n)
         }
     }
 
diff --git a/tests/src/cmdline/rsvg_convert.rs b/tests/src/cmdline/rsvg_convert.rs
index 50a72874..9f482d14 100644
--- a/tests/src/cmdline/rsvg_convert.rs
+++ b/tests/src/cmdline/rsvg_convert.rs
@@ -105,7 +105,7 @@ fn output_format_pdf() {
         .arg("--format=pdf")
         .assert()
         .success()
-        .stdout(file::is_pdf());
+        .stdout(file::is_pdf().with_page_count(1));
 }
 
 #[test]
@@ -174,29 +174,29 @@ fn multiple_input_files_accepted_for_eps_output() {
 }
 
 #[test]
-fn multiple_input_files_accepted_for_pdf_output() {
+fn multiple_input_files_accepted_for_ps_output() {
     let one = Path::new("fixtures/dimensions/521-with-viewbox.svg");
     let two = Path::new("fixtures/dimensions/sub-rect-no-unit.svg");
     RsvgConvert::new()
-        .arg("--format=pdf")
+        .arg("--format=ps")
         .arg(one)
         .arg(two)
         .assert()
         .success()
-        .stdout(file::is_pdf());
+        .stdout(file::is_ps());
 }
 
 #[test]
-fn multiple_input_files_accepted_for_ps_output() {
+fn multiple_input_files_create_multi_page_pdf_output() {
     let one = Path::new("fixtures/dimensions/521-with-viewbox.svg");
     let two = Path::new("fixtures/dimensions/sub-rect-no-unit.svg");
     RsvgConvert::new()
-        .arg("--format=ps")
+        .arg("--format=pdf")
         .arg(one)
         .arg(two)
         .assert()
         .success()
-        .stdout(file::is_ps());
+        .stdout(file::is_pdf().with_page_count(2));
 }
 
 #[test]


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