Improve Segment constructor to correctly handle undefined values.

Closes #1095
This commit is contained in:
Jürg Lehni 2016-07-03 13:30:56 +02:00
parent cfa215051d
commit 45ffc6fb88
2 changed files with 21 additions and 1 deletions

View file

@ -130,7 +130,7 @@ var Segment = Base.extend(/** @lends Segment# */{
} else {
point = arg0;
}
} else if (typeof arg0 === 'object') {
} else if (arg0 == null || typeof arg0 === 'object') {
// It doesn't matter if all of these arguments exist.
// new SegmentPoint() produces creates points with (0, 0) otherwise.
point = arg0;

View file

@ -12,6 +12,11 @@
QUnit.module('Segment');
test('new Segment()', function() {
var segment = new Segment(null, null, null);
equals(segment.toString(), '{ point: { x: 0, y: 0 } }');
});
test('new Segment(point)', function() {
var segment = new Segment(new Point(10, 10));
equals(segment.toString(), '{ point: { x: 10, y: 10 } }');
@ -22,6 +27,11 @@ test('new Segment(x, y)', function() {
equals(segment.toString(), '{ point: { x: 10, y: 10 } }');
});
test('new Segment(undefined)', function() {
var segment = new Segment(undefined);
equals(segment.toString(), '{ point: { x: 0, y: 0 } }');
});
test('new Segment(object)', function() {
var segment = new Segment({ point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } });
equals(segment.toString(), '{ point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } }');
@ -32,6 +42,16 @@ test('new Segment(point, handleIn, handleOut)', function() {
equals(segment.toString(), '{ point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } }');
});
test('new Segment(null, null, null)', function() {
var segment = new Segment(null, null, null);
equals(segment.toString(), '{ point: { x: 0, y: 0 } }');
});
test('new Segment(undefined, null, null)', function() {
var segment = new Segment(undefined, null, null);
equals(segment.toString(), '{ point: { x: 0, y: 0 } }');
});
test('new Segment(x, y, inX, inY, outX, outY)', function() {
var segment = new Segment(10, 10, 5, 5, 15, 15);
equals(segment.toString(), '{ point: { x: 10, y: 10 }, handleIn: { x: 5, y: 5 }, handleOut: { x: 15, y: 15 } }');