unit testing
This commit is contained in:
+11
-11
@@ -49,15 +49,15 @@ module.exports = (options) => {
|
||||
const list = Object.values(nodes).filter(n => n.x !== undefined);
|
||||
if (list.length === 0)
|
||||
return {x: 0, y: 0, w: 0, h: 0}; //empty
|
||||
let minX = 0;
|
||||
let minY = 0;
|
||||
let maxX = 0;
|
||||
let maxY = 0;
|
||||
let minX = null;
|
||||
let minY = null;
|
||||
let maxX = null;
|
||||
let maxY = null;
|
||||
list.forEach(n => {
|
||||
minX = Math.min(n.x, minX);
|
||||
minY = Math.min(n.y, minY);
|
||||
maxX = Math.max(n.x, maxX);
|
||||
maxY = Math.max(n.y, maxY);
|
||||
minX = minX !== null ? Math.min(n.x, minX) : n.x;
|
||||
minY = minY !== null ? Math.min(n.y, minY) : n.y;
|
||||
maxX = maxX !== null ? Math.max(n.x, maxX) : n.x;
|
||||
maxY = maxY !== null ? Math.max(n.y, maxY) : n.y;
|
||||
});
|
||||
return {x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1};
|
||||
},
|
||||
@@ -73,13 +73,13 @@ module.exports = (options) => {
|
||||
list.forEach(n => {
|
||||
map[n.x - b.x][n.y - b.y] = true;
|
||||
});
|
||||
for (let x = 0; x < b.w; x++) {
|
||||
for (let y = 0; y < b.h; y++) {
|
||||
for (let y = 0; y < b.h; y++) {
|
||||
for (let x = 0; x < b.w; x++) {
|
||||
if (!map[x][y])
|
||||
return {x: x + b.x, y: y + b.y};
|
||||
}
|
||||
}
|
||||
if (options['expand'] === 'h')
|
||||
if (options['expand'] !== 'v')
|
||||
return {x: b.x + b.w, y: b.y}; //expand horizontally
|
||||
else
|
||||
return {x: b.x, y: b.y + b.h}; //expand vertically
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
test('sample test', () => {
|
||||
|
||||
});
|
||||
@@ -0,0 +1,115 @@
|
||||
/* jshint -W117 */
|
||||
const placing = require('../src/placing');
|
||||
|
||||
describe('getBounds', () => {
|
||||
test('no nodes', () => {
|
||||
const res = placing({debug: true}).getBounds({});
|
||||
expect(res).toEqual({x: 0, y: 0, w: 0, h: 0});
|
||||
});
|
||||
test('no placed nodes', () => {
|
||||
const res = placing({debug: true}).getBounds({
|
||||
'a': {}, 'b': {}
|
||||
});
|
||||
expect(res).toEqual({x: 0, y: 0, w: 0, h: 0});
|
||||
});
|
||||
test('first node', () => {
|
||||
const res = placing({debug: true}).getBounds({
|
||||
'a': {x: 0, y: 0}, 'b': {}
|
||||
});
|
||||
expect(res).toEqual({x: 0, y: 0, w: 1, h: 1});
|
||||
});
|
||||
test('one node not 0,0', () => {
|
||||
const res = placing({debug: true}).getBounds({
|
||||
'a': {x: 5, y: 6}, 'b': {}
|
||||
});
|
||||
expect(res).toEqual({x: 5, y: 6, w: 1, h: 1});
|
||||
});
|
||||
test('2 nodes', () => {
|
||||
const res = placing({debug: true}).getBounds({
|
||||
'a': {x: 0, y: 0}, 'b': {x: 1, y: 1}, 'c': {}
|
||||
});
|
||||
expect(res).toEqual({x: 0, y: 0, w: 2, h: 2});
|
||||
});
|
||||
test('2 nodes special', () => {
|
||||
const res = placing({debug: true}).getBounds({
|
||||
'a': {x: 1, y: 2}, 'b': {x: -5, y: 6}, 'c': {}
|
||||
});
|
||||
expect(res).toEqual({x: -5, y: 2, w: 7, h: 5});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNewPos', () => {
|
||||
test('no nodes', () => {
|
||||
const res = placing({debug: true}).getNewPos({});
|
||||
expect(res).toEqual({x: 0, y: 0});
|
||||
});
|
||||
test('no placed nodes', () => {
|
||||
const res = placing({debug: true}).getNewPos({
|
||||
'a': {}, 'b': {}
|
||||
});
|
||||
expect(res).toEqual({x: 0, y: 0});
|
||||
});
|
||||
test('one node', () => {
|
||||
const res = placing({debug: true, expand: 'h'}).getNewPos({
|
||||
'a': {x: 0, y: 0}, 'b': {}
|
||||
});
|
||||
expect(res).toEqual({x: 1, y: 0});
|
||||
});
|
||||
test('one node expand vert', () => {
|
||||
const res = placing({debug: true, expand: 'v'}).getNewPos({
|
||||
'a': {x: 0, y: 0}, 'b': {}
|
||||
});
|
||||
expect(res).toEqual({x: 0, y: 1});
|
||||
});
|
||||
test('one node not 0,0', () => {
|
||||
const res = placing({debug: true, expand: 'h'}).getNewPos({
|
||||
'a': {x: 5, y: 6}, 'b': {}
|
||||
});
|
||||
expect(res).toEqual({x: 6, y: 6});
|
||||
});
|
||||
test('2 nodes', () => {
|
||||
const res = placing({debug: true, expand: 'h'}).getNewPos({
|
||||
'a': {x: 0, y: 0}, 'b': {x: 1, y: 1}
|
||||
});
|
||||
expect(res).toEqual({x: 1, y: 0});
|
||||
});
|
||||
});
|
||||
|
||||
describe('nodeBetween', () => {
|
||||
test('only 2 nodes', () => {
|
||||
const res = placing({debug: true}).nodeBetween({
|
||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 1, y: 0}
|
||||
}, 'a', 'b');
|
||||
expect(res).toBe(false);
|
||||
});
|
||||
test('other node not between', () => {
|
||||
const res = placing({debug: true}).nodeBetween({
|
||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 0, y: 1}, 'c': {name: 'c', x: 1, y: 0}
|
||||
}, 'a', 'b');
|
||||
expect(res).toBe(false);
|
||||
});
|
||||
test('between aligned h', () => {
|
||||
const res = placing({debug: true}).nodeBetween({
|
||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 0}, 'c': {name: 'c', x: 1, y: 0}
|
||||
}, 'a', 'b');
|
||||
expect(res).toBe(true);
|
||||
});
|
||||
test('between aligned v', () => {
|
||||
const res = placing({debug: true}).nodeBetween({
|
||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 0, y: 2}, 'c': {name: 'c', x: 0, y: 1}
|
||||
}, 'a', 'b');
|
||||
expect(res).toBe(true);
|
||||
});
|
||||
test('between diagonal', () => {
|
||||
const res = placing({debug: true}).nodeBetween({
|
||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}, 'c': {name: 'c', x: 1, y: 1}
|
||||
}, 'a', 'b');
|
||||
expect(res).toBe(true);
|
||||
});
|
||||
test('between diagonal 2', () => {
|
||||
const res = placing({debug: true}).nodeBetween({
|
||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 1}, 'c': {name: 'c', x: 1, y: 1}
|
||||
}, 'a', 'b');
|
||||
expect(res).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user