Skip to content

Commit

Permalink
Fix #6056 allows all characters except [ and ]
Browse files Browse the repository at this point in the history
Add example in demos/architecture.html
Adds some rudimentary unit tests for architecture parser
  • Loading branch information
mgrubb committed Dec 12, 2024
1 parent df636c6 commit deaf0e4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-carpets-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mermaid-js/parser': patch
---

fix: Allow most characters in architecture node titles
8 changes: 8 additions & 0 deletions demos/architecture.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ <h2>Default icon (?) from unknown icon name</h2>
</pre>
<hr />

<h2>Non-Alphanumeric Title</h2>
<pre class="mermaid">
architecture-beta
service noalpha(server)[Non-Alpha]
service noalpha2(disk)[A/B/C]
</pre>
<hr />

<h2>Split Direction</h2>
<pre class="mermaid">
architecture-beta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import "../common/common";
entry Architecture:
NEWLINE*
"architecture-beta"
NEWLINE*
(
NEWLINE* TitleAndAccessibilities
| NEWLINE* Statement*
| NEWLINE*
TitleAndAccessibilities
| Statement*
)
;

Expand Down Expand Up @@ -50,6 +50,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: /<|>/;
56 changes: 56 additions & 0 deletions packages/parser/tests/architecture.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it } from 'vitest';

import { Architecture } from '../src/language/index.js';
import { expectNoErrorsOrAlternatives, architectureParse as parse } from './test-util.js';

describe('architecture', () => {
it.each([
`architecture-beta`,
` architecture-beta `,
`\tarchitecture-beta\t`,
`
\tarchitecture-beta
`,
`
\tarchitecture-beta
`,
])('should handle regular architecture', (context: string) => {
const result = parse(context);
expectNoErrorsOrAlternatives(result);
expect(result.value.$type).toBe(Architecture);
});

it.each([
`architecture-beta
group foo(cloud)`,
`architecture-beta
service foo(server)
`,
])('should handle group and service', (context: string) => {
const result = parse(context);
expectNoErrorsOrAlternatives(result);
expect(result.value.$type).toBe(Architecture);
});

it.each([
`architecture-beta
service foo(cloud)[Foo]
service bar(cloud)[Foo-Bar]
service bar2(cloud)[Foo/Bar]
service bar3(cloud)[Foo:Bar]
service bar4(cloud)["Foo:Bar"]
`,
])('should handle labels with non-alpha characters', (context: string) => {
const result = parse(context);
expectNoErrorsOrAlternatives(result);
expect(result.value.$type).toBe(Architecture);
});
});
14 changes: 14 additions & 0 deletions packages/parser/tests/test-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -52,6 +55,17 @@ export function createPieTestServices() {
}
export const pieParse = createPieTestServices().parse;

const architectureServices: ArchitectureServices = createArchitectureServices().Architecture;
const architectureParser: LangiumParser = architectureServices.parser.LangiumParser;
export function createArchitectureTestServices() {
const parse = (input: string) => {
return architectureParser.parse<Architecture>(input);
};

return { services: architectureServices, parse };
}
export const architectureParse = createArchitectureTestServices().parse;

const gitGraphServices: GitGraphServices = createGitGraphServices().GitGraph;
const gitGraphParser: LangiumParser = gitGraphServices.parser.LangiumParser;
export function createGitGraphTestServices() {
Expand Down

0 comments on commit deaf0e4

Please sign in to comment.