From 785768628da465127b50054e46e0e521ef5fbf02 Mon Sep 17 00:00:00 2001 From: Marcus Speight Date: Fri, 4 Oct 2024 09:45:57 +0100 Subject: [PATCH] Allow architecture diagrams to support any character for titles --- .../architecture/architecture.langium | 8 +-- packages/parser/tests/architecture.test.ts | 68 +++++++++++++++++++ packages/parser/tests/test-util.ts | 14 ++++ 3 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 packages/parser/tests/architecture.test.ts diff --git a/packages/parser/src/language/architecture/architecture.langium b/packages/parser/src/language/architecture/architecture.langium index 11af262432..8a0e647017 100644 --- a/packages/parser/src/language/architecture/architecture.langium +++ b/packages/parser/src/language/architecture/architecture.langium @@ -4,11 +4,7 @@ import "../common/common"; entry Architecture: NEWLINE* "architecture-beta" - ( - NEWLINE* TitleAndAccessibilities - | NEWLINE* Statement* - | NEWLINE* - ) + NEWLINE* TitleAndAccessibilities? Statement* ; fragment Statement: @@ -50,6 +46,6 @@ terminal ARROW_DIRECTION: 'L' | 'R' | 'T' | 'B'; terminal ARCH_ID: /[\w]+/; terminal ARCH_TEXT_ICON: /\("[^"]+"\)/; terminal ARCH_ICON: /\([\w-:]+\)/; -terminal ARCH_TITLE: /\[[\w ]+\]/; +terminal ARCH_TITLE: /\[[^\]]+\]/; terminal ARROW_GROUP: /\{group\}/; terminal ARROW_INTO: /<|>/; diff --git a/packages/parser/tests/architecture.test.ts b/packages/parser/tests/architecture.test.ts new file mode 100644 index 0000000000..51d99883bd --- /dev/null +++ b/packages/parser/tests/architecture.test.ts @@ -0,0 +1,68 @@ +import { describe, expect, it } from 'vitest'; + +import { Architecture } from '../src/index.js'; +import { expectNoErrorsOrAlternatives, architectureParse as parse } from './test-util.js'; + +describe('architecture', () => { + it.each([ + `architecture-beta`, + ` architecture-beta `, + `\tarchitecture-beta\t`, + ` + \tarchitecture-beta + `, + ])('should handle regular architecture-beta', (context: string) => { + const result = parse(context); + expectNoErrorsOrAlternatives(result); + expect(result.value.$type).toBe(Architecture); + }); + + it.each([ + `architecture-beta group api(cloud)[API]`, + ` architecture-beta group api(cloud)[API] `, + `\tarchitecture-beta\tgroup api(cloud)[API]\t`, + ` + architecture-beta\tgroup api(cloud)[API] + `, + ])('should handle architecture-beta & group in same line', (context: string) => { + const result = parse(context); + expectNoErrorsOrAlternatives(result); + expect(result.value.$type).toBe(Architecture); + + const { groups } = result.value; + expect(groups[0]).toBeTruthy(); + }); + + it.each([ + `architecture-beta + group api(cloud)[API]`, + `architecture-beta + group api(cloud)[API] + `, + `architecture-beta + group api(cloud)[API]`, + `architecture-beta + group api(cloud)[API] + `, + ])('should handle architecture-beta + group in different line', (context: string) => { + const result = parse(context); + expectNoErrorsOrAlternatives(result); + expect(result.value.$type).toBe(Architecture); + + const { groups } = result.value; + expect(groups[0].title).toBe('API'); + }); + + it.each([ + `architecture-beta group api(cloud)[a.b-t]`, + `architecture-beta group api(cloud)[user:password@some_domain.com] + `, + ])('should handle special character in a title', (context: string) => { + const result = parse(context); + expectNoErrorsOrAlternatives(result); + expect(result.value.$type).toBe(Architecture); + + const { groups } = result.value; + expect(groups[0]).toBeTruthy(); + }); +}); diff --git a/packages/parser/tests/test-util.ts b/packages/parser/tests/test-util.ts index 5cb487758b..d6eefd6f1a 100644 --- a/packages/parser/tests/test-util.ts +++ b/packages/parser/tests/test-util.ts @@ -7,11 +7,14 @@ import type { PieServices, GitGraph, GitGraphServices, + Architecture, + ArchitectureServices, } from '../src/language/index.js'; import { createInfoServices, createPieServices, createGitGraphServices, + createArchitectureServices, } from '../src/language/index.js'; const consoleMock = vi.spyOn(console, 'log').mockImplementation(() => undefined); @@ -62,3 +65,14 @@ export function createGitGraphTestServices() { return { services: gitGraphServices, parse }; } export const gitGraphParse = createGitGraphTestServices().parse; + +const architectureServices: ArchitectureServices = createArchitectureServices().Architecture; +const architectureParser: LangiumParser = architectureServices.parser.LangiumParser; +export function createArchitectureTestServices() { + const parse = (input: string) => { + return architectureParser.parse(input); + }; + + return { services: architectureServices, parse }; +} +export const architectureParse = createArchitectureTestServices().parse;