/*
* 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.grammar;
import ch.dbalabs.intuitivedsl.exception.DslDefinitionException;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Unit tests for the GrammarCompiler component.
* Verifies AST generation, structural constraints, and keyword merging.
*
* @author DBA Labs
*/
class GrammarCompilerTest {
@Test
void shouldCompileBasicSyntaxIntoAST() {
GrammarCompiler compiler = new GrammarCompiler();
String syntax = "CREATE USER username [PASSWORD password] ;";
GroupNode root = compiler.compile(syntax);
assertThat(root.type()).isEqualTo(GroupType.REQUIRED);
AlternativeNode mainBranch = (AlternativeNode) root.children().get(0);
// CREATE and USER are merged into "CREATE USER" -> 4 nodes total
assertThat(mainBranch.children()).hasSize(4);
assertThat(mainBranch.children().get(0)).isInstanceOf(KeywordNode.class)
.extracting("word").isEqualTo("CREATE USER");
assertThat(mainBranch.children().get(1)).isInstanceOf(ParameterNode.class)
.extracting("name").isEqualTo("username");
assertThat(mainBranch.children().get(2)).isInstanceOf(GroupNode.class);
assertThat(mainBranch.children().get(3)).isInstanceOf(DelimiterNode.class)
.extracting("value").isEqualTo(";");
}
@Test
void shouldHandleDynamicMacros() {
GrammarCompiler compiler = new GrammarCompiler();
GroupNode root = compiler.compile("ALLOW ${roles} ;");
AlternativeNode alt = (AlternativeNode) root.children().get(0);
assertThat(alt.children().get(1)).isInstanceOf(MacroNode.class)
.extracting("macroName").isEqualTo("roles");
}
@Test
void shouldThrowExceptionOnEmptyBranch() {
GrammarCompiler compiler = new GrammarCompiler();
assertThatThrownBy(() -> compiler.compile("{ | A }"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("Empty alternative branch");
}
@Test
void shouldCompileRepeatableParameter() {
GrammarCompiler compiler = new GrammarCompiler();
String syntax = "GRANT ACCESS TO target_user ... ;";
GroupNode root = compiler.compile(syntax);
AlternativeNode mainBranch = (AlternativeNode) root.children().get(0);
// GRANT, ACCESS, TO are merged -> "GRANT ACCESS TO", target_user, ;
assertThat(mainBranch.children()).hasSize(3);
assertThat(mainBranch.children().get(0)).isInstanceOf(KeywordNode.class)
.extracting("word").isEqualTo("GRANT ACCESS TO");
SyntaxNode paramNode = mainBranch.children().get(1);
assertThat(paramNode).isInstanceOf(ParameterNode.class);
assertThat(((ParameterNode) paramNode).name()).isEqualTo("target_user");
assertThat(paramNode.isRepeatable()).isTrue();
boolean hasParasiticDot = mainBranch.children().stream()
.anyMatch(node -> node instanceof DelimiterNode d && d.value().equals("."));
assertThat(hasParasiticDot)
.as("The AST should not contain parasitic dot delimiters from the ellipsis operator.")
.isFalse();
}
@Test
void shouldCompileRepeatableRequiredGroup() {
GrammarCompiler compiler = new GrammarCompiler();
String syntax = "BLOCK TRAFFIC FROM { IP ip_address | SUBNET subnet_mask } ... ;";
GroupNode root = compiler.compile(syntax);
AlternativeNode mainBranch = (AlternativeNode) root.children().get(0);
// BLOCK, TRAFFIC, FROM are merged -> "BLOCK TRAFFIC FROM", GroupNode, ;
assertThat(mainBranch.children()).hasSize(3);
assertThat(mainBranch.children().get(0)).isInstanceOf(KeywordNode.class)
.extracting("word").isEqualTo("BLOCK TRAFFIC FROM");
SyntaxNode groupNode = mainBranch.children().get(1);
assertThat(groupNode).isInstanceOf(GroupNode.class);
assertThat(((GroupNode) groupNode).type()).isEqualTo(GroupType.REQUIRED);
assertThat(groupNode.isRepeatable()).isTrue();
}
@Test
void shouldCompileRepeatableOptionalGroup() {
GrammarCompiler compiler = new GrammarCompiler();
String syntax = "GENERATE REPORT [ WITH metric_name ] ... ;";
GroupNode root = compiler.compile(syntax);
AlternativeNode mainBranch = (AlternativeNode) root.children().get(0);
// GENERATE, REPORT are merged -> "GENERATE REPORT", GroupNode, ;
assertThat(mainBranch.children()).hasSize(3);
assertThat(mainBranch.children().get(0)).isInstanceOf(KeywordNode.class)
.extracting("word").isEqualTo("GENERATE REPORT");
SyntaxNode groupNode = mainBranch.children().get(1);
assertThat(groupNode).isInstanceOf(GroupNode.class);
assertThat(((GroupNode) groupNode).type()).isEqualTo(GroupType.OPTIONAL);
assertThat(groupNode.isRepeatable()).isTrue();
}
}