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 specifying perspective values.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Nov 19, 2023
1 parent 680e616 commit a3dc82f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fixes https://github.com/structurizr/dsl/issues/364 (.DS_Store file causes exception during !include <directory> on Windows).
- Adds a `getDslParser()` method to the `StructurizrDslPluginContext` class (https://github.com/structurizr/dsl/issues/361).
- Adds the ability to specify the workspace scope via a `scope` keyword inside the workspace `configuration`.
- Adds support for specifying perspective values.
- Updates structurizr/java to [v1.28.0](https://github.com/structurizr/java/releases/tag/v1.28.0).

## 1.33.0 (27th October 2023)
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/com/structurizr/dsl/ModelItemParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class ModelItemParser extends AbstractParser {

private final static int PERSPECTIVE_NAME_INDEX = 0;
private final static int PERSPECTIVE_DESCRIPTION_INDEX = 1;
private final static int PERSPECTIVE_VALUE_INDEX = 2;

void parseTags(ModelItemDslContext context, Tokens tokens) {
// tags <tags> [tags]
Expand Down Expand Up @@ -54,20 +55,25 @@ void parseUrl(ModelItemDslContext context, Tokens tokens) {
}

void parsePerspective(ModelItemPerspectivesDslContext context, Tokens tokens) {
// <name> <description>
// <name> <description> [value]

if (tokens.hasMoreThan(PERSPECTIVE_DESCRIPTION_INDEX)) {
throw new RuntimeException("Too many tokens, expected: <name> <description>");
if (tokens.hasMoreThan(PERSPECTIVE_VALUE_INDEX)) {
throw new RuntimeException("Too many tokens, expected: <name> <description> [value]");
}

if (tokens.size() != 2) {
throw new RuntimeException("Expected: <name> <description>");
if (!tokens.includes(PERSPECTIVE_DESCRIPTION_INDEX)) {
throw new RuntimeException("Expected: <name> <description> [value]");
}

String name = tokens.get(PERSPECTIVE_NAME_INDEX);
String value = tokens.get(PERSPECTIVE_DESCRIPTION_INDEX);
String description = tokens.get(PERSPECTIVE_DESCRIPTION_INDEX);
String value = "";

context.getModelItem().addPerspective(name, value);
if (tokens.includes(PERSPECTIVE_VALUE_INDEX)) {
value = tokens.get(PERSPECTIVE_VALUE_INDEX);
}

context.getModelItem().addPerspective(name, description, value);
}

}
1 change: 1 addition & 0 deletions src/test/dsl/test.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ workspace "Name" "Description" {
}
perspectives {
"Security" "A description..."
"Technical Debt" "Tech debt is high due to delivering feature X rapidly." "High"
}
}

Expand Down
36 changes: 31 additions & 5 deletions src/test/java/com/structurizr/dsl/ModelItemParserTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.structurizr.dsl;

import com.structurizr.model.Perspective;
import com.structurizr.model.SoftwareSystem;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -108,10 +109,22 @@ void test_parseUrl_SetsTheUrl_WhenAUrlIsSpecified() {
void test_parsePerspective_ThrowsAnException_WhenThereAreTooManyTokens() {
try {
ModelItemPerspectivesDslContext context = new ModelItemPerspectivesDslContext(null);
parser.parsePerspective(context, tokens("name", "description", "extra"));
parser.parsePerspective(context, tokens("name", "description", "value", "extra"));
fail();
} catch (Exception e) {
assertEquals("Too many tokens, expected: <name> <description>", e.getMessage());
assertEquals("Too many tokens, expected: <name> <description> [value]", e.getMessage());
}
}

@Test
void test_parsePerspective_ThrowsAnException_WhenNoNameIsSpecified() {
try {
SoftwareSystem softwareSystem = model.addSoftwareSystem("Name", "Description");
ModelItemPerspectivesDslContext context = new ModelItemPerspectivesDslContext(softwareSystem);
parser.parsePerspective(context, tokens());
fail();
} catch (Exception e) {
assertEquals("Expected: <name> <description> [value]", e.getMessage());
}
}

Expand All @@ -123,17 +136,30 @@ void test_parsePerspective_ThrowsAnException_WhenNoDescriptionIsSpecified() {
parser.parsePerspective(context, tokens("name"));
fail();
} catch (Exception e) {
assertEquals("Expected: <name> <description>", e.getMessage());
assertEquals("Expected: <name> <description> [value]", e.getMessage());
}
}

@Test
void test_parsePerspective_AddsThePerspective_WhenAValueIsSpecified() {
void test_parsePerspective_AddsThePerspective_WhenADescriptionIsSpecified() {
SoftwareSystem softwareSystem = model.addSoftwareSystem("Name", "Description");
ModelItemPerspectivesDslContext context = new ModelItemPerspectivesDslContext(softwareSystem);
parser.parsePerspective(context, tokens("Security", "Description"));

assertEquals("Description", softwareSystem.getPerspectives().stream().filter(p -> p.getName().equals("Security")).findFirst().get().getDescription());
Perspective perspective = softwareSystem.getPerspectives().stream().filter(p -> p.getName().equals("Security")).findFirst().get();
assertEquals("Description", perspective.getDescription());
assertEquals("", perspective.getValue());
}

@Test
void test_parsePerspective_AddsThePerspective_WhenADescriptionAndValueIsSpecified() {
SoftwareSystem softwareSystem = model.addSoftwareSystem("Name", "Description");
ModelItemPerspectivesDslContext context = new ModelItemPerspectivesDslContext(softwareSystem);
parser.parsePerspective(context, tokens("Security", "Description", "Value"));

Perspective perspective = softwareSystem.getPerspectives().stream().filter(p -> p.getName().equals("Security")).findFirst().get();
assertEquals("Description", perspective.getDescription());
assertEquals("Value", perspective.getValue());
}

}

0 comments on commit a3dc82f

Please sign in to comment.