Lines
96.77 %
Functions
100 %
Branches
//! Golden HTML fixtures of the browser tests.
const FIXTURES: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/frontend/tests/fixtures/");
/// What the pre-commit hook leaves of an html file: no trailing whitespace
/// and no run of blank lines.
fn hook_stable(html: &str) -> String {
let mut out = String::new();
let mut blanks = 0;
for line in html.lines().map(str::trim_end) {
blanks = if line.is_empty() { blanks + 1 } else { 0 };
if blanks < 2 {
out.push_str(line);
out.push('\n');
}
out
/// Asserts that the `<input` element containing each needle carries the
/// given `autocomplete` token.
pub(crate) fn assert_autocomplete(html: &str, expected: &[(&str, &str)]) {
for (needle, token) in expected {
let input = html
.split("<input")
.skip(1)
.map(|chunk| chunk.split('>').next().unwrap_or_default())
.find(|element| element.contains(needle));
assert!(
input.is_some_and(|e| e.contains(&format!("autocomplete=\"{token}\""))),
"{needle}: {input:?}"
);
pub(crate) fn assert_fixture(name: &str, rendered: &str) {
let path = format!("{FIXTURES}{name}");
let html = hook_stable(rendered);
if std::env::var_os("UPDATE_FIXTURES").is_some() {
std::fs::write(&path, &html).unwrap();
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
html,
"regenerate with UPDATE_FIXTURES=1 cargo test -p web the_browser_fixture"