Skip to content

Commit

Permalink
#2028 Adding legacy code
Browse files Browse the repository at this point in the history
  • Loading branch information
knsv committed Dec 19, 2024
1 parent 48a0d41 commit 2352b42
Show file tree
Hide file tree
Showing 14 changed files with 2,001 additions and 6 deletions.
5 changes: 3 additions & 2 deletions cypress/platform/knsv2.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@
%% swimlane 1 - A E
%% swimlane 2 - B
%% swimlane 3 - C D
lane First
%% lane First
A
end
%% end
A --> B(I am B, the wide one) --> C
C --> D & F

D --> E
A --> E
E --> B

B@{ shape: diam}
</pre>
Expand Down
8 changes: 4 additions & 4 deletions cypress/platform/shape-tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
logLevel: 1,
});

let shape = 'card';
// let simplified = true;
let simplified = false;
let shape = 'circle';
let simplified = true;
// let simplified = false;
let algorithm = 'elk';
// let algorithm = 'dagre';
let code = `---
Expand Down Expand Up @@ -86,7 +86,7 @@
layout: ${algorithm}
---
flowchart LR
A["Abrakadabra"] --> C["C"] & C & C & C & C
A["Abrakadabra"] --> C["I am the circle"] & C & C & C & C
%% A["Abrakadabra"] --> C
A@{ shape: ${shape}}
C@{ shape: ${shape}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import plugin from './detector.js';
import { describe, it } from 'vitest';

const { detector } = plugin;

describe('swimlane detector', () => {
it('should fail for dagre-d3', () => {
expect(
detector('swimlane', {
flowchart: {
defaultRenderer: 'dagre-d3',
},
})
).toBe(false);

Check failure on line 14 in packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.spec.ts

View workflow job for this annotation

GitHub Actions / unit-test

packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.spec.ts > swimlane detector > should fail for dagre-d3

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.spec.ts:14:7
});
it('should fail for dagre-wrapper', () => {
expect(
detector('flowchart', {
flowchart: {
defaultRenderer: 'dagre-wrapper',
},
})
).toBe(false);
});
it('should succeed for elk', () => {
expect(
detector('flowchart', {
flowchart: {
defaultRenderer: 'elk',
},
})
).toBe(true);

Check failure on line 32 in packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.spec.ts

View workflow job for this annotation

GitHub Actions / unit-test

packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.spec.ts > swimlane detector > should succeed for elk

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.spec.ts:32:7
expect(
detector('graph', {
flowchart: {
defaultRenderer: 'elk',
},
})
).toBe(true);
});

it('should detect swimlane', () => {
expect(detector('swimlane')).toBe(true);
});

it('should not detect class with defaultRenderer set to elk', () => {
expect(
detector('class', {
flowchart: {
defaultRenderer: 'elk',
},
})
).toBe(false);
});
});
29 changes: 29 additions & 0 deletions packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import type {
ExternalDiagramDefinition,
DiagramDetector,
DiagramLoader,
} from '../../../diagram-api/types.js';
const id = 'swimlane';


const detector: DiagramDetector = (txt, config): boolean => {

Check failure on line 10 in packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.ts

View workflow job for this annotation

GitHub Actions / lint

'config' is defined but never used. Allowed unused args must match /^_/u
if (txt.match(/^\s*swimlane/)) {

Check failure on line 11 in packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.ts

View workflow job for this annotation

GitHub Actions / lint

Use the `RegExp#exec()` method instead
return true;
}
return false;
};


const loader: DiagramLoader = async () => {
const { diagram } = await import('./swimlane-definition.js');
return { id, diagram };
};

const plugin: ExternalDiagramDefinition = {
id,
detector,
loader,
};

export default plugin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { findCommonAncestor, TreeData } from './render-utils.js';
describe('when rendering a flowchart using elk ', () => {
let lookupDb: TreeData;
beforeEach(() => {
lookupDb = {
parentById: {
B4: 'inner',
B5: 'inner',
C4: 'inner2',
C5: 'inner2',
B2: 'Ugge',
B3: 'Ugge',
inner: 'Ugge',
inner2: 'Ugge',
B6: 'outer',
},
childrenById: {
inner: ['B4', 'B5'],
inner2: ['C4', 'C5'],
Ugge: ['B2', 'B3', 'inner', 'inner2'],
outer: ['B6'],
},
};
});
it('to find parent of siblings in a subgraph', () => {
expect(findCommonAncestor('B4', 'B5', lookupDb)).toBe('inner');
});
it('to find an uncle', () => {
expect(findCommonAncestor('B4', 'B2', lookupDb)).toBe('Ugge');
});
it('to find a cousin', () => {
expect(findCommonAncestor('B4', 'C4', lookupDb)).toBe('Ugge');
});
it('to find a grandparent', () => {
expect(findCommonAncestor('B4', 'B6', lookupDb)).toBe('root');
});
it('to find ancestor of siblings in the root', () => {
expect(findCommonAncestor('B1', 'outer', lookupDb)).toBe('root');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export interface TreeData {
parentById: Record<string, string>;
childrenById: Record<string, string[]>;
}

export const findCommonAncestor = (id1: string, id2: string, treeData: TreeData) => {
const { parentById } = treeData;
const visited = new Set();
let currentId = id1;
while (currentId) {
visited.add(currentId);
if (currentId === id2) {
return currentId;
}
currentId = parentById[currentId];
}
currentId = id2;
while (currentId) {
if (visited.has(currentId)) {
return currentId;
}
currentId = parentById[currentId];
}
return 'root';
};
Loading

0 comments on commit 2352b42

Please sign in to comment.