/*
* This file is part of the Intuitive DSL project.
* Copyright (c) 2026 DBA Labs - Switzerland. All rights reserved.
*
* This program is dual-licensed under a commercial license and the AGPLv3.
* For commercial licensing, contact us at [email protected] or visit https://www.dbalabs.ch.
*
* AGPLv3 licensing:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 (19 November 2007).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/agpl-3.0.html>.
*/
package ch.dbalabs.intuitivedsl.parser;
import ch.dbalabs.intuitivedsl.exception.DslSyntaxException;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Unit tests for the Lexer component.
*
* @author DBA Labs
*/
class LexerTest {
@Test
void shouldTokenizeWordsStringsAndDelimitersProperly() {
// Arrange
Lexer lexer = new Lexer();
String input = "SHOW 'mon test' ;";
// Act
List<Token> tokens = lexer.tokenize(input);
// Assert
// We expect 4 tokens: WORD, STRING, DELIMITER, and the mandatory EOF
assertThat(tokens).hasSize(4);
assertThat(tokens.get(0))
.extracting(Token::value, Token::type)
.containsExactly("SHOW", TokenType.WORD);
// The quotes should be stripped from the string value
assertThat(tokens.get(1))
.extracting(Token::value, Token::type)
.containsExactly("mon test", TokenType.STRING);
assertThat(tokens.get(2))
.extracting(Token::value, Token::type)
.containsExactly(";", TokenType.DELIMITER);
assertThat(tokens.get(3).type()).isEqualTo(TokenType.EOF);
}
@Test
void shouldHandleEscapedQuotesInStringLiterals() {
// Arrange
Lexer lexer = new Lexer();
String input = "INSERT 'O''Reilly' ;";
// Act
List<Token> tokens = lexer.tokenize(input);
// Assert
assertThat(tokens.get(1))
.extracting(Token::value, Token::type)
.containsExactly("O'Reilly", TokenType.STRING);
}
@Test
void shouldThrowExceptionOnUnclosedStringLiteral() {
// Arrange
Lexer lexer = new Lexer();
String input = "SHOW 'unclosed string ;";
// Act & Assert
assertThatThrownBy(() -> lexer.tokenize(input))
.isInstanceOf(DslSyntaxException.class)
.satisfies(e -> {
DslSyntaxException ex = (DslSyntaxException) e;
assertThat(ex.getExpectedPossibilities()).contains("closing quote (')");
});
}
}