mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-11 16:51:53 +03:00
4fe125f405
currently, some tests are failing because - i'm not sure what the "correct" formatting is - and how to handle spaces (especially newlines) Perhaps it's OK to ignore newlines for now, and just always start with `{<space>` and close with `<space>}`?
276 lines
4.9 KiB
Rust
276 lines
4.9 KiB
Rust
#[macro_use]
|
|
extern crate pretty_assertions;
|
|
#[macro_use]
|
|
extern crate indoc;
|
|
extern crate bumpalo;
|
|
extern crate roc;
|
|
|
|
#[cfg(test)]
|
|
mod test_format {
|
|
use bumpalo::Bump;
|
|
use roc::parse;
|
|
use roc::parse::ast::{format, Attempting, Expr};
|
|
use roc::parse::blankspace::space0_before;
|
|
use roc::parse::parser::{loc, Fail, Parser, State};
|
|
|
|
fn parse_with<'a>(arena: &'a Bump, input: &'a str) -> Result<Expr<'a>, Fail> {
|
|
let state = State::new(&input, Attempting::Module);
|
|
let parser = space0_before(loc(parse::expr(0)), 0);
|
|
let answer = parser.parse(&arena, state);
|
|
|
|
answer
|
|
.map(|(loc_expr, _)| loc_expr.value)
|
|
.map_err(|(fail, _)| fail)
|
|
}
|
|
|
|
fn assert_formats_to(input: &str, expected: &str) {
|
|
let arena = Bump::new();
|
|
let input = input.trim_end();
|
|
let expected = expected.trim_end();
|
|
|
|
match parse_with(&arena, input) {
|
|
Ok(actual) => assert_eq!(format(&arena, &actual, 0, false), expected),
|
|
Err(error) => panic!("Unexpected parse failure when parsing this for formatting:\n\n{:?}\n\nParse error was:\n\n{:?}\n\n", input, error)
|
|
}
|
|
}
|
|
|
|
fn assert_formats_same(input: &str) {
|
|
assert_formats_to(input, input);
|
|
}
|
|
|
|
// STRING LITERALS
|
|
|
|
#[test]
|
|
fn empty_string() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
""
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn zero() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
0
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn zero_point_zero() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
0.0
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn int_with_underscores() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
1_23_456
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn float_with_underscores() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
1_23_456.7_89_10
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn basic_string() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
"blah"
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn escaped_unicode_string() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
"unicode: \u{A00A}!"
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn multi_arg_closure() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
\a b c -> a b c
|
|
"#
|
|
));
|
|
}
|
|
|
|
// DEFS
|
|
|
|
#[test]
|
|
fn single_def() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
x = 5
|
|
|
|
42
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn two_defs() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
x = 5
|
|
y = 10
|
|
|
|
42
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn parenthetical_def() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
(UserId userId) = 5
|
|
y = 10
|
|
|
|
42
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn record_destructuring() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
{ x, y } = 5
|
|
|
|
42
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn def_closure() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
identity = \a -> a
|
|
|
|
identity 42
|
|
"#
|
|
));
|
|
}
|
|
|
|
// RECORD LITERALS
|
|
|
|
#[test]
|
|
fn empty_record() {
|
|
assert_formats_same("{}");
|
|
}
|
|
|
|
#[test]
|
|
fn one_field() {
|
|
assert_formats_same("{ x : 4 }");
|
|
}
|
|
|
|
#[test]
|
|
fn two_fields() {
|
|
assert_formats_same("{ x : 4, y : 42 }");
|
|
}
|
|
|
|
#[test]
|
|
fn two_fields_newline() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
{ x : 4
|
|
, y : 42
|
|
}
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn one_unnamed_field() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
foo = 4
|
|
|
|
{ foo }
|
|
"#
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn field_with_comments() {
|
|
assert_formats_same("{ ### before ### x ### after ### : 4 }");
|
|
}
|
|
|
|
#[test]
|
|
fn unnamed_field_with_comments() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
foo = 4
|
|
|
|
{ ### before ### foo ### after ### }
|
|
"#
|
|
));
|
|
}
|
|
|
|
// IF
|
|
|
|
#[test]
|
|
fn single_line_if() {
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
if foo bar then a b c else d e f
|
|
"#
|
|
));
|
|
|
|
assert_formats_same(indoc!(
|
|
r#"
|
|
if foo (a b c) then a b c else d e f
|
|
"#
|
|
));
|
|
}
|
|
|
|
// NEWLINES
|
|
|
|
#[test]
|
|
fn multiple_blank_lines_collapse_to_one() {
|
|
assert_formats_to(
|
|
indoc!(
|
|
r#"
|
|
x = 5
|
|
|
|
|
|
|
|
y = 10
|
|
|
|
|
|
|
|
42
|
|
"#
|
|
),
|
|
indoc!(
|
|
r#"
|
|
x = 5
|
|
|
|
y = 10
|
|
|
|
42
|
|
"#
|
|
),
|
|
);
|
|
}
|
|
}
|