#[cfg(test)]
mod serialized_document_tests {
use dc_bundle::definition_file::{load_design_def, save_design_def};
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use tempfile::tempdir;
#[test]
fn load_save_load() {
let mut doc_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
doc_path.push("../../reference-apps/helloworld/helloworld-app/src/main/assets/figma/HelloWorldDoc_pxVlixodJqZL95zo2RzTHl.dcf");
let (header, doc) = load_design_def(doc_path).expect("Failed to load design bundle.");
println!("Deserialized header: {}", &header);
println!("Deserialized doc: {}", &doc);
let tmp_dir = tempdir().unwrap();
let tmp_doc_path = PathBuf::from(tmp_dir.path()).join("tmp_pxVlixodJqZL95zo2RzTHl.dcf");
save_design_def(&tmp_doc_path, &header, &doc)
.expect("Failed to save temporary DesignCompose Definition.");
let (tmp_header, tmp_doc) =
load_design_def(&tmp_doc_path).expect("Failed to load tmp DesignCompose Definition.");
println!("Tmp deserialized header: {}", &tmp_header);
println!("Tmp deserialized doc: {}", &tmp_doc);
tmp_dir.close().unwrap();
}
#[test]
#[should_panic]
fn load_missing_doc() {
let mut doc_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
doc_path.push("this.doc.does.not.exist.dcf");
let (_tmp_header, _tmp_doc) =
load_design_def(&doc_path).expect("Failed to load tmp DesignCompose Definition.");
}
#[test]
#[should_panic]
fn load_bad_doc() {
let tmp_dir = tempdir().unwrap();
let garbage_doc_path = PathBuf::from(tmp_dir.path()).join("tmp.garbage.file.dcf");
let mut file =
File::create(&garbage_doc_path).expect("Failed to create new garbage binary doc file.");
let data: Vec<u8> = (0..48).map(|v| v).collect();
file.write_all(&data).expect("Failed to write garbage data to garbage file.");
let (_tmp_header, _tmp_doc) = load_design_def(&garbage_doc_path)
.expect("Failed to load garbage DesignCompose Definition.");
drop(file);
tmp_dir.close().unwrap();
}
}