From 72d0cfe4660e86bfdafab5839722dfa2d6b3419b Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 5 Nov 2018 20:52:18 -0800 Subject: [PATCH] More macro test consolidation --- schala-lang/language/src/parsing.rs | 142 +++++++++++++--------------- 1 file changed, 64 insertions(+), 78 deletions(-) diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 4d944db..ad2c7a5 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -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!( - Lambda { params: vec![(rc!(x), None)], type_anno: None, body: vec![exst!(s "x + 1")] } - ) } + 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,74 +1552,65 @@ fn a(x) { #[test] fn patterns() { - parse_test! { - "if x is Some(a) then { 4 } else { 9 }", AST(vec![ - 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))]))) } - ) - ]) + 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!(s "4")], Some(vec![exst!(s "9")]))) } + ) } - parse_test! { - "if x is Some(a) then 4 else 9", AST(vec![ - 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))]))) } - ) - ]) + 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!(s "4")], Some(vec![exst!(s "9")]))) } + ) } - parse_test! { - "if x is Something { a, b: x } then { 4 } else { 9 }", AST(vec![ - exst!( - IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch( + 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( Pattern::Record(rc!(Something), vec![ (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![ - exst!( - IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch( - Pattern::Literal(PatternLiteral::NumPattern { neg: true, num: NatLiteral(1) }), - vec![exst!(NatLiteral(1))], - Some(vec![exst!(NatLiteral(2))]), - )) - } - ) - ]) + parse_test_wrap_ast! { + "if x is -1 then 1 else 2", + exst!( + IfExpression { + discriminator: bx!(Discriminator::Simple(ex!(s "x"))), + body: bx!(IfExpressionBody::SimplePatternMatch( + Pattern::Literal(PatternLiteral::NumPattern { neg: true, num: NatLiteral(1) }), + vec![exst!(NatLiteral(1))], + Some(vec![exst!(NatLiteral(2))]), + )) + } + ) } - parse_test! { - "if x is 1 then 1 else 2", AST(vec![ - exst!( - IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch( + parse_test_wrap_ast! { + "if x is 1 then 1 else 2", + exst!( + IfExpression { + discriminator: bx!(Discriminator::Simple(ex!(s "x"))), + body: bx!(IfExpressionBody::SimplePatternMatch( Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1) }), vec![exst!(s "1")], Some(vec![exst!(s "2")]), - )) - } - ) - ]) + )) + } + ) } parse_test! { @@ -1641,19 +1628,18 @@ 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"))), body: bx!(IfExpressionBody::SimplePatternMatch( - Pattern::Literal(PatternLiteral::StringPattern(rc!(gnosticism))), - vec![exst!(s "1")], - Some(vec![exst!(s "2")]), - )) + Pattern::Literal(PatternLiteral::StringPattern(rc!(gnosticism))), + vec![exst!(s "1")], + Some(vec![exst!(s "2")]), + )) } ) - ]) } } }