mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 15:51:12 +00:00
Also check formatting is stable in tests; ignore a test that's invalid / incorrect
This commit is contained in:
parent
595ce5e81a
commit
270cc49867
2 changed files with 57 additions and 7 deletions
|
@ -14,23 +14,32 @@ mod test_fmt {
|
||||||
use roc_parse::parser::{Parser, State};
|
use roc_parse::parser::{Parser, State};
|
||||||
use roc_test_utils::assert_multiline_str_eq;
|
use roc_test_utils::assert_multiline_str_eq;
|
||||||
|
|
||||||
fn expr_formats_to(input: &str, expected: &str) {
|
fn _expect_format_helper(input: &str, expected: &str) {
|
||||||
let arena = Bump::new();
|
let arena = Bump::new();
|
||||||
let input = input.trim_end();
|
|
||||||
let expected = expected.trim_end();
|
|
||||||
|
|
||||||
match roc_parse::test_helpers::parse_expr_with(&arena, input.trim()) {
|
match roc_parse::test_helpers::parse_expr_with(&arena, input.trim()) {
|
||||||
Ok(actual) => {
|
Ok(actual) => {
|
||||||
let mut buf = String::new_in(&arena);
|
let mut buf = String::new_in(&arena);
|
||||||
|
|
||||||
actual.format_with_options(&mut buf, Parens::NotNeeded, Newlines::Yes, 0);
|
actual.format_with_options(&mut buf, Parens::NotNeeded, Newlines::Yes, 0);
|
||||||
|
|
||||||
assert_multiline_str_eq!(expected, buf.as_str())
|
assert_multiline_str_eq!(expected, 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)
|
Err(error) => panic!("Unexpected parse failure when parsing this for formatting:\n\n{}\n\nParse error was:\n\n{:?}\n\n", input, error)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expr_formats_to(input: &str, expected: &str) {
|
||||||
|
let input = input.trim_end();
|
||||||
|
let expected = expected.trim_end();
|
||||||
|
|
||||||
|
// First check that input formats to the expected version
|
||||||
|
_expect_format_helper(input, expected);
|
||||||
|
|
||||||
|
// Parse the expected result format it, asserting that it doesn't change
|
||||||
|
// It's important that formatting be stable / idempotent
|
||||||
|
_expect_format_helper(expected, expected);
|
||||||
|
}
|
||||||
|
|
||||||
fn expr_formats_same(input: &str) {
|
fn expr_formats_same(input: &str) {
|
||||||
expr_formats_to(input, input);
|
expr_formats_to(input, input);
|
||||||
}
|
}
|
||||||
|
@ -88,6 +97,32 @@ mod test_fmt {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn def_with_comment_on_same_line() {
|
||||||
|
// TODO(joshuawarner32): make trailing comments format stabily
|
||||||
|
// This test currently fails because the comment ends up as SpaceBefore for the following `a`
|
||||||
|
// This works fine when formatted _once_ - but if you format again, the formatter wants to
|
||||||
|
// insert a newline between `a = "Hello"` and the comment, further muddying the waters.
|
||||||
|
// Clearly the formatter shouldn't be allowed to migrate a comment around like that.
|
||||||
|
expr_formats_to(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
a = "Hello" # This variable is for greeting
|
||||||
|
|
||||||
|
a
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
a = "Hello"
|
||||||
|
# This variable is for greeting
|
||||||
|
a
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn def_with_comment_and_extra_space() {
|
fn def_with_comment_and_extra_space() {
|
||||||
expr_formats_to(
|
expr_formats_to(
|
||||||
|
@ -1834,7 +1869,7 @@ mod test_fmt {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn when_with_alternatives() {
|
fn when_with_alternatives_1() {
|
||||||
expr_formats_same(indoc!(
|
expr_formats_same(indoc!(
|
||||||
r#"
|
r#"
|
||||||
when b is
|
when b is
|
||||||
|
@ -1847,6 +1882,10 @@ mod test_fmt {
|
||||||
5
|
5
|
||||||
"#
|
"#
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn when_with_alternatives_2() {
|
||||||
expr_formats_same(indoc!(
|
expr_formats_same(indoc!(
|
||||||
r#"
|
r#"
|
||||||
when b is
|
when b is
|
||||||
|
@ -1856,6 +1895,10 @@ mod test_fmt {
|
||||||
1
|
1
|
||||||
"#
|
"#
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn when_with_alternatives_3() {
|
||||||
expr_formats_to(
|
expr_formats_to(
|
||||||
indoc!(
|
indoc!(
|
||||||
r#"
|
r#"
|
||||||
|
@ -1873,6 +1916,13 @@ mod test_fmt {
|
||||||
"#
|
"#
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
// This test is actually invalid, since the result of the formatter doesn't parse.
|
||||||
|
// TODO(joshuawarner32): fix either the formatter or parser to allow this
|
||||||
|
fn when_with_alternatives_4() {
|
||||||
expr_formats_to(
|
expr_formats_to(
|
||||||
indoc!(
|
indoc!(
|
||||||
r#"
|
r#"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue