2019-09-16 04:37:20 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate pretty_assertions;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate indoc;
|
|
|
|
extern crate bumpalo;
|
|
|
|
extern crate roc;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-09-16 07:25:31 +03:00
|
|
|
mod test_format {
|
2019-09-16 04:37:20 +03:00
|
|
|
use bumpalo::Bump;
|
|
|
|
use roc::parse;
|
2019-09-20 06:04:50 +03:00
|
|
|
use roc::parse::ast::{format, Attempting, Expr};
|
2019-09-30 19:02:09 +03:00
|
|
|
use roc::parse::blankspace::space0_before;
|
|
|
|
use roc::parse::parser::{loc, Fail, Parser, State};
|
2019-09-16 04:37:20 +03:00
|
|
|
|
|
|
|
fn parse_with<'a>(arena: &'a Bump, input: &'a str) -> Result<Expr<'a>, Fail> {
|
|
|
|
let state = State::new(&input, Attempting::Module);
|
2019-09-30 19:02:09 +03:00
|
|
|
let parser = space0_before(loc(parse::expr(0)), 0);
|
2019-09-16 04:37:20 +03:00
|
|
|
let answer = parser.parse(&arena, state);
|
|
|
|
|
2019-09-30 19:02:09 +03:00
|
|
|
answer
|
|
|
|
.map(|(loc_expr, _)| loc_expr.value)
|
|
|
|
.map_err(|(fail, _)| fail)
|
2019-09-16 04:37:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-09-20 06:04:50 +03:00
|
|
|
Ok(actual) => assert_eq!(format(&arena, &actual, 0), expected),
|
2019-09-16 04:37:20 +03:00
|
|
|
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#"
|
|
|
|
""
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
}
|
2019-09-16 06:29:06 +03:00
|
|
|
|
|
|
|
#[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
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-09-16 07:25:31 +03:00
|
|
|
#[test]
|
|
|
|
fn basic_string() {
|
|
|
|
assert_formats_same(indoc!(
|
|
|
|
r#"
|
|
|
|
"blah"
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn escaped_unicode_string() {
|
|
|
|
assert_formats_same(indoc!(
|
|
|
|
r#"
|
|
|
|
"unicode: \u{A00A}!"
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-09-30 19:02:09 +03:00
|
|
|
#[test]
|
|
|
|
fn single_def() {
|
|
|
|
assert_formats_same(indoc!(
|
|
|
|
r#"# comment to reset indentation
|
|
|
|
x = 5
|
|
|
|
|
|
|
|
42
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-09-30 19:03:28 +03:00
|
|
|
#[test]
|
|
|
|
fn two_defs() {
|
|
|
|
assert_formats_same(indoc!(
|
|
|
|
r#"# comment to reset indentation
|
|
|
|
x = 5
|
|
|
|
y = 10
|
|
|
|
|
|
|
|
42
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-09-16 07:25:31 +03:00
|
|
|
// RECORD LITERALS
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_record() {
|
|
|
|
assert_formats_same("{}");
|
|
|
|
}
|
2019-09-16 04:37:20 +03:00
|
|
|
}
|