Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(common): add title test cases #74

Merged
merged 1 commit into from
May 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions packages/mermaid-parser/tests/common/title.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { LangiumDocument, createServicesForGrammar } from 'langium';
import { parseHelper } from 'langium/lib/test';
import { beforeAll, describe, expect, it } from 'vitest';

import {
Mermaid,
MermaidGrammar,
MermiadTokenBuilder,
} from '../../src/language';

describe('title', () => {
let parser: (input: string) => Promise<LangiumDocument<Mermaid>>;

beforeAll(async () => {
const services = await createServicesForGrammar({
grammar: MermaidGrammar(),
module: {
parser: {
TokenBuilder: () => new MermiadTokenBuilder(),
},
},
});
parser = parseHelper<Mermaid>(services);
});

describe('normal', () => {
it.each([
// without whitespaces
`pie title`,

// with spaces
`pie title `,

// with tabs
`pie\title\t`,

// with extra whitespaces
`pie

title\t

`,
])('should handle empty title', async (string_: string) => {
const { parseResult: result } = await parser(string_);
expect(result.parserErrors).toHaveLength(0);
expect(result.lexerErrors).toHaveLength(0);

const value = result.value;
expect(value.title).toBeUndefined();
expect(value.accDescr).toBeUndefined();
expect(value.accTitle).toBeUndefined();
});

it.each([
// without whitespaces
`pie title sample`,

// with spaces
`pie title sample `,

// with tabs
`pie\ttitle\tsample\t`,

// with extra whitespaces
`pie

title\t sample

`,
])('should handle regular title', async (string_: string) => {
const { parseResult: result } = await parser(string_);
expect(result.parserErrors).toHaveLength(0);
expect(result.lexerErrors).toHaveLength(0);

const value = result.value;
expect(value.title).toBe('sample');
expect(value.accDescr).toBeUndefined();
expect(value.accTitle).toBeUndefined();
});

it.todo('should handle title with accTitle', async () => {
const string_ = `pie title sample + accTitle: test`;
const { parseResult: result } = await parser(string_);
expect(result.parserErrors).toHaveLength(0);
expect(result.lexerErrors).toHaveLength(0);

const value = result.value;
expect(value.title).toBe('sample + accTitle: test');
expect(value.accDescr).toBeUndefined();
expect(value.accTitle).toBeUndefined();
});

it.todo('should handle title with single line accDescr', async () => {
const string_ = `pie title sample + accDescr: test`;
const { parseResult: result } = await parser(string_);
expect(result.parserErrors).toHaveLength(0);
expect(result.lexerErrors).toHaveLength(0);

const value = result.value;
expect(value.title).toBe('sample + accDescr: test');
expect(value.accDescr).toBeUndefined();
expect(value.accTitle).toBeUndefined();
});

it.todo('should handle title with multi line accDescr', async () => {
const string_ = `pie title sample + accDescr {test}`;
const { parseResult: result } = await parser(string_);
expect(result.parserErrors).toHaveLength(0);
expect(result.lexerErrors).toHaveLength(0);

const value = result.value;
expect(value.title).toBe('sample + accDescr {test}');
expect(value.accDescr).toBeUndefined();
expect(value.accTitle).toBeUndefined();
});
});

describe('duplicate', () => {
describe('inside', () => {
it('should handle title inside title', async () => {
const string_ = `pie title title test`;
const { parseResult: result } = await parser(string_);
expect(result.parserErrors).toHaveLength(0);
expect(result.lexerErrors).toHaveLength(0);

const value = result.value;
expect(value.title).toBe('title test');
expect(value.accDescr).toBeUndefined();
expect(value.accTitle).toBeUndefined();
});
});

describe('after', () => {
it('should handle regular title after empty title', async () => {
const string_ = `pie title:
title sample`;
const { parseResult: result } = await parser(string_);
expect(result.parserErrors).toHaveLength(0);
expect(result.lexerErrors).toHaveLength(0);

const value = result.value;
expect(value.title).toBe('sample');
expect(value.accDescr).toBeUndefined();
expect(value.accTitle).toBeUndefined();
});

it('should handle empty title after regular title', async () => {
const string_ = `pie title sample
title`;
const { parseResult: result } = await parser(string_);
expect(result.parserErrors).toHaveLength(0);
expect(result.lexerErrors).toHaveLength(0);

const value = result.value;
expect(value.title).toBeUndefined();
expect(value.accDescr).toBeUndefined();
expect(value.accTitle).toBeUndefined();
});

it('should handle regular title after regular title', async () => {
const string_ = `pie title test
title sample`;
const { parseResult: result } = await parser(string_);
expect(result.parserErrors).toHaveLength(0);
expect(result.lexerErrors).toHaveLength(0);

const value = result.value;
expect(value.title).toBe('sample');
expect(value.accDescr).toBeUndefined();
expect(value.accTitle).toBeUndefined();
});
});
});
});