/*
* 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;
class LexerHardeningTest {
private final Lexer lexer = new Lexer();
@Test
void shouldTokenizeEllipsisAsSingleDelimiter() {
List<Token> tokens = lexer.tokenize("GRANT ACCESS TO alice ... ;");
assertThat(tokens)
.extracting(Token::value, Token::type)
.containsExactly(
org.assertj.core.groups.Tuple.tuple("GRANT", TokenType.WORD),
org.assertj.core.groups.Tuple.tuple("ACCESS", TokenType.WORD),
org.assertj.core.groups.Tuple.tuple("TO", TokenType.WORD),
org.assertj.core.groups.Tuple.tuple("alice", TokenType.WORD),
org.assertj.core.groups.Tuple.tuple("...", TokenType.DELIMITER),
org.assertj.core.groups.Tuple.tuple(";", TokenType.DELIMITER),
org.assertj.core.groups.Tuple.tuple("EOF", TokenType.EOF)
);
}
@Test
void shouldBindQuotedStringContainingGrammarDelimitersAsSingleStringToken() {
List<Token> tokens = lexer.tokenize("LOG MESSAGE 'error: (db) [critical] ; retry?' ;");
assertThat(tokens).hasSize(5);
assertThat(tokens.get(2).type()).isEqualTo(TokenType.STRING);
assertThat(tokens.get(2).value()).isEqualTo("error: (db) [critical] ; retry?");
}
@Test
void shouldHandleEmptyQuotedString() {
List<Token> tokens = lexer.tokenize("SET DESCRIPTION '' ;");
assertThat(tokens.get(2).type()).isEqualTo(TokenType.STRING);
assertThat(tokens.get(2).value()).isEmpty();
}
@Test
void shouldHandleEscapedQuotesAtEndOfQuotedString() {
List<Token> tokens = lexer.tokenize("SET TITLE 'abc''' ;");
assertThat(tokens.get(2).type()).isEqualTo(TokenType.STRING);
assertThat(tokens.get(2).value()).isEqualTo("abc'");
}
@Test
void shouldRejectUnclosedStringWithPreciseExpectedToken() {
assertThatThrownBy(() -> lexer.tokenize("SET TITLE 'unterminated ;"))
.isInstanceOf(DslSyntaxException.class)
.satisfies(error -> {
DslSyntaxException ex = (DslSyntaxException) error;
assertThat(ex.getUnexpectedToken()).isNull();
assertThat(ex.getExpectedPossibilities()).containsExactly("closing quote (')");
});
}
@Test
void shouldNotSplitQuotedEllipsisIntoDelimiterTokens() {
List<Token> tokens = lexer.tokenize("SET NOTE 'repeat ... literally' ;");
assertThat(tokens.get(2).type()).isEqualTo(TokenType.STRING);
assertThat(tokens.get(2).value()).isEqualTo("repeat ... literally");
assertThat(tokens.stream().filter(t -> "...".equals(t.value()))).isEmpty();
}
}