From 858d79bb4c4d86b67280736c9d13612edf92d606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Sun, 23 Aug 2015 12:08:16 +0200 Subject: [PATCH] test: fix up some small issues --- Makefile | 1 - lib/context2d.js | 2 +- package.json | 3 +-- src/CanvasRenderingContext2d.cc | 3 ++- test/canvas.test.js | 33 +++++++++++++++---------- test/image.test.js | 44 +++++++++++++++++---------------- 6 files changed, 47 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 165474e..01ff723 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ test: $(ADDON) @./node_modules/.bin/mocha \ --reporter $(REPORTER) \ --ui exports \ - --require should \ test/*.test.js test-server: $(ADDON) diff --git a/lib/context2d.js b/lib/context2d.js index 49804bc..0941861 100644 --- a/lib/context2d.js +++ b/lib/context2d.js @@ -75,7 +75,7 @@ var parseFont = exports.parseFont = function(str){ font.style = captures[2] || 'normal'; font.size = parseFloat(captures[3]); font.unit = captures[4]; - font.family = captures[5].replace(/["']/g, '').split(',')[0]; + font.family = captures[5].replace(/["']/g, '').split(',')[0].trim(); // TODO: dpi // TODO: remaining unit conversion diff --git a/package.json b/package.json index ab8f8dd..5f0ce5d 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,7 @@ "body-parser": "^1.13.3", "express": "^4.13.2", "jade": "^1.11.0", - "mocha": "*", - "should": "*" + "mocha": "*" }, "engines": { "node": ">=0.8.0 <3" diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index e1ad7a2..c2666c8 100755 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include "Canvas.h" @@ -1307,7 +1308,7 @@ NAN_GETTER(Context2d::GetLineWidth) { NAN_SETTER(Context2d::SetLineWidth) { double n = value->NumberValue(); - if (n > 0) { + if (n > 0 && n != std::numeric_limits::infinity()) { Context2d *context = ObjectWrap::Unwrap(args.This()); cairo_set_line_width(context->context(), n); } diff --git a/test/canvas.test.js b/test/canvas.test.js index cb45811..2f66bdd 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -13,11 +13,11 @@ console.log(' cairo: %s', Canvas.cairoVersion); module.exports = { 'test .version': function(){ - Canvas.version.should.match(/^\d+\.\d+\.\d+$/); + assert.ok(/^\d+\.\d+\.\d+$/.test(Canvas.version)); }, 'test .cairoVersion': function(){ - Canvas.cairoVersion.should.match(/^\d+\.\d+\.\d+$/); + assert.ok(/^\d+\.\d+\.\d+$/.test(Canvas.cairoVersion)); }, 'test .parseFont()': function(){ @@ -39,17 +39,17 @@ module.exports = { , '20px monospace' , { size: 20, unit: 'px', family: 'monospace' } , '50px Arial, sans-serif' - , { size: 50, unit: 'px', family: 'Arial, sans-serif' } + , { size: 50, unit: 'px', family: 'Arial' } , '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' - , { size: 50, unit: 'px', family: 'Helvetica , Arial, sans-serif' } + , { size: 50, unit: 'px', family: 'Helvetica' } , '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' - , { size: 50, unit: 'px', family: 'Helvetica Neue, foo bar baz , sans-serif' } + , { size: 50, unit: 'px', family: 'Helvetica Neue' } , "50px 'Helvetica Neue'" - , { size: 50, unit: 'px', family: "Helvetica Neue" } + , { size: 50, unit: 'px', family: 'Helvetica Neue' } , 'italic 20px Arial' , { size: 20, unit: 'px', style: 'italic', family: 'Arial' } , 'oblique 20px Arial' @@ -70,9 +70,11 @@ module.exports = { var str = tests[i++] , obj = tests[i] , actual = parseFont(str); + if (!obj.style) obj.style = '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; assert.equal(10, ctx.lineWidth); - // ctx.lineWidth = Infinity; + ctx.lineWidth = Infinity; + assert.equal(10, ctx.lineWidth); + ctx.lineWidth = -Infinity; assert.equal(10, ctx.lineWidth); ctx.lineWidth = -5; assert.equal(10, ctx.lineWidth); @@ -362,10 +366,11 @@ module.exports = { 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){ assert.ok(!err); 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); }, - 'test Canvas#toDataURL() async': function(){ + 'test Canvas#toDataURL() async': function(done){ new Canvas(200,200).toDataURL(function(err, str){ assert.ok(!err); 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){ assert.ok(!err); assert.ok(0 == str.indexOf('data:image/png;base64,')); + done(); }); }, diff --git a/test/image.test.js b/test/image.test.js index 0e27327..2e15b73 100644 --- a/test/image.test.js +++ b/test/image.test.js @@ -16,40 +16,39 @@ module.exports = { 'test Image#onload': function(){ var img = new Image - , n = 0; + , onloadCalled = 0; assert.strictEqual(null, img.onload); - assert.strictEqual(false, img.complete); + img.onload = function(){ - ++n; - assert.equal(img.src, png); + onloadCalled += 1; + assert.strictEqual(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(320, img.width); assert.strictEqual(320, img.height); - assert.equal(1, n); }, - + 'test Image#onerror': function(){ var img = new Image , error - , n = 0; + , onerrorCalled = 0; assert.strictEqual(null, img.onerror); - assert.strictEqual(false, img.complete); + img.onload = function(){ assert.fail('called onload'); }; - + img.onerror = function(err){ - ++n; + onerrorCalled += 1; error = err; }; @@ -59,27 +58,30 @@ module.exports = { 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.strictEqual(false, img.complete); - assert.equal(1, n); }, - + 'test Image#{width,height}': function(){ var img = new Image - , n = 0; - + , onloadCalled = 0; + assert.strictEqual(0, img.width); assert.strictEqual(0, img.height); + img.onload = function(){ - ++n; + onloadCalled += 1; assert.strictEqual(320, img.width); 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(){