From a3dc82f5f9f58683d6d9c456f92077dfa2cf74e3 Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Sun, 19 Nov 2023 13:07:39 +0000 Subject: [PATCH] Adds support for specifying perspective values. --- docs/changelog.md | 1 + .../com/structurizr/dsl/ModelItemParser.java | 20 +++++++---- src/test/dsl/test.dsl | 1 + .../structurizr/dsl/ModelItemParserTests.java | 36 ++++++++++++++++--- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 7f4513c..0a73027 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -5,6 +5,7 @@ - Fixes https://github.com/structurizr/dsl/issues/364 (.DS_Store file causes exception during !include 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) diff --git a/src/main/java/com/structurizr/dsl/ModelItemParser.java b/src/main/java/com/structurizr/dsl/ModelItemParser.java index 64be47f..823728b 100644 --- a/src/main/java/com/structurizr/dsl/ModelItemParser.java +++ b/src/main/java/com/structurizr/dsl/ModelItemParser.java @@ -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] @@ -54,20 +55,25 @@ void parseUrl(ModelItemDslContext context, Tokens tokens) { } void parsePerspective(ModelItemPerspectivesDslContext context, Tokens tokens) { - // + // [value] - if (tokens.hasMoreThan(PERSPECTIVE_DESCRIPTION_INDEX)) { - throw new RuntimeException("Too many tokens, expected: "); + if (tokens.hasMoreThan(PERSPECTIVE_VALUE_INDEX)) { + throw new RuntimeException("Too many tokens, expected: [value]"); } - if (tokens.size() != 2) { - throw new RuntimeException("Expected: "); + if (!tokens.includes(PERSPECTIVE_DESCRIPTION_INDEX)) { + throw new RuntimeException("Expected: [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); } } \ No newline at end of file diff --git a/src/test/dsl/test.dsl b/src/test/dsl/test.dsl index e18d34a..4dcd877 100644 --- a/src/test/dsl/test.dsl +++ b/src/test/dsl/test.dsl @@ -35,6 +35,7 @@ workspace "Name" "Description" { } perspectives { "Security" "A description..." + "Technical Debt" "Tech debt is high due to delivering feature X rapidly." "High" } } diff --git a/src/test/java/com/structurizr/dsl/ModelItemParserTests.java b/src/test/java/com/structurizr/dsl/ModelItemParserTests.java index d7f9d21..379ce8e 100644 --- a/src/test/java/com/structurizr/dsl/ModelItemParserTests.java +++ b/src/test/java/com/structurizr/dsl/ModelItemParserTests.java @@ -1,5 +1,6 @@ package com.structurizr.dsl; +import com.structurizr.model.Perspective; import com.structurizr.model.SoftwareSystem; import org.junit.jupiter.api.Test; @@ -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: ", e.getMessage()); + assertEquals("Too many tokens, expected: [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: [value]", e.getMessage()); } } @@ -123,17 +136,30 @@ void test_parsePerspective_ThrowsAnException_WhenNoDescriptionIsSpecified() { parser.parsePerspective(context, tokens("name")); fail(); } catch (Exception e) { - assertEquals("Expected: ", e.getMessage()); + assertEquals("Expected: [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()); } } \ No newline at end of file