mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-17 11:14:12 +01:00
added parser test and combined the two gitGraph tests
This commit is contained in:
75
packages/mermaid/src/diagrams/git/gitGraph.parser.spec.ts
Normal file
75
packages/mermaid/src/diagrams/git/gitGraph.parser.spec.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||||
|
import { parser } from './gitGraphParser.js';
|
||||||
|
import db from './gitGraphAst.js';
|
||||||
|
import { parse } from 'path';
|
||||||
|
|
||||||
|
const parseInput = async (input: string) => {
|
||||||
|
await parser.parse(input);
|
||||||
|
};
|
||||||
|
|
||||||
|
const spyOn = vi.spyOn;
|
||||||
|
|
||||||
|
describe('GitGraph Parsing', function () {
|
||||||
|
beforeEach(() => {
|
||||||
|
db.clear();
|
||||||
|
});
|
||||||
|
it('should parse a default commit statement', async () => {
|
||||||
|
const input = `gitGraph:
|
||||||
|
commit
|
||||||
|
`;
|
||||||
|
const commitSpy = spyOn(db, 'commit');
|
||||||
|
await parseInput(input);
|
||||||
|
|
||||||
|
expect(commitSpy).toHaveBeenCalledWith('', undefined, 0, []);
|
||||||
|
commitSpy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse a basic branch statement with just a name', async () => {
|
||||||
|
const input = `gitGraph:
|
||||||
|
branch newBranch
|
||||||
|
`;
|
||||||
|
const branchSpy = spyOn(db, 'branch');
|
||||||
|
await parseInput(input);
|
||||||
|
expect(branchSpy).toHaveBeenCalledWith('newBranch', 0);
|
||||||
|
branchSpy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse a basic checkout statement', async () => {
|
||||||
|
const input = `gitGraph:
|
||||||
|
branch newBranch
|
||||||
|
checkout newBranch
|
||||||
|
`;
|
||||||
|
const checkoutSpy = spyOn(db, 'checkout');
|
||||||
|
await parseInput(input);
|
||||||
|
expect(checkoutSpy).toHaveBeenCalledWith('newBranch');
|
||||||
|
checkoutSpy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse a basic merge statement', async () => {
|
||||||
|
const input = `gitGraph:
|
||||||
|
commit
|
||||||
|
branch newBranch
|
||||||
|
checkout newBranch
|
||||||
|
commit
|
||||||
|
checkout main
|
||||||
|
merge newBranch`;
|
||||||
|
const mergeSpy = spyOn(db, 'merge');
|
||||||
|
await parseInput(input);
|
||||||
|
expect(mergeSpy).toHaveBeenCalledWith('newBranch', '', undefined, []);
|
||||||
|
mergeSpy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse cherry-picking', async () => {
|
||||||
|
const input = `gitGraph
|
||||||
|
commit id: "ZERO"
|
||||||
|
branch develop
|
||||||
|
commit id:"A"
|
||||||
|
checkout main
|
||||||
|
cherry-pick id:"A"
|
||||||
|
`;
|
||||||
|
const cherryPickSpy = spyOn(db, 'cherryPick');
|
||||||
|
await parseInput(input);
|
||||||
|
expect(cherryPickSpy).toHaveBeenCalledWith('A', '', undefined, undefined);
|
||||||
|
cherryPickSpy.mockRestore();
|
||||||
|
});
|
||||||
|
});
|
||||||
1322
packages/mermaid/src/diagrams/git/gitGraph.spec.ts
Normal file
1322
packages/mermaid/src/diagrams/git/gitGraph.spec.ts
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,231 +0,0 @@
|
|||||||
import db from './gitGraphAst.js';
|
|
||||||
import { parser } from './gitGraphParser.js';
|
|
||||||
|
|
||||||
describe('when parsing a gitGraph', function () {
|
|
||||||
beforeEach(function () {
|
|
||||||
db.clear();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle a gitGraph definition', async () => {
|
|
||||||
const str = `gitGraph:\n commit\n`;
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
const commits = db.getCommits();
|
|
||||||
|
|
||||||
expect(commits.size).toBe(1);
|
|
||||||
expect(db.getCurrentBranch()).toBe('main');
|
|
||||||
expect(db.getDirection()).toBe('LR');
|
|
||||||
expect(db.getBranches().size).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle set direction top to bottom', async () => {
|
|
||||||
const str = 'gitGraph TB:\n' + 'commit\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
const commits = db.getCommits();
|
|
||||||
|
|
||||||
expect(commits.size).toBe(1);
|
|
||||||
expect(db.getCurrentBranch()).toBe('main');
|
|
||||||
expect(db.getDirection()).toBe('TB');
|
|
||||||
expect(db.getBranches().size).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle set direction bottom to top', async () => {
|
|
||||||
const str = 'gitGraph BT:\n' + 'commit\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
const commits = db.getCommits();
|
|
||||||
|
|
||||||
expect(commits.size).toBe(1);
|
|
||||||
expect(db.getCurrentBranch()).toBe('main');
|
|
||||||
expect(db.getDirection()).toBe('BT');
|
|
||||||
expect(db.getBranches().size).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should checkout a branch', async () => {
|
|
||||||
const str = 'gitGraph:\n' + 'branch new\n' + 'checkout new\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
const commits = db.getCommits();
|
|
||||||
|
|
||||||
expect(commits.size).toBe(0);
|
|
||||||
expect(db.getCurrentBranch()).toBe('new');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should switch a branch', async () => {
|
|
||||||
const str = 'gitGraph:\n' + 'branch new\n' + 'switch new\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
const commits = db.getCommits();
|
|
||||||
|
|
||||||
expect(commits.size).toBe(0);
|
|
||||||
expect(db.getCurrentBranch()).toBe('new');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should add commits to checked out branch', async () => {
|
|
||||||
const str = 'gitGraph:\n' + 'branch new\n' + 'checkout new\n' + 'commit\n' + 'commit\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
const commits = db.getCommits();
|
|
||||||
|
|
||||||
expect(commits.size).toBe(2);
|
|
||||||
expect(db.getCurrentBranch()).toBe('new');
|
|
||||||
const branchCommit = db.getBranches().get('new');
|
|
||||||
expect(branchCommit).not.toBeNull();
|
|
||||||
expect(commits.get(branchCommit).parent).not.toBeNull();
|
|
||||||
});
|
|
||||||
it('should handle commit with args', async () => {
|
|
||||||
const str = 'gitGraph:\n' + 'commit "a commit"\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
const commits = db.getCommits();
|
|
||||||
|
|
||||||
expect(commits.size).toBe(1);
|
|
||||||
const key = commits.keys().next().value;
|
|
||||||
expect(commits.get(key).message).toBe('a commit');
|
|
||||||
expect(db.getCurrentBranch()).toBe('main');
|
|
||||||
});
|
|
||||||
|
|
||||||
it.skip('should reset a branch', async () => {
|
|
||||||
const str =
|
|
||||||
'gitGraph:\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'branch newbranch\n' +
|
|
||||||
'checkout newbranch\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'reset main\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
|
|
||||||
const commits = db.getCommits();
|
|
||||||
expect(commits.size).toBe(3);
|
|
||||||
expect(db.getCurrentBranch()).toBe('newbranch');
|
|
||||||
expect(db.getBranches().get('newbranch')).toEqual(db.getBranches().get('main'));
|
|
||||||
expect(db.getHead().id).toEqual(db.getBranches().get('newbranch'));
|
|
||||||
});
|
|
||||||
|
|
||||||
it.skip('reset can take an argument', async () => {
|
|
||||||
const str =
|
|
||||||
'gitGraph:\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'branch newbranch\n' +
|
|
||||||
'checkout newbranch\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'reset main^\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
|
|
||||||
const commits = db.getCommits();
|
|
||||||
expect(commits.size).toBe(3);
|
|
||||||
expect(db.getCurrentBranch()).toBe('newbranch');
|
|
||||||
const main = commits.get(db.getBranches().get('main'));
|
|
||||||
expect(db.getHead().id).toEqual(main.parent);
|
|
||||||
});
|
|
||||||
|
|
||||||
it.skip('should handle fast forwardable merges', async () => {
|
|
||||||
const str =
|
|
||||||
'gitGraph:\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'branch newbranch\n' +
|
|
||||||
'checkout newbranch\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'checkout main\n' +
|
|
||||||
'merge newbranch\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
|
|
||||||
const commits = db.getCommits();
|
|
||||||
expect(commits.size).toBe(4);
|
|
||||||
expect(db.getCurrentBranch()).toBe('main');
|
|
||||||
expect(db.getBranches().get('newbranch')).toEqual(db.getBranches().get('main'));
|
|
||||||
expect(db.getHead().id).toEqual(db.getBranches().get('newbranch'));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle cases when merge is a noop', async () => {
|
|
||||||
const str =
|
|
||||||
'gitGraph:\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'branch newbranch\n' +
|
|
||||||
'checkout newbranch\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'merge main\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
|
|
||||||
const commits = db.getCommits();
|
|
||||||
expect(commits.size).toBe(4);
|
|
||||||
expect(db.getCurrentBranch()).toBe('newbranch');
|
|
||||||
expect(db.getBranches().get('newbranch')).not.toEqual(db.getBranches().get('main'));
|
|
||||||
expect(db.getHead().id).toEqual(db.getBranches().get('newbranch'));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle merge with 2 parents', async () => {
|
|
||||||
const str =
|
|
||||||
'gitGraph:\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'branch newbranch\n' +
|
|
||||||
'checkout newbranch\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'checkout main\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'merge newbranch\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
|
|
||||||
const commits = db.getCommits();
|
|
||||||
expect(commits.size).toBe(5);
|
|
||||||
expect(db.getCurrentBranch()).toBe('main');
|
|
||||||
expect(db.getBranches().get('newbranch')).not.toEqual(db.getBranches().get('main'));
|
|
||||||
expect(db.getHead().id).toEqual(db.getBranches().get('main'));
|
|
||||||
});
|
|
||||||
|
|
||||||
it.skip('should handle ff merge when history walk has two parents (merge commit)', async () => {
|
|
||||||
const str =
|
|
||||||
'gitGraph:\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'branch newbranch\n' +
|
|
||||||
'checkout newbranch\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'checkout main\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'merge newbranch\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'checkout newbranch\n' +
|
|
||||||
'merge main\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
|
|
||||||
const commits = db.getCommits();
|
|
||||||
expect(commits.size).toBe(7);
|
|
||||||
expect(db.getCurrentBranch()).toBe('newbranch');
|
|
||||||
expect(db.getBranches().get('newbranch')).toEqual(db.getBranches().get('main'));
|
|
||||||
expect(db.getHead().id).toEqual(db.getBranches().get('main'));
|
|
||||||
|
|
||||||
db.prettyPrint();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should generate an array of known branches', async () => {
|
|
||||||
const str =
|
|
||||||
'gitGraph:\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'branch b1\n' +
|
|
||||||
'checkout b1\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'commit\n' +
|
|
||||||
'branch b2\n';
|
|
||||||
|
|
||||||
await parser.parse(str);
|
|
||||||
const branches = db.getBranchesAsObjArray();
|
|
||||||
|
|
||||||
expect(branches).toHaveLength(3);
|
|
||||||
expect(branches[0]).toHaveProperty('name', 'main');
|
|
||||||
expect(branches[1]).toHaveProperty('name', 'b1');
|
|
||||||
expect(branches[2]).toHaveProperty('name', 'b2');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -50,8 +50,9 @@ const parseStatement = (statement: any) => {
|
|||||||
const parseCommit = (commit: CommitAst) => {
|
const parseCommit = (commit: CommitAst) => {
|
||||||
const id = commit.id;
|
const id = commit.id;
|
||||||
const message = commit.message ?? '';
|
const message = commit.message ?? '';
|
||||||
const tags = commit.tags ?? undefined;
|
|
||||||
const type = commit.type !== undefined ? commitType[commit.type] : commitType.NORMAL;
|
const type = commit.type !== undefined ? commitType[commit.type] : commitType.NORMAL;
|
||||||
|
const tags = commit.tags ?? undefined;
|
||||||
|
|
||||||
db.commit(message, id, type, tags);
|
db.commit(message, id, type, tags);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -64,8 +65,8 @@ const parseBranch = (branch: BranchAst) => {
|
|||||||
const parseMerge = (merge: MergeAst) => {
|
const parseMerge = (merge: MergeAst) => {
|
||||||
const branch = merge.branch;
|
const branch = merge.branch;
|
||||||
const id = merge.id ?? '';
|
const id = merge.id ?? '';
|
||||||
const tags = merge.tags ?? undefined;
|
|
||||||
const type = merge.type !== undefined ? commitType[merge.type] : undefined;
|
const type = merge.type !== undefined ? commitType[merge.type] : undefined;
|
||||||
|
const tags = merge.tags ?? undefined;
|
||||||
db.merge(branch, id, type, tags);
|
db.merge(branch, id, type, tags);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -5,5 +5,5 @@
|
|||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"types": ["vitest/importMeta", "vitest/globals"]
|
"types": ["vitest/importMeta", "vitest/globals"]
|
||||||
},
|
},
|
||||||
"include": ["./src/**/*.ts", "./package.json"]
|
"include": ["./src/**/*.ts", "./package.json", "src/diagrams/gantt/ganttDb.js"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user