use text_document::{LatexExportOptions, TextDocument};
const RICH_DOCUMENT: &str = "\
# Chapter One
Some **bold** and _italic_ text, plus a note[^n1].
## A Subheading
1. First item
2. Second item
before {width=64 height=48} after
[^n1]: The footnote body.
";
fn doc() -> TextDocument {
let d = TextDocument::new();
d.set_djot_sync(RICH_DOCUMENT).expect("set_djot_sync");
d
}
#[test]
fn with_options_matches_the_positional_wrapper_with_preamble() {
let d = doc();
let positional = d.to_latex("book", true).expect("positional to_latex");
let via_options = d
.to_latex_with_options(LatexExportOptions {
document_class: "book".into(),
include_preamble: true,
omit_images: false,
})
.expect("to_latex_with_options");
assert_eq!(positional, via_options);
}
#[test]
fn with_options_matches_the_positional_wrapper_without_preamble() {
let d = doc();
let positional = d.to_latex("article", false).expect("positional to_latex");
let via_options = d
.to_latex_with_options(LatexExportOptions {
document_class: "article".into(),
include_preamble: false,
omit_images: false,
})
.expect("to_latex_with_options");
assert_eq!(positional, via_options);
}
#[test]
fn to_latex_convenience_wrapper_still_keeps_images_by_default() {
let tex = doc().to_latex("article", true).expect("to_latex");
assert!(tex.contains("\\includegraphics["), "{tex}");
}
#[test]
fn default_matches_the_historic_bare_shape() {
let d = doc();
let via_default = d
.to_latex_with_options(LatexExportOptions::default())
.expect("default options");
let via_explicit_bare = d
.to_latex_with_options(LatexExportOptions {
document_class: String::new(),
include_preamble: false,
omit_images: false,
})
.expect("explicit bare options");
assert_eq!(via_default, via_explicit_bare);
assert!(!via_default.contains("\\documentclass"), "{via_default}");
}
#[test]
fn default_options_struct_has_historic_values() {
let o = LatexExportOptions::default();
assert_eq!(o.document_class, "");
assert!(!o.include_preamble);
assert!(!o.omit_images);
}
#[test]
fn empty_document_class_falls_back_to_article() {
let tex = doc()
.to_latex_with_options(LatexExportOptions {
document_class: String::new(),
include_preamble: true,
omit_images: false,
})
.expect("to_latex_with_options");
assert!(tex.contains("\\documentclass{article}"), "{tex}");
}
#[test]
fn explicit_document_class_is_honored() {
let tex = doc()
.to_latex_with_options(LatexExportOptions {
document_class: "report".into(),
include_preamble: true,
omit_images: false,
})
.expect("to_latex_with_options");
assert!(tex.contains("\\documentclass{report}"), "{tex}");
}
#[test]
fn include_preamble_true_wraps_the_body() {
let tex = doc()
.to_latex_with_options(LatexExportOptions {
document_class: "article".into(),
include_preamble: true,
omit_images: false,
})
.expect("to_latex_with_options");
assert!(tex.starts_with("\\documentclass{article}"), "{tex}");
assert!(tex.contains("\\begin{document}"), "{tex}");
assert!(tex.contains("\\end{document}"), "{tex}");
assert!(
tex.contains("\\setcounter{secnumdepth}{-1}"),
"the preamble must suppress LaTeX's own section numbering: {tex}"
);
}
#[test]
fn include_preamble_false_returns_body_only() {
let tex = doc()
.to_latex_with_options(LatexExportOptions {
document_class: "article".into(),
include_preamble: false,
omit_images: false,
})
.expect("to_latex_with_options");
assert!(!tex.contains("\\documentclass"), "{tex}");
assert!(!tex.contains("\\begin{document}"), "{tex}");
assert!(!tex.contains("\\end{document}"), "{tex}");
assert!(tex.contains("Chapter One"), "{tex}");
}
#[test]
fn omit_images_true_drops_includegraphics() {
let tex = doc()
.to_latex_with_options(LatexExportOptions {
document_class: "article".into(),
include_preamble: true,
omit_images: true,
})
.expect("to_latex_with_options");
assert!(!tex.contains("\\includegraphics"), "{tex}");
assert!(tex.contains("before") && tex.contains("after"), "{tex}");
}
#[test]
fn omit_images_false_keeps_includegraphics() {
let tex = doc()
.to_latex_with_options(LatexExportOptions {
document_class: "article".into(),
include_preamble: true,
omit_images: false,
})
.expect("to_latex_with_options");
assert!(tex.contains("\\includegraphics["), "{tex}");
assert!(tex.contains("pic.png"), "{tex}");
}
#[test]
fn headings_formatting_list_and_footnote_all_survive() {
let tex = doc()
.to_latex_with_options(LatexExportOptions {
document_class: "article".into(),
include_preamble: true,
omit_images: false,
})
.expect("to_latex_with_options");
assert!(tex.contains("\\section{Chapter One}"), "{tex}");
assert!(tex.contains("\\subsection{A Subheading}"), "{tex}");
assert!(tex.contains("\\textbf{bold}"), "{tex}");
assert!(tex.contains("\\textit{italic}"), "{tex}");
assert!(tex.contains("\\begin{enumerate}"), "{tex}");
assert!(tex.contains("First item"), "{tex}");
assert!(tex.contains("\\footnote{"), "{tex}");
assert!(tex.contains("The footnote body"), "{tex}");
}
#[test]
fn latex_export_options_carries_no_comment_field() {
let LatexExportOptions {
document_class: _,
include_preamble: _,
omit_images: _,
} = LatexExportOptions::default();
}