Skip to content

Commit

Permalink
unit tests for IO
Browse files Browse the repository at this point in the history
  • Loading branch information
zenozeng committed Jun 17, 2015
1 parent 62150d9 commit 03794e9
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 198 deletions.
10 changes: 5 additions & 5 deletions dist/p5.svg.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
;(function() {
/*! p5.svg.js v0.1.1 June 17, 2015 */
/*! p5.svg.js v0.1.1 June 18, 2015 */
var core, p5SVGElement, svgcanvas, renderingsvg, io, src_app;
(function (root, factory) {
if (typeof define === 'function' && define.amd)
Expand Down Expand Up @@ -1367,7 +1367,7 @@ var core, p5SVGElement, svgcanvas, renderingsvg, io, src_app;
p5.prototype.noSVG = function () {
if (this.svg) {
this.svg.remove();
this.svg = null;
this.svg = null; // this.canvas = null;
}
};
/**
Expand Down Expand Up @@ -1574,11 +1574,11 @@ var core, p5SVGElement, svgcanvas, renderingsvg, io, src_app;
'jpeg',
'png',
'jpg',
'svg'
'svg',
''
];
var ext = this._checkFileExtension(filename, '');
var ext = this._checkFileExtension(filename, '')[1];
var useSVG = svg && svg.nodeName && svg.nodeName.toLowerCase() === 'svg' && supportedExtensions.indexOf(ext) > -1;
useSVG = useSVG || arguments.length === 0;
if (useSVG) {
this.saveSVG(svg, filename);
} else {
Expand Down
6 changes: 6 additions & 0 deletions logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,9 @@
- Rewrite svg2img, _makeSVGFrame and saveSVG to allow saving a non-default SVG
- add noSVG
- Extends p5's save method with SVG support

- Unit test for pending jobs in saveFrames

- Unit tests for save()
5 changes: 2 additions & 3 deletions src/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,10 @@ define(function (require) {
svg = svg || this.svg;

var filename = args[0];
var supportedExtensions = ['jpeg', 'png', 'jpg', 'svg'];
var ext = this._checkFileExtension(filename, '');
var supportedExtensions = ['jpeg', 'png', 'jpg', 'svg', ''];
var ext = this._checkFileExtension(filename, '')[1];

var useSVG = svg && svg.nodeName && svg.nodeName.toLowerCase() === 'svg' && supportedExtensions.indexOf(ext) > -1;
useSVG = useSVG || arguments.length === 0;

if (useSVG) {
this.saveSVG(svg, filename);
Expand Down
1 change: 1 addition & 0 deletions src/rendering/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ define(function(require) {
if (this.svg) {
this.svg.remove();
this.svg = null;
// this.canvas = null;
}
};

Expand Down
4 changes: 3 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ <h2>SVG & Canvas</h2>
"unit/shape/attributes",
"unit/shape/vertex",
"unit/shape/curves",
"unit/io/io",
"unit/io/save",
"unit/io/save-svg",
"unit/io/save-frames",
"unit/rendering/rendering"
], function() {
mocha.run();
Expand Down
189 changes: 0 additions & 189 deletions test/unit/io/io.js

This file was deleted.

128 changes: 128 additions & 0 deletions test/unit/io/save-frames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
define(function(require) {
var p5 = require('p5');
require('p5.svg');
var assert = require('chai').assert;

describe('IO/saveFrames', function() {
it('should capture canvas frames', function(done) {
new p5(function(p) {
p.setup = function() {
p.createCanvas(100, 100);
p.strokeWeight(3);
p.saveFrames('hello', 'png', 0.5, 10, function(frames) {
try {
assert.ok(frames.length > 1);
done();
} catch (e) {
done(e);
} finally {
p.noCanvas();
}
});
};
p.draw = function() {
var i = p.frameCount * 2;
p.line(0, 0, i, i);
};
});
});

it('should capture svg frames', function(done) {
new p5(function(p) {
p.setup = function() {
p.createSVG(100, 100);
p.strokeWeight(3);
p.saveFrames('hello', 'svg', 0.5, 10, function(frames) {
try {
assert.ok(frames.length > 1);
done();
} catch (e) {
done(e);
}
p.noSVG();
});
};
p.draw = function() {
var i = p.frameCount * 2;
p.line(0, 0, i, i);
};
});
});

it('should capture svg frames even omitting duration and fps', function(done) {
this.timeout(0);
new p5(function(p) {
p.setup = function() {
p.createSVG(100, 100);
p.strokeWeight(3);
p.saveFrames('hello', 'svg', null, null, function(frames) {
try {
assert.ok(frames.length > 1);
done();
} catch (e) {
done(e);
}
p.noSVG();
});
};
p.draw = function() {
var i = p.frameCount * 2;
p.line(0, 0, i, i);
};
});
});

it('should download svg frames', function(done) {
new p5(function(p) {
p.setup = function() {
p.createSVG(100, 100);
var _downloadFile = p.downloadFile;
var count = 0;
p.downloadFile = function() {
count++;
if (count > 1) {
done();
p.noSVG();
}
};
p.saveFrames('hello', 'svg', 0.5, 10);
};
p.draw = function() {
var i = p.frameCount * 2;
p.line(0, 0, i, i);
};
});
});

it('should wait all pending jobs done', function(done) {
new p5(function(p) {
p.setup = function() {
p.createSVG(100, 100);
var _downloadFile = p.downloadFile;
var pending = 0;
var _makeSVGFrame = p._makeSVGFrame;
p._makeSVGFrame = function(options) {
// slow version
pending++;
setTimeout(function() {
_makeSVGFrame.call(p, options);
}, 500);
};
p.downloadFile = function() {
pending--;
if (pending === 0) {
done();
p.noSVG();
}
};
p.saveFrames('hello', 'svg', 0.5, 10);
};
p.draw = function() {
var i = p.frameCount * 2;
p.line(0, 0, i, i);
};
});
});
});

});
Loading

0 comments on commit 03794e9

Please sign in to comment.