added more unit tests to gitGraphParser.ts and gitGraphRenderer.ts

This commit is contained in:
Austin Fulbright 2024-08-10 11:22:22 -04:00
parent 269284c6d7
commit d684e0d924
3 changed files with 156 additions and 28 deletions

View File

@ -1,6 +1,5 @@
import { log } from '../../logger.js';
import { random } from '../../utils.js';
import { getConfig } from '../../diagram-api/diagramAPI.js';
import common from '../common/common.js';
import {
setAccTitle,
@ -11,9 +10,11 @@ import {
setDiagramTitle,
getDiagramTitle,
} from '../common/commonDb.js';
import defaultConfig from '../../defaultConfig.js';
import type { DiagramOrientation, Commit, GitGraphDB, CommitType } from './gitGraphTypes.js';
import { ImperativeState } from '../../utils/imperativeState.js';
import DEFAULT_CONFIG from '../../defaultConfig.js';
import type { GitGraphDiagramConfig } from '../../config.type.js';
interface GitGraphState {
commits: Map<string, Commit>;
head: Commit | null;
@ -25,8 +26,13 @@ interface GitGraphState {
options: any;
}
const mainBranchName = defaultConfig.gitGraph.mainBranchName;
const mainBranchOrder = defaultConfig.gitGraph.mainBranchOrder;
const DEFAULT_GITGRAPH_CONFIG: Required<GitGraphDiagramConfig> = DEFAULT_CONFIG.gitGraph;
const mainBranchName = DEFAULT_GITGRAPH_CONFIG.mainBranchName;
const mainBranchOrder = DEFAULT_GITGRAPH_CONFIG.mainBranchOrder;
const config: Required<GitGraphDiagramConfig> = structuredClone(DEFAULT_GITGRAPH_CONFIG);
const getConfig = (): Required<GitGraphDiagramConfig> => structuredClone(config);
const state = new ImperativeState<GitGraphState>(() => ({
commits: new Map(),
@ -476,7 +482,7 @@ export const commitType: CommitType = {
export const db: GitGraphDB = {
commitType,
getConfig: () => getConfig().gitGraph,
getConfig,
setDirection,
setOptions,
getOptions,

View File

@ -11,10 +11,10 @@ import type {
MergeAst,
CommitAst,
BranchAst,
GitGraphDBProvider,
GitGraphDBParseProvider,
} from './gitGraphTypes.js';
const populate = (ast: GitGraph, db: GitGraphDBProvider) => {
const populate = (ast: GitGraph, db: GitGraphDBParseProvider) => {
populateCommonDb(ast, db);
// @ts-ignore: this wont exist if the direction is not specified
if (ast.dir) {
@ -26,7 +26,7 @@ const populate = (ast: GitGraph, db: GitGraphDBProvider) => {
}
};
const parseStatement = (statement: any, db: GitGraphDBProvider) => {
const parseStatement = (statement: any, db: GitGraphDBParseProvider) => {
const parsers: Record<string, (stmt: any) => void> = {
Commit: (stmt) => db.commit(...parseCommit(stmt)),
Branch: (stmt) => db.branch(...parseBranch(stmt)),
@ -93,7 +93,7 @@ export const parser: ParserDefinition = {
if (import.meta.vitest) {
const { it, expect, describe } = import.meta.vitest;
const mockDB: GitGraphDBProvider = {
const mockDB: GitGraphDBParseProvider = {
commitType: commitType,
setDirection: vi.fn(),
commit: vi.fn(),
@ -124,5 +124,87 @@ if (import.meta.vitest) {
parseStatement(branch, mockDB);
expect(mockDB.branch).toHaveBeenCalledWith('newBranch', 1);
});
it('should parse a checkout statement', () => {
const checkout = {
$type: 'Checkout',
branch: 'newBranch',
};
parseStatement(checkout, mockDB);
expect(mockDB.checkout).toHaveBeenCalledWith('newBranch');
});
it('should parse a merge statement', () => {
const merge = {
$type: 'Merge',
branch: 'newBranch',
id: '1',
tags: ['tag1', 'tag2'],
type: 'NORMAL',
};
parseStatement(merge, mockDB);
expect(mockDB.merge).toHaveBeenCalledWith('newBranch', '1', 0, ['tag1', 'tag2']);
});
it('should parse a cherry picking statement', () => {
const cherryPick = {
$type: 'CherryPicking',
id: '1',
tags: ['tag1', 'tag2'],
parent: '2',
};
parseStatement(cherryPick, mockDB);
expect(mockDB.cherryPick).toHaveBeenCalledWith('1', '', ['tag1', 'tag2'], '2');
});
it('should parse a langium generated gitGraph ast', () => {
const dummy: GitGraph = {
$type: 'GitGraph',
statements: [],
};
const gitGraphAst: GitGraph = {
$type: 'GitGraph',
statements: [
{
$container: dummy,
$type: 'Commit',
id: '1',
message: 'test',
tags: ['tag1', 'tag2'],
type: 'NORMAL',
},
{
$container: dummy,
$type: 'Branch',
name: 'newBranch',
order: 1,
},
{
$container: dummy,
$type: 'Merge',
branch: 'newBranch',
id: '1',
tags: ['tag1', 'tag2'],
type: 'NORMAL',
},
{
$container: dummy,
$type: 'Checkout',
branch: 'newBranch',
},
{
$container: dummy,
$type: 'CherryPicking',
id: '1',
tags: ['tag1', 'tag2'],
parent: '2',
},
],
};
populate(gitGraphAst, mockDB);
expect(mockDB.commit).toHaveBeenCalledWith('test', '1', 0, ['tag1', 'tag2']);
expect(mockDB.branch).toHaveBeenCalledWith('newBranch', 1);
expect(mockDB.merge).toHaveBeenCalledWith('newBranch', '1', 0, ['tag1', 'tag2']);
expect(mockDB.checkout).toHaveBeenCalledWith('newBranch');
});
});
}

View File

@ -988,7 +988,6 @@ if (import.meta.vitest) {
});
describe('commitPosition', () => {
dir = 'TB';
const commits = new Map<string, Commit>([
[
'commitZero',
@ -1088,28 +1087,69 @@ if (import.meta.vitest) {
},
],
]);
const expectedCommitPos = new Map<string, CommitPositionOffset>([
['commitZero', { x: 0, y: 40, posWithOffset: 40 }],
['commitA', { x: 107.49609375, y: 90, posWithOffset: 90 }],
['commitB', { x: 107.49609375, y: 140, posWithOffset: 140 }],
['commitM', { x: 0, y: 190, posWithOffset: 190 }],
['commitC', { x: 224.03515625, y: 240, posWithOffset: 240 }],
['commit5_8928ea0', { x: 224.03515625, y: 290, posWithOffset: 290 }],
['commitD', { x: 224.03515625, y: 340, posWithOffset: 340 }],
['commit7_ed848ba', { x: 224.03515625, y: 390, posWithOffset: 390 }],
]);
let pos = 0;
branchPos.set('main', { pos: 0, index: 0 });
branchPos.set('feature', { pos: 107.49609375, index: 1 });
branchPos.set('release', { pos: 224.03515625, index: 2 });
let pos = 30;
commits.forEach((commit, key) => {
it(`should give the correct position for commit ${key}`, () => {
const position = getCommitPosition(commit, pos, false);
expect(position).toEqual(expectedCommitPos.get(key));
pos += 50;
describe('TB', () => {
pos = 30;
dir = 'TB';
const expectedCommitPositionTB = new Map<string, CommitPositionOffset>([
['commitZero', { x: 0, y: 40, posWithOffset: 40 }],
['commitA', { x: 107.49609375, y: 90, posWithOffset: 90 }],
['commitB', { x: 107.49609375, y: 140, posWithOffset: 140 }],
['commitM', { x: 0, y: 190, posWithOffset: 190 }],
['commitC', { x: 224.03515625, y: 240, posWithOffset: 240 }],
['commit5_8928ea0', { x: 224.03515625, y: 290, posWithOffset: 290 }],
['commitD', { x: 224.03515625, y: 340, posWithOffset: 340 }],
['commit7_ed848ba', { x: 224.03515625, y: 390, posWithOffset: 390 }],
]);
commits.forEach((commit, key) => {
it(`should give the correct position for commit ${key}`, () => {
const position = getCommitPosition(commit, pos, false);
expect(position).toEqual(expectedCommitPositionTB.get(key));
pos += 50;
});
});
});
describe('LR', () => {
let pos = 30;
dir = 'LR';
const expectedCommitPositionLR = new Map<string, CommitPositionOffset>([
['commitZero', { x: 0, y: 40, posWithOffset: 40 }],
['commitA', { x: 107.49609375, y: 90, posWithOffset: 90 }],
['commitB', { x: 107.49609375, y: 140, posWithOffset: 140 }],
['commitM', { x: 0, y: 190, posWithOffset: 190 }],
['commitC', { x: 224.03515625, y: 240, posWithOffset: 240 }],
['commit5_8928ea0', { x: 224.03515625, y: 290, posWithOffset: 290 }],
['commitD', { x: 224.03515625, y: 340, posWithOffset: 340 }],
['commit7_ed848ba', { x: 224.03515625, y: 390, posWithOffset: 390 }],
]);
commits.forEach((commit, key) => {
it(`should give the correct position for commit ${key}`, () => {
const position = getCommitPosition(commit, pos, false);
expect(position).toEqual(expectedCommitPositionLR.get(key));
pos += 50;
});
});
});
describe('getCommitClassType', () => {
const expectedCommitClassType = new Map<string, string>([
['commitZero', 'commit-normal'],
['commitA', 'commit-normal'],
['commitB', 'commit-normal'],
['commitM', 'commit-merge'],
['commitC', 'commit-normal'],
['commit5_8928ea0', 'commit-cherry-pick'],
['commitD', 'commit-normal'],
['commit7_ed848ba', 'commit-cherry-pick'],
]);
commits.forEach((commit, key) => {
it(`should give the correct class type for commit ${key}`, () => {
const classType = getCommitClassType(commit);
expect(classType).toBe(expectedCommitClassType.get(key));
});
});
});
});