Skip to content

Commit ebc9d74

Browse files
authored
Merge pull request #48 from darby42/add_mock_constructor
Adding mock constructor
2 parents 9fc7119 + a3de1da commit ebc9d74

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ xml-rs = "0.8.20"
1717
percent-encoding = "2.3.1"
1818
thiserror = "1.0.59"
1919

20+
[features]
21+
mock = []
22+
2023
[dependencies.zip]
2124
version = "1.1.3"
2225
default-features = false

src/doc.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ pub struct EpubDoc<R: Read + Seek> {
113113
pub cover_id: Option<String>,
114114
}
115115

116+
/// A EpubDoc used for testing purposes
117+
#[cfg(feature = "mock")]
118+
impl EpubDoc<std::io::Cursor<Vec<u8>>> {
119+
pub fn mock() -> Result<Self, DocError> {
120+
// binary for empty zip file so that archive can be created
121+
let data: Vec<u8> = vec![
122+
0x50, 0x4b, 0x05, 0x06, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
123+
00, 00,
124+
];
125+
126+
let archive = EpubArchive::from_reader(std::io::Cursor::new(data))?;
127+
Ok(Self {
128+
archive,
129+
spine: vec![],
130+
toc: vec![],
131+
resources: HashMap::new(),
132+
metadata: HashMap::new(),
133+
root_file: PathBuf::new(),
134+
root_base: PathBuf::new(),
135+
current: 0,
136+
extra_css: vec![],
137+
unique_identifier: None,
138+
cover_id: None,
139+
})
140+
}
141+
}
142+
116143
impl EpubDoc<BufReader<File>> {
117144
/// Opens the epub file in `path`.
118145
///

tests/doc.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
use epub::doc::EpubDoc;
22
use std::path::Path;
33

4+
#[test]
5+
#[cfg(feature = "mock")]
6+
fn doc_mock() {
7+
let doc = EpubDoc::mock();
8+
assert!(doc.is_ok());
9+
}
10+
411
#[test]
512
fn doc_open() {
613
let doc = EpubDoc::new("test.epub");

0 commit comments

Comments
 (0)