more unit tests

This commit is contained in:
Clément GOUIN
2019-07-12 09:04:33 +02:00
parent 81cc8da4b3
commit 1316387bec
2 changed files with 48 additions and 33 deletions
+10 -20
View File
@@ -168,46 +168,36 @@ module.exports = (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++) {
if (list[n1].x === list[n2].x && list[n1].y === list[n2].y) {
for (let n1 = 0; n1 < list.length - 1; n1++)
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];
src = nodes[link.from];
dst = nodes[link.to];
if (src.x !== undefined && dst.x !== undefined) {
if (self.nodeBetween(nodes, link.from, link.to)) {
if (self.nodeBetween(nodes, link.from, link.to))
return false;
}
switch (link.direction) {
case 'up':
case 'top':
if (dst.y - src.y >= 0) {
return false;
}
if (dst.y - src.y >= 0) return false;
break;
case 'down':
case 'bottom':
if (dst.y - src.y <= 0) {
return false;
}
if (dst.y - src.y <= 0) return false;
break;
case 'left':
if (dst.x - src.x >= 0) {
return false;
}
if (dst.x - src.x >= 0) return false;
break;
case 'right':
if (dst.x - src.x <= 0) {
return false;
}
if (dst.x - src.x <= 0) return false;
break;
}
if (!options['diagonals'] && src.x !== dst.x && src.y !== dst.y)
return false;
}
}
return true;
+38 -13
View File
@@ -253,11 +253,11 @@ describe('getPosition', () => {
describe('isValid', () => {
test('no nodes', () => {
const res = placing({debug: true}).isValid({}, []);
const res = placing({debug: true, diagonals: true}).isValid({}, []);
expect(res).toBe(true);
});
test('no placed nodes', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {}, 'b': {}
}, [
{from: 'a', to: 'b'}
@@ -265,7 +265,7 @@ describe('isValid', () => {
expect(res).toBe(true);
});
test('one nodes', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {}
}, [
{from: 'a', to: 'b'}
@@ -273,13 +273,13 @@ describe('isValid', () => {
expect(res).toBe(true);
});
test('overlapping nodes', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 0, y: 0}
}, []);
expect(res).toBe(false);
});
test('in between node', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 0}, 'c': {name: 'c', x: 1, y: 0}
}, [
{from: 'a', to: 'b'}
@@ -287,7 +287,7 @@ describe('isValid', () => {
expect(res).toBe(false);
});
test('invalid right link', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}
}, [
{from: 'a', to: 'b', direction: 'right'}
@@ -295,7 +295,7 @@ describe('isValid', () => {
expect(res).toBe(false);
});
test('invalid left link', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
}, [
{from: 'a', to: 'b', direction: 'left'}
@@ -303,7 +303,7 @@ describe('isValid', () => {
expect(res).toBe(false);
});
test('invalid up link', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
}, [
{from: 'a', to: 'b', direction: 'up'}
@@ -311,7 +311,7 @@ describe('isValid', () => {
expect(res).toBe(false);
});
test('invalid down link', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: -2}
}, [
{from: 'a', to: 'b', direction: 'down'}
@@ -319,7 +319,7 @@ describe('isValid', () => {
expect(res).toBe(false);
});
test('valid right link', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
}, [
{from: 'a', to: 'b', direction: 'right'}
@@ -327,7 +327,7 @@ describe('isValid', () => {
expect(res).toBe(true);
});
test('valid left link', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}
}, [
{from: 'a', to: 'b', direction: 'left'}
@@ -335,7 +335,7 @@ describe('isValid', () => {
expect(res).toBe(true);
});
test('valid up link', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: -2}
}, [
{from: 'a', to: 'b', direction: 'up'}
@@ -343,13 +343,21 @@ describe('isValid', () => {
expect(res).toBe(true);
});
test('valid down link', () => {
const res = placing({debug: true}).isValid({
const res = placing({debug: true, diagonals: true}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
}, [
{from: 'a', to: 'b', direction: 'down'}
]);
expect(res).toBe(true);
});
test('invalid diagonal link', () => {
const res = placing({debug: true, diagonals: false}).isValid({
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}
}, [
{from: 'a', to: 'b'}
]);
expect(res).toBe(false);
});
});
describe('correctPlacing', () => {
@@ -443,6 +451,23 @@ describe('compute', () => {
'6': {name: '6', x: 0, y: 1}
});
});
test('6 nodes 6 links no directions no diagonals', () => {
const nodes = placing({'max-link-length': 2, diagonals: false}).compute(createNodes(6), [
{from: '1', to: '2'},
{from: '1', to: '3'},
{from: '3', to: '4'},
{from: '4', to: '5'},
{from: '3', to: '6'}
]);
expect(nodes).toEqual({
'1': {name: '1', x: 0, y: 0},
'2': {name: '2', x: 0, y: 1},
'3': {name: '3', x: 1, y: 0},
'4': {name: '4', x: 1, y: 1},
'5': {name: '5', x: 2, y: 1},
'6': {name: '6', x: 2, y: 0}
});
});
test('3 nodes impossible', () => {
const nodes = placing({'max-link-length': 2, diagonals: false}).compute(createNodes(3), [
{from: '1', to: '2', direction: 'left'},