add matrix, set and mina tests

master
adeveria 2013-09-18 15:34:25 -07:00
parent 1ed7ac65f8
commit b04c154dfe
5 changed files with 384 additions and 1 deletions

184
test/matrix.js Normal file
View File

@ -0,0 +1,184 @@
describe("Matrix methods", function () {
// TODO: Write this test
it("Matrix.add - matrix", function() {
var matrix1 = new Savage.Matrix(1, 2, 3, 4, 5, 6);
var matrix2 = new Savage.Matrix(1, 2, 3, 4, 5, 6);
var result = matrix1.add(matrix2);
console.log('result', result);
});
// TODO: Write this test
it("Matrix.add - numbers", function() {
var matrix1 = new Savage.Matrix(1, 2, 3, 4, 5, 6);
var result = matrix1.add(1, 2, 3, 4, 5, 6);
console.log('result', result);
});
it("Matrix.clone", function() {
var matrix1 = new Savage.Matrix(1, 2, 3, 4, 5, 6);
var clone = matrix1.clone();
expect(clone).to.not.be(matrix1);
expect(clone).to.eql({
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6
});
});
it("Matrix.invert", function() {
var matrix1 = new Savage.Matrix(1, 2, 3, 4, 5, 6);
var inverse = matrix1.invert();
expect(inverse).to.eql({
a: -2,
b: 1,
c: 1.5,
d: -0.5,
e: 1,
f: -2
});
console.log('inverse', inverse);
});
// TODO: Write this test
it("Matrix.rotate", function() {
var matrix1 = new Savage.Matrix(1, 0, 0, 1, 0, 0);
matrix1.rotate(90, 0, 0);
console.log('m1', matrix1);
});
it("Matrix.scale - x", function() {
var matrix = new Savage.Matrix(1, 0, 0, 1, 20, 30);
matrix.scale(2);
expect(matrix).to.eql({
a: 2,
b: 0,
c: 0,
d: 2,
e: 20,
f: 30
});
matrix.scale(0.5);
expect(matrix).to.eql({
a: 1,
b: 0,
c: 0,
d: 1,
e: 20,
f: 30
});
});
it("Matrix.scale - x, y", function() {
var matrix = new Savage.Matrix(1, 0, 0, 1, 20, 30);
matrix.scale(2, 3);
expect(matrix).to.eql({
a: 2,
b: 0,
c: 0,
d: 3,
e: 20,
f: 30
});
matrix.scale(0.5, 1);
expect(matrix).to.eql({
a: 1,
b: 0,
c: 0,
d: 3,
e: 20,
f: 30
});
});
it("Matrix.scale - x, y, cx", function() {
var matrix = new Savage.Matrix(1, 0, 0, 1, 20, 30);
matrix.scale(2, 3, 5);
expect(matrix).to.eql({
a: 2,
b: 0,
c: 0,
d: 3,
e: 15,
f: 25
});
});
it("Matrix.scale - x, y, cx, cy", function() {
var matrix = new Savage.Matrix(1, 0, 0, 1, 20, 30);
matrix.scale(2, 3, 5, -5);
expect(matrix).to.eql({
a: 2,
b: 0,
c: 0,
d: 3,
e: 15,
f: 40
});
});
it("Matrix.split", function() {
var matrix = new Savage.Matrix(1, 0, 0, 1, 0, 0);
var result = matrix.split();
expect(result.dx).to.be(0);
expect(result.dy).to.be(0);
expect(result.scalex).to.be(1);
expect(result.scaley).to.be(1);
expect(result.shear).to.be(0);
expect(result.rotate).to.be(0);
expect(result.isSimple).to.be(true);
matrix = new Savage.Matrix(1.5, 0, 0, 0.5, 20, 25);
result = matrix.split();
expect(result.dx).to.be(20);
expect(result.dy).to.be(25);
expect(result.scalex).to.be(1.5);
expect(result.scaley).to.be(0.5);
expect(result.shear).to.be(0);
expect(result.rotate).to.be(0);
expect(result.isSimple).to.be(true);
});
it("Matrix.toTransformString", function() {
var matrix = new Savage.Matrix(1.5, 0, 0, 0.5, 20, 25);
console.log(matrix.toTransformString());
var transformArrs = Savage.parseTransformString(
"matrix(1, 2, 3, 4, 5, 6) " +
"translate(7) " +
"translate(8 9) " +
"scale(10) " +
"scale(11, 12) " +
"rotate(13) " +
"rotate(14 15 16) " +
"skewX(17) " +
"skewY(18) "
);
console.log(transformArrs.toString());
});
it("Matrix.translate", function() {
var matrix = new Savage.Matrix(1, 0, 0, 1, 20, 30);
matrix.translate(10, -10);
expect(matrix).to.eql({
a: 1,
b: 0,
c: 0,
d: 1,
e: 30,
f: 20
});
matrix.translate(-1, -2);
expect(matrix).to.eql({
a: 1,
b: 0,
c: 0,
d: 1,
e: 29,
f: 18
});
});
it("Matrix.x", function() {
var matrix = new Savage.Matrix(1, 0, 0, 1, 20, 30);
var result = matrix.x(10, -10);
expect(result).to.be(30);
});
it("Matrix.y", function() {
var matrix = new Savage.Matrix(1, 0, 0, 1, 20, 30);
var result = matrix.y(10, -10);
expect(result).to.be(20);
});
});

87
test/mina.js Normal file
View File

@ -0,0 +1,87 @@
describe("Mina methods", function () {
var s;
beforeEach(function () {
s = Savage(100, 100);
});
afterEach(function () {
s.remove();
});
var validateDescriptor = function(obj) {
expect(obj).to.be.an('object');
expect(obj.id).to.be.a('string');
expect(obj.start).to.be.a('number');
expect(obj.end).to.be.a('number');
expect(obj.b).to.be.a('number');
expect(obj.s).to.be.a('number');
expect(obj.dur).to.be.a('number');
expect(obj.spd).to.be.a('number');
expect(obj.get).to.be.a('function');
expect(obj.set).to.be.a('function');
expect(obj.easing).to.be.a('function');
expect(obj.status).to.be.a('function');
expect(obj.speed).to.be.a('function');
expect(obj.duration).to.be.a('function');
expect(obj.stop).to.be.a('function');
};
it("mina", function() {
var n;
var animDescriptor = mina(10, 20, 0, 1000, function(newN) {
n = newN;
}, function() {});
validateDescriptor(animDescriptor);
expect(animDescriptor.start).to.be(10);
expect(animDescriptor.end).to.be(20);
expect(animDescriptor.b).to.be(0);
expect(animDescriptor.s).to.be(0);
expect(animDescriptor.dur).to.be(1000);
expect(animDescriptor.easing).to.be(mina.linear);
});
it("mina.getById", function() {
var anim1 = mina(10, 20, 0, 1000, function() {}, function() {});
var anim2 = mina(10, 20, 0, 1000, function() {}, function() {});
expect(mina.getById(anim1.id)).to.be(anim1);
expect(mina.getById(anim2.id)).to.be(anim2);
});
it("mina.time", function() {
var now = (new Date).getTime();
expect(mina.time()).to.be(now);
});
it("mina.backin", function() {
expect(mina.backin(0)).to.be(0);
expect(mina.backin(1)).to.be(1);
});
it("mina.backout", function() {
expect(mina.backout(0)).to.be(0);
expect(mina.backout(1)).to.be(1);
});
it("mina.bounce", function() {
expect(mina.bounce(0)).to.be(0);
expect(mina.bounce(1)).to.be(1);
});
it("mina.easein", function() {
expect(mina.easein(0)).to.be(0);
expect(mina.easein(1)).to.be(1);
});
it("mina.easeinout", function() {
expect(mina.easeinout(0)).to.be(0);
expect(mina.easeinout(1)).to.be(1);
});
it("mina.easeout", function() {
expect(mina.easeout(0)).to.be(0);
expect(mina.easeout(1)).to.be(1);
});
it("mina.elastic", function() {
expect(mina.elastic(0)).to.be(0);
expect(mina.elastic(1)).to.be(1);
});
it("mina.linear", function() {
expect(mina.linear(0)).to.be(0);
expect(mina.linear(0.2)).to.be(0.2);
expect(mina.linear(0.7)).to.be(0.7);
expect(mina.linear(1)).to.be(1);
});
});

View File

@ -259,7 +259,6 @@ describe("Path methods", function () {
"M80 80 A 10 10 0 0 0 120 80"; "M80 80 A 10 10 0 0 0 120 80";
var matrix = new Savage.Matrix(1, 0, 0, 1, 1000, 0); var matrix = new Savage.Matrix(1, 0, 0, 1, 1000, 0);
var transformedPath = Savage.path.map(absPath, matrix); var transformedPath = Savage.path.map(absPath, matrix);
console.log('transformedPath', transformedPath);
var i = 0; var i = 0;
var checkNext = function(arr) { var checkNext = function(arr) {

110
test/set.js Normal file
View File

@ -0,0 +1,110 @@
describe("Set methods", function () {
var s;
beforeEach(function () {
s = Savage(100, 100);
});
afterEach(function () {
s.remove();
});
it("Set.clear", function() {
var rect1 = s.rect(10, 20, 30, 40);
var rect2 = s.rect(10, 20, 30, 40);
var set = Savage.set(rect1, rect2);
expect(set.length).to.be(2);
set.clear();
expect(set.length).to.be(0);
});
it("Set.exclude", function() {
var rect1 = s.rect(10, 20, 30, 40);
var rect2 = s.rect(10, 20, 30, 40);
var rect3 = s.rect(10, 20, 30, 40);
var set = Savage.set(rect1, rect2, rect3);
expect(set.length).to.be(3);
var excluded = set.exclude(rect2);
expect(set.length).to.be(2);
expect(excluded).to.be(true);
excluded = set.exclude(rect2);
expect(set.length).to.be(2);
expect(excluded).to.be(false);
});
it("Set.forEach", function() {
var rect1 = s.rect(10, 20, 30, 40);
var rect2 = s.rect(10, 20, 30, 40);
var rect3 = s.rect(10, 20, 30, 40);
var set = Savage.set(rect1, rect2, rect3);
var i = 0;
var arr = [rect1, rect2, rect3];
var result = set.forEach(function(item) {
expect(arr[i]).to.be(item);
expect(this.isContext).to.be(true);
i++;
}, {isContext: true});
expect(result).to.be(set);
expect(i).to.be(3);
});
it("Set.pop", function() {
var rect1 = s.rect(10, 20, 30, 40);
var rect2 = s.rect(10, 20, 30, 40);
var rect3 = s.rect(10, 20, 30, 40);
var set = Savage.set(rect1, rect2, rect3);
expect(set.length).to.be(3);
var result = set.pop();
expect(set.length).to.be(2);
expect(result).to.be(rect3);
expect(set[0]).to.be(rect1);
expect(set[1]).to.be(rect2);
result = set.pop();
expect(set.length).to.be(1);
expect(result).to.be(rect2);
expect(set[0]).to.be(rect1);
});
it("Set.push", function() {
var rect1 = s.rect(10, 20, 30, 40);
var rect2 = s.rect(10, 20, 30, 40);
var rect3 = s.rect(10, 20, 30, 40);
var set = Savage.set(rect1, rect2);
expect(set.length).to.be(2);
set.push(rect3);
expect(set.length).to.be(3);
expect(set[0]).to.be(rect1);
expect(set[1]).to.be(rect2);
expect(set[2]).to.be(rect3);
});
it("Set.splice - remove only", function() {
var rect1 = s.rect(10, 20, 30, 40);
var rect2 = s.rect(10, 20, 30, 40);
var rect3 = s.rect(10, 20, 30, 40);
var rect4 = s.rect(10, 20, 30, 40);
var set = Savage.set(rect1, rect2, rect3);
var removedSet = set.splice(1, 2);
expect(set.length).to.be(2);
expect(set[0]).to.be(rect1);
expect(set[1]).to.be(rect4);
expect(removedSet.length).to.be(2);
expect(removedSet[0]).to.be(rect2);
expect(removedSet[1]).to.be(rect3);
emptySet = set.splice(0, 0);
expect(set.length).to.be(2);
expect(emptySet.length).to.be(0);
});
it("Set.splice - remove & insert", function() {
var rect1 = s.rect(10, 20, 30, 40);
var rect2 = s.rect(10, 20, 30, 40);
var rect3 = s.rect(10, 20, 30, 40);
var rect4 = s.rect(10, 20, 30, 40);
var set = Savage.set(rect1, rect2, rect3);
var removedSet = set.splice(2, 1, rect4);
expect(set.length).to.be(3);
expect(set[0]).to.be(rect1);
expect(set[1]).to.be(rect2);
expect(set[2]).to.be(rect4);
removedSet = set.splice(0, 3, rect4, rect3, rect2, rect1);
expect(set[0]).to.be(rect4);
expect(set[1]).to.be(rect3);
expect(set[2]).to.be(rect2);
expect(set[3]).to.be(rect1);
expect(removedSet[0]).to.be(rect1);
expect(removedSet[1]).to.be(rect2);
expect(removedSet[2]).to.be(rect4);
});
});

View File

@ -39,7 +39,10 @@
<script src="attrs.js"></script> <script src="attrs.js"></script>
<script src="path.js"></script> <script src="path.js"></script>
<script src="element.js"></script> <script src="element.js"></script>
<script src="set.js"></script>
<script src="filter.js"></script> <script src="filter.js"></script>
<script src="matrix.js"></script>
<script src="mina.js"></script>
<script> <script>
mocha.checkLeaks(); mocha.checkLeaks();
window.onload = function () { window.onload = function () {