Skip to content

Commit 827f92c

Browse files
committed
Improve guard not boolean error msg, formatting, and more spcific const names
1 parent 89fe6d0 commit 827f92c

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/main/java/de/monticore/sctransitions4code/_cocos/TransitionPreconditionsAreBoolean.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99
import de.monticore.types3.TypeCheck3;
1010
import de.se_rwth.commons.logging.Log;
1111

12+
import static de.monticore.symbols.basicsymbols.BasicSymbolsMill.BOOLEAN;
13+
1214
/**
1315
* Checks that the preconditions of transitions evaluate to boolean values.
1416
*/
1517
public class TransitionPreconditionsAreBoolean implements SCTransitions4CodeASTTransitionBodyCoCo {
1618

17-
public static final String ERROR_CODE = "0xCC111";
19+
public static final String GUARD_NOT_BOOLEAN_ERROR_CODE = "0xCC111";
20+
21+
public static final String GUARD_NOT_BOOLEAN_ERROR_MSG = "Expected '%s' but provided '%s'.";
1822

19-
protected static final String MESSAGE = "Guard expressions must be boolean. Your guard expression is of type '%s'.";
23+
@Deprecated
24+
public static final String ERROR_CODE = GUARD_NOT_BOOLEAN_ERROR_CODE;
25+
26+
@Deprecated
27+
protected static final String MESSAGE = GUARD_NOT_BOOLEAN_ERROR_MSG;
2028

2129
/**
2230
* Used to extract the type to which the transition precondition evaluates to.
@@ -36,14 +44,18 @@ public TransitionPreconditionsAreBoolean(IDerive typeDeriver) {
3644

3745
@Override
3846
public void check(ASTTransitionBody node) {
39-
if(node.isPresentPre()) {
47+
if (node.isPresentPre()) {
4048
SymTypeExpression preType = TypeCheck3.typeOf(node.getPre());
4149
if (preType.isObscureType()) {
42-
Log.debug(String.format("Coco '%s' is not checked on transition guard expression at %s, because the " +
43-
"expression is malformed.", this.getClass().getSimpleName(), node.get_SourcePositionStart()), "Cocos");
44-
} else if(!SymTypeRelations.isBoolean(preType)) {
50+
Log.debug(() -> String.format(
51+
"Coco '%s' skipped for transition guard at %s. The type is obscure, an error should already have been logged.",
52+
this.getClass().getSimpleName(),
53+
node.get_SourcePositionStart()),
54+
"Cocos"
55+
);
56+
} else if (!SymTypeRelations.isBoolean(preType)) {
4557
Log.error(
46-
String.format(ERROR_CODE + " " + MESSAGE, preType.print()),
58+
String.format(ERROR_CODE + " " + GUARD_NOT_BOOLEAN_ERROR_MSG, BOOLEAN, preType.print()),
4759
node.get_SourcePositionStart(), node.get_SourcePositionEnd()
4860
);
4961
}

src/test/java/de/monticore/cocos/TransitionPreconditionsAreBooleanTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import java.util.stream.Collectors;
2121

22+
import static de.monticore.sctransitions4code._cocos.TransitionPreconditionsAreBoolean.GUARD_NOT_BOOLEAN_ERROR_CODE;
2223
import static org.junit.jupiter.api.Assertions.assertEquals;
2324

2425
public class TransitionPreconditionsAreBooleanTest extends GeneralAbstractTest {
@@ -75,11 +76,11 @@ public void testCocoInvalidNotBoolean() throws IOException {
7576
// Then
7677
List<String> findings = Log.getFindings().stream()
7778
.filter(Finding::isError)
78-
.map(finding -> finding.getMsg().substring(0, TransitionPreconditionsAreBoolean.ERROR_CODE.length()))
79+
.map(finding -> finding.getMsg().substring(0, GUARD_NOT_BOOLEAN_ERROR_CODE.length()))
7980
.collect(Collectors.toList());
8081

8182
assertEquals(
82-
Lists.newArrayList(TransitionPreconditionsAreBoolean.ERROR_CODE, TransitionPreconditionsAreBoolean.ERROR_CODE),
83+
Lists.newArrayList(GUARD_NOT_BOOLEAN_ERROR_CODE, GUARD_NOT_BOOLEAN_ERROR_CODE),
8384
findings);
8485
}
8586

0 commit comments

Comments
 (0)