Skip to content

Commit

Permalink
More tests (with correct syntax), removed debug statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul committed Jan 9, 2021
1 parent 451b35b commit c3efb60
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 95 deletions.
5 changes: 0 additions & 5 deletions boa/src/syntax/parser/expression/assignment/arrow_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,8 @@ where

fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("ArrowFunction", "Parsing");

println!("Parsing arrow func");

let next_token = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?;

println!("Next token {}", next_token);

let params = if let TokenKind::Punctuator(Punctuator::OpenParen) = &next_token.kind() {
// CoverParenthesizedExpressionAndArrowParameterList
cursor.expect(Punctuator::OpenParen, "arrow function")?;
Expand Down
243 changes: 153 additions & 90 deletions boa/src/syntax/parser/function/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::syntax::{
ast::node::{
ArrowFunctionDecl, BinOp, FormalParameter, FunctionDecl, Identifier, Node, Return, LetDecl, LetDeclList
ArrowFunctionDecl, BinOp, FormalParameter, FunctionDecl, Identifier, LetDecl, LetDeclList,
Node, Return,
},
ast::op::NumOp,
parser::tests::check_parser,
Expand Down Expand Up @@ -177,130 +178,192 @@ fn check_arrow_empty_return_semicolon_insertion() {
);
}

/// Checks an arrow function assignment.
#[test]
fn check_arrow_assignment() {
check_parser(
"let foo = a => { return a };",
vec![LetDeclList::from(
vec![
LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(Some(Identifier::from("a").into()), None).into()],
).into())
"let foo = (a) => { return a };",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
]
).into()]
.into(),
),
)])
.into()],
);
}

/// Checks an arrow function assignment without {}.
#[test]
fn check_arrow_assignment_nobrackets() {
check_parser(
"let foo = a => return a;",
vec![LetDeclList::from(
vec![
LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(Some(Identifier::from("a").into()), None).into()],
).into())
"let foo = (a) => a;",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
]
).into()]
.into(),
),
)])
.into()],
);
}

/// Checks an arrow function assignment with parenthesis.
#[test]
fn check_arrow_assignment_parenthesis_brackets() {
fn check_arrow_assignment_noparenthesis() {
check_parser(
"let foo = (a) => { return a };",
vec![LetDeclList::from(
vec![
LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(Some(Identifier::from("a").into()), None).into()],
).into())
"let foo = a => { return a };",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
]
).into()]
.into(),
),
)])
.into()],
);
}

/// Checks an arrow function assignment with parenthesis without {}.
#[test]
fn check_arrow_assignment_parenthesis_nobrackets() {
fn check_arrow_assignment_noparenthesis_nobrackets() {
check_parser(
"let foo = (a) => return a;",
vec![LetDeclList::from(
vec![
LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(Some(Identifier::from("a").into()), None).into()],
).into())
"let foo = a => a;",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
]
).into()]
.into(),
),
)])
.into()],
);
}

/// Checks an arrow function assignment with 2 arguments with parenthesis.
#[test]
fn check_arrow_assignment_2arg_parenthesis() {
fn check_arrow_assignment_2arg() {
check_parser(
"let foo = (a, b) => { return a };",
vec![LetDeclList::from(
vec![
LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false)
],
vec![Return::new::<Node, Option<_>, Option<_>>(Some(Identifier::from("a").into()), None).into()],
).into())
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
]
).into()]
.into(),
),
)])
.into()],
);
}

/// Checks an arrow function assignment with 2 arguments with parenthesis.
#[test]
fn check_arrow_assignment_2arg_parenthesis_nobrackets() {
fn check_arrow_assignment_2arg_nobrackets() {
check_parser(
"let foo = (a, b) => return a;",
vec![LetDeclList::from(
vec![
LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false)
],
vec![Return::new::<Node, Option<_>, Option<_>>(Some(Identifier::from("a").into()), None).into()],
).into())
"let foo = (a, b) => a;",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
]
).into()]
.into(),
),
)])
.into()],
);
}

#[test]
fn check_arrow_assignment_3arg() {
check_parser(
"let foo = (a, b, c) => { return a };",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false),
FormalParameter::new("c", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

#[test]
fn check_arrow_assignment_3arg_nobrackets() {
check_parser(
"let foo = (a, b, c) => a;",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false),
FormalParameter::new("c", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

0 comments on commit c3efb60

Please sign in to comment.