Add even more tests
This commit is contained in:
parent
6f47dc69b6
commit
59894a1b64
@ -43,7 +43,7 @@
|
||||
- [x] Parse function declaration arguments (`Type id`)
|
||||
- [x] Parse function return datatype (`fun f() -> Type`)
|
||||
- [x] Return parsing to variables to var/val
|
||||
- [ ] Write tests
|
||||
- [x] Write tests
|
||||
|
||||
|
||||
## v0.0.10
|
||||
|
@ -47,3 +47,45 @@ fn parse_many<'a>(
|
||||
_ => Ok((prev_expr, pos)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::lexic::get_tokens;
|
||||
|
||||
#[test]
|
||||
fn should_parse_comparison() {
|
||||
let tokens = get_tokens(&String::from("a >= b")).unwrap();
|
||||
let result = try_parse(&tokens, 0);
|
||||
|
||||
match result {
|
||||
Ok((expr, _)) => match expr {
|
||||
Expression::BinaryOperator(exp1, exp2, op) => {
|
||||
match (*exp1, *exp2) {
|
||||
(Expression::Identifier(id1), Expression::Identifier(id2)) => {
|
||||
assert_eq!("a", id1);
|
||||
assert_eq!("b", id2);
|
||||
}
|
||||
_ => panic!("Expected 2 identifiers"),
|
||||
}
|
||||
assert_eq!(">=", op)
|
||||
}
|
||||
_ => panic!("Expected a binary expression with 2 identifiers"),
|
||||
},
|
||||
Err(err) => {
|
||||
panic!("{:?}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_parse_unfinished_comparison() {
|
||||
let tokens = get_tokens(&String::from("a >=")).unwrap();
|
||||
let result = try_parse(&tokens, 0);
|
||||
|
||||
match result {
|
||||
Err(ParsingError::Unmatched) => assert!(true),
|
||||
_ => panic!("Expected an Unmatched error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,3 +42,45 @@ fn parse_many<'a>(
|
||||
_ => Ok((prev_expr, pos)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::lexic::get_tokens;
|
||||
|
||||
#[test]
|
||||
fn should_parse_comparison() {
|
||||
let tokens = get_tokens(&String::from("a == b")).unwrap();
|
||||
let result = try_parse(&tokens, 0);
|
||||
|
||||
match result {
|
||||
Ok((expr, _)) => match expr {
|
||||
Expression::BinaryOperator(exp1, exp2, op) => {
|
||||
match (*exp1, *exp2) {
|
||||
(Expression::Identifier(id1), Expression::Identifier(id2)) => {
|
||||
assert_eq!("a", id1);
|
||||
assert_eq!("b", id2);
|
||||
}
|
||||
_ => panic!("Expected 2 identifiers"),
|
||||
}
|
||||
assert_eq!("==", op)
|
||||
}
|
||||
_ => panic!("Expected a binary expression with 2 identifiers"),
|
||||
},
|
||||
Err(err) => {
|
||||
panic!("{:?}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_parse_unfinished_comparison() {
|
||||
let tokens = get_tokens(&String::from("a ==")).unwrap();
|
||||
let result = try_parse(&tokens, 0);
|
||||
|
||||
match result {
|
||||
Err(ParsingError::Unmatched) => assert!(true),
|
||||
_ => panic!("Expected an Unmatched error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,3 +42,47 @@ fn parse_many<'a>(
|
||||
_ => Ok((prev_expr, pos)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::lexic::get_tokens;
|
||||
|
||||
#[test]
|
||||
fn should_parse_comparison() {
|
||||
let tokens = get_tokens(&String::from("a * b")).unwrap();
|
||||
let result = try_parse(&tokens, 0);
|
||||
|
||||
match result {
|
||||
Ok((expr, _)) => match expr {
|
||||
Expression::BinaryOperator(exp1, exp2, op) => {
|
||||
match (*exp1, *exp2) {
|
||||
(Expression::Identifier(id1), Expression::Identifier(id2)) => {
|
||||
assert_eq!("a", id1);
|
||||
assert_eq!("b", id2);
|
||||
}
|
||||
_ => panic!("Expected 2 identifiers"),
|
||||
}
|
||||
assert_eq!("*", op)
|
||||
}
|
||||
_ => panic!("Expected a binary expression with 2 identifiers"),
|
||||
},
|
||||
Err(err) => {
|
||||
panic!("{:?}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_parse_unfinished_comparison() {
|
||||
let tokens = get_tokens(&String::from("a /")).unwrap();
|
||||
let result = try_parse(&tokens, 0);
|
||||
|
||||
match result {
|
||||
Err(ParsingError::Unmatched) => assert!(true),
|
||||
_ => panic!("Expected an Unmatched error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,3 +42,45 @@ fn parse_many<'a>(
|
||||
_ => Ok((prev_expr, pos)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::lexic::get_tokens;
|
||||
|
||||
#[test]
|
||||
fn should_parse_comparison() {
|
||||
let tokens = get_tokens(&String::from("a + b")).unwrap();
|
||||
let result = try_parse(&tokens, 0);
|
||||
|
||||
match result {
|
||||
Ok((expr, _)) => match expr {
|
||||
Expression::BinaryOperator(exp1, exp2, op) => {
|
||||
match (*exp1, *exp2) {
|
||||
(Expression::Identifier(id1), Expression::Identifier(id2)) => {
|
||||
assert_eq!("a", id1);
|
||||
assert_eq!("b", id2);
|
||||
}
|
||||
_ => panic!("Expected 2 identifiers"),
|
||||
}
|
||||
assert_eq!("+", op)
|
||||
}
|
||||
_ => panic!("Expected a binary expression with 2 identifiers"),
|
||||
},
|
||||
Err(err) => {
|
||||
panic!("{:?}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_parse_unfinished_comparison() {
|
||||
let tokens = get_tokens(&String::from("a -")).unwrap();
|
||||
let result = try_parse(&tokens, 0);
|
||||
|
||||
match result {
|
||||
Err(ParsingError::Unmatched) => assert!(true),
|
||||
_ => panic!("Expected an Unmatched error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user