Browse Source

test: fix up some small issues

v1.x
Linus Unnebäck 10 years ago
parent
commit
858d79bb4c
  1. 1
      Makefile
  2. 2
      lib/context2d.js
  3. 3
      package.json
  4. 3
      src/CanvasRenderingContext2d.cc
  5. 33
      test/canvas.test.js
  6. 36
      test/image.test.js

1
Makefile

@ -9,7 +9,6 @@ test: $(ADDON)
@./node_modules/.bin/mocha \ @./node_modules/.bin/mocha \
--reporter $(REPORTER) \ --reporter $(REPORTER) \
--ui exports \ --ui exports \
--require should \
test/*.test.js test/*.test.js
test-server: $(ADDON) test-server: $(ADDON)

2
lib/context2d.js

@ -75,7 +75,7 @@ var parseFont = exports.parseFont = function(str){
font.style = captures[2] || 'normal'; font.style = captures[2] || 'normal';
font.size = parseFloat(captures[3]); font.size = parseFloat(captures[3]);
font.unit = captures[4]; font.unit = captures[4];
font.family = captures[5].replace(/["']/g, '').split(',')[0]; font.family = captures[5].replace(/["']/g, '').split(',')[0].trim();
// TODO: dpi // TODO: dpi
// TODO: remaining unit conversion // TODO: remaining unit conversion

3
package.json

@ -30,8 +30,7 @@
"body-parser": "^1.13.3", "body-parser": "^1.13.3",
"express": "^4.13.2", "express": "^4.13.2",
"jade": "^1.11.0", "jade": "^1.11.0",
"mocha": "*", "mocha": "*"
"should": "*"
}, },
"engines": { "engines": {
"node": ">=0.8.0 <3" "node": ">=0.8.0 <3"

3
src/CanvasRenderingContext2d.cc

@ -8,6 +8,7 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include "Canvas.h" #include "Canvas.h"
@ -1307,7 +1308,7 @@ NAN_GETTER(Context2d::GetLineWidth) {
NAN_SETTER(Context2d::SetLineWidth) { NAN_SETTER(Context2d::SetLineWidth) {
double n = value->NumberValue(); double n = value->NumberValue();
if (n > 0) { if (n > 0 && n != std::numeric_limits<double>::infinity()) {
Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This()); Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This());
cairo_set_line_width(context->context(), n); cairo_set_line_width(context->context(), n);
} }

33
test/canvas.test.js

@ -13,11 +13,11 @@ console.log(' cairo: %s', Canvas.cairoVersion);
module.exports = { module.exports = {
'test .version': function(){ 'test .version': function(){
Canvas.version.should.match(/^\d+\.\d+\.\d+$/); assert.ok(/^\d+\.\d+\.\d+$/.test(Canvas.version));
}, },
'test .cairoVersion': function(){ 'test .cairoVersion': function(){
Canvas.cairoVersion.should.match(/^\d+\.\d+\.\d+$/); assert.ok(/^\d+\.\d+\.\d+$/.test(Canvas.cairoVersion));
}, },
'test .parseFont()': function(){ 'test .parseFont()': function(){
@ -39,17 +39,17 @@ module.exports = {
, '20px monospace' , '20px monospace'
, { size: 20, unit: 'px', family: 'monospace' } , { size: 20, unit: 'px', family: 'monospace' }
, '50px Arial, sans-serif' , '50px Arial, sans-serif'
, { size: 50, unit: 'px', family: 'Arial, sans-serif' } , { size: 50, unit: 'px', family: 'Arial' }
, 'bold italic 50px Arial, sans-serif' , 'bold italic 50px Arial, sans-serif'
, { style: 'italic', weight: 'bold', size: 50, unit: 'px', family: 'Arial, sans-serif' } , { style: 'italic', weight: 'bold', size: 50, unit: 'px', family: 'Arial' }
, '50px Helvetica , Arial, sans-serif' , '50px Helvetica , Arial, sans-serif'
, { size: 50, unit: 'px', family: 'Helvetica , Arial, sans-serif' } , { size: 50, unit: 'px', family: 'Helvetica' }
, '50px "Helvetica Neue", sans-serif' , '50px "Helvetica Neue", sans-serif'
, { size: 50, unit: 'px', family: 'Helvetica Neue, sans-serif' } , { size: 50, unit: 'px', family: 'Helvetica Neue' }
, '50px "Helvetica Neue", "foo bar baz" , sans-serif' , '50px "Helvetica Neue", "foo bar baz" , sans-serif'
, { size: 50, unit: 'px', family: 'Helvetica Neue, foo bar baz , sans-serif' } , { size: 50, unit: 'px', family: 'Helvetica Neue' }
, "50px 'Helvetica Neue'" , "50px 'Helvetica Neue'"
, { size: 50, unit: 'px', family: "Helvetica Neue" } , { size: 50, unit: 'px', family: 'Helvetica Neue' }
, 'italic 20px Arial' , 'italic 20px Arial'
, { size: 20, unit: 'px', style: 'italic', family: 'Arial' } , { size: 20, unit: 'px', style: 'italic', family: 'Arial' }
, 'oblique 20px Arial' , 'oblique 20px Arial'
@ -70,9 +70,11 @@ module.exports = {
var str = tests[i++] var str = tests[i++]
, obj = tests[i] , obj = tests[i]
, actual = parseFont(str); , actual = parseFont(str);
if (!obj.style) obj.style = 'normal'; if (!obj.style) obj.style = 'normal';
if (!obj.weight) obj.weight = 'normal'; if (!obj.weight) obj.weight = 'normal';
// actual.should.eql(obj);
assert.deepEqual(obj, actual);
} }
}, },
@ -270,7 +272,9 @@ module.exports = {
ctx.lineWidth = 10.0; ctx.lineWidth = 10.0;
assert.equal(10, ctx.lineWidth); assert.equal(10, ctx.lineWidth);
// ctx.lineWidth = Infinity; ctx.lineWidth = Infinity;
assert.equal(10, ctx.lineWidth);
ctx.lineWidth = -Infinity;
assert.equal(10, ctx.lineWidth); assert.equal(10, ctx.lineWidth);
ctx.lineWidth = -5; ctx.lineWidth = -5;
assert.equal(10, ctx.lineWidth); assert.equal(10, ctx.lineWidth);
@ -362,10 +366,11 @@ module.exports = {
assert.equal('PNG', buf.slice(1,4).toString()); assert.equal('PNG', buf.slice(1,4).toString());
}, },
'test Canvas#toBuffer() async': function(){ 'test Canvas#toBuffer() async': function(done){
new Canvas(200, 200).toBuffer(function(err, buf){ new Canvas(200, 200).toBuffer(function(err, buf){
assert.ok(!err); assert.ok(!err);
assert.equal('PNG', buf.slice(1,4).toString()); assert.equal('PNG', buf.slice(1,4).toString());
done();
}); });
}, },
@ -389,17 +394,19 @@ module.exports = {
assert.equal('currently only image/png is supported', err.message); assert.equal('currently only image/png is supported', err.message);
}, },
'test Canvas#toDataURL() async': function(){ 'test Canvas#toDataURL() async': function(done){
new Canvas(200,200).toDataURL(function(err, str){ new Canvas(200,200).toDataURL(function(err, str){
assert.ok(!err); assert.ok(!err);
assert.ok(0 == str.indexOf('data:image/png;base64,')); assert.ok(0 == str.indexOf('data:image/png;base64,'));
done();
}); });
}, },
'test Canvas#toDataURL() async with type': function(){ 'test Canvas#toDataURL() async with type': function(done){
new Canvas(200,200).toDataURL('image/png', function(err, str){ new Canvas(200,200).toDataURL('image/png', function(err, str){
assert.ok(!err); assert.ok(!err);
assert.ok(0 == str.indexOf('data:image/png;base64,')); assert.ok(0 == str.indexOf('data:image/png;base64,'));
done();
}); });
}, },

36
test/image.test.js

@ -16,40 +16,39 @@ module.exports = {
'test Image#onload': function(){ 'test Image#onload': function(){
var img = new Image var img = new Image
, n = 0; , onloadCalled = 0;
assert.strictEqual(null, img.onload); assert.strictEqual(null, img.onload);
assert.strictEqual(false, img.complete); assert.strictEqual(false, img.complete);
img.onload = function(){ img.onload = function(){
++n; onloadCalled += 1;
assert.equal(img.src, png); assert.strictEqual(img.src, png);
}; };
img.src = png; img.src = png;
assert.equal(img.src, png); assert.strictEqual(1, onloadCalled);
assert.strictEqual(img.src, png);
assert.equal(img.src, png);
assert.strictEqual(true, img.complete); assert.strictEqual(true, img.complete);
assert.strictEqual(320, img.width); assert.strictEqual(320, img.width);
assert.strictEqual(320, img.height); assert.strictEqual(320, img.height);
assert.equal(1, n);
}, },
'test Image#onerror': function(){ 'test Image#onerror': function(){
var img = new Image var img = new Image
, error , error
, n = 0; , onerrorCalled = 0;
assert.strictEqual(null, img.onerror); assert.strictEqual(null, img.onerror);
assert.strictEqual(false, img.complete); assert.strictEqual(false, img.complete);
img.onload = function(){ img.onload = function(){
assert.fail('called onload'); assert.fail('called onload');
}; };
img.onerror = function(err){ img.onerror = function(err){
++n; onerrorCalled += 1;
error = err; error = err;
}; };
@ -59,27 +58,30 @@ module.exports = {
assert.fail('error did not invoke onerror(): ' + err); assert.fail('error did not invoke onerror(): ' + err);
} }
assert.equal(img.src, png + 's'); assert.strictEqual(1, onerrorCalled);
assert.strictEqual(img.src, png + 's');
assert.strictEqual(false, img.complete);
assert.ok(error instanceof Error, 'did not invoke onerror() with error'); assert.ok(error instanceof Error, 'did not invoke onerror() with error');
assert.strictEqual(false, img.complete);
assert.equal(1, n);
}, },
'test Image#{width,height}': function(){ 'test Image#{width,height}': function(){
var img = new Image var img = new Image
, n = 0; , onloadCalled = 0;
assert.strictEqual(0, img.width); assert.strictEqual(0, img.width);
assert.strictEqual(0, img.height); assert.strictEqual(0, img.height);
img.onload = function(){ img.onload = function(){
++n; onloadCalled += 1;
assert.strictEqual(320, img.width); assert.strictEqual(320, img.width);
assert.strictEqual(320, img.height); assert.strictEqual(320, img.height);
}; };
img.src = png;
assert.equal(1, n); img.src = png;
assert.strictEqual(1, onloadCalled);
assert.strictEqual(320, img.width);
assert.strictEqual(320, img.height);
}, },
'test Image#src set empty buffer': function(){ 'test Image#src set empty buffer': function(){

Loading…
Cancel
Save