more unit tests

This commit is contained in:
Klemek
2019-07-13 14:41:24 +02:00
parent 921dcec385
commit 75dd510a6e
2 changed files with 14 additions and 2 deletions
+6 -2
View File
@@ -17,7 +17,7 @@ const newmap = (w, h, fill) => new Array(w).fill(0).map(() => new Array(h).fill(
*/
const DEFAULT_OPTIONS = {
'max-link-length': 2,
'max-link-length': 3,
'diagonals': true,
};
@@ -179,9 +179,11 @@ module.exports = (options = DEFAULT_OPTIONS) => {
//check overlapping
const list = Object.values(nodes).filter(n => n.x !== undefined);
for (let n1 = 0; n1 < list.length - 1; n1++)
for (let n2 = n1 + 1; n2 < list.length; n2++)
for (let n2 = n1 + 1; n2 < list.length; n2++) {
if (list[n1].x === list[n2].x && list[n1].y === list[n2].y)
return false;
}
for (let li = 0; li < links.length; li++) {
link = links[li];
@@ -190,6 +192,8 @@ module.exports = (options = DEFAULT_OPTIONS) => {
if (src.x !== undefined && dst.x !== undefined) {
if (self.nodeBetween(nodes, link.from, link.to))
return false;
if (Math.pow(dst.x - src.x, 2) + Math.pow(dst.y - src.y, 2) > Math.pow(options['max-link-length'], 2))
return false;
switch (link.direction) {
case 'up':
case 'top':
+8
View File
@@ -286,6 +286,14 @@ describe('isValid', () => {
]);
expect(res).toBe(false);
});
test('too far node', () => {
const res = placing({diagonals: true, 'max-link-length': 4}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 4, y: 2}
}, [
{from: 'a', to: 'b'}
]);
expect(res).toBe(false);
});
test('invalid right link', () => {
const res = placing({diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}