-
Notifications
You must be signed in to change notification settings - Fork 5
Add CoCo to check if constructors in CDs have the same name as their classes. #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TomBursch
merged 12 commits into
MontiCore:dev
from
vanol21:constructors-in-cds-must-not-have-any-name
May 6, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d8cb8cb
add coco
vanol21 ec4d197
add coco
vanol21 34ceced
add tests
vanol21 89f9e03
Merge branch 'dev' into constructors-in-cds-must-not-have-any-name
TomBursch d8035ac
add endPosition
vanol21 4158ba8
Merge remote-tracking branch 'origin/constructors-in-cds-must-not-hav…
vanol21 eda0691
Clear findings after each test
vanol21 2f9c691
spotless
vanol21 70553a0
spotless
vanol21 76f13e5
Revert changes
vanol21 32a6daa
solve spotless
vanol21 8deadaf
solve spotless
vanol21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
cdlang/src/main/java/de/monticore/cdbasis/cocos/ConstructorNameEqualsClassNameCoCo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* (c) https://github.com/MontiCore/monticore */ | ||
| package de.monticore.cdbasis.cocos; | ||
|
|
||
| import de.monticore.cd4codebasis._ast.ASTCDConstructor; | ||
| import de.monticore.cdbasis._ast.ASTCDClass; | ||
| import de.monticore.cdbasis._cocos.CDBasisASTCDClassCoCo; | ||
| import de.se_rwth.commons.logging.Log; | ||
|
|
||
| /** | ||
| * Validates that all constructors of a CD class have the same name as the class. | ||
| */ | ||
| public class ConstructorNameEqualsClassNameCoCo implements CDBasisASTCDClassCoCo { | ||
|
|
||
| public static final String ERROR_CODE = "0xCDC50"; | ||
| public static final String ERROR_MESSAGE = ERROR_CODE | ||
| + ": Invalid constructor name '%s' in class '%s'. " | ||
| + "Constructors must have the same name as their class."; | ||
|
|
||
| @Override | ||
| public void check(ASTCDClass node) { | ||
| String className = node.getName(); | ||
|
|
||
| for (ASTCDConstructor constructor : node.getCDConstructorList()) { | ||
| if (!constructor.getName().equals(className)) { | ||
| Log.error(String.format(ERROR_MESSAGE, constructor.getName(), className), constructor | ||
| .get_SourcePositionStart(), constructor.get_SourcePositionEnd()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
53 changes: 53 additions & 0 deletions
53
...test/java/de/monticore/testcd4codebasis/cocos/ConstructorNameEqualsClassNameCoCoTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* (c) https://github.com/MontiCore/monticore */ | ||
| package de.monticore.testcd4codebasis.cocos; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import de.monticore.cdbasis._ast.ASTCDCompilationUnit; | ||
| import de.monticore.cdbasis.cocos.ConstructorNameEqualsClassNameCoCo; | ||
| import de.monticore.testcd4codebasis.CD4CodeBasisTestBasis; | ||
| import de.se_rwth.commons.logging.Log; | ||
| import java.io.IOException; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ConstructorNameEqualsClassNameCoCoTest extends CD4CodeBasisTestBasis { | ||
|
|
||
| @AfterEach | ||
| public void after() { | ||
| Log.getFindings().clear(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValid() throws IOException { | ||
| // Given | ||
| coCoChecker.addCoCo(new ConstructorNameEqualsClassNameCoCo()); | ||
|
|
||
| // When | ||
| final ASTCDCompilationUnit ast = p.parse(getFilePath( | ||
| "cd4codebasis/cocos/ConstructorNameEqualsClassNameValid.cd")).orElseThrow(); | ||
| coCoChecker.checkAll(ast); | ||
|
|
||
| // Then | ||
| assertTrue(Log.getFindings().isEmpty(), "Valid constructors should not produce errors"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalid() throws IOException { | ||
|
TomBursch marked this conversation as resolved.
|
||
| // Given | ||
| coCoChecker.addCoCo(new ConstructorNameEqualsClassNameCoCo()); | ||
|
|
||
| // When | ||
| final ASTCDCompilationUnit ast = p.parse(getFilePath( | ||
| "cd4codebasis/cocos/ConstructorNameEqualsClassNameInvalid.cd")).orElseThrow(); | ||
| coCoChecker.checkAll(ast); | ||
|
|
||
| // Then | ||
| assertEquals(2, Log.getFindings().size(), "Invalid constructors should produce 2 errors"); | ||
| assertTrue(Log.getFindings().get(0).getMsg().startsWith("0xCDC50")); | ||
| assertTrue(Log.getFindings().get(1).getMsg().startsWith("0xCDC50")); | ||
| } | ||
|
|
||
| } | ||
15 changes: 15 additions & 0 deletions
15
...c/test/resources/de/monticore/cd4codebasis/cocos/ConstructorNameEqualsClassNameInvalid.cd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* (c) https://github.com/MontiCore/monticore */ | ||
|
|
||
| classdiagram ConstructorNameEqualsClassNameInvalid { | ||
|
|
||
| class Payment { | ||
| public pay(); | ||
| public Payment(int amount); | ||
| } | ||
|
|
||
| class Order { | ||
| public Order(); | ||
| public create(); | ||
| } | ||
|
|
||
| } |
17 changes: 17 additions & 0 deletions
17
...src/test/resources/de/monticore/cd4codebasis/cocos/ConstructorNameEqualsClassNameValid.cd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* (c) https://github.com/MontiCore/monticore */ | ||
|
|
||
| classdiagram ConstructorNameEqualsClassNameValid { | ||
|
|
||
| class Payment { | ||
| public Payment(); | ||
| public Payment(int amount); | ||
| public void process(); | ||
| } | ||
|
|
||
| class Handler { | ||
| public void execute(); | ||
| } | ||
|
|
||
| class Delivery { } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.