use crate::tests_common::test_format;
use crate::{format, parse_normalized};
fn roundtrip(input: &str) {
let ast1 = parse_normalized(input).expect("input must parse");
let f1 = format(input).expect("format must succeed");
let ast2 = parse_normalized(&f1)
.unwrap_or_else(|e| panic!("formatted output must re-parse: {e:?}\n{f1}"));
assert_eq!(
ast1, ast2,
"AST changed after format\ninput: {input:?}\nformatted: {f1:?}"
);
let f2 = format(&f1).expect("second format must succeed");
assert_eq!(f1, f2, "format not idempotent\nf1: {f1:?}\nf2: {f2:?}");
}
#[test]
fn fuzz_or_without_selectors_dropped() {
roundtrip("t or a");
}
#[test]
fn fuzz_integer_selection_relex_as_float() {
roundtrip("1\n.a");
assert_eq!(format("1 .a").unwrap(), "1 .a\n");
test_format("1.5.x");
test_format("1..x");
}
#[test]
fn fuzz_indented_string_escape_newline_grows() {
roundtrip("''''\\\nX''");
}
#[test]
fn fuzz_indented_string_blank_line_indent() {
roundtrip("[''\n \n h'']");
}
#[test]
fn fuzz_indented_string_blank_line_preserves_excess() {
test_format("''\n \n b''");
test_format("''\n \nb''");
}
#[test]
fn fuzz_block_comment_with_cr_reparses() {
roundtrip("2/*\0\r\0");
roundtrip("/*a\r\0*/3");
roundtrip("/*a\rb*/3");
roundtrip("2 /* x\r */\n");
}
#[test]
fn fuzz_list_open_trail_comment_before_lang_annotation() {
roundtrip("[#x\n/*s*/\"\"\n]");
roundtrip("[#x\n/*s*/''ls''\n]");
assert_eq!(format("[\n/*s*/\"\"\n]").unwrap(), "[\n /* s */ \"\"\n]\n");
}
#[test]
fn fuzz_leading_blank_lines_app_layout() {
roundtrip("\n\nc\n\"${f\n\n}\"");
roundtrip("\n\nc d \"${f\n\n}\"");
roundtrip("\r\rc\n''${f\n\n}''");
roundtrip("(\n\nc \"${f\n\n}\")");
assert_eq!(
format("# foo\nc \"${f\n\n}\"").unwrap(),
"# foo\nc\n \"${\n f\n\n }\"\n"
);
}
#[test]
fn fuzz_lang_annotation_on_app_head_after_div() {
roundtrip("H/\n/*h*/''''p");
assert_eq!(
format("a / /* sh */ \"\" p\n").unwrap(),
"a / /* sh */ \"\" p\n"
);
assert_eq!(
format("a + /* sh */ \"\" p\n").unwrap(),
"a + /* sh */ \"\" p\n"
);
assert_eq!(
format("{a = /* sh */ \"\" p;}\n").unwrap(),
"{ a = /* sh */ \"\" p; }\n"
);
roundtrip("(/* sh */ \"\" p)");
roundtrip("/* sh */ \"\" p");
}
#[test]
fn fuzz_trailing_comment_on_multiline_simple_string() {
roundtrip("[\"\n\"#c\nt]");
roundtrip("{a=\"\n\"#c\n;b=1;}");
roundtrip("f \"\n\"#c\nt");
roundtrip("(\"\n\"#c\n)");
test_format("[''\na\n''#c\nt]");
}
#[test]
fn fuzz_paren_open_trail_comment_absorbable() {
roundtrip("{o=(# c\n''\"\n'');}");
roundtrip("{o=( # c\n[a\nb]);}");
roundtrip("{o=( # c\n{a=1;\nb=2;});}");
roundtrip("f (# c\n[a\nb])");
test_format("(# c\nx)");
}