mod harness;
use harness::{render, render_stdin, strip, temp_doc_ext};
const SCRIPT: &str = "#!/bin/sh\n# deploy the thing\nset -eu\n\nfor f in *; do\n\techo \"$f\"\ndone\n";
fn lines(out: &str) -> Vec<String> {
strip(out).lines().map(|l| l.trim_end().to_string()).collect()
}
#[test]
fn an_unknown_extension_renders_verbatim() {
let doc = temp_doc_ext("script", "sh", SCRIPT.as_bytes());
let out = lines(&render(&doc, &["--no-alt", "--plain", "--width", "80"]));
assert_eq!(
out,
[
"#!/bin/sh",
"# deploy the thing",
"set -eu",
"",
"for f in *; do",
" echo \"$f\"",
"done",
],
"{out:#?}"
);
}
#[test]
fn an_extensionless_file_is_text_and_is_not_sniffed() {
let body = "id,name\n1,alice\n2,bo\n3,carol\n";
let doc = temp_doc_ext("plainname", "conf", body.as_bytes());
let out = lines(&render(&doc, &["--no-alt", "--plain", "--width", "80"]));
assert_eq!(out, ["id,name", "1,alice", "2,bo", "3,carol"], "{out:#?}");
assert!(!out[0].starts_with('\u{250c}'), "read as a grid: {out:#?}");
}
#[test]
fn the_flag_forces_text_over_an_extension() {
let md = "# Title\n\nSome **bold** prose.\n";
let doc = temp_doc_ext("forced", "md", md.as_bytes());
let out = lines(&render(&doc, &["--no-alt", "--plain", "--width", "80", "--format", "text"]));
assert_eq!(out, ["# Title", "", "Some **bold** prose."], "{out:#?}");
let normal = strip(&render(&doc, &["--no-alt", "--plain", "--width", "80"]));
assert!(!normal.contains("# Title"), "{normal}");
assert!(normal.contains("bold"), "{normal}");
}
#[test]
fn a_txt_file_is_text() {
let doc = temp_doc_ext("notes", "txt", b"# TODO\n- buy milk\n");
let out = lines(&render(&doc, &["--no-alt", "--plain", "--width", "80"]));
assert_eq!(out, ["# TODO", "- buy milk"], "{out:#?}");
}
#[test]
fn hostile_bytes_render_and_exit_cleanly() {
let mut body = b"crlf\r\ncr\rlf\n\x1b[2Jescape\n".to_vec();
body.extend_from_slice(b"\xff\xfe not utf8\nno trailing newline");
let doc = temp_doc_ext("hostile", "log", &body);
let raw = render(&doc, &["--no-alt", "--plain", "--width", "80"]);
let out = lines(&raw);
assert_eq!(out.len(), 6, "{out:#?}");
assert_eq!(out[0], "crlf");
assert_eq!(out[1], "cr");
assert_eq!(out[2], "lf");
assert_eq!(out[3], "\u{b7}[2Jescape");
assert!(out[4].contains('\u{fffd}'), "{:?}", out[4]);
assert_eq!(out[5], "no trailing newline");
}
#[test]
fn toc_of_a_text_file_is_empty() {
let doc = temp_doc_ext("toc", "sh", SCRIPT.as_bytes());
assert_eq!(render(&doc, &["--toc"]), "");
}
#[test]
fn a_pipe_is_still_sniffed() {
let out = strip(&render_stdin(b"# Title\n\nprose\n", &["--no-alt", "--plain", "--width", "80"]));
assert!(!out.contains("# Title"), "a piped document is markdown: {out}");
let forced = lines(&render_stdin(
b"# Title\n\nprose\n",
&["--no-alt", "--plain", "--width", "80", "--format", "text"],
));
assert_eq!(forced, ["# Title", "", "prose"], "{forced:#?}");
}