Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Adds support for - characters in identifiers (see #341).
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Sep 28, 2023
1 parent 6195278 commit b43ac61
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/structurizr/dsl/IdentifiersRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class IdentifiersRegister {

private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("\\w+");
private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("\\w[a-zA-Z0-9_-]*");

private IdentifierScope identifierScope = IdentifierScope.Flat;

Expand Down Expand Up @@ -180,8 +180,12 @@ public String findIdentifier(Relationship relationship) {
}

void validateIdentifierName(String identifier) {
if (identifier.startsWith("-")) {
throw new RuntimeException("Identifiers cannot start with a - character");
}

if (!IDENTIFIER_PATTERN.matcher(identifier).matches()) {
throw new RuntimeException("Identifiers can only contain the following characters: a-zA-Z_0-9");
throw new RuntimeException("Identifiers can only contain the following characters: a-zA-Z0-9_-");
}
}

Expand Down
28 changes: 27 additions & 1 deletion src/test/java/com/structurizr/dsl/IdentifierRegisterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,33 @@

class IdentifierRegisterTests extends AbstractTests {

private IdentifiersRegister register = new IdentifiersRegister();
private final IdentifiersRegister register = new IdentifiersRegister();

@Test
void test_validateIdentifierName() {
new IdentifiersRegister().validateIdentifierName("a");
new IdentifiersRegister().validateIdentifierName("abc");
new IdentifiersRegister().validateIdentifierName("ABC");
new IdentifiersRegister().validateIdentifierName("softwaresystem");
new IdentifiersRegister().validateIdentifierName("SoftwareSystem");
new IdentifiersRegister().validateIdentifierName("123456");
new IdentifiersRegister().validateIdentifierName("_softwareSystem");
new IdentifiersRegister().validateIdentifierName("SoftwareSystem-1");

try {
new IdentifiersRegister().validateIdentifierName("-softwareSystem");
fail();
} catch (Exception e) {
assertEquals("Identifiers cannot start with a - character", e.getMessage());
}

try {
new IdentifiersRegister().validateIdentifierName("SoftwareSystém");
fail();
} catch (Exception e) {
assertEquals("Identifiers can only contain the following characters: a-zA-Z0-9_-", e.getMessage());
}
}

@Test
void test_register_ThrowsAnException_WhenTheElementHasAlreadyBeenRegisteredWithADifferentIdentifier() {
Expand Down

0 comments on commit b43ac61

Please sign in to comment.