Skip to content

Commit

Permalink
tests for parser
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaris13 committed Dec 10, 2024
1 parent 0c3fb88 commit 272e3d2
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 1 deletion.
84 changes: 83 additions & 1 deletion src/tests/expression_literal_value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod tests {
use crate::expression_literal_value::LiteralValue;
use crate::token::{Token, TokenType};

use std::rc::Rc;

#[test]
fn literal_value_to_string() {
let literals = vec![
Expand All @@ -27,6 +29,81 @@ mod tests {
assert_eq!(result, responses);
}

#[test]
fn test_debug_output() {
let test_cases = vec![
(LiteralValue::IntValue(42), "42"),
(LiteralValue::FValue(3.14), "3.14"),
(LiteralValue::StringValue("hello".to_string()), "\"hello\""),
(LiteralValue::True, "true"),
(LiteralValue::False, "false"),
(LiteralValue::Nil, "nil"),
(
LiteralValue::Callable {
name: "my_func".to_string(),
arity: 2,
fun: Rc::new(|_, _| Ok(LiteralValue::Nil)),
},
"Callable { name: my_func, arity: 2 }",
),
];

for (input, expected) in test_cases {
assert_eq!(format!("{:?}", input), expected);
}
}

#[test]
fn test_partial_eq() {
let test_cases = vec![
(LiteralValue::IntValue(42), LiteralValue::IntValue(42), true),
(LiteralValue::FValue(3.14), LiteralValue::FValue(3.14), true),
(
LiteralValue::FValue(3.14),
LiteralValue::FValue(3.14159),
false,
),
(
LiteralValue::StringValue("hello".to_string()),
LiteralValue::StringValue("hello".to_string()),
true,
),
(LiteralValue::True, LiteralValue::True, true),
(LiteralValue::False, LiteralValue::False, true),
(LiteralValue::Nil, LiteralValue::Nil, true),
(
LiteralValue::Callable {
name: "my_func".to_string(),
arity: 2,
fun: Rc::new(|_, _| Ok(LiteralValue::Nil)),
},
LiteralValue::Callable {
name: "my_func".to_string(),
arity: 2,
fun: Rc::new(|_, _| Ok(LiteralValue::Nil)),
},
true,
),
(
LiteralValue::Callable {
name: "my_func".to_string(),
arity: 2,
fun: Rc::new(|_, _| Ok(LiteralValue::Nil)),
},
LiteralValue::Callable {
name: "other_func".to_string(),
arity: 2,
fun: Rc::new(|_, _| Ok(LiteralValue::Nil)),
},
false,
),
];

for (a, b, expected) in test_cases {
assert_eq!(a == b, expected);
}
}

#[test]
fn literal_value_to_type() {
let literals = vec![
Expand All @@ -36,9 +113,14 @@ mod tests {
LiteralValue::IntValue(12),
LiteralValue::StringValue(String::from("Hello")),
LiteralValue::FValue(1.1),
LiteralValue::Callable {
name: "other_func".to_string(),
arity: 2,
fun: Rc::new(|_, _| Ok(LiteralValue::Nil)),
},
];

let responses = vec!["Nil", "Bool", "Bool", "Int", "String", "Float"];
let responses = vec!["Nil", "Bool", "Bool", "Int", "String", "Float", "Callable"];

let result = literals
.iter()
Expand Down
42 changes: 42 additions & 0 deletions src/tests/interpreter_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,46 @@ mod tests {
Ok(LiteralValue::IntValue(20))
);
}

#[test]
fn expression_with_function_statement__test() {
let source = "
var a=0;
fun addOne(a) {
a = a + 1;
}
var b = addOne(a);
var c = clock();
";
let mut scanner = Scanner::new(source);
let tokens = scanner.scan_tokens().unwrap();

let mut parser = Parser::new(tokens);
let statements = parser.parse().unwrap();

let mut interpreter: Interpreter = Interpreter::new();
let variable_count = interpreter.environment.borrow().values.len();
let result = interpreter.interpret_statements(statements);

assert!(result.is_ok());

assert_eq!(
interpreter.environment.borrow().values.len(),
variable_count + 4
);
assert_eq!(
interpreter.environment.borrow().get("a"),
Ok(LiteralValue::IntValue(0))
);
assert_eq!(
interpreter.environment.borrow().get("b"),
Ok(LiteralValue::Nil)
);
assert_ne!(
interpreter.environment.borrow().get("c"),
Ok(LiteralValue::Nil)
);
}
}
56 changes: 56 additions & 0 deletions src/tests/parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,62 @@ mod tests {
assert_eq!(string_expression[0], response);
}

// #[test]
// fn test_call_operator_limits() {
// let source = "test(
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
// 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
// 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
// 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
// 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
// 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
// 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
// 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
// 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
// 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
// 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
// 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
// 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
// 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
// 141, 142, 143, 144, 145, 146, 147, 148, 149, 150,
// 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
// 161, 162, 163, 164, 165, 166, 167, 168, 169, 170,
// 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
// 181, 182, 183, 184, 185, 186, 187, 188, 189, 190,
// 191, 192, 193, 194, 195, 196, 197, 198, 199, 200,
// 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
// 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
// 221, 222, 223, 224, 225, 226, 227, 228, 229, 230,
// 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
// 241, 242, 243, 244, 245, 246, 247, 248, 249, 250,
// 251, 252, 253, 254, 255, 256
// );";
// let mut scanner: Scanner = Scanner::new(source);

// let tokens = scanner.scan_tokens().unwrap();

// let mut parser = Parser::new(tokens);

// let expression = parser.parse();
// let response = Return {
// keyword: Token {
// token_type: TokenReturn,
// lexeme: String::from("return"),
// literal: None,
// line: 1,
// },
// value: Some(Literal {
// value: ExpressionLiteralValue::IntValue(12),
// }),
// };

// assert!(expression.is_ok());

// let string_expression = expression.unwrap();
// assert_eq!(string_expression.len(), 1);
// assert_eq!(string_expression[0], response);
// }

#[test]
fn test_blocks() {
let source = "{a=1;}";
Expand Down

0 comments on commit 272e3d2

Please sign in to comment.