Remove few more prototype-isms, start transitioning to QUnit (it's not ideal, but should do for now).

This commit is contained in:
Juriy Zaytsev 2010-06-26 22:57:02 -04:00
parent 8ee515e647
commit be20251d85
12 changed files with 376 additions and 349 deletions

4
dist/all.js vendored
View file

@ -5916,7 +5916,7 @@ Canvas.base.getElementOffset = getElementOffset;
*/
toObject: function() {
return Canvas.base.object.extend(this.callSuper('toObject'), {
points: this.points.clone()
points: this.points.concat()
});
},
@ -7236,7 +7236,7 @@ Canvas.base.getElementOffset = getElementOffset;
* @return {Canvas.Text} an instance
*/
Canvas.Text.fromObject = function(object) {
return new Canvas.Text(object.text, Object.clone(object));
return new Canvas.Text(object.text, Canvas.base.object.clone(object));
};
/**

View file

@ -68,7 +68,7 @@
*/
toObject: function() {
return Canvas.base.object.extend(this.callSuper('toObject'), {
points: this.points.clone()
points: this.points.concat()
});
},

View file

@ -177,7 +177,7 @@
* @return {Canvas.Text} an instance
*/
Canvas.Text.fromObject = function(object) {
return new Canvas.Text(object.text, Object.clone(object));
return new Canvas.Text(object.text, Canvas.base.object.clone(object));
};
/**

View file

@ -130,7 +130,7 @@ function init() {
this.assert(Canvas.parseElements);
function getOptions(options) {
return Object.extend(Object.clone({
return Canvas.base.object.extend(Canvas.base.object.clone({
left: 10, top: 20, width: 30, height: 40 }), options || { });
}

View file

@ -3,26 +3,22 @@
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Canvas.Polyline unit tests</title>
<title>Canvas.Polyline unit tests</title>
<link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css">
<!--[if IE]>
<script type="text/javascript" src="../../core/excanvas.js"></script>
<![endif]-->
<link rel="stylesheet" href="../lib/unittest.css" type="text/css">
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript" src="../../dist/all.js"></script>
<script src="../../dist/all.js" type="text/javascript"></script>
<script src="../lib/unittest.js" type="text/javascript"></script>
<script src="../lib/event.simulate.js" type="text/javascript"></script>
<script src="canvas_polyline.js" type="text/javascript"></script>
<script type="text/javascript">
init();
</script>
<script type="text/javascript" src="canvas_polyline.js"></script>
</head>
<body>
<h2>Canvas.Polyline unit tests</h2>
<div id="testlog"></div>
<h1 id="qunit-header">Canvas.Polyline unit tests</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -1,4 +1,4 @@
function init() {
(function() {
function getPoints() {
return [
@ -26,76 +26,86 @@ function init() {
'points': getPoints()
};
new Test.Unit.Runner({
testConstructor: function() {
this.assert(Canvas.Polyline);
var polyline = new Canvas.Polyline(getPoints());
this.assertInstanceOf(Canvas.Polyline, polyline);
this.assertInstanceOf(Canvas.Object, polyline);
this.assertIdentical('polyline', polyline.type);
this.assertObjectIdentical(getPoints(), polyline.get('points'));
},
testComplexity: function() {
var polyline = new Canvas.Polyline(getPoints());
this.assertRespondsTo('complexity', polyline);
},
testToObject: function() {
var polyline = new Canvas.Polyline(getPoints());
this.assertRespondsTo('toObject', polyline);
this.assertObjectIdentical(REFERENCE_OBJECT, polyline.toObject());
},
testCanvasPolylineFromObject: function() {
this.assertRespondsTo('fromObject', Canvas.Polyline);
var polyline = Canvas.Polyline.fromObject(REFERENCE_OBJECT);
this.assertInstanceOf(Canvas.Polyline, polyline);
this.assertObjectIdentical(REFERENCE_OBJECT, polyline.toObject());
},
testCanvasPolylineFromElement: function() {
this.assertRespondsTo('fromElement', Canvas.Polyline);
var elPolyline = document.createElement('polyline');
elPolyline.setAttribute('points', '10,12 20,22');
var polyline = Canvas.Polyline.fromElement(elPolyline);
this.assertInstanceOf(Canvas.Polyline, polyline);
this.assertObjectIdentical(REFERENCE_OBJECT, polyline.toObject());
var elPolylineWithAttrs = document.createElement('polyline');
elPolylineWithAttrs.setAttribute('points', '10,10 20,20 30,30 10,10');
elPolylineWithAttrs.setAttribute('fill', 'rgb(255,255,255)');
elPolylineWithAttrs.setAttribute('fill-opacity', '0.34');
elPolylineWithAttrs.setAttribute('stroke-width', '3');
elPolylineWithAttrs.setAttribute('stroke', 'blue');
elPolylineWithAttrs.setAttribute('transform', 'translate(-10,-20) scale(2)');
var polylineWithAttrs = Canvas.Polyline.fromElement(elPolylineWithAttrs);
var expectedPoints = [{x: 10, y: 10}, {x: 20, y: 20}, {x: 30, y: 30}, {x: 10, y: 10}];
this.assertObjectIdentical(Object.extend(REFERENCE_OBJECT, {
'width': 20,
'height': 20,
'fill': 'rgb(255,255,255)',
'stroke': 'blue',
'strokeWidth': 3,
'opacity': 0.34,
'points': expectedPoints
}), polylineWithAttrs.toObject());
this.assertEnumEqual([ 2, 0, 0, 2, -10, -20 ], polylineWithAttrs.get('transformMatrix'));
var elPolylineWithoutPoints = document.createElement('polyline');
this.assertRaise('TypeError', function(){
Canvas.Polyline.fromElement(elPolylineWithoutPoints);
}, 'missing points attribute should result in error');
this.assertNull(Canvas.Polyline.fromElement());
}
test('constructor', function(){
ok(Canvas.Polyline);
var polyline = new Canvas.Polyline(getPoints());
ok(polyline instanceof Canvas.Polyline);
ok(polyline instanceof Canvas.Object);
equals(polyline.type, 'polyline');
same(polyline.get('points'), getPoints());
});
}
test('complexity', function(){
var polyline = new Canvas.Polyline(getPoints());
ok(typeof polyline.complexity == 'function');
});
test('toObject', function(){
var polyline = new Canvas.Polyline(getPoints());
ok(typeof polyline.toObject == 'function');
same(polyline.toObject(), REFERENCE_OBJECT);
});
test('Canvas.Polyline.fromObject', function(){
ok(typeof Canvas.Polyline.fromObject == 'function');
var polyline = Canvas.Polyline.fromObject(REFERENCE_OBJECT);
ok(polyline instanceof Canvas.Polyline);
same(polyline.toObject(), REFERENCE_OBJECT);
});
/*
testCanvasPolylineFromObject: function() {
this.assertRespondsTo('fromObject', Canvas.Polyline);
var polyline = Canvas.Polyline.fromObject(REFERENCE_OBJECT);
this.assertInstanceOf(Canvas.Polyline, polyline);
this.assertObjectIdentical(REFERENCE_OBJECT, polyline.toObject());
},
testCanvasPolylineFromElement: function() {
this.assertRespondsTo('fromElement', Canvas.Polyline);
var elPolyline = document.createElement('polyline');
elPolyline.setAttribute('points', '10,12 20,22');
var polyline = Canvas.Polyline.fromElement(elPolyline);
this.assertInstanceOf(Canvas.Polyline, polyline);
this.assertObjectIdentical(REFERENCE_OBJECT, polyline.toObject());
var elPolylineWithAttrs = document.createElement('polyline');
elPolylineWithAttrs.setAttribute('points', '10,10 20,20 30,30 10,10');
elPolylineWithAttrs.setAttribute('fill', 'rgb(255,255,255)');
elPolylineWithAttrs.setAttribute('fill-opacity', '0.34');
elPolylineWithAttrs.setAttribute('stroke-width', '3');
elPolylineWithAttrs.setAttribute('stroke', 'blue');
elPolylineWithAttrs.setAttribute('transform', 'translate(-10,-20) scale(2)');
var polylineWithAttrs = Canvas.Polyline.fromElement(elPolylineWithAttrs);
var expectedPoints = [{x: 10, y: 10}, {x: 20, y: 20}, {x: 30, y: 30}, {x: 10, y: 10}];
this.assertObjectIdentical(Object.extend(REFERENCE_OBJECT, {
'width': 20,
'height': 20,
'fill': 'rgb(255,255,255)',
'stroke': 'blue',
'strokeWidth': 3,
'opacity': 0.34,
'points': expectedPoints
}), polylineWithAttrs.toObject());
this.assertEnumEqual([ 2, 0, 0, 2, -10, -20 ], polylineWithAttrs.get('transformMatrix'));
var elPolylineWithoutPoints = document.createElement('polyline');
this.assertRaise('TypeError', function(){
Canvas.Polyline.fromElement(elPolylineWithoutPoints);
}, 'missing points attribute should result in error');
this.assertNull(Canvas.Polyline.fromElement());
}
*/
})();

View file

@ -3,30 +3,22 @@
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Canvas.Rect unit tests</title>
<style type="text/css">
.canvas_container { position: relative; border: 1px solid #aaa; }
</style>
<title>Canvas.Rect unit tests</title>
<link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css">
<!--[if IE]>
<script type="text/javascript" src="../../core/excanvas.js"></script>
<![endif]-->
<link rel="stylesheet" href="../lib/unittest.css" type="text/css">
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript" src="../../dist/all.js"></script>
<script src="../../dist/all.js" type="text/javascript"></script>
<script src="../lib/unittest.js" type="text/javascript"></script>
<script src="../lib/event.simulate.js" type="text/javascript"></script>
<script src="canvas_rect.js" type="text/javascript"></script>
<script type="text/javascript">
init();
</script>
<script type="text/javascript" src="canvas_rect.js"></script>
</head>
<body>
<h2>Canvas.Rect unit tests</h2>
<div id="testlog"></div>
<h1 id="qunit-header">Canvas.Rect unit tests</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -1,4 +1,4 @@
function init() {
(function() {
var REFERENCE_RECT = {
'type': 'rect',
@ -18,73 +18,76 @@ function init() {
'opacity': 1
};
new Test.Unit.Runner({
testConstructor: function() {
this.assert(Canvas.Rect);
var rect = new Canvas.Rect();
this.assertInstanceOf(Canvas.Rect, rect);
this.assertInstanceOf(Canvas.Object, rect);
this.assertIdentical('rect', rect.get('type'));
},
testComplexity: function() {
var rect = new Canvas.Rect();
this.assertRespondsTo('complexity', rect);
},
testToObject: function() {
var rect = new Canvas.Rect();
this.assertRespondsTo('toObject', rect);
var object = rect.toObject();
this.assertObjectIdentical(REFERENCE_RECT, object);
},
testCanvasRectFromObject: function() {
this.assertRespondsTo('fromObject', Canvas.Rect);
var rect = Canvas.Rect.fromObject(REFERENCE_RECT);
this.assertInstanceOf(Canvas.Rect, rect);
this.assertObjectIdentical(REFERENCE_RECT, rect.toObject());
},
testCanvasRectFromElement: function() {
this.assertRespondsTo('fromElement', Canvas.Rect);
var elRect = document.createElement('rect');
var rect = Canvas.Rect.fromElement(elRect);
this.assertInstanceOf(Canvas.Rect, rect);
this.assertObjectIdentical(REFERENCE_RECT, rect.toObject());
var elRectWithAttrs = document.createElement('rect');
elRectWithAttrs.setAttribute('x', 10);
elRectWithAttrs.setAttribute('y', 20);
elRectWithAttrs.setAttribute('width', 222);
elRectWithAttrs.setAttribute('height', 333);
elRectWithAttrs.setAttribute('rx', 11);
elRectWithAttrs.setAttribute('ry', 12);
elRectWithAttrs.setAttribute('fill', 'rgb(255,255,255)');
elRectWithAttrs.setAttribute('fill-opacity', 0.45);
elRectWithAttrs.setAttribute('stroke', 'blue');
elRectWithAttrs.setAttribute('stroke-width', 3);
//elRectWithAttrs.setAttribute('transform', 'translate(-10,-20) scale(2) rotate(45) translate(5,10)');
var rectWithAttrs = Canvas.Rect.fromElement(elRectWithAttrs);
this.assertInstanceOf(Canvas.Rect, rectWithAttrs);
var expectedObject = Object.extend(REFERENCE_RECT, {
left: 121,
top: 186.5,
width: 222,
height: 333,
fill: 'rgb(255,255,255)',
opacity: 0.45,
stroke: 'blue',
strokeWidth: 3
});
this.assertObjectIdentical(expectedObject, rectWithAttrs.toObject());
this.assertNull(Canvas.Rect.fromElement());
}
test('constructor', function(){
ok(Canvas.Rect);
var rect = new Canvas.Rect();
ok(rect instanceof Canvas.Rect);
ok(rect instanceof Canvas.Object);
same(rect.get('type'), 'rect');
});
}
test('complexity', function() {
var rect = new Canvas.Rect();
ok(typeof rect.complexity == 'function');
});
test('toObject', function() {
var rect = new Canvas.Rect();
ok(typeof rect.toObject == 'function');
var object = rect.toObject();
same(object, REFERENCE_RECT);
});
test('Canvas.Rect.fromObject', function() {
ok(typeof Canvas.Rect.fromObject == 'function');
var rect = Canvas.Rect.fromObject(REFERENCE_RECT);
ok(rect instanceof Canvas.Rect);
same(rect.toObject(), REFERENCE_RECT);
});
test('Canvas.Rect.fromElement', function() {
ok(typeof Canvas.Rect.fromElement == 'function');
var elRect = document.createElement('rect');
var rect = Canvas.Rect.fromElement(elRect);
ok(rect instanceof Canvas.Rect);
same(rect.toObject(), REFERENCE_RECT);
var elRectWithAttrs = document.createElement('rect');
elRectWithAttrs.setAttribute('x', 10);
elRectWithAttrs.setAttribute('y', 20);
elRectWithAttrs.setAttribute('width', 222);
elRectWithAttrs.setAttribute('height', 333);
elRectWithAttrs.setAttribute('rx', 11);
elRectWithAttrs.setAttribute('ry', 12);
elRectWithAttrs.setAttribute('fill', 'rgb(255,255,255)');
elRectWithAttrs.setAttribute('fill-opacity', 0.45);
elRectWithAttrs.setAttribute('stroke', 'blue');
elRectWithAttrs.setAttribute('stroke-width', 3);
//elRectWithAttrs.setAttribute('transform', 'translate(-10,-20) scale(2) rotate(45) translate(5,10)');
var rectWithAttrs = Canvas.Rect.fromElement(elRectWithAttrs);
ok(rectWithAttrs instanceof Canvas.Rect);
var expectedObject = Canvas.base.object.extend(REFERENCE_RECT, {
left: 121,
top: 186.5,
width: 222,
height: 333,
fill: 'rgb(255,255,255)',
opacity: 0.45,
stroke: 'blue',
strokeWidth: 3
});
same(rectWithAttrs.toObject(), expectedObject);
ok(Canvas.Rect.fromElement() === null);
});
})();

View file

@ -3,15 +3,22 @@
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Canvas.Text unit tests</title>
<title>Canvas.Text unit tests</title>
<link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css">
<script src="../lib/yuiloader-min.js" type="text/javascript"></script>
<!--[if IE]>
<script type="text/javascript" src="../../core/excanvas.js"></script>
<![endif]-->
<script src="bootstrap.js" type="text/javascript"></script>
<script src="canvas_text.js" type="text/javascript"></script>
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript" src="../../dist/all.js"></script>
<script type="text/javascript" src="canvas_text.js"></script>
</head>
<body>
<h2>Canvas.Text unit tests</h2>
<div id="testlog"></div>
<h1 id="qunit-header">Canvas.Text unit tests</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -1,4 +1,4 @@
function init(){
(function() {
var REFERENCE_TEXT_OBJECT = {
'type': 'text',
@ -6,7 +6,7 @@ function init(){
'top': 10,
'width': 100,
'height': 100,
'fill': '#000000',
'fill': 'rgb(0,0,0)',
'overlayFill': null,
'stroke': null,
'strokeWidth': 1,
@ -23,86 +23,106 @@ function init(){
'path': null
};
new Test.Unit.Runner({
testConstructor: function() {
this.assert(Canvas.Text);
var text = new Canvas.Text('foo');
this.assertInstanceOf(Canvas.Text, text);
this.assertInstanceOf(Canvas.Object, text);
this.assertIdentical('text', text.get('type'));
this.assertIdentical('foo', text.get('text'));
},
testToString: function() {
var text = new Canvas.Text('foo');
this.assertRespondsTo('toString', text);
this.assertIdentical('#<Canvas.Text (0): {"text": "foo", "fontfamily": "Modernist_One_400"}>', text.toString());
},
testToObject: function() {
var text = new Canvas.Text('foo');
this.assertRespondsTo('toObject', text);
this.assertObjectIdentical(REFERENCE_TEXT_OBJECT, text.toObject());
},
testComplexity: function() {
var text = new Canvas.Text('foo');
this.assertRespondsTo('complexity', text);
},
testSet: function() {
var text = new Canvas.Text('foo');
this.assertRespondsTo('set', text);
this.assertIdentical(text, text.set('text', 'bar'), 'should be chainable');
},
testSetColor: function() {
var text = new Canvas.Text('foo');
this.assertRespondsTo('setColor', text);
this.assertIdentical(text, text.setColor('123456'), 'should be chainable');
this.assertIdentical('123456', text.get('fill'));
},
testSetFontsize: function() {
var text = new Canvas.Text('foo');
this.assertRespondsTo('setFontsize', text);
this.assertIdentical(text, text.setFontsize(12));
this.assertIdentical(12, text.get('fontsize'));
},
testGetText: function() {
var text = new Canvas.Text('foo');
this.assertRespondsTo('getText', text);
this.assertIdentical('foo', text.getText());
this.assertIdentical(text.get('text'), text.getText());
},
testSetText: function() {
var text = new Canvas.Text('foo');
this.assertRespondsTo('setText', text);
this.assertIdentical(text, text.setText('bar'), 'should be chainable');
this.assertIdentical('bar', text.getText());
},
testCanvasTextFromObject: function() {
this.assertRespondsTo('fromObject', Canvas.Text);
var text = Canvas.Text.fromObject(REFERENCE_TEXT_OBJECT);
this.assertObjectIdentical(REFERENCE_TEXT_OBJECT, text.toObject());
},
testCanvasTextAlreadyDefined: function() {
var warnWasCalled = false;
console.warn = function() {
warnWasCalled = true;
}
loadScript('../../canvas/canvas_text.class.js');
this.wait(1000, function(){
this.assert(warnWasCalled);
});
},
testCanvasObjectDoesntExist: function() {
var warnWasCalled = false;
console.warn = function() {
warnWasCalled = true;
}
delete Canvas.Text;
delete Canvas.Object;
loadScript('../../canvas/canvas_text.class.js');
this.wait(1000, function() {
this.assert(warnWasCalled);
});
}
test('constructor', function() {
ok(Canvas.Text);
var text = new Canvas.Text('foo');
ok(text instanceof Canvas.Text);
ok(text instanceof Canvas.Object);
equals(text.get('type'), 'text');
equals(text.get('text'), 'foo');
});
}
test('toString', function() {
var text = new Canvas.Text('foo');
ok(typeof text.toString == 'function');
equals(text.toString(), '#<Canvas.Text (0): {"text":"foo","fontfamily":"Modernist_One_400"}>');
});
test('toObject', function() {
var text = new Canvas.Text('foo');
ok(typeof text.toObject == 'function');
same(text.toObject(), REFERENCE_TEXT_OBJECT);
});
test('complexity', function(){
var text = new Canvas.Text('foo');
ok(typeof text.complexity == 'function');
});
test('set', function() {
var text = new Canvas.Text('foo');
ok(typeof text.set == 'function');
equals(text.set('text', 'bar'), text, 'should be chainable');
});
test('setColor', function(){
var text = new Canvas.Text('foo');
ok(typeof text.setColor == 'function');
equals(text.setColor('123456'), text, 'should be chainable');
equals(text.get('fill'), '123456');
});
test('setFontsize', function(){
var text = new Canvas.Text('foo');
ok(typeof text.setFontsize == 'function');
equals(text.setFontsize(12), text);
equals(text.get('fontsize'), 12);
});
test('getText', function(){
var text = new Canvas.Text('foo');
ok(typeof text.getText == 'function');
equals(text.getText(), 'foo');
equals(text.getText(), text.get('text'));
});
test('setText', function(){
var text = new Canvas.Text('foo');
ok(typeof text.setText == 'function');
equals(text.setText('bar'), text, 'should be chainable');
equals(text.getText(), 'bar');
});
test('Canvas.Text.fromObject', function(){
ok(typeof Canvas.Text.fromObject == 'function');
var text = Canvas.Text.fromObject(REFERENCE_TEXT_OBJECT);
same(text.toObject(), REFERENCE_TEXT_OBJECT);
});
asyncTest('Text already defined', function() {
var warnWasCalled = false;
console.warn = function() {
warnWasCalled = true;
};
var el = document.createElement('script');
el.src = '../../src/canvas_text.class.js';
document.body.appendChild(el);
setTimeout(function() {
ok(warnWasCalled);
start();
}, 500);
});
asyncTest('Object doesn\'t exist', function() {
var warnWasCalled = false;
console.warn = function() {
warnWasCalled = true;
}
delete Canvas.Text;
delete Canvas.Object;
var el = document.createElement('script');
el.src = '../../src/canvas_text.class.js';
document.body.appendChild(el);
setTimeout(function(){
ok(warnWasCalled);
start();
}, 500);
});
})();

View file

@ -5,20 +5,21 @@
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Canvas.util unit tests</title>
<link rel="stylesheet" href="../lib/unittest.css" type="text/css">
<link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css">
<script src="../../dist/all.js" type="text/javascript"></script>
<script src="../lib/unittest.js" type="text/javascript"></script>
<script src="../lib/event.simulate.js" type="text/javascript"></script>
<!--[if IE]>
<script type="text/javascript" src="../../core/excanvas.js"></script>
<![endif]-->
<script src="canvas_util.js" type="text/javascript"></script>
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript" src="../../dist/all.js"></script>
<script type="text/javascript">
init();
</script>
<script type="text/javascript" src="canvas_util.js"></script>
</head>
<body>
<h2>Canvas.util.* unit tests</h2>
<div id="testlog"></div>
<h1 id="qunit-header">Canvas.util unit tests</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -1,76 +1,74 @@
function init(){
var runner = new Test.Unit.Runner({
(function(){
test('toFixed', function(){
ok(typeof Canvas.util.toFixed == 'function');
testToFixed: function() {
this.assertRespondsTo('toFixed', Canvas.util);
function test(what) {
equals(Canvas.util.toFixed(what, 2), 166.67, 'should leave 2 fractional digits');
equals(Canvas.util.toFixed(what, 5), 166.66667, 'should leave 5 fractional digits');
equals(Canvas.util.toFixed(what, 0), 167, 'should leave 0 fractional digits');
function test(what) {
this.assertIdentical(166.67, Canvas.util.toFixed(what, 2), 'should leave 2 fractional digits');
this.assertIdentical(166.66667, Canvas.util.toFixed(what, 5), 'should leave 5 fractional digits');
this.assertIdentical(167, Canvas.util.toFixed(what, 0), 'should leave 0 fractional digits');
var fractionless = (typeof what == 'number')
? parseInt(what)
: what.substring(0, what.indexOf('.'));
var fractionless = (typeof what == 'number')
? parseInt(what)
: what.substring(0, what.indexOf('.'));
this.assertIdentical(166, Canvas.util.toFixed(fractionless, 2), 'should leave fractionless number as is');
}
test.call(this, '166.66666666666666666666'); // string
test.call(this, 166.66666666666666666666); // number
},
testRemoveFromArray: function() {
var testArray = [1,2,3,4,5];
this.assertRespondsTo('removeFromArray', Canvas.util);
Canvas.util.removeFromArray(testArray, 2);
this.assertEnumEqual([1,3,4,5], testArray);
this.assertIdentical(testArray, Canvas.util.removeFromArray(testArray, 1), 'should return reference to original array');
testArray = [1,2,3,1];
Canvas.util.removeFromArray(testArray, 1);
this.assertEnumEqual([2,3,1], testArray, 'only first occurance of value should be deleted');
testArray = [1,2,3];
Canvas.util.removeFromArray(testArray, 12);
this.assertEnumEqual([1,2,3], testArray, 'deleting unexistent value should not affect array');
testArray = [];
Canvas.util.removeFromArray(testArray, 1);
this.assertEnumEqual([], testArray, 'deleting any value from empty array should not affect it');
testArray = ['0'];
Canvas.util.removeFromArray(testArray, 0);
this.assertEnumEqual(['0'], testArray, 'should use (strict) identity comparison, rather than equality one');
},
testDegreesToRadians: function() {
this.assertRespondsTo('degreesToRadians', Canvas.util);
this.assertIdentical(0, Canvas.util.degreesToRadians(0));
this.assertIdentical(Math.PI / 2, Canvas.util.degreesToRadians(90));
this.assertIdentical(Math.PI, Canvas.util.degreesToRadians(180));
this.assertNaN(Canvas.util.degreesToRadians());
},
testGetRandomInt: function() {
this.assertRespondsTo('getRandomInt', Canvas.util);
var randomInts = [];
for (var i = 100; i--; ) {
var randomInt = Canvas.util.getRandomInt(100, 200);
randomInts.push(randomInt);
this.assert(randomInt >= 100 && randomInt <= 200);
}
var areAllTheSame = randomInts.every(function(value){
return value === randomInts[0];
});
this.assert(!areAllTheSame);
equals(Canvas.util.toFixed(fractionless, 2), 166, 'should leave fractionless number as is');
}
test.call(this, '166.66666666666666666666'); // string
test.call(this, 166.66666666666666666666); // number
});
}
test('removeFromArray', function() {
var testArray = [1,2,3,4,5];
ok(typeof Canvas.util.removeFromArray == 'function');
Canvas.util.removeFromArray(testArray, 2);
same([1,3,4,5], testArray);
equals(Canvas.util.removeFromArray(testArray, 1), testArray, 'should return reference to original array');
testArray = [1,2,3,1];
Canvas.util.removeFromArray(testArray, 1);
same([2,3,1], testArray, 'only first occurance of value should be deleted');
testArray = [1,2,3];
Canvas.util.removeFromArray(testArray, 12);
same([1,2,3], testArray, 'deleting unexistent value should not affect array');
testArray = [];
Canvas.util.removeFromArray(testArray, 1);
same([], testArray, 'deleting any value from empty array should not affect it');
testArray = ['0'];
Canvas.util.removeFromArray(testArray, 0);
same(['0'], testArray, 'should use (strict) identity comparison, rather than equality one');
});
test('degreesToRadians', function(){
ok(typeof Canvas.util.degreesToRadians == 'function');
equals(Canvas.util.degreesToRadians(0), 0);
equals(Canvas.util.degreesToRadians(90), Math.PI / 2);
equals(Canvas.util.degreesToRadians(180), Math.PI);
same(Canvas.util.degreesToRadians(), NaN);
});
test('getRandomInt', function(){
ok(typeof Canvas.util.getRandomInt == 'function');
var randomInts = [];
for (var i = 100; i--; ) {
var randomInt = Canvas.util.getRandomInt(100, 200);
randomInts.push(randomInt);
ok(randomInt >= 100 && randomInt <= 200);
}
var areAllTheSame = randomInts.every(function(value){
return value === randomInts[0];
});
ok(!areAllTheSame);
});
})();