diff --git a/kalk/src/parser.rs b/kalk/src/parser.rs index d8aa876..47f6f89 100644 --- a/kalk/src/parser.rs +++ b/kalk/src/parser.rs @@ -310,6 +310,7 @@ fn is_at_end(context: &mut Context) -> bool { mod tests { use super::*; use crate::lexer::{Token, TokenKind::*}; + use test_case::test_case; fn parse_with_context(context: &mut Context, tokens: Vec) -> Result { context.tokens = tokens; @@ -342,6 +343,11 @@ mod tests { fn binary(left: Box, op: TokenKind, right: Box) -> Box { Box::new(Expr::Binary(left, op, right)) } + + fn unary(op: TokenKind, expr: Box) -> Box { + Box::new(Expr::Unary(op, expr)) + } + fn group(expr: Box) -> Box { Box::new(Expr::Group(expr)) } @@ -421,6 +427,21 @@ mod tests { ); } + #[test_case(Deg)] + #[test_case(Rad)] + fn test_unary(angle_unit: TokenKind) { + let tokens = vec![ + token(Minus, ""), + token(Literal, "1"), + token(angle_unit.clone(), ""), + ]; + + assert_eq!( + parse(tokens).unwrap(), + Stmt::Expr(unary(Minus, Box::new(Expr::Unit(literal("1"), angle_unit)))) + ); + } + #[test] fn test_var_decl() { let tokens = vec![