-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
2,001 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / unit-testpackages/mermaid/src/diagrams/swimlane/lost-and-found/detector.spec.ts > swimlane detector > should fail for dagre-d3
|
||
}); | ||
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 GitHub Actions / unit-testpackages/mermaid/src/diagrams/swimlane/lost-and-found/detector.spec.ts > swimlane detector > should succeed for elk
|
||
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
29
packages/mermaid/src/diagrams/swimlane/lost-and-found/detector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { | ||
if (txt.match(/^\s*swimlane/)) { | ||
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; |
40 changes: 40 additions & 0 deletions
40
packages/mermaid/src/diagrams/swimlane/lost-and-found/render-utils.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/mermaid/src/diagrams/swimlane/lost-and-found/render-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
}; |
Oops, something went wrong.