mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-11 03:19:42 +02:00
⚗️ Add parser tests for architecture and other edge cases encountered
This commit is contained in:
88
packages/parser/tests/architecture.test.ts
Normal file
88
packages/parser/tests/architecture.test.ts
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
|
||||||
|
import { Architecture } from '../src/language/index.js';
|
||||||
|
import { expectNoErrorsOrAlternatives, architectureParse as parse } from './test-util.js';
|
||||||
|
|
||||||
|
describe('architecture', () => {
|
||||||
|
describe('should handle architecture definition', () => {
|
||||||
|
it.each([
|
||||||
|
`architecture-beta`,
|
||||||
|
` architecture-beta `,
|
||||||
|
`\tarchitecture-beta\t`,
|
||||||
|
`
|
||||||
|
\tarchitecture-beta
|
||||||
|
`,
|
||||||
|
])('should handle regular architecture', (context: string) => {
|
||||||
|
const result = parse(context);
|
||||||
|
expectNoErrorsOrAlternatives(result);
|
||||||
|
expect(result.value.$type).toBe(Architecture);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('should handle TitleAndAccessibilities', () => {
|
||||||
|
it.each([
|
||||||
|
`architecture-beta title sample title`,
|
||||||
|
` architecture-beta title sample title `,
|
||||||
|
`\tarchitecture-beta\ttitle sample title\t`,
|
||||||
|
`architecture-beta
|
||||||
|
\ttitle sample title
|
||||||
|
`,
|
||||||
|
])('should handle regular architecture + title in same line', (context: string) => {
|
||||||
|
const result = parse(context);
|
||||||
|
expectNoErrorsOrAlternatives(result);
|
||||||
|
expect(result.value.$type).toBe(Architecture);
|
||||||
|
|
||||||
|
const { title } = result.value;
|
||||||
|
expect(title).toBe('sample title');
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
`architecture-beta
|
||||||
|
title sample title`,
|
||||||
|
`architecture-beta
|
||||||
|
title sample title
|
||||||
|
`,
|
||||||
|
])('should handle regular architecture + title in next line', (context: string) => {
|
||||||
|
const result = parse(context);
|
||||||
|
expectNoErrorsOrAlternatives(result);
|
||||||
|
expect(result.value.$type).toBe(Architecture);
|
||||||
|
|
||||||
|
const { title } = result.value;
|
||||||
|
expect(title).toBe('sample title');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle regular architecture + title + accTitle + accDescr', () => {
|
||||||
|
const context = `architecture-beta
|
||||||
|
title sample title
|
||||||
|
accTitle: sample accTitle
|
||||||
|
accDescr: sample accDescr
|
||||||
|
`;
|
||||||
|
const result = parse(context);
|
||||||
|
expectNoErrorsOrAlternatives(result);
|
||||||
|
expect(result.value.$type).toBe(Architecture);
|
||||||
|
|
||||||
|
const { title, accTitle, accDescr } = result.value;
|
||||||
|
expect(title).toBe('sample title');
|
||||||
|
expect(accTitle).toBe('sample accTitle');
|
||||||
|
expect(accDescr).toBe('sample accDescr');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle regular architecture + title + accTitle + multi-line accDescr', () => {
|
||||||
|
const context = `architecture-beta
|
||||||
|
title sample title
|
||||||
|
accTitle: sample accTitle
|
||||||
|
accDescr {
|
||||||
|
sample accDescr
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
const result = parse(context);
|
||||||
|
expectNoErrorsOrAlternatives(result);
|
||||||
|
expect(result.value.$type).toBe(Architecture);
|
||||||
|
|
||||||
|
const { title, accTitle, accDescr } = result.value;
|
||||||
|
expect(title).toBe('sample title');
|
||||||
|
expect(accTitle).toBe('sample accTitle');
|
||||||
|
expect(accDescr).toBe('sample accDescr');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@@ -63,6 +63,12 @@ describe('Parsing Branch Statements', () => {
|
|||||||
expect(branch.name).toBe('master');
|
expect(branch.name).toBe('master');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should parse a branch name starting with numbers', () => {
|
||||||
|
const result = parse(`gitGraph\n commit\n branch 1.0.1\n`);
|
||||||
|
const branch = result.value.statements[1] as Branch;
|
||||||
|
expect(branch.name).toBe('1.0.1');
|
||||||
|
});
|
||||||
|
|
||||||
it('should parse a branch with an order property', () => {
|
it('should parse a branch with an order property', () => {
|
||||||
const result = parse(`gitGraph\n commit\n branch feature order:1\n`);
|
const result = parse(`gitGraph\n commit\n branch feature order:1\n`);
|
||||||
const branch = result.value.statements[1] as Branch;
|
const branch = result.value.statements[1] as Branch;
|
||||||
|
@@ -4,6 +4,7 @@ import { Pie } from '../src/language/index.js';
|
|||||||
import { expectNoErrorsOrAlternatives, pieParse as parse } from './test-util.js';
|
import { expectNoErrorsOrAlternatives, pieParse as parse } from './test-util.js';
|
||||||
|
|
||||||
describe('pie', () => {
|
describe('pie', () => {
|
||||||
|
describe('should handle pie definition with or without showData', () => {
|
||||||
it.each([
|
it.each([
|
||||||
`pie`,
|
`pie`,
|
||||||
` pie `,
|
` pie `,
|
||||||
@@ -32,7 +33,9 @@ describe('pie', () => {
|
|||||||
const { showData } = result.value;
|
const { showData } = result.value;
|
||||||
expect(showData).toBeTruthy();
|
expect(showData).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
describe('should handle TitleAndAccessibilities', () => {
|
||||||
|
describe('should handle TitleAndAccessibilities without showData', () => {
|
||||||
it.each([
|
it.each([
|
||||||
`pie title sample title`,
|
`pie title sample title`,
|
||||||
` pie title sample title `,
|
` pie title sample title `,
|
||||||
@@ -68,7 +71,9 @@ describe('pie', () => {
|
|||||||
const { title } = result.value;
|
const { title } = result.value;
|
||||||
expect(title).toBe('sample title');
|
expect(title).toBe('sample title');
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('should handle TitleAndAccessibilities with showData', () => {
|
||||||
it.each([
|
it.each([
|
||||||
`pie showData title sample title`,
|
`pie showData title sample title`,
|
||||||
`pie showData title sample title
|
`pie showData title sample title
|
||||||
@@ -103,9 +108,10 @@ describe('pie', () => {
|
|||||||
expect(showData).toBeTruthy();
|
expect(showData).toBeTruthy();
|
||||||
expect(title).toBe('sample title');
|
expect(title).toBe('sample title');
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('sections', () => {
|
describe('should handle sections', () => {
|
||||||
describe('normal', () => {
|
|
||||||
it.each([
|
it.each([
|
||||||
`pie
|
`pie
|
||||||
"GitHub":100
|
"GitHub":100
|
||||||
@@ -169,6 +175,22 @@ describe('pie', () => {
|
|||||||
expect(sections[1].value).toBe(50);
|
expect(sections[1].value).toBe(50);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should handle value with positive decimal', () => {
|
||||||
|
const context = `pie
|
||||||
|
"ash": 60.67
|
||||||
|
"bat": 40`;
|
||||||
|
const result = parse(context);
|
||||||
|
expectNoErrorsOrAlternatives(result);
|
||||||
|
expect(result.value.$type).toBe(Pie);
|
||||||
|
|
||||||
|
const { sections } = result.value;
|
||||||
|
expect(sections[0].label).toBe('ash');
|
||||||
|
expect(sections[0].value).toBe(60.67);
|
||||||
|
|
||||||
|
expect(sections[1].label).toBe('bat');
|
||||||
|
expect(sections[1].value).toBe(40);
|
||||||
|
});
|
||||||
|
|
||||||
it('should handle sections with accTitle', () => {
|
it('should handle sections with accTitle', () => {
|
||||||
const context = `pie accTitle: sample wow
|
const context = `pie accTitle: sample wow
|
||||||
"GitHub": 100
|
"GitHub": 100
|
||||||
@@ -226,4 +248,3 @@ describe('pie', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
import type { LangiumParser, ParseResult } from 'langium';
|
import type { LangiumParser, ParseResult } from 'langium';
|
||||||
import { expect, vi } from 'vitest';
|
import { expect, vi } from 'vitest';
|
||||||
import type {
|
import type {
|
||||||
|
Architecture,
|
||||||
|
ArchitectureServices,
|
||||||
Info,
|
Info,
|
||||||
InfoServices,
|
InfoServices,
|
||||||
Pie,
|
Pie,
|
||||||
@@ -13,6 +15,7 @@ import type {
|
|||||||
GitGraphServices,
|
GitGraphServices,
|
||||||
} from '../src/language/index.js';
|
} from '../src/language/index.js';
|
||||||
import {
|
import {
|
||||||
|
createArchitectureServices,
|
||||||
createInfoServices,
|
createInfoServices,
|
||||||
createPieServices,
|
createPieServices,
|
||||||
createRadarServices,
|
createRadarServices,
|
||||||
@@ -47,6 +50,17 @@ export function createInfoTestServices() {
|
|||||||
}
|
}
|
||||||
export const infoParse = createInfoTestServices().parse;
|
export const infoParse = createInfoTestServices().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 pieServices: PieServices = createPieServices().Pie;
|
const pieServices: PieServices = createPieServices().Pie;
|
||||||
const pieParser: LangiumParser = pieServices.parser.LangiumParser;
|
const pieParser: LangiumParser = pieServices.parser.LangiumParser;
|
||||||
export function createPieTestServices() {
|
export function createPieTestServices() {
|
||||||
|
Reference in New Issue
Block a user