Added unary expression unit test for the parser.

This commit is contained in:
PaddiM8 2020-06-05 19:03:16 +02:00
parent f0889064b2
commit 5e1753410c

View File

@ -310,6 +310,7 @@ fn is_at_end(context: &mut Context) -> bool {
mod tests { mod tests {
use super::*; use super::*;
use crate::lexer::{Token, TokenKind::*}; use crate::lexer::{Token, TokenKind::*};
use test_case::test_case;
fn parse_with_context(context: &mut Context, tokens: Vec<Token>) -> Result<Stmt, String> { fn parse_with_context(context: &mut Context, tokens: Vec<Token>) -> Result<Stmt, String> {
context.tokens = tokens; context.tokens = tokens;
@ -342,6 +343,11 @@ mod tests {
fn binary(left: Box<Expr>, op: TokenKind, right: Box<Expr>) -> Box<Expr> { fn binary(left: Box<Expr>, op: TokenKind, right: Box<Expr>) -> Box<Expr> {
Box::new(Expr::Binary(left, op, right)) Box::new(Expr::Binary(left, op, right))
} }
fn unary(op: TokenKind, expr: Box<Expr>) -> Box<Expr> {
Box::new(Expr::Unary(op, expr))
}
fn group(expr: Box<Expr>) -> Box<Expr> { fn group(expr: Box<Expr>) -> Box<Expr> {
Box::new(Expr::Group(expr)) 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] #[test]
fn test_var_decl() { fn test_var_decl() {
let tokens = vec![ let tokens = vec![