From 740c073f6f172e66359b30c6e77ca34795895386 Mon Sep 17 00:00:00 2001 From: Vedat Can Keklik Date: Sat, 7 Dec 2024 21:07:22 +0300 Subject: [PATCH] fix: Test fixes --- lykiadb-server/src/engine/error.rs | 65 ++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/lykiadb-server/src/engine/error.rs b/lykiadb-server/src/engine/error.rs index 1a3cbaa3..28f4a967 100644 --- a/lykiadb-server/src/engine/error.rs +++ b/lykiadb-server/src/engine/error.rs @@ -88,8 +88,8 @@ pub fn report_error(source_name: &str, source: &str, error: ExecutionError, mut } ExecutionError::Parse(ParseError::NoTokens) => { print( - "There is nothing to parse.", - "What about adding some tokens?", + "There is nothing to parse", + "", Span::default(), ); } @@ -167,8 +167,10 @@ pub fn report_error(source_name: &str, source: &str, error: ExecutionError, mut } #[cfg(test)] mod tests { + use core::panic; + use super::*; - use lykiadb_lang::{kw, tokenizer::token::{Keyword, Token, TokenType}, Identifier, Literal}; + use lykiadb_lang::{kw, sym, tokenizer::token::{Keyword, Symbol, Token, TokenType}, Identifier, Literal}; fn capture_error_output(filename: &str, source: &str, error: ExecutionError) -> String { let mut output = Vec::new(); @@ -236,6 +238,55 @@ mod tests { assert!(output.contains("Add a Identifier { dollar: true } token after \"var\".")); } + #[test] + fn test_parser_no_tokens() { + let source = ""; + let error = ExecutionError::Parse(ParseError::NoTokens); + + let output = capture_error_output("test.txt", source, error); + + assert!(output.contains("There is nothing to parse")); + } + + #[test] + fn test_parser_unexpected_token() { + let source = "let x = ;"; + let error = ExecutionError::Parse(ParseError::UnexpectedToken { + token: Token { + tok_type: sym!(Symbol::Semicolon), + lexeme: Some(";".to_string()), + span: Span { + start: 8, + end: 9, + line: 0, + line_end: 0, + }, + literal: None, + }, + }); + + let output = capture_error_output("test.txt", source, error); + assert!(output.contains("Unexpected token")); + assert!(output.contains("Unexpected token ;")); + } + + #[test] + fn test_interpreter_unexpected_statement() { + let source = "break;"; + let error = ExecutionError::Interpret(InterpretError::UnexpectedStatement { + span: Span { + start: 0, + end: 5, + line: 0, + line_end: 0, + }, + }); + + let output = capture_error_output("test.txt", source, error); + assert!(output.contains("Unexpected statement")); + assert!(output.contains("Remove this")); + } + #[test] fn test_parser_invalid_assignment() { let source = "5 = 10"; @@ -261,7 +312,7 @@ mod tests { // Planner Error Tests #[test] fn test_planner_duplicate_object() { - let source = "CREATE TABLE users; CREATE TABLE users;"; + let source = "Select * from users, users;"; let error = ExecutionError::Plan(PlannerError::DuplicateObjectInScope { previous: Identifier::new("users", false), ident: Identifier::new("users", false), @@ -274,10 +325,10 @@ mod tests { #[test] fn test_planner_subquery_not_allowed() { - let source = "SELECT * FROM (SELECT * FROM users) WHERE id IN (SELECT id FROM users)"; + let source = "SELECT * FROM users inner join orders on users.id = (SELECT id FROM users);"; let error = ExecutionError::Plan(PlannerError::SubqueryNotAllowed(Span { - start: 14, - end: 33, + start: 47, + end: 70, line: 0, line_end: 0, }));