use std::fs;
use badness::formatter::{FormatStyle, format_file_with_packages, format_with_style_flavored};
use badness::parser::LatexFlavor;
const DOC: &str = "\\usepackage{mypkg}\n\\begin{myenv}\n{x}\nbody text here\n\\end{myenv}\n";
#[test]
fn local_package_environment_arity_glues_begin_argument() {
let dir = tempfile::tempdir().unwrap();
fs::write(
dir.path().join("mypkg.sty"),
"\\newenvironment{myenv}[1]{start #1}{end}\n",
)
.unwrap();
let main = dir.path().join("main.tex");
let with_pkg =
format_file_with_packages(DOC, &main, FormatStyle::default(), LatexFlavor::Document)
.expect("formats cleanly");
assert!(
with_pkg.contains("\\begin{myenv}{x}"),
"expected the package arity to glue the argument, got:\n{with_pkg}"
);
let without_pkg =
format_with_style_flavored(DOC, FormatStyle::default(), LatexFlavor::Document)
.expect("formats cleanly");
assert!(!without_pkg.contains("\\begin{myenv}{x}"));
assert_ne!(with_pkg, without_pkg);
}
#[test]
fn format_never_reads_the_aux_file() {
let dir = tempfile::tempdir().unwrap();
let main = dir.path().join("main.tex");
let doc = "\\section{Intro}\n\\label{sec:a}\nSee \\ref{sec:a}.\n";
let without_aux =
format_file_with_packages(doc, &main, FormatStyle::default(), LatexFlavor::Document)
.expect("formats cleanly");
fs::write(
dir.path().join("main.aux"),
"\\newlabel{sec:a}{{1}{1}{Intro}{section.1}{}}\n",
)
.unwrap();
let with_aux =
format_file_with_packages(doc, &main, FormatStyle::default(), LatexFlavor::Document)
.expect("formats cleanly");
assert_eq!(
without_aux, with_aux,
"the formatter must not read the .aux file"
);
}
#[test]
fn formatting_is_idempotent_with_packages() {
let dir = tempfile::tempdir().unwrap();
fs::write(
dir.path().join("mypkg.sty"),
"\\newenvironment{myenv}[1]{start #1}{end}\n",
)
.unwrap();
let main = dir.path().join("main.tex");
let once = format_file_with_packages(DOC, &main, FormatStyle::default(), LatexFlavor::Document)
.expect("formats cleanly");
let twice =
format_file_with_packages(&once, &main, FormatStyle::default(), LatexFlavor::Document)
.expect("formats cleanly");
assert_eq!(once, twice, "format must be idempotent");
}