Move syntax tests to a dedicated crate

* test_fmt moves out of fmt crate
* test_parse _mostly_ moves out of parse crate and into `test_snapshots.rs` (some simple tests remain)
* now there's only two fuzz targets, fuzz_expr and fuzz_module, that cover both parsing and formatting
* added a system to auto-add new snapshot entries for new test files
* took some commented-out tests in `test_parse` and converted them to snapshot tests
* moved test_fmt's verification of formatting consistency into test_snapshots
* fixed a huge derp on my part where the fmt fuzzer in #4758 was completely useless (broken by refactoring just prior to submitting the PR)
* fixed a formatting bug found by fuzzing (bound_variable.expr.roc) - that I missed earlier due to ^^^ that derp
* no longer have roc_test_utils as a dependency in fmt - which was causing problems for the wasm build
This commit is contained in:
Joshua Warner 2022-12-26 12:50:31 -08:00
parent f0b3c3eb08
commit bfeddc470a
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
701 changed files with 1929 additions and 1516 deletions

View file

@ -288,9 +288,18 @@ impl<'a> Formattable for TypeAnnotation<'a> {
buf.push(')')
}
}
BoundVariable(v) => buf.push_str(v),
Wildcard => buf.push('*'),
Inferred => buf.push('_'),
BoundVariable(v) => {
buf.indent(indent);
buf.push_str(v)
}
Wildcard => {
buf.indent(indent);
buf.push('*')
}
Inferred => {
buf.indent(indent);
buf.push('_')
}
TagUnion { tags, ext } => {
fmt_collection(buf, indent, Braces::Square, *tags, newlines);
@ -355,8 +364,10 @@ impl<'a> Formattable for TypeAnnotation<'a> {
ann.format_with_options(buf, parens, newlines, indent);
fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent);
}
Malformed(raw) => buf.push_str(raw),
Malformed(raw) => {
buf.indent(indent);
buf.push_str(raw)
}
}
}
}

View file

@ -9,7 +9,6 @@ pub mod expr;
pub mod module;
pub mod pattern;
pub mod spaces;
pub mod test_helpers;
use bumpalo::{collections::String, Bump};
use roc_parse::ast::Module;

View file

@ -1,77 +0,0 @@
use bumpalo::Bump;
use roc_test_utils::assert_multiline_str_eq;
use crate::{
annotation::{Formattable, Newlines, Parens},
Buf,
};
/// Parse and re-format the given input, and pass the output to `check_formatting`
/// for verification. The expectation is that `check_formatting` assert the result matches
/// expectations (or, overwrite the expectation based on a command-line flag)
/// Optionally, based on the value of `check_idempotency`, also verify that the formatting
/// is idempotent - that if we reformat the output, we get the same result.
pub fn expr_formats(input: &str, check_formatting: impl Fn(&str), check_idempotency: bool) {
let arena = Bump::new();
let input = input.trim();
match roc_parse::test_helpers::parse_expr_with(&arena, input) {
Ok(actual) => {
use crate::spaces::RemoveSpaces;
let mut buf = Buf::new_in(&arena);
actual.format_with_options(&mut buf, Parens::NotNeeded, Newlines::Yes, 0);
let output = buf.as_str();
check_formatting(output);
let reparsed_ast = roc_parse::test_helpers::parse_expr_with(&arena, output).unwrap_or_else(|err| {
panic!(
"After formatting, the source code no longer parsed!\n\n\
Parse error was: {:?}\n\n\
The code that failed to parse:\n\n{}\n\n\
The original ast was:\n\n{:#?}\n\n",
err, output, actual
);
});
let ast_normalized = actual.remove_spaces(&arena);
let reparsed_ast_normalized = reparsed_ast.remove_spaces(&arena);
// HACK!
// We compare the debug format strings of the ASTs, because I'm finding in practice that _somewhere_ deep inside the ast,
// the PartialEq implementation is returning `false` even when the Debug-formatted impl is exactly the same.
// I don't have the patience to debug this right now, so let's leave it for another day...
// TODO: fix PartialEq impl on ast types
if format!("{:?}", ast_normalized) != format!("{:?}", reparsed_ast_normalized) {
panic!(
"Formatting bug; formatting didn't reparse to the same AST (after removing spaces)\n\n\
* * * Source code before formatting:\n{}\n\n\
* * * Source code after formatting:\n{}\n\n\
* * * AST before formatting:\n{:#?}\n\n\
* * * AST after formatting:\n{:#?}\n\n",
input,
output,
ast_normalized,
reparsed_ast_normalized
);
}
// Now verify that the resultant formatting is _idempotent_ - i.e. that it doesn't change again if re-formatted
if check_idempotency {
let mut reformatted_buf = Buf::new_in(&arena);
reparsed_ast.format_with_options(&mut reformatted_buf, Parens::NotNeeded, Newlines::Yes, 0);
if output != reformatted_buf.as_str() {
eprintln!("Formatting bug; formatting is not stable.\nOriginal code:\n{}\n\nFormatted code:\n{}\n\n", input, output);
eprintln!("Reformatting the formatted code changed it again, as follows:\n\n");
assert_multiline_str_eq!(output, reformatted_buf.as_str());
}
}
}
Err(error) => panic!("Unexpected parse failure when parsing this for formatting:\n\n{}\n\nParse error was:\n\n{:?}\n\n", input, error)
};
}