diff --git a/test/filter.js b/test/filter.js new file mode 100644 index 0000000..c748351 --- /dev/null +++ b/test/filter.js @@ -0,0 +1,83 @@ +describe("Filter methods", function () { + it("Savage.filter.blur", function() { + var str = Savage.filter.blur(3); + expect(str).to.be(''); + str = Savage.filter.blur(0.123, 8); + expect(str).to.be(''); + }); + it("Savage.filter.brightness", function() { + var str = Savage.filter.brightness(0.3); + expect(str).to.be(''); + str = Savage.filter.brightness(1); + expect(str).to.be(''); + }); + it("Savage.filter.contrast", function() { + var str = Savage.filter.contrast(0.1); + expect(str).to.be(''); + str = Savage.filter.contrast(3); + expect(str).to.be(''); + }); + it("Savage.filter.grayscale", function() { + var str = Savage.filter.grayscale(0.5); + expect(str).to.be(''); + str = Savage.filter.grayscale(1); + expect(str).to.be(''); + }); + it("Savage.filter.hueRotate", function() { + var str = Savage.filter.hueRotate(180); + expect(str).to.be(''); + str = Savage.filter.hueRotate(90); + expect(str).to.be(''); + }); + it("Savage.filter.hueRotate", function() { + var str = Savage.filter.hueRotate(180); + expect(str).to.be(''); + str = Savage.filter.hueRotate(90); + expect(str).to.be(''); + }); + it("Savage.filter.invert", function() { + var str = Savage.filter.invert(0.6); + expect(str).to.be(''); + str = Savage.filter.invert(1); + expect(str).to.be(''); + }); + it("Savage.filter.saturate", function() { + var str = Savage.filter.saturate(0.3); + expect(str).to.be(''); + str = Savage.filter.saturate(1); + expect(str).to.be(''); + }); + it("Savage.filter.sepia", function() { + var str = Savage.filter.sepia(0.3); + expect(str).to.be(''); + str = Savage.filter.sepia(1); + expect(str).to.be(''); + }); + it("Savage.filter.shadow - dx & dy", function() { + var str = Savage.filter.shadow(5, 5); + expect(str).to.be(''); + str = Savage.filter.shadow(-1, 3); + expect(str).to.be(''); + }); + it("Savage.filter.shadow - dx & dy, blur", function() { + var str = Savage.filter.shadow(5, 5, 5); + expect(str).to.be(''); + str = Savage.filter.shadow(-1, 3, 10); + expect(str).to.be(''); + }); + it("Savage.filter.shadow - dx & dy, color", function() { + var str = Savage.filter.shadow(5, 5, '#F00'); + expect(str).to.be(''); + str = Savage.filter.shadow(-1, 3, 'hsla(128, 50%, 50%, 0.8)'); + expect(str).to.be(''); + }); + + it("Savage.filter.shadow - dx & dy, blur & color", function() { + var str = Savage.filter.shadow(5, 5, 5, '#F00'); + expect(str).to.be(''); + str = Savage.filter.shadow(-1, 3, 10, 'hsla(128, 50%, 50%, 0.8)'); + expect(str).to.be(''); + }); + + +}); \ No newline at end of file diff --git a/test/matrix.js b/test/matrix.js new file mode 100644 index 0000000..ea99c58 --- /dev/null +++ b/test/matrix.js @@ -0,0 +1,172 @@ +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, 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); + }); + +}); \ No newline at end of file diff --git a/test/mina.js b/test/mina.js new file mode 100644 index 0000000..4fa0166 --- /dev/null +++ b/test/mina.js @@ -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); + }); +}); \ No newline at end of file diff --git a/test/path.js b/test/path.js index 784fd91..f400240 100644 --- a/test/path.js +++ b/test/path.js @@ -102,4 +102,277 @@ describe("Path methods", function () { height: 10 })).to.be(false); }); + it("Savage.path.toAbsolute", function() { + var relPath = "M 10 10" + + "h 40" + + "v 30" + + "h -40" + + "l 0 -30" + + "m 0 40" + + "l 30 0" + + "l 0 40" + + "l -30 0" + + "m 0 10" + + "c 20 20 40 20 40 0" + + "m -40 40" + + "c 10 -25 20 -25 30 0" + + "s 10 25 20 0" + + "m 20 -130" + + "q 30 30 60 0" + + "m -60 40" + + "q 10 20 20 0" + + "t 20 0" + + "m -40 30" + + "a 10 10 0 0 0 40 0"; + var absPath = Savage.path.toAbsolute(relPath); + var i = 0; + var checkNext = function(arr) { + expect(absPath[i++]).to.eql(arr); + } + checkNext(['M', 10, 10]); + checkNext(['H', 50]); + checkNext(['V', 40]); + checkNext(['H', 10]); + checkNext(['L', 10, 10]); + checkNext(['M', 10, 50]); + checkNext(['L', 40, 50]); + checkNext(['L', 40, 90]); + checkNext(['L', 10, 90]); + checkNext(['M', 10, 100]); + checkNext(['C', 30, 120, 50, 120, 50, 100]); + checkNext(['M', 10, 140]); + checkNext(['C', 20, 115, 30, 115, 40, 140]); + checkNext(['S', 50, 165, 60, 140]); + checkNext(['M', 80, 10]); + checkNext(['Q', 110, 40, 140, 10]); + checkNext(['M', 80, 50]); + checkNext(['Q', 90, 70, 100, 50]); + checkNext(['T', 120, 50]); + checkNext(['M', 80, 80]); + checkNext(['A', 10, 10, 0, 0, 0, 120, 80]); + }); + it("Savage.path.toRelative", function() { + var absPath = "M10 10 H 50 V 40 H 10 L 10 10" + + "M10 50 L 40 50 L 40 90 L 10 90" + + "M10 100 C 30 120, 50 120, 50 100" + + "M10 140 C 20 115, 30, 115, 40 140 S 50 165, 60 140" + + "M80 10 Q 110 40, 140 10" + + "M80 50 Q 90 70, 100 50 T 120 50" + + "M80 80 A 10 10 0 0 0 120 80"; + var relPath = Savage.path.toRelative(absPath); + var i = 0; + var checkNext = function(arr) { + expect(relPath[i++]).to.eql(arr); + } + + checkNext(['M', 10, 10]); + checkNext(['h', 40]); + checkNext(['v', 30]); + checkNext(['h', -40]); + checkNext(['l', 0, -30]); + checkNext(['m', 0, 40]); + checkNext(['l', 30, 0]); + checkNext(['l', 0, 40]); + checkNext(['l', -30, 0]); + checkNext(['m', 0, 10]); + checkNext(['c', 20, 20, 40, 20, 40, 0]); + checkNext(['m', -40, 40]); + checkNext(['c', 10, -25, 20, -25, 30, 0]); + checkNext(['s', 10, 25, 20, 0]); + checkNext(['m', 20, -130]); + checkNext(['q', 30, 30, 60, 0]); + checkNext(['m', -60, 40]); + checkNext(['q', 10, 20, 20, 0]); + checkNext(['t', 20, 0]); + checkNext(['m', -40, 30]); + checkNext(['a', 10, 10, 0, 0, 0, 40, 0]); + }); + it("Savage.path.toCubic", function() { + var absPath = "M10 10 H 50 V 40 H 10 L 10 10" + + "M10 50 L 40 50 L 40 90 L 10 90" + + "M10 100 C 30 120, 50 120, 50 100" + + "M10 140 C 20 115, 30, 115, 40 140 S 50 165, 60 140" + + "M80 10 Q 110 40, 140 10" + + "M80 50 Q 90 70, 100 50 T 120 50" + + "M80 80 A 10 10 0 0 0 120 80"; + var relPath = "M 10 10" + + "h 40" + + "v 30" + + "h -40" + + "l 0 -30" + + "m 0 40" + + "l 30 0" + + "l 0 40" + + "l -30 0" + + "m 0 10" + + "c 20 20 40 20 40 0" + + "m -40 40" + + "c 10 -25 20 -25 30 0" + + "s 10 25 20 0" + + "m 20 -130" + + "q 30 30 60 0" + + "m -60 40" + + "q 10 20 20 0" + + "t 20 0" + + "m -40 30" + + "a 10 10 0 0 0 40 0"; + var cubicPathFromAbs = Savage.path.toCubic(absPath); + var cubicPathFromRel = Savage.path.toCubic(relPath); + var i = 0; + var checkNext = function(arr) { + expect(cubicPathFromAbs[i]).to.eql(arr); + expect(cubicPathFromRel[i]).to.eql(arr); + i++; + } + + checkNext(['M', 10, 10]); + checkNext(['C', 10, 10, 50, 10, 50, 10]); + checkNext(['C', 50, 10, 50, 40, 50, 40]); + checkNext(['C', 50, 40, 10, 40, 10, 40]); + checkNext(['C', 10, 40, 10, 10, 10, 10]); + checkNext(['M', 10, 50]); + checkNext(['C', 10, 50, 40, 50, 40, 50]); + checkNext(['C', 40, 50, 40, 90, 40, 90]); + checkNext(['C', 40, 90, 10, 90, 10, 90]); + checkNext(['M', 10, 100]); + checkNext(['C', 30, 120, 50, 120, 50, 100]); + checkNext(['M', 10, 140]); + checkNext(['C', 20, 115, 30, 115, 40, 140]); + checkNext(['C', 50, 165, 50, 165, 60, 140]); + checkNext(['M', 80, 10]); + checkNext(['C', 100, 29.999999999999996, 120, 29.999999999999996, 140, 10]); + checkNext(['M', 80, 50]); + checkNext(['C', 86.66666666666666, 63.33333333333333, 93.33333333333333, 63.33333333333333, 100, 50]); + checkNext(['C', 106.66666666666666, 36.666666666666664, 113.33333333333333, 36.666666666666664, 120, 50]); + checkNext(['M', 80, 80]); + checkNext(['C', 80, 95.39600717839002, 96.66666666666667, 105.01851166488379, 110, 97.32050807568878]); + checkNext(['C', 116.18802153517007, 93.74785217660714, 120, 87.14531179816328, 120, 80]); + + }); + it("Savage.path.map", function() { + var absPath = "M10 10 H 50 V 40 H 10 L 10 10" + + "M10 50 L 40 50 L 40 90 L 10 90" + + "M10 100 C 30 120, 50 120, 50 100" + + "M10 140 C 20 115, 30, 115, 40 140 S 50 165, 60 140" + + "M80 10 Q 110 40, 140 10" + + "M80 50 Q 90 70, 100 50 T 120 50" + + "M80 80 A 10 10 0 0 0 120 80"; + var matrix = new Savage.Matrix(1, 0, 0, 1, 1000, 0); + var transformedPath = Savage.path.map(absPath, matrix); + + var i = 0; + var checkNext = function(arr) { + expect(transformedPath[i++]).to.eql(arr); + } + checkNext(['M', 1010, 10]); + checkNext(['C', 1010, 10, 1050, 10, 1050, 10]); + checkNext(['C', 1050, 10, 1050, 40, 1050, 40]); + checkNext(['C', 1050, 40, 1010, 40, 1010, 40]); + checkNext(['C', 1010, 40, 1010, 10, 1010, 10]); + checkNext(['M', 1010, 50]); + checkNext(['C', 1010, 50, 1040, 50, 1040, 50]); + checkNext(['C', 1040, 50, 1040, 90, 1040, 90]); + checkNext(['C', 1040, 90, 1010, 90, 1010, 90]); + checkNext(['M', 1010, 100]); + checkNext(['C', 1030, 120, 1050, 120, 1050, 100]); + checkNext(['M', 1010, 140]); + checkNext(['C', 1020, 115, 1030, 115, 1040, 140]); + checkNext(['C', 1050, 165, 1050, 165, 1060, 140]); + checkNext(['M', 1080, 10]); + checkNext(['C', 1100, 29.999999999999996, 1120, 29.999999999999996, 1140, 10]); + checkNext(['M', 1080, 50]); + checkNext(['C', 1086.66666666666666, 63.33333333333333, 1093.33333333333333, 63.33333333333333, 1100, 50]); + checkNext(['C', 1106.66666666666666, 36.666666666666664, 1113.33333333333333, 36.666666666666664, 1120, 50]); + checkNext(['M', 1080, 80]); + checkNext(['C', 1080, 95.39600717839002, 1096.66666666666667, 105.01851166488379, 1110, 97.32050807568878]); + checkNext(['C', 1116.18802153517007, 93.74785217660714, 1120, 87.14531179816328, 1120, 80]); + }); + it("Savage.path.isPointInside", function () { + var path = "M10 10 H 50 V 40 H 10 L 10 10 Z" + + "M10 50 L 40 50 L 40 90 L 10 90 Z" + + "M10 100 C 30 120, 50 120, 50 100 Z" + + "M10 140 C 20 115, 30, 115, 40 140 S 50 165, 60 140 Z" + + "M80 10 Q 110 40, 140 10 Z" + + "M80 50 Q 90 70, 100 50 T 120 50 Z" + + "M80 80 A 10 10 0 0 0 120 80 Z"; + + expect(Savage.path.isPointInside(path, 15, 35)).to.be(true); + expect(Savage.path.isPointInside(path, 35, 75)).to.be(true); + expect(Savage.path.isPointInside(path, 15, 102)).to.be(true); + expect(Savage.path.isPointInside(path, 15, 135)).to.be(true); + expect(Savage.path.isPointInside(path, 50, 145)).to.be(true); + expect(Savage.path.isPointInside(path, 130, 15)).to.be(true); + expect(Savage.path.isPointInside(path, 110, 45)).to.be(true); + expect(Savage.path.isPointInside(path, 85, 55)).to.be(true); + expect(Savage.path.isPointInside(path, 115, 82)).to.be(true); + expect(Savage.path.isPointInside(path, 95, 98)).to.be(true); + + expect(Savage.path.isPointInside(path, 5, 5)).to.be(false); + expect(Savage.path.isPointInside(path, 25, 48)).to.be(false); + expect(Savage.path.isPointInside(path, 42, 87)).to.be(false); + expect(Savage.path.isPointInside(path, 12, 105)).to.be(false); + expect(Savage.path.isPointInside(path, 47, 113)).to.be(false); + expect(Savage.path.isPointInside(path, 47, 135)).to.be(false); + expect(Savage.path.isPointInside(path, 25, 142)).to.be(false); + expect(Savage.path.isPointInside(path, 15, 125)).to.be(false); + expect(Savage.path.isPointInside(path, 43, 152)).to.be(false); + expect(Savage.path.isPointInside(path, 58, 152)).to.be(false); + expect(Savage.path.isPointInside(path, 90, 21)).to.be(false); + expect(Savage.path.isPointInside(path, 130, 21)).to.be(false); + expect(Savage.path.isPointInside(path, 95, 48)).to.be(false); + expect(Savage.path.isPointInside(path, 110, 55)).to.be(false); + expect(Savage.path.isPointInside(path, 100, 70)).to.be(false); + expect(Savage.path.isPointInside(path, 115, 96)).to.be(false); + expect(Savage.path.isPointInside(path, 85, 96)).to.be(false); + }); + + it("Savage.path.intersection", function () { + var path1 = "M10 10 H 50 V 40 H 10 L 10 10 Z" + + "M10 50 L 40 50 L 40 90 L 10 90 Z" + + "M10 100 C 30 120, 50 120, 50 100 Z" + + "M10 140 C 20 115, 30, 115, 40 140 S 50 165, 60 140 Z" + + "M80 10 Q 110 40, 140 10 Z" + + "M80 50 Q 90 70, 100 50 T 120 50 Z" + + "M80 80 A 10 10 0 0 0 120 80 Z"; + var path2 = "M10,0 L80,200 L20, 200 L30, 0 L110, 15 L90, 150"; + var intersection = Savage.path.intersection(path1, path2); + expect(intersection.length).to.be(22); + var first = intersection[0]; + expect(first.x).to.be.a('number'); + expect(first.y).to.be.a('number'); + expect(first.t1).to.be.a('number'); + expect(first.t2).to.be.a('number'); + expect(first.segment1).to.be.a('number'); + expect(first.segment2).to.be.a('number'); + expect(first.bez1).to.be.an('array'); + expect(first.bez2).to.be.an('array'); + + var checkXY = function(index, x, y) { + expect(+intersection[index].x.toFixed(2)).to.be(x); + expect(+intersection[index].y.toFixed(2)).to.be(y); + } + + checkXY(0, 13.5, 10); + checkXY(1, 29.5, 10); + checkXY(2, 24, 40); + checkXY(3, 28, 40); + checkXY(4, 27.5, 50); + checkXY(5, 27.5, 50); + checkXY(6, 40, 85.71); + checkXY(7, 25.5, 90); + checkXY(8, 48.08, 108.79); + checkXY(9, 24.45, 110.99); + checkXY(10, 45, 100); + checkXY(11, 25, 100); + checkXY(12, 23.92, 121.52); + checkXY(13, 59.46, 141.32); + checkXY(14, 59, 140); + checkXY(15, 23, 140); + checkXY(16, 108.53, 24.91); + checkXY(17, 83.33, 10); + checkXY(18, 106, 42); + checkXY(19, 104.81, 50); + checkXY(20, 97.45, 99.7); + checkXY(21, 100.37, 80); + }); }); diff --git a/test/set.js b/test/set.js new file mode 100644 index 0000000..1b7a6f0 --- /dev/null +++ b/test/set.js @@ -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); + }); +}); \ No newline at end of file diff --git a/test/test.html b/test/test.html index 322e982..ace0511 100644 --- a/test/test.html +++ b/test/test.html @@ -39,6 +39,10 @@ + + + +