Skip to content

Commit

Permalink
feat: adjust failing test syntax
Browse files Browse the repository at this point in the history
* also add a formatter test
  • Loading branch information
rvcas committed May 25, 2023
1 parent 00ac6b6 commit 7b3e1c6
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
1 change: 0 additions & 1 deletion crates/aiken-lang/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ fn str_to_keyword(word: &str) -> Option<Token> {
"test" => Some(Token::Test),
"error" => Some(Token::ErrorTerm),
"validator" => Some(Token::Validator),
"fail" => Some(Token::Fail),
_ => None,
}
}
Expand Down
11 changes: 10 additions & 1 deletion crates/aiken-lang/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,17 @@ impl<'comments> Formatter<'comments> {
arguments: args,
body,
end_position,
can_error,
..
}) => self.definition_fn(&false, "test", name, args, &None, body, *end_position),
}) => self.definition_fn(
&false,
if *can_error { "!test" } else { "test" },
name,
args,
&None,
body,
*end_position,
),

Definition::TypeAlias(TypeAlias {
alias,
Expand Down
6 changes: 4 additions & 2 deletions crates/aiken-lang/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,10 @@ pub fn fn_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseEr
}

pub fn test_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
just(Token::Test)
.ignore_then(just(Token::Fail).ignored().or_not())
just(Token::Bang)
.ignored()
.or_not()
.then_ignore(just(Token::Test))
.then(select! {Token::Name {name} => name})
.then_ignore(just(Token::LeftParen))
.then_ignore(just(Token::RightParen))
Expand Down
1 change: 0 additions & 1 deletion crates/aiken-lang/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = ParseError> {
"todo" => Token::Todo,
"type" => Token::Type,
"when" => Token::When,
"fail" => Token::Fail,
"validator" => Token::Validator,
_ => {
if s.chars().next().map_or(false, |c| c.is_uppercase()) {
Expand Down
2 changes: 0 additions & 2 deletions crates/aiken-lang/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ pub enum Token {
When,
Trace,
Validator,
Fail,
}

impl fmt::Display for Token {
Expand Down Expand Up @@ -165,7 +164,6 @@ impl fmt::Display for Token {
Token::Test => "test",
Token::ErrorTerm => "error",
Token::Validator => "validator",
Token::Fail => "fail",
};
write!(f, "\"{s}\"")
}
Expand Down
13 changes: 13 additions & 0 deletions crates/aiken-lang/src/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,16 @@ fn match_record() {

assert_fmt(src, src);
}

#[test]
fn test_fail() {
let src = indoc! { r#"
!test foo() {
expect Some(a) = bar
a
}
"#};

assert_fmt(src, src);
}
16 changes: 8 additions & 8 deletions crates/aiken-lang/src/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn windows_newline() {
#[test]
fn test_fail() {
let code = indoc! {r#"
test fail invalid_inputs() {
!test invalid_inputs() {
expect True = False
False
Expand All @@ -53,17 +53,17 @@ fn test_fail() {
vec![ast::UntypedDefinition::Test(ast::Function {
arguments: vec![],
body: expr::UntypedExpr::Sequence {
location: Span::new((), 31..59),
location: Span::new((), 27..55),
expressions: vec![
expr::UntypedExpr::Assignment {
location: Span::new((), 31..50),
location: Span::new((), 27..46),
value: Box::new(expr::UntypedExpr::Var {
location: Span::new((), 45..50),
location: Span::new((), 41..46),
name: "False".to_string(),
}),
pattern: ast::UntypedPattern::Constructor {
is_record: false,
location: Span::new((), 38..42),
location: Span::new((), 34..38),
name: "True".to_string(),
arguments: vec![],
module: None,
Expand All @@ -75,18 +75,18 @@ fn test_fail() {
annotation: None,
},
expr::UntypedExpr::Var {
location: Span::new((), 54..59),
location: Span::new((), 50..55),
name: "False".to_string(),
},
],
},
doc: None,
location: Span::new((), 0..26),
location: Span::new((), 0..22),
name: "invalid_inputs".to_string(),
public: false,
return_annotation: None,
return_type: (),
end_position: 60,
end_position: 56,
can_error: true,
})],
);
Expand Down

0 comments on commit 7b3e1c6

Please sign in to comment.