More macro test consolidation

This commit is contained in:
greg 2018-11-05 20:52:18 -08:00
parent cea2f63b44
commit 72d0cfe466

View File

@ -1113,7 +1113,10 @@ mod parse_tests {
($string:tt) => { Rc::new(stringify!($string).to_string()) }
}
macro_rules! parse_test {
($string:expr, $correct:expr) => { assert_eq!(parse($string).unwrap(), $correct) }
($string:expr, $correct:expr) => { assert_eq!(parse($string).unwrap(), $correct) };
}
macro_rules! parse_test_wrap_ast {
($string:expr, $correct:expr) => { parse_test!($string, AST(vec![$correct])) }
}
macro_rules! parse_error {
($string:expr) => { assert!(parse($string).is_err()) }
@ -1128,12 +1131,6 @@ mod parse_tests {
($name:expr) => { TypeSingletonName { name: Rc::new($name.to_string()), params: vec![] } };
}
/* new style of test macros */
macro_rules! single_expr {
($exprtype:expr) => { AST(vec![Statement::ExpressionStatement(Expression($exprtype, None))]) };
($exprtype:expr, $type:expr) => { AST(vec![Statement::ExpressionStatement(Expression($exprtype, $type))]) }
}
macro_rules! ex {
($expr_type:expr) => { Expression($expr_type, None) };
(s $expr_text:expr) => {
@ -1166,16 +1163,16 @@ mod parse_tests {
#[test]
fn parsing_number_literals_and_binexps() {
parse_test! { ".2", single_expr!(FloatLiteral(0.2)) };
parse_test! { "8.1", single_expr!(FloatLiteral(8.1)) };
parse_test_wrap_ast! { ".2", exst!(FloatLiteral(0.2)) };
parse_test_wrap_ast! { "8.1", exst!(FloatLiteral(8.1)) };
parse_test! { "0b010", single_expr!(NatLiteral(2)) };
parse_test! { "0b0_1_0_", single_expr!(NatLiteral(2)) }
parse_test_wrap_ast! { "0b010", exst!(NatLiteral(2)) };
parse_test_wrap_ast! { "0b0_1_0_", exst!(NatLiteral(2)) }
parse_test! {"0xff", single_expr!(NatLiteral(255)) };
parse_test! {"0xf_f_", single_expr!(NatLiteral(255)) };
parse_test_wrap_ast! {"0xff", exst!(NatLiteral(255)) };
parse_test_wrap_ast! {"0xf_f_", exst!(NatLiteral(255)) };
parse_test!("0xf_f_+1", AST(vec![exst!(binexp!("+", NatLiteral(255), NatLiteral(1)))]));
parse_test_wrap_ast! {"0xf_f_+1", exst!(binexp!("+", NatLiteral(255), NatLiteral(1))) };
parse_test! {"3; 4; 4.3", AST(
vec![exst!(NatLiteral(3)), exst!(NatLiteral(4)),
@ -1482,9 +1479,10 @@ fn a(x) {
#[test]
fn parsing_lambdas() {
parse_test! { r#"\(x) { x + 1}"#, single_expr!(
parse_test_wrap_ast! { r#"\(x) { x + 1}"#, exst!(
Lambda { params: vec![(rc!(x), None)], type_anno: None, body: vec![exst!(s "x + 1")] }
) }
)
}
parse_test!(r#"\ (x: Int, y) { a;b;c;}"#, AST(vec![
exst!(Lambda {
@ -1503,15 +1501,13 @@ fn a(x) {
)),
arguments: vec![ex!(NatLiteral(1))] })]));
parse_test! {
parse_test_wrap_ast! {
r#"\(x: Int): String { "q" }"#,
AST(vec![
exst!(Lambda {
params: vec![(rc!(x), Some(ty!("Int")))],
type_anno: Some(ty!("String")),
body: vec![exst!(s r#""q""#)]
})
])
}
}
@ -1542,7 +1538,7 @@ fn a(x) {
"for { a <- maybeValue } return 1", AST(vec![
exst!(ForExpression {
enumerators: vec![Enumerator { id: rc!(a), generator: ex!(val!("maybeValue")) }],
body: bx!(MonadicReturn(ex!(NatLiteral(1))))
body: bx!(MonadicReturn(ex!(s "1")))
})])
}
@ -1556,29 +1552,24 @@ fn a(x) {
#[test]
fn patterns() {
parse_test! {
"if x is Some(a) then { 4 } else { 9 }", AST(vec![
exst!(
parse_test_wrap_ast! {
"if x is Some(a) then { 4 } else { 9 }", exst!(
IfExpression {
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exst!(NatLiteral(4))], Some(vec![exst!(NatLiteral(9))]))) }
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) }
)
])
}
parse_test! {
"if x is Some(a) then 4 else 9", AST(vec![
exst!(
parse_test_wrap_ast! {
"if x is Some(a) then 4 else 9", exst!(
IfExpression {
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exst!(NatLiteral(4))], Some(vec![exst!(NatLiteral(9))]))) }
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) }
)
])
}
parse_test! {
"if x is Something { a, b: x } then { 4 } else { 9 }", AST(vec![
exst!(
parse_test_wrap_ast! {
"if x is Something { a, b: x } then { 4 } else { 9 }", exst!(
IfExpression {
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
body: bx!(IfExpressionBody::SimplePatternMatch(
@ -1586,18 +1577,16 @@ fn a(x) {
(rc!(a),Pattern::Literal(PatternLiteral::StringPattern(rc!(a)))),
(rc!(b),Pattern::Literal(PatternLiteral::VarPattern(rc!(x))))
]),
vec![exst!(NatLiteral(4))], Some(vec![exst!(NatLiteral(9))])))
vec![exst!(s "4")], Some(vec![exst!(s "9")])))
}
)
])
}
}
#[test]
fn pattern_literals() {
parse_test! {
"if x is -1 then 1 else 2", AST(vec![
parse_test_wrap_ast! {
"if x is -1 then 1 else 2",
exst!(
IfExpression {
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
@ -1608,11 +1597,10 @@ fn a(x) {
))
}
)
])
}
parse_test! {
"if x is 1 then 1 else 2", AST(vec![
parse_test_wrap_ast! {
"if x is 1 then 1 else 2",
exst!(
IfExpression {
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
@ -1623,7 +1611,6 @@ fn a(x) {
))
}
)
])
}
parse_test! {
@ -1641,8 +1628,8 @@ fn a(x) {
])
}
parse_test! {
"if x is \"gnosticism\" then 1 else 2", AST(vec![
parse_test_wrap_ast! {
"if x is \"gnosticism\" then 1 else 2",
exst!(
IfExpression {
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
@ -1653,7 +1640,6 @@ fn a(x) {
))
}
)
])
}
}
}