/*
* 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.assertThatThrownBy;
class DefinitionContractNegativeCoverageTest {
private final GrammarCompiler compiler = new GrammarCompiler();
@Test
void shouldRejectNestedEmptyAlternativeInsideOptionalGroup() {
assertThatThrownBy(() -> compiler.compile("DO [ { A | | B } ] ;"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("Empty alternative branch");
}
@Test
void shouldRejectNestedEmptyAlternativeInsideRequiredGroup() {
assertThatThrownBy(() -> compiler.compile("DO { A | [ ] } ;"))
.isInstanceOf(DslDefinitionException.class);
}
@Test
void shouldRejectCompletelyEmptyOptionalGroup() {
assertThatThrownBy(() -> compiler.compile("DO [ ] ;"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("Empty optional group");
}
@Test
void shouldRejectCompletelyEmptyRequiredGroup() {
assertThatThrownBy(() -> compiler.compile("DO { } ;"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("Empty required group");
}
@Test
void shouldRejectEmptyMacroNameInModernSyntax() {
assertThatThrownBy(() -> compiler.compile("RUN ${} ;"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("Empty macro name");
}
@Test
void shouldRejectEmptyMacroNameInLegacySyntax() {
assertThatThrownBy(() -> compiler.compile("RUN <> ;"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("Empty legacy macro name");
}
@Test
void shouldRejectDanglingEllipsisWithoutPrecedingNode() {
assertThatThrownBy(() -> compiler.compile("... RUN target ;"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("preceding node");
}
@Test
void shouldRejectRepeatedAlternativeSeparatorAtTopLevel() {
assertThatThrownBy(() -> compiler.compile("DO { A || B } ;"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("Empty alternative branch");
}
@Test
void shouldRejectMismatchedNestedClosersInDeepDefinitions() {
assertThatThrownBy(() -> compiler.compile("DO { A [ B | C } ] ;"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("Mismatched brackets/braces");
}
@Test
void shouldRejectTrailingEmptyAlternativeInDeepDefinitions() {
assertThatThrownBy(() -> compiler.compile("DO { A | { B | } } ;"))
.isInstanceOf(DslDefinitionException.class)
.hasMessageContaining("Empty alternative branch detected at the end");
}
}