From 1e9aa91c5d46e019584f2cf284428070384f35f4 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 5 Nov 2018 19:57:11 -0800 Subject: [PATCH] More concise test macros --- TODO.md | 1 + schala-lang/language/src/parsing.rs | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 15452c1..a4fb039 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,7 @@ # TODO Items +-fuzz test schala *A neat idea for pattern matching optimization would be if you could match on one of several things in a list ex: diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 5767ec1..7386f2a 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -1090,6 +1090,7 @@ fn parse_hex(digits: String) -> ParseResult { #[cfg(test)] mod parse_tests { use ::std::rc::Rc; + use super::tokenize; use super::ParseResult; use builtin::{PrefixOp, BinOp}; use ast::{AST, Expression, Statement, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody}; @@ -1103,7 +1104,6 @@ mod parse_tests { use super::ForBody::*; fn parse(input: &str) -> ParseResult { - use super::tokenize; let tokens: Vec<::tokenizing::Token> = tokenize(input); let mut parser = super::Parser::new(tokens); parser.parse() @@ -1124,6 +1124,13 @@ mod parse_tests { macro_rules! exprstatement { ($expr_type:expr) => { Statement::ExpressionStatement(Expression($expr_type, None)) }; ($expr_type:expr, $type_anno:expr) => { Statement::ExpressionStatement(Expression($expr_type, Some($type_anno))) }; + (s $statement_text:expr) => { + { + let tokens: Vec<::tokenizing::Token> = tokenize($statement_text); + let mut parser = super::Parser::new(tokens); + parser.statement().unwrap() + } + } } macro_rules! ty { ($name:expr) => { Singleton(tys!($name)) } @@ -1132,7 +1139,6 @@ mod parse_tests { ($name:expr) => { TypeSingletonName { name: Rc::new($name.to_string()), params: vec![] } }; } - /* new style of test macros */ macro_rules! single_expr { @@ -1140,8 +1146,16 @@ mod parse_tests { ($exprtype:expr, $type:expr) => { AST(vec![Statement::ExpressionStatement(Expression($exprtype, $type))]) } } macro_rules! ex { - ($expr_type:expr) => { Expression($expr_type, None) } + ($expr_type:expr) => { Expression($expr_type, None) }; + (s $expr_text:expr) => { + { + let tokens: Vec<::tokenizing::Token> = tokenize($expr_text); + let mut parser = super::Parser::new(tokens); + parser.expression().unwrap() + } + }; } + macro_rules! binexp { ($op:expr, $lhs:expr, $rhs:expr) => { BinExp(BinOp::from_sigil($op), bx!(Expression($lhs, None)), bx!(Expression($rhs, None))) } } @@ -1201,7 +1215,7 @@ mod parse_tests { parse_test!("()", AST(vec![exprstatement!(TupleLiteral(vec![]))])); parse_test!("(\"hella\", 34)", AST(vec![exprstatement!( TupleLiteral( - vec![ex!(StringLiteral(rc!(hella))), ex!(NatLiteral(34))] + vec![ex!(s r#""hella""#), ex!(s "34")] ) )])); parse_test!("((1+2), \"slough\")", AST(vec![exprstatement!(TupleLiteral(vec![ @@ -1500,7 +1514,7 @@ fn a(x) { exprstatement!(Lambda { params: vec![(rc!(x), Some(ty!("Int")))], type_anno: Some(ty!("String")), - body: vec![exprstatement!(StringLiteral(rc!(q)))] + body: vec![exprstatement!(s r#""q""#)] }) ]) }