Skip to content

Commit d819ac2

Browse files
committed
Add test cases
1 parent 67c2333 commit d819ac2

4 files changed

Lines changed: 68 additions & 12 deletions

File tree

src/annotations.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,10 +796,13 @@ mod tests {
796796
#[test]
797797
fn test_annotations() {
798798
test!(
799-
crate::tests::slice(|w| {
800-
w.annotation(Ref::new(1)).rect(Rect::new(0.0, 0.0, 1.0, 1.0));
801-
w.annotation(Ref::new(2)).rect(Rect::new(1.0, 1.0, 0.0, 0.0));
802-
}),
799+
crate::tests::slice(
800+
|w| {
801+
w.annotation(Ref::new(1)).rect(Rect::new(0.0, 0.0, 1.0, 1.0));
802+
w.annotation(Ref::new(2)).rect(Rect::new(1.0, 1.0, 0.0, 0.0));
803+
},
804+
WriteSettings::default()
805+
),
803806
b"1 0 obj",
804807
b"<<",
805808
b" /Type /Annot",

src/lib.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ pub use self::object::{
199199
TypedDict, Writer,
200200
};
201201

202+
use self::writers::*;
203+
use crate::chunk::WriteSettings;
202204
use std::fmt::{self, Debug, Formatter};
203205
use std::io::Write;
204206
use std::ops::{Deref, DerefMut};
205207

206-
use self::writers::*;
207-
208208
/// A builder for a PDF file.
209209
///
210210
/// This type constructs a PDF file in-memory. Aside from a few specific
@@ -227,6 +227,14 @@ impl Pdf {
227227
Self::with_capacity(8 * 1024)
228228
}
229229

230+
/// Create a new PDF with the given write settings.
231+
pub fn new_with(write_settings: WriteSettings) -> Self {
232+
let mut pdf = Self::new();
233+
pdf.write_settings = write_settings;
234+
235+
pdf
236+
}
237+
230238
/// Create a new PDF with the specified initial buffer capacity.
231239
pub fn with_capacity(capacity: usize) -> Self {
232240
let mut chunk = Chunk::with_capacity(capacity);
@@ -412,11 +420,11 @@ mod tests {
412420
}
413421

414422
/// Return the slice of bytes written during the execution of `f`.
415-
pub fn slice<F>(f: F) -> Vec<u8>
423+
pub fn slice<F>(f: F, write_settings: WriteSettings) -> Vec<u8>
416424
where
417425
F: FnOnce(&mut Pdf),
418426
{
419-
let mut w = Pdf::new();
427+
let mut w = Pdf::new_with(write_settings);
420428
let start = w.len();
421429
f(&mut w);
422430
let end = w.len();
@@ -425,12 +433,16 @@ mod tests {
425433
}
426434

427435
/// Return the slice of bytes written for an object.
428-
pub fn slice_obj<F>(f: F) -> Vec<u8>
436+
pub fn slice_obj<F>(f: F, write_settings: WriteSettings) -> Vec<u8>
429437
where
430438
F: FnOnce(Obj<'_>),
431439
{
432-
let buf = slice(|w| f(w.indirect(Ref::new(1))));
433-
buf[8..buf.len() - 9].to_vec()
440+
let buf = slice(|w| f(w.indirect(Ref::new(1))), write_settings);
441+
if write_settings.pretty {
442+
buf[8..buf.len() - 9].to_vec()
443+
} else {
444+
buf[8..buf.len() - 8].to_vec()
445+
}
434446
}
435447

436448
#[test]

src/macros.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ macro_rules! test {
1919
#[cfg(test)]
2020
macro_rules! test_obj {
2121
(|$obj:ident| $write:expr, $($tts:tt)*) => {{
22-
test!(crate::tests::slice_obj(|$obj| { $write; }), $($tts)*)
22+
test!(crate::tests::slice_obj(|$obj| { $write; }, crate::WriteSettings::default()), $($tts)*)
23+
}}
24+
}
25+
26+
/// Test how an object is written, without pretty-printing.
27+
#[cfg(test)]
28+
macro_rules! test_obj_no_pretty {
29+
(|$obj:ident| $write:expr, $($tts:tt)*) => {{
30+
test!(crate::tests::slice_obj(|$obj| { $write; }, crate::WriteSettings { pretty: false }), $($tts)*)
2331
}}
2432
}
2533

src/object.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,4 +1686,37 @@ mod tests {
16861686
b"startxref\n94\n%%EOF",
16871687
)
16881688
}
1689+
1690+
#[test]
1691+
fn test_arrays_no_pretty() {
1692+
test_obj_no_pretty!(|obj| obj.array(), b"[]");
1693+
test_obj_no_pretty!(
1694+
|obj| obj
1695+
.array()
1696+
.item(12)
1697+
.item(Name(b"Hi"))
1698+
.item(Name(b"Hi2"))
1699+
.item(false)
1700+
.item(TextStr("A string"))
1701+
.item(Null)
1702+
.item(23.40),
1703+
b"[12/Hi/Hi2 false(A String)null 23.4]"
1704+
);
1705+
}
1706+
1707+
#[test]
1708+
fn test_dicts_no_pretty() {
1709+
test_obj_no_pretty!(|obj| obj.dict(), b"<<>>");
1710+
test_obj_no_pretty!(
1711+
|obj| obj
1712+
.dict()
1713+
.pair(Name(b"Key1"), 12)
1714+
.pair(Name(b"Key2"), Name(b"Hi"))
1715+
.pair(Name(b"Key3"), false)
1716+
.pair(Name(b"Key4"), TextStr("A string"))
1717+
.pair(Name(b"Key5"), Null)
1718+
.pair(Name(b"Key6"), 23.40),
1719+
b"<</Key1 12/Key2/Hi/Key3 false/Key4(A string)/Key5 null/Key6 23.4>>"
1720+
);
1721+
}
16891722
}

0 commit comments

Comments
 (0)