mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-11 16:51:53 +03:00
Add basic Expr formatter
This commit is contained in:
parent
75799abe22
commit
97209a9454
@ -1,6 +1,7 @@
|
||||
use operator::Operator;
|
||||
use parse::problems::Problem;
|
||||
use region::Loc;
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
|
||||
pub type Ident = str;
|
||||
pub type VariantName = str;
|
||||
@ -157,3 +158,14 @@ pub enum Attempting {
|
||||
Module,
|
||||
Identifier,
|
||||
}
|
||||
|
||||
impl<'a> Display for Expr<'a> {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
use self::Expr::*;
|
||||
|
||||
match self {
|
||||
EmptyStr => write!(f, "\"\""),
|
||||
_ => panic!("TODO"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
51
tests/test_format.rs
Normal file
51
tests/test_format.rs
Normal file
@ -0,0 +1,51 @@
|
||||
#[macro_use]
|
||||
extern crate pretty_assertions;
|
||||
#[macro_use]
|
||||
extern crate indoc;
|
||||
extern crate bumpalo;
|
||||
extern crate combine; // OBSOLETE
|
||||
extern crate roc;
|
||||
|
||||
mod helpers;
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_formatter {
|
||||
use bumpalo::Bump;
|
||||
use roc::parse;
|
||||
use roc::parse::ast::{Attempting, Expr};
|
||||
use roc::parse::parser::{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 = parse::expr();
|
||||
let answer = parser.parse(&arena, state);
|
||||
|
||||
answer.map(|(expr, _)| expr).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!("{}", actual), 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#"
|
||||
""
|
||||
"#
|
||||
));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user