Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tbo47 committed Sep 28, 2024
1 parent e9b481d commit 7ebaaba
Show file tree
Hide file tree
Showing 43 changed files with 5,854 additions and 0 deletions.
23 changes: 23 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Original dagre-d3 copyright: Copyright (c) 2013 Chris Pettitt
Original dagre copyright: Copyright (c) 2012-2014 Chris Pettitt
Original graphlib copyright: Copyright (c) 2012-2014 Chris Pettitt

Copyright (c) 2022-2024 Thibaut Lassalle, David Newell, Alois Klink, Sidharth Vinod and dagre-es contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
98 changes: 98 additions & 0 deletions src/dagre/acyclic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import * as _ from 'lodash-es';
import { describe, expect, it } from 'vitest';

import * as acyclic from './acyclic.js';
import { Graph } from '../graphlib/index.js';
import { findCycles } from '../graphlib/alg/find-cycles.js';

describe('acyclic', function () {
var ACYCLICERS = ['greedy', 'dfs', 'unknown-should-still-work'];
var g;

beforeEach(function () {
g = new Graph({ multigraph: true }).setDefaultEdgeLabel(function () {
return { minlen: 1, weight: 1 };
});
});

_.forEach(ACYCLICERS, function (acyclicer) {
describe(acyclicer, function () {
beforeEach(function () {
g.setGraph({ acyclicer: acyclicer });
});

describe('run', function () {
it('does not change an already acyclic graph', function () {
g.setPath(['a', 'b', 'd']);
g.setPath(['a', 'c', 'd']);
acyclic.run(g);
var results = _.map(g.edges(), stripLabel);
expect(_.sortBy(results, ['v', 'w'])).to.eql([
{ v: 'a', w: 'b' },
{ v: 'a', w: 'c' },
{ v: 'b', w: 'd' },
{ v: 'c', w: 'd' },
]);
});

it('breaks cycles in the input graph', function () {
g.setPath(['a', 'b', 'c', 'd', 'a']);
acyclic.run(g);
expect(findCycles(g)).to.eql([]);
});

it('creates a multi-edge where necessary', function () {
g.setPath(['a', 'b', 'a']);
acyclic.run(g);
expect(findCycles(g)).to.eql([]);
if (g.hasEdge('a', 'b')) {
expect(g.outEdges('a', 'b')).to.have.length(2);
} else {
expect(g.outEdges('b', 'a')).to.have.length(2);
}
expect(g.edgeCount()).to.equal(2);
});
});

describe('undo', function () {
it('does not change edges where the original graph was acyclic', function () {
g.setEdge('a', 'b', { minlen: 2, weight: 3 });
acyclic.run(g);
acyclic.undo(g);
expect(g.edge('a', 'b')).to.eql({ minlen: 2, weight: 3 });
expect(g.edges()).to.have.length(1);
});

it('can restore previosuly reversed edges', function () {
g.setEdge('a', 'b', { minlen: 2, weight: 3 });
g.setEdge('b', 'a', { minlen: 3, weight: 4 });
acyclic.run(g);
acyclic.undo(g);
expect(g.edge('a', 'b')).to.eql({ minlen: 2, weight: 3 });
expect(g.edge('b', 'a')).to.eql({ minlen: 3, weight: 4 });
expect(g.edges()).to.have.length(2);
});
});
});
});

describe('greedy-specific functionality', function () {
it('prefers to break cycles at low-weight edges', function () {
g.setGraph({ acyclicer: 'greedy' });
g.setDefaultEdgeLabel(function () {
return { minlen: 1, weight: 2 };
});
g.setPath(['a', 'b', 'c', 'd', 'a']);
g.setEdge('c', 'd', { weight: 1 });
acyclic.run(g);
expect(findCycles(g)).to.eql([]);
expect(g.hasEdge('c', 'd')).to.be.false;
});
});
});

function stripLabel(edge) {
var c = _.clone(edge);
delete c.label;
return c;
}
142 changes: 142 additions & 0 deletions src/dagre/add-border-segments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { describe, expect, it } from 'vitest';
import { addBorderSegments } from './add-border-segments.js';
import { Graph } from '../graphlib/index.js';

describe('addBorderSegments', function () {
var g;

beforeEach(function () {
g = new Graph({ compound: true });
});

it('does not add border nodes for a non-compound graph', function () {
var g = new Graph();
g.setNode('a', { rank: 0 });
addBorderSegments(g);
expect(g.nodeCount()).to.equal(1);
expect(g.node('a')).to.eql({ rank: 0 });
});

it('does not add border nodes for a graph with no clusters', function () {
g.setNode('a', { rank: 0 });
addBorderSegments(g);
expect(g.nodeCount()).to.equal(1);
expect(g.node('a')).to.eql({ rank: 0 });
});

it('adds a border for a single-rank subgraph', function () {
g.setNode('sg', { minRank: 1, maxRank: 1 });
addBorderSegments(g);

var bl = g.node('sg').borderLeft[1];
var br = g.node('sg').borderRight[1];
expect(g.node(bl)).eqls({
dummy: 'border',
borderType: 'borderLeft',
rank: 1,
width: 0,
height: 0,
});
expect(g.parent(bl)).equals('sg');
expect(g.node(br)).eqls({
dummy: 'border',
borderType: 'borderRight',
rank: 1,
width: 0,
height: 0,
});
expect(g.parent(br)).equals('sg');
});

it('adds a border for a multi-rank subgraph', function () {
g.setNode('sg', { minRank: 1, maxRank: 2 });
addBorderSegments(g);

var sgNode = g.node('sg');
var bl2 = sgNode.borderLeft[1];
var br2 = sgNode.borderRight[1];
expect(g.node(bl2)).eqls({
dummy: 'border',
borderType: 'borderLeft',
rank: 1,
width: 0,
height: 0,
});
expect(g.parent(bl2)).equals('sg');
expect(g.node(br2)).eqls({
dummy: 'border',
borderType: 'borderRight',
rank: 1,
width: 0,
height: 0,
});
expect(g.parent(br2)).equals('sg');

var bl1 = sgNode.borderLeft[2];
var br1 = sgNode.borderRight[2];
expect(g.node(bl1)).eqls({
dummy: 'border',
borderType: 'borderLeft',
rank: 2,
width: 0,
height: 0,
});
expect(g.parent(bl1)).equals('sg');
expect(g.node(br1)).eqls({
dummy: 'border',
borderType: 'borderRight',
rank: 2,
width: 0,
height: 0,
});
expect(g.parent(br1)).equals('sg');

expect(g.hasEdge(sgNode.borderLeft[1], sgNode.borderLeft[2])).to.be.true;
expect(g.hasEdge(sgNode.borderRight[1], sgNode.borderRight[2])).to.be.true;
});

it('adds borders for nested subgraphs', function () {
g.setNode('sg1', { minRank: 1, maxRank: 1 });
g.setNode('sg2', { minRank: 1, maxRank: 1 });
g.setParent('sg2', 'sg1');
addBorderSegments(g);

var bl1 = g.node('sg1').borderLeft[1];
var br1 = g.node('sg1').borderRight[1];
expect(g.node(bl1)).eqls({
dummy: 'border',
borderType: 'borderLeft',
rank: 1,
width: 0,
height: 0,
});
expect(g.parent(bl1)).equals('sg1');
expect(g.node(br1)).eqls({
dummy: 'border',
borderType: 'borderRight',
rank: 1,
width: 0,
height: 0,
});
expect(g.parent(br1)).equals('sg1');

var bl2 = g.node('sg2').borderLeft[1];
var br2 = g.node('sg2').borderRight[1];
expect(g.node(bl2)).eqls({
dummy: 'border',
borderType: 'borderLeft',
rank: 1,
width: 0,
height: 0,
});
expect(g.parent(bl2)).equals('sg2');
expect(g.node(br2)).eqls({
dummy: 'border',
borderType: 'borderRight',
rank: 1,
width: 0,
height: 0,
});
expect(g.parent(br2)).equals('sg2');
});
});
71 changes: 71 additions & 0 deletions src/dagre/coordinate-system.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Graph } from '../graphlib/index.js';
import * as coordinateSystem from './coordinate-system.js';
import { describe, expect, it } from 'vitest';

describe('coordinateSystem', function () {
var g;

beforeEach(function () {
g = new Graph();
});

describe('coordinateSystem.adjust', function () {
beforeEach(function () {
g.setNode('a', { width: 100, height: 200 });
});

it('does nothing to node dimensions with rankdir = TB', function () {
g.setGraph({ rankdir: 'TB' });
coordinateSystem.adjust(g);
expect(g.node('a')).eqls({ width: 100, height: 200 });
});

it('does nothing to node dimensions with rankdir = BT', function () {
g.setGraph({ rankdir: 'BT' });
coordinateSystem.adjust(g);
expect(g.node('a')).eqls({ width: 100, height: 200 });
});

it('swaps width and height for nodes with rankdir = LR', function () {
g.setGraph({ rankdir: 'LR' });
coordinateSystem.adjust(g);
expect(g.node('a')).eqls({ width: 200, height: 100 });
});

it('swaps width and height for nodes with rankdir = RL', function () {
g.setGraph({ rankdir: 'RL' });
coordinateSystem.adjust(g);
expect(g.node('a')).eqls({ width: 200, height: 100 });
});
});

describe('coordinateSystem.undo', function () {
beforeEach(function () {
g.setNode('a', { width: 100, height: 200, x: 20, y: 40 });
});

it('does nothing to points with rankdir = TB', function () {
g.setGraph({ rankdir: 'TB' });
coordinateSystem.undo(g);
expect(g.node('a')).eqls({ x: 20, y: 40, width: 100, height: 200 });
});

it('flips the y coordinate for points with rankdir = BT', function () {
g.setGraph({ rankdir: 'BT' });
coordinateSystem.undo(g);
expect(g.node('a')).eqls({ x: 20, y: -40, width: 100, height: 200 });
});

it('swaps dimensions and coordinates for points with rankdir = LR', function () {
g.setGraph({ rankdir: 'LR' });
coordinateSystem.undo(g);
expect(g.node('a')).eqls({ x: 40, y: 20, width: 200, height: 100 });
});

it('swaps dims and coords and flips x for points with rankdir = RL', function () {
g.setGraph({ rankdir: 'RL' });
coordinateSystem.undo(g);
expect(g.node('a')).eqls({ x: -40, y: 20, width: 200, height: 100 });
});
});
});
61 changes: 61 additions & 0 deletions src/dagre/data/list.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, expect, it } from 'vitest';

import { List } from './list.js';

describe('data.List', function () {
var list;

beforeEach(function () {
list = new List();
});

describe('dequeue', function () {
it('returns undefined with an empty list', function () {
expect(list.dequeue()).to.be.undefined;
});

it('unlinks and returns the first entry', function () {
var obj = {};
list.enqueue(obj);
expect(list.dequeue()).to.equal(obj);
});

it('unlinks and returns multiple entries in FIFO order', function () {
var obj1 = {};
var obj2 = {};
list.enqueue(obj1);
list.enqueue(obj2);

expect(list.dequeue()).to.equal(obj1);
expect(list.dequeue()).to.equal(obj2);
});

it('unlinks and relinks an entry if it is re-enqueued', function () {
var obj1 = {};
var obj2 = {};
list.enqueue(obj1);
list.enqueue(obj2);
list.enqueue(obj1);

expect(list.dequeue()).to.equal(obj2);
expect(list.dequeue()).to.equal(obj1);
});

it('unlinks and relinks an entry if it is enqueued on another list', function () {
var obj = {};
var list2 = new List();
list.enqueue(obj);
list2.enqueue(obj);

expect(list.dequeue()).to.be.undefined;
expect(list2.dequeue()).to.equal(obj);
});

it('can return a string representation', function () {
list.enqueue({ entry: 1 });
list.enqueue({ entry: 2 });

expect(list.toString()).to.equal('[{"entry":1}, {"entry":2}]');
});
});
});
Loading

0 comments on commit 7ebaaba

Please sign in to comment.