Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: testing that nargo fmt is idempotent #4765

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions tooling/nargo_fmt/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,55 @@ fn generate_formatter_tests(test_file: &mut File, test_data_dir: &Path) {
let output_source_path = outputs_dir.join(file_name).display().to_string();
let output_source = std::fs::read_to_string(output_source_path.clone()).unwrap();

let skip_idempotent_test =
// TODO(https://github.com/noir-lang/noir/issues/4766): spurious trailing space
test_name == "array" ||
// TODO(https://github.com/noir-lang/noir/issues/4767): pre-comment space
// TODO(https://github.com/noir-lang/noir/issues/4768): spurious newline
test_name == "tuple";

write!(
test_file,
r##"
#[test]
fn format_{test_name}() {{
let input = r#"{input_source}"#;
let expected_output = r#"{output_source}"#;
#[test]
fn format_{test_name}() {{
let input = r#"{input_source}"#;
let expected_output = r#"{output_source}"#;


let (parsed_module, _errors) = noirc_frontend::parse_program(input);
let (parsed_module, _errors) = noirc_frontend::parse_program(input);

let config = nargo_fmt::Config::of("{config}").unwrap();
let fmt_text = nargo_fmt::format(input, parsed_module, &config);
let config = nargo_fmt::Config::of("{config}").unwrap();
let fmt_text = nargo_fmt::format(input, parsed_module, &config);

if std::env::var("UPDATE_EXPECT").is_ok() {{
std::fs::write("{output_source_path}", fmt_text.clone()).unwrap();
}}
if std::env::var("UPDATE_EXPECT").is_ok() {{
std::fs::write("{output_source_path}", fmt_text.clone()).unwrap();
}}

similar_asserts::assert_eq!(fmt_text, expected_output);
}}
similar_asserts::assert_eq!(fmt_text, expected_output);
}}
"##
)
.expect("Could not write templated test file.");

if !skip_idempotent_test {
write!(
test_file,
r##"
#[test]
fn format_idempotent_{test_name}() {{
let expected_output = r#"{output_source}"#;

let (parsed_module, _errors) = noirc_frontend::parse_program(expected_output);

let config = nargo_fmt::Config::of("{config}").unwrap();
let fmt_text = nargo_fmt::format(expected_output, parsed_module, &config);

similar_asserts::assert_eq!(fmt_text, expected_output);
}}
"##
)
.expect("Could not write templated test file.");
}
}
}
Loading