added parser test and combined the two gitGraph tests

This commit is contained in:
Austin Fulbright
2024-08-06 19:26:13 -04:00
parent 38e048b94e
commit 8fe0ed1d03
6 changed files with 1401 additions and 1333 deletions

View 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();
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -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');
});
});

View File

@@ -50,8 +50,9 @@ const parseStatement = (statement: any) => {
const parseCommit = (commit: CommitAst) => {
const id = commit.id;
const message = commit.message ?? '';
const tags = commit.tags ?? undefined;
const type = commit.type !== undefined ? commitType[commit.type] : commitType.NORMAL;
const tags = commit.tags ?? undefined;
db.commit(message, id, type, tags);
};
@@ -64,8 +65,8 @@ const parseBranch = (branch: BranchAst) => {
const parseMerge = (merge: MergeAst) => {
const branch = merge.branch;
const id = merge.id ?? '';
const tags = merge.tags ?? undefined;
const type = merge.type !== undefined ? commitType[merge.type] : undefined;
const tags = merge.tags ?? undefined;
db.merge(branch, id, type, tags);
};

File diff suppressed because it is too large Load Diff

View File

@@ -5,5 +5,5 @@
"outDir": "./dist",
"types": ["vitest/importMeta", "vitest/globals"]
},
"include": ["./src/**/*.ts", "./package.json"]
"include": ["./src/**/*.ts", "./package.json", "src/diagrams/gantt/ganttDb.js"]
}