From 9c046ec877fe6c853bd11e3ee42523c4eb4dd841 Mon Sep 17 00:00:00 2001 From: "xiaochen.gaoxc" Date: Mon, 22 May 2017 23:11:03 +0800 Subject: [PATCH] fix: skip regExp & Function property --- .travis.yml | 1 - lib/v1/encoder.js | 2 +- package.json | 3 +- test/array.test.js | 14 +- test/binary.test.js | 145 +++++------ test/boolean.test.js | 22 +- test/date.test.js | 86 +++---- test/decode.circular.test.js | 15 +- test/double.test.js | 461 ++++++++++++++++++++++------------- test/exception.test.js | 154 +++++++----- test/int.test.js | 331 ++++++++++++------------- test/list.test.js | 151 ++++++------ test/long.test.js | 447 +++++++++++++++++---------------- test/map.test.js | 80 +++--- test/null.test.js | 14 +- test/object.test.js | 203 +++++++-------- test/string.test.js | 317 ++++++++++++++---------- test/utils.test.js | 10 +- test/v1.test.js | 210 ++++++++-------- 19 files changed, 1471 insertions(+), 1195 deletions(-) diff --git a/.travis.yml b/.travis.yml index 839e7c3..67165de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,5 @@ node_js: - '5' - '4' - '0.12' - - '0.10' script: "make test-cov" after_script: "npm i codecov && codecov" diff --git a/lib/v1/encoder.js b/lib/v1/encoder.js index 078eb4b..2df0948 100644 --- a/lib/v1/encoder.js +++ b/lib/v1/encoder.js @@ -432,7 +432,7 @@ proto.writeList = proto.writeArray; */ proto.write = function (val) { var type = typeof val; - if (is.nullOrUndefined(val) || is.NaN(val)) { + if (is.nullOrUndefined(val) || is.NaN(val) || is.function(val) || is.regExp(val)) { return this.writeNull(); } switch (type) { diff --git a/package.json b/package.json index 183fd48..0a142bf 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ "js-to-java": "2", "jshint": "*", "long": "2", - "mocha": "*", - "should": "5" + "mocha": "*" } } diff --git a/test/array.test.js b/test/array.test.js index 9f407f3..0bc6e0d 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -8,11 +8,7 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); describe('array.test.js', function () { @@ -28,7 +24,7 @@ describe('array.test.js', function () { } ]); var a = hessian.decode(b); - a.should.eql([null, [1]]); + assert.deepEqual(a, [null, [1]]); }); it('should write undefined v1', function () { @@ -43,7 +39,7 @@ describe('array.test.js', function () { } ]); var a = hessian.decode(b); - a.should.eql([null, [1]]); + assert.deepEqual(a, [null, [1]]); }); it('should write null v2', function () { @@ -58,7 +54,7 @@ describe('array.test.js', function () { } ], '2.0'); var a = hessian.decode(b, '2.0'); - a.should.eql([null, [1]]); + assert.deepEqual(a, [null, [1]]); }); it('should write undefined v2', function () { @@ -73,6 +69,6 @@ describe('array.test.js', function () { } ], '2.0'); var a = hessian.decode(b, '2.0'); - a.should.eql([null, [1]]); + assert.deepEqual(a, [null, [1]]); }); }); \ No newline at end of file diff --git a/test/binary.test.js b/test/binary.test.js index c9f0ce2..93e7f26 100644 --- a/test/binary.test.js +++ b/test/binary.test.js @@ -10,11 +10,7 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); var utils = require('./utils'); @@ -24,89 +20,94 @@ describe('binary.test.js', function () { }); it('should write "foo"', function () { - hessian.encode(new Buffer('foo')).should.eql( - Buffer.concat([new Buffer(['B'.charCodeAt(0), 0x00, 0x03]), new Buffer('foo')])); + assert.deepEqual( + hessian.encode(new Buffer('foo')), + Buffer.concat([new Buffer(['B'.charCodeAt(0), 0x00, 0x03]), new Buffer('foo')]) + ); }); it('should read and write empty binary', function () { var empty = hessian.decode(new Buffer(['B'.charCodeAt(0), 0x00, 0x00])); - empty.should.be.a.Buffer; - empty.should.length(0); + assert(Buffer.isBuffer(empty)); + assert(empty.length === 0); - hessian.encode(new Buffer('')).should.eql(new Buffer(['B'.charCodeAt(0), 0x00, 0x00])); + assert.deepEqual( + hessian.encode(new Buffer('')), + new Buffer(['B'.charCodeAt(0), 0x00, 0x00]) + ); }); it('should write and read as java impl', function () { var bytes = new Buffer(65535); bytes.fill(0x41); var buf = hessian.encode(bytes, '1.0'); - buf.should.length(utils.bytes('v1/bytes/65535').length); - buf.should.eql(utils.bytes('v1/bytes/65535')); - hessian.decode(utils.bytes('v1/bytes/65535'), '1.0').should.eql(bytes); + assert(buf.length === utils.bytes('v1/bytes/65535').length); + assert.deepEqual(buf, utils.bytes('v1/bytes/65535')); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/65535'), '1.0'), bytes); var bytes = new Buffer(32768); bytes.fill(0x41); var buf = hessian.encode(bytes, '1.0'); - buf.should.length(utils.bytes('v1/bytes/32768').length); - buf.should.eql(utils.bytes('v1/bytes/32768')); - hessian.decode(utils.bytes('v1/bytes/32768'), '1.0').should.eql(bytes); + assert(buf.length === utils.bytes('v1/bytes/32768').length); + assert.deepEqual(buf, utils.bytes('v1/bytes/32768')); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32768'), '1.0'), bytes); var bytes = new Buffer(32769); bytes.fill(0x41); var buf = hessian.encode(bytes, '1.0'); - buf.should.length(utils.bytes('v1/bytes/32769').length); - buf.should.eql(utils.bytes('v1/bytes/32769')); - hessian.decode(utils.bytes('v1/bytes/32769'), '1.0').should.eql(bytes); + assert(buf.length === utils.bytes('v1/bytes/32769').length); + assert.deepEqual(buf, utils.bytes('v1/bytes/32769')); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '1.0'), bytes); var bytes = new Buffer(32767); bytes.fill(0x41); var buf = hessian.encode(bytes, '1.0'); - buf.should.length(utils.bytes('v1/bytes/32767').length); - buf.should.eql(utils.bytes('v1/bytes/32767')); - hessian.decode(utils.bytes('v1/bytes/32767'), '1.0').should.eql(bytes); + assert(buf.length === utils.bytes('v1/bytes/32767').length); + assert.deepEqual(buf, utils.bytes('v1/bytes/32767')); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32767'), '1.0'), bytes); var bytes = new Buffer(32769); bytes.fill(0x41); var buf = hessian.encode(bytes, '1.0'); - buf.should.length(utils.bytes('v1/bytes/32769').length); - buf.should.eql(utils.bytes('v1/bytes/32769')); - hessian.decode(utils.bytes('v1/bytes/32769'), '1.0').should.eql(bytes); + assert(buf.length === utils.bytes('v1/bytes/32769').length); + assert.deepEqual(buf, utils.bytes('v1/bytes/32769')); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '1.0'), bytes); var bytes = new Buffer(42769); bytes.fill(0x41); var buf = hessian.encode(bytes, '1.0'); - buf.should.length(utils.bytes('v1/bytes/42769').length); - buf.should.eql(utils.bytes('v1/bytes/42769')); - hessian.decode(utils.bytes('v1/bytes/42769'), '1.0').should.eql(bytes); + assert(buf.length === utils.bytes('v1/bytes/42769').length); + assert.deepEqual(buf, utils.bytes('v1/bytes/42769')); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/42769'), '1.0'), bytes); var bytes = new Buffer(82769); bytes.fill(0x41); var buf = hessian.encode(bytes, '1.0'); - buf.should.length(utils.bytes('v1/bytes/82769').length); - buf.should.eql(utils.bytes('v1/bytes/82769')); - hessian.decode(utils.bytes('v1/bytes/82769'), '1.0').should.eql(bytes); + assert(buf.length === utils.bytes('v1/bytes/82769').length); + assert.deepEqual(buf, utils.bytes('v1/bytes/82769')); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/82769'), '1.0'), bytes); }); describe('v2.0', function () { it('should read zero length binary data', function () { var buf = hessian.decode(new Buffer([0x20]), '2.0'); - buf.should.length(0); - buf.should.eql(new Buffer(0)); + assert(buf.length === 0); + assert.deepEqual(buf, new Buffer(0)); }); it('should read short datas', function () { var decoder = new hessian.DecoderV2(new Buffer([0x20, 0x23, 0x23, 0x02, 0x03, 0x20])); var buf = decoder.read(); - buf.should.length(0); - buf.should.eql(new Buffer(0)); + assert(buf.length === 0); + assert.deepEqual(buf, new Buffer(0)); buf = decoder.read(); - buf.should.length(3); - buf.should.eql(new Buffer([0x23, 2, 3])); + assert(buf.length === 3); + assert.deepEqual(buf, new Buffer([0x23, 2, 3])); buf = decoder.read(); - buf.should.length(0); - buf.should.eql(new Buffer(0)); + assert(buf.length === 0); + assert.deepEqual(buf, new Buffer(0)); }); it('should read max length short datas', function () { @@ -114,15 +115,15 @@ describe('binary.test.js', function () { input.fill(0x2f); input[0] = 0x2f; var buf = hessian.decode(input, '2.0'); - buf.should.length(15); + assert(buf.length === 15); var output = new Buffer(15); output.fill(0x2f); - buf.should.eql(output); + assert.deepEqual(buf, output); }); it('should read long binary', function () { var buf = hessian.encode(new Buffer(65535), '2.0'); - buf[0].should.equal(0x62); + assert(buf[0] === 0x62); hessian.decode(buf, '2.0'); buf = hessian.encode(new Buffer(65536), '2.0'); @@ -133,92 +134,92 @@ describe('binary.test.js', function () { }); it('should write short binary', function () { - hessian.encode(new Buffer(''), '2.0').should.eql(new Buffer([0x20])); + assert.deepEqual(hessian.encode(new Buffer(''), '2.0'), new Buffer([0x20])); }); it('should write and read as java impl', function () { var bytes = new Buffer(65535); bytes.fill(0x41); var buf = hessian.encode(bytes, '2.0'); - buf.should.length(utils.bytes('v2/bytes/65535').length); - buf.should.eql(utils.bytes('v2/bytes/65535')); - hessian.decode(utils.bytes('v2/bytes/65535'), '2.0').should.eql(bytes); + assert(buf.length === utils.bytes('v2/bytes/65535').length); + assert.deepEqual(buf, utils.bytes('v2/bytes/65535')); + assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/65535'), '2.0'), bytes); var bytes = new Buffer(32768); bytes.fill(0x41); var buf = hessian.encode(bytes, '2.0'); - buf.should.length(utils.bytes('v2/bytes/32768').length); - buf.should.eql(utils.bytes('v2/bytes/32768')); - hessian.decode(utils.bytes('v2/bytes/32768'), '2.0').should.eql(bytes); + assert(buf.length === utils.bytes('v2/bytes/32768').length); + assert.deepEqual(buf, utils.bytes('v2/bytes/32768')); + assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/32768'), '2.0'), bytes); var bytes = new Buffer(32769); bytes.fill(0x41); var buf = hessian.encode(bytes, '2.0'); - buf.should.length(utils.bytes('v2/bytes/32769').length); - buf.should.eql(utils.bytes('v2/bytes/32769')); - hessian.decode(utils.bytes('v2/bytes/32769'), '2.0').should.eql(bytes); + assert(buf.length === utils.bytes('v2/bytes/32769').length); + assert.deepEqual(buf, utils.bytes('v2/bytes/32769')); + assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/32769'), '2.0'), bytes); var bytes = new Buffer(32767); bytes.fill(0x41); var buf = hessian.encode(bytes, '2.0'); - buf.should.length(utils.bytes('v2/bytes/32767').length); - buf.should.eql(utils.bytes('v2/bytes/32767')); - hessian.decode(utils.bytes('v2/bytes/32767'), '2.0').should.eql(bytes); + assert(buf.length === utils.bytes('v2/bytes/32767').length); + assert.deepEqual(buf, utils.bytes('v2/bytes/32767')); + assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/32767'), '2.0'), bytes); var bytes = new Buffer(32769); bytes.fill(0x41); var buf = hessian.encode(bytes, '2.0'); - buf.should.length(utils.bytes('v2/bytes/32769').length); - buf.should.eql(utils.bytes('v2/bytes/32769')); - hessian.decode(utils.bytes('v2/bytes/32769'), '2.0').should.eql(bytes); + assert(buf.length === utils.bytes('v2/bytes/32769').length); + assert.deepEqual(buf, utils.bytes('v2/bytes/32769')); + assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/32769'), '2.0'), bytes); var bytes = new Buffer(42769); bytes.fill(0x41); var buf = hessian.encode(bytes, '2.0'); - buf.should.length(utils.bytes('v2/bytes/42769').length); - buf.should.eql(utils.bytes('v2/bytes/42769')); - hessian.decode(utils.bytes('v2/bytes/42769'), '2.0').should.eql(bytes); + assert(buf.length === utils.bytes('v2/bytes/42769').length); + assert.deepEqual(buf, utils.bytes('v2/bytes/42769')); + assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/42769'), '2.0'), bytes); var bytes = new Buffer(82769); bytes.fill(0x41); var buf = hessian.encode(bytes, '2.0'); - buf.should.length(utils.bytes('v2/bytes/82769').length); - buf.should.eql(utils.bytes('v2/bytes/82769')); - hessian.decode(utils.bytes('v2/bytes/82769'), '2.0').should.eql(bytes); + assert(buf.length === utils.bytes('v2/bytes/82769').length); + assert.deepEqual(buf, utils.bytes('v2/bytes/82769')); + assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/82769'), '2.0'), bytes); }); it('should read java hessian 1.0 bin format', function () { var bytes = new Buffer(65535); bytes.fill(0x41); - hessian.decode(utils.bytes('v1/bytes/65535'), '2.0').should.eql(bytes); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/65535'), '2.0'), bytes); var bytes = new Buffer(32767); bytes.fill(0x41); - hessian.decode(utils.bytes('v1/bytes/32767'), '2.0').should.eql(bytes); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32767'), '2.0'), bytes); var bytes = new Buffer(32768); bytes.fill(0x41); - hessian.decode(utils.bytes('v1/bytes/32768'), '2.0').should.eql(bytes); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32768'), '2.0'), bytes); var bytes = new Buffer(32769); bytes.fill(0x41); - hessian.decode(utils.bytes('v1/bytes/32769'), '2.0').should.eql(bytes); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '2.0'), bytes); var bytes = new Buffer(32767); bytes.fill(0x41); - hessian.decode(utils.bytes('v1/bytes/32767'), '2.0').should.eql(bytes); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32767'), '2.0'), bytes); var bytes = new Buffer(32769); bytes.fill(0x41); - hessian.decode(utils.bytes('v1/bytes/32769'), '2.0').should.eql(bytes); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '2.0'), bytes); var bytes = new Buffer(42769); bytes.fill(0x41); - hessian.decode(utils.bytes('v1/bytes/42769'), '2.0').should.eql(bytes); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/42769'), '2.0'), bytes); var bytes = new Buffer(82769); bytes.fill(0x41); - hessian.decode(utils.bytes('v1/bytes/82769'), '2.0').should.eql(bytes); + assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/82769'), '2.0'), bytes); }); }); }); diff --git a/test/boolean.test.js b/test/boolean.test.js index 8df94e3..83584fb 100644 --- a/test/boolean.test.js +++ b/test/boolean.test.js @@ -10,30 +10,26 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); describe('boolean.test.js', function () { it('should read true and false', function () { - hessian.decode(new Buffer('T')).should.equal(true); - hessian.decode(new Buffer('F')).should.equal(false); + assert(hessian.decode(new Buffer('T')) === true); + assert(hessian.decode(new Buffer('F')) === false); }); it('should write true and false', function () { - hessian.encode(true).should.eql(new Buffer('T')); - hessian.encode(false).should.eql(new Buffer('F')); + assert.deepEqual(hessian.encode(true), new Buffer('T')); + assert.deepEqual(hessian.encode(false), new Buffer('F')); }); describe('v2.0', function () { it('should read write as 1.0', function () { - hessian.encode(true, '2.0').should.eql(new Buffer('T')); - hessian.encode(false, '2.0').should.eql(new Buffer('F')); - hessian.decode(new Buffer('T'), '2.0').should.equal(true); - hessian.decode(new Buffer('F'), '2.0').should.equal(false); + assert.deepEqual(hessian.encode(true, '2.0'), new Buffer('T')); + assert.deepEqual(hessian.encode(false, '2.0'), new Buffer('F')); + assert(hessian.decode(new Buffer('T'), '2.0') === true); + assert(hessian.decode(new Buffer('F'), '2.0') === false); }); }); }); diff --git a/test/date.test.js b/test/date.test.js index 90c54b0..f4efa7b 100644 --- a/test/date.test.js +++ b/test/date.test.js @@ -10,11 +10,7 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); var utils = require('./utils'); @@ -23,79 +19,85 @@ describe('date.test.js', function () { it('should read date 2:51:31 May 8, 1998', function () { var d = hessian.decode(dateBuffer); - d.should.be.an.Date; - d.getFullYear().should.equal(1998); - d.getTime().should.equal(894621091000); - d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT'); - d.toISOString().should.equal('1998-05-08T09:51:31.000Z'); + assert(Object.prototype.toString.call(d) === '[object Date]'); + assert(d.getFullYear() === 1998); + assert(d.getTime() === 894621091000); + assert(d.toUTCString() === 'Fri, 08 May 1998 09:51:31 GMT'); + assert(d.toISOString() === '1998-05-08T09:51:31.000Z'); }); it('should write date 2:51:31 May 8, 1998', function () { - hessian.encode(new Date(894621091000)).should.eql(dateBuffer); + assert.deepEqual(hessian.encode(new Date(894621091000)), dateBuffer); }); it('should write date 0 and read', function () { - hessian.encode(new Date(0)).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0])); + assert.deepEqual( + hessian.encode(new Date(0)), + new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]) + ); }); it('should read date 09:51:31 May 8, 1998 UTC', function () { var d = hessian.decode(utils.bytes('v1/date/894621091000'), '1.0'); - d.should.be.a.Date; - d.getFullYear().should.equal(1998); - d.getTime().should.equal(894621091000); - d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT'); - d.toISOString().should.equal('1998-05-08T09:51:31.000Z'); + assert(Object.prototype.toString.call(d) === '[object Date]'); + assert(d.getFullYear() === 1998); + assert(d.getTime() === 894621091000); + assert(d.toUTCString() === 'Fri, 08 May 1998 09:51:31 GMT'); + assert(d.toISOString() === '1998-05-08T09:51:31.000Z'); }); it('should read date 09:51:00 May 8, 1998 UTC', function () { var d = hessian.decode(utils.bytes('v1/date/894621060000'), '1.0'); - d.should.be.a.Date; - d.getFullYear().should.equal(1998); - d.getTime().should.equal(894621060000); - d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT'); - d.toISOString().should.equal('1998-05-08T09:51:00.000Z'); + assert(Object.prototype.toString.call(d) === '[object Date]'); + assert(d.getFullYear() === 1998); + assert(d.getTime() === 894621060000); + assert(d.toUTCString() === 'Fri, 08 May 1998 09:51:00 GMT'); + assert(d.toISOString() === '1998-05-08T09:51:00.000Z'); }); it('should write date', function () { var now = new Date(1398280514000); - hessian.encode(now, '1.0').should.eql(utils.bytes('v1/date/now')); + assert.deepEqual(hessian.encode(now, '1.0'), utils.bytes('v1/date/now')); // read it - hessian.decode(utils.bytes('v1/date/now'), '1.0').should.eql(now); + assert.deepEqual(hessian.decode(utils.bytes('v1/date/now'), '1.0'), now); }); describe('hessian 2.0', function () { it('should read date 09:51:31 May 8, 1998 UTC', function () { var d = hessian.decode(utils.bytes('v2/date/894621091000'), '2.0'); - d.should.be.a.Date; - d.getFullYear().should.equal(1998); - d.getTime().should.equal(894621091000); - d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT'); - d.toISOString().should.equal('1998-05-08T09:51:31.000Z'); + assert(Object.prototype.toString.call(d) === '[object Date]'); + assert(d.getFullYear() === 1998); + assert(d.getTime() === 894621091000); + assert(d.toUTCString() === 'Fri, 08 May 1998 09:51:31 GMT'); + assert(d.toISOString() === '1998-05-08T09:51:31.000Z'); }); it('should read Compact: date in minutes, 09:51:00 May 8, 1998 UTC', function () { var d = hessian.decode(utils.bytes('v2/date/894621060000'), '2.0'); - d.should.be.a.Date; - d.getFullYear().should.equal(1998); - d.getTime().should.equal(894621060000); - d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT'); - d.toISOString().should.equal('1998-05-08T09:51:00.000Z'); + assert(Object.prototype.toString.call(d) === '[object Date]'); + assert(d.getFullYear() === 1998); + assert(d.getTime() === 894621060000); + assert(d.toUTCString() === 'Fri, 08 May 1998 09:51:00 GMT'); + assert(d.toISOString() === '1998-05-08T09:51:00.000Z'); }); it('should write and read date', function () { var now = new Date(1398280514000); - hessian.encode(now, '2.0').should.eql(utils.bytes('v2/date/now')); + assert.deepEqual(hessian.encode(now, '2.0'), utils.bytes('v2/date/now')); // read it - hessian.decode(utils.bytes('v2/date/now'), '2.0').should.eql(now); + assert.deepEqual(hessian.decode(utils.bytes('v2/date/now'), '2.0'), now); }); it('should read 1.0 format', function () { - hessian.decode(utils.bytes('v1/date/894621091000'), '2.0').getTime() - .should.equal(894621091000); - hessian.decode(utils.bytes('v1/date/894621060000'), '2.0').getTime() - .should.equal(894621060000); - hessian.decode(utils.bytes('v1/date/now'), '2.0').getTime() - .should.equal(1398280514000); + assert( + hessian.decode(utils.bytes('v1/date/894621091000'), '2.0').getTime() === 894621091000 + ); + assert( + hessian.decode(utils.bytes('v1/date/894621060000'), '2.0').getTime() === 894621060000 + ); + assert( + hessian.decode(utils.bytes('v1/date/now'), '2.0').getTime() === 1398280514000 + ); }); }); }); diff --git a/test/decode.circular.test.js b/test/decode.circular.test.js index fb95759..bcb9777 100644 --- a/test/decode.circular.test.js +++ b/test/decode.circular.test.js @@ -10,11 +10,7 @@ 'use strict'; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); var utils = require('./utils'); @@ -23,16 +19,17 @@ describe('test/decode.circular.test.js', function () { it('v1 decode()', function () { var data = new Buffer([77, 116, 0, 49, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 99, 111, 110, 102, 105, 103, 115, 101, 114, 118, 101, 114, 46, 99, 111, 110, 102, 114, 101, 103, 95, 116, 101, 115, 116, 46, 79, 110, 108, 105, 110, 101, 77, 111, 100, 117, 108, 101, 83, 0, 6, 109, 111, 100, 117, 108, 101, 83, 0, 1, 97, 83, 0, 4, 100, 101, 115, 99, 83, 0, 1, 98, 83, 0, 8, 118, 101, 114, 115, 105, 111, 110, 115, 86, 108, 0, 0, 0, 1, 77, 116, 0, 57, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 99, 111, 110, 102, 105, 103, 115, 101, 114, 118, 101, 114, 46, 99, 111, 110, 102, 114, 101, 103, 95, 116, 101, 115, 116, 46, 79, 110, 108, 105, 110, 101, 77, 111, 100, 117, 108, 101, 36, 86, 101, 114, 115, 105, 111, 110, 83, 0, 7, 118, 101, 114, 115, 105, 111, 110, 83, 0, 1, 99, 83, 0, 6, 97, 115, 115, 101, 116, 115, 86, 108, 0, 0, 0, 1, 83, 0, 1, 105, 122, 83, 0, 6, 116, 104, 105, 115, 36, 48, 82, 0, 0, 0, 0, 122, 122, 122]); var rs = hessian.decode(data); - JSON.stringify(rs).should.eql('{"module":"a","desc":"b","versions":[{"version":"c","assets":["i"]}]}'); + assert( + JSON.stringify(rs) === '{"module":"a","desc":"b","versions":[{"version":"c","assets":["i"]}]}' + ); }); it('v2 decode()', function () { var javabuf = utils.bytes('v2/object/ConnectionRequest'); var connreq1 = hessian.decode(javabuf, '2.0'); - connreq1.should.have.keys('ctx'); - connreq1.ctx.should.have.keys('id'); - JSON.stringify(connreq1).should.eql('{"ctx":{"id":101}}'); + assert(connreq1.ctx && connreq1.ctx.id); + assert(JSON.stringify(connreq1) === '{"ctx":{"id":101}}'); }); }); \ No newline at end of file diff --git a/test/double.test.js b/test/double.test.js index 7cfdc4e..a88b80a 100644 --- a/test/double.test.js +++ b/test/double.test.js @@ -10,11 +10,7 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var java = require('js-to-java'); var hessian = require('../'); var utils = require('./utils'); @@ -24,96 +20,159 @@ describe('double.test.js', function () { 0x40, 0x28, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00]); it('should read double 12.25', function () { - hessian.decode(doubleBuffer).should.equal(12.25); + assert(hessian.decode(doubleBuffer) === 12.25); }); it('should write double 12.25', function () { - hessian.encode(12.25).should.eql(doubleBuffer); - hessian.encode(java.double(12.25)).should.eql(doubleBuffer); - hessian.encode({ + assert.deepEqual(hessian.encode(12.25), doubleBuffer); + assert.deepEqual(hessian.encode(java.double(12.25)), doubleBuffer); + assert.deepEqual(hessian.encode({ $class: 'double', $: 12.25 - }).should.eql(doubleBuffer); + }), doubleBuffer); }); it('should write double 100', function () { - hessian.encode(java.double(100)).should.eql( - new Buffer(['D'.charCodeAt(0), 0x40, 0x59, 0, 0, 0, 0, 0, 0])); + assert.deepEqual( + hessian.encode(java.double(100)), + new Buffer(['D'.charCodeAt(0), 0x40, 0x59, 0, 0, 0, 0, 0, 0]) + ); }); it('should write double 0', function () { - hessian.encode(java.double(0)).should.eql( - new Buffer(['D'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0])); + assert.deepEqual( + hessian.encode(java.double(0)), + new Buffer(['D'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]) + ); }); it('should write as java impl', function () { - hessian.encode(java.double(0), '1.0').should.eql(utils.bytes('v1/double/0')); - hessian.encode(java.double(0.0), '1.0').should.eql(utils.bytes('v1/double/0')); - hessian.encode(java.double(1), '1.0').should.eql(utils.bytes('v1/double/1')); - hessian.encode(java.double(1.0), '1.0').should.eql(utils.bytes('v1/double/1')); - hessian.encode(java.double(10), '1.0').should.eql(utils.bytes('v1/double/10')); - hessian.encode(java.double(10.123), '1.0').should.eql(utils.bytes('v1/double/10.123')); - hessian.encode(java.double(10.1), '1.0').should.eql(utils.bytes('v1/double/10.1')); - hessian.encode(java.double(-128), '1.0').should.eql(utils.bytes('v1/double/-128')); - hessian.encode(java.double(-127.9999), '1.0').should.eql(utils.bytes('v1/double/-127.9999')); - hessian.encode(java.double(127), '1.0').should.eql(utils.bytes('v1/double/127')); - hessian.encode(java.double(126.9989), '1.0').should.eql(utils.bytes('v1/double/126.9989')); - hessian.encode(java.double(-32768), '1.0').should.eql(utils.bytes('v1/double/-32768')); - hessian.encode(java.double(-32767.999), '1.0').should.eql(utils.bytes('v1/double/-32767.999')); - hessian.encode(java.double(32767), '1.0').should.eql(utils.bytes('v1/double/32767')); - hessian.encode(java.double(32766.99999), '1.0').should.eql(utils.bytes('v1/double/32766.99999')); - hessian.encode(java.double(32768), '1.0').should.eql(utils.bytes('v1/double/32768')); - hessian.encode(java.double(32767.99999), '1.0').should.eql(utils.bytes('v1/double/32767.99999')); + assert.deepEqual(hessian.encode(java.double(0), '1.0'), utils.bytes('v1/double/0')); + assert.deepEqual(hessian.encode(java.double(0.0), '1.0'), utils.bytes('v1/double/0')); + assert.deepEqual(hessian.encode(java.double(1), '1.0'), utils.bytes('v1/double/1')); + assert.deepEqual(hessian.encode(java.double(1.0), '1.0'), utils.bytes('v1/double/1')); + assert.deepEqual(hessian.encode(java.double(10), '1.0'), utils.bytes('v1/double/10')); + assert.deepEqual( + hessian.encode(java.double(10.123), '1.0'), + utils.bytes('v1/double/10.123') + ); + assert.deepEqual(hessian.encode(java.double(10.1), '1.0'), utils.bytes('v1/double/10.1')); + assert.deepEqual(hessian.encode(java.double(-128), '1.0'), utils.bytes('v1/double/-128')); + assert.deepEqual( + hessian.encode(java.double(-127.9999), '1.0'), + utils.bytes('v1/double/-127.9999') + ); + assert.deepEqual(hessian.encode(java.double(127), '1.0'), utils.bytes('v1/double/127')); + assert.deepEqual( + hessian.encode(java.double(126.9989), '1.0'), + utils.bytes('v1/double/126.9989') + ); + assert.deepEqual( + hessian.encode(java.double(-32768), '1.0'), + utils.bytes('v1/double/-32768') + ); + assert.deepEqual( + hessian.encode(java.double(-32767.999), '1.0'), + utils.bytes('v1/double/-32767.999') + ); + assert.deepEqual(hessian.encode(java.double(32767), '1.0'), utils.bytes('v1/double/32767')); + assert.deepEqual( + hessian.encode(java.double(32766.99999), '1.0'), + utils.bytes('v1/double/32766.99999') + ); + assert.deepEqual(hessian.encode(java.double(32768), '1.0'), utils.bytes('v1/double/32768')); + assert.deepEqual( + hessian.encode(java.double(32767.99999), '1.0'), + utils.bytes('v1/double/32767.99999') + ); // float byte - hessian.encode(java.double(-2147483649), '1.0').should.eql(utils.bytes('v1/double/-2147483649')); - hessian.encode(java.double(-2147483648), '1.0').should.eql(utils.bytes('v1/double/-2147483648')); - hessian.encode(java.double(-2147483647), '1.0').should.eql(utils.bytes('v1/double/-2147483647')); - hessian.encode(java.double(-2147483610.123), '1.0').should.eql(utils.bytes('v1/double/-2147483610.123')); - hessian.encode(java.double(2147483648), '1.0').should.eql(utils.bytes('v1/double/2147483648')); - hessian.encode(java.double(2147483647), '1.0').should.eql(utils.bytes('v1/double/2147483647')); - hessian.encode(java.double(2147483646), '1.0').should.eql(utils.bytes('v1/double/2147483646')); - hessian.encode(java.double(2147483646.456), '1.0').should.eql(utils.bytes('v1/double/2147483646.456')); + assert.deepEqual( + hessian.encode(java.double(-2147483649), '1.0'), + utils.bytes('v1/double/-2147483649') + ); + assert.deepEqual( + hessian.encode(java.double(-2147483648), '1.0'), + utils.bytes('v1/double/-2147483648') + ); + assert.deepEqual( + hessian.encode(java.double(-2147483647), '1.0'), + utils.bytes('v1/double/-2147483647') + ); + assert.deepEqual( + hessian.encode(java.double(-2147483610.123), '1.0'), + utils.bytes('v1/double/-2147483610.123') + ); + assert.deepEqual( + hessian.encode(java.double(2147483648), '1.0'), + utils.bytes('v1/double/2147483648') + ); + assert.deepEqual( + hessian.encode(java.double(2147483647), '1.0'), + utils.bytes('v1/double/2147483647') + ); + assert.deepEqual( + hessian.encode(java.double(2147483646), '1.0'), + utils.bytes('v1/double/2147483646') + ); + assert.deepEqual( + hessian.encode(java.double(2147483646.456), '1.0'), + utils.bytes('v1/double/2147483646.456') + ); }); it('should read java bin format', function () { - hessian.decode(utils.bytes('v1/double/0'), '1.0').should.equal(0); - hessian.decode(utils.bytes('v1/double/1'), '1.0').should.equal(1); - hessian.decode(utils.bytes('v1/double/10'), '1.0').should.equal(10); - hessian.decode(utils.bytes('v1/double/10.123'), '1.0').should.equal(10.123); - hessian.decode(utils.bytes('v1/double/10.1'), '1.0').should.equal(10.1); - hessian.decode(utils.bytes('v1/double/-128'), '1.0').should.equal(-128); - hessian.decode(utils.bytes('v1/double/-127.9999'), '1.0').should.equal(-127.9999); - hessian.decode(utils.bytes('v1/double/127'), '1.0').should.equal(127); - hessian.decode(utils.bytes('v1/double/126.9989'), '1.0').should.equal(126.9989); - hessian.decode(utils.bytes('v1/double/-32768'), '1.0').should.equal(-32768); - hessian.decode(utils.bytes('v1/double/-32767.999'), '1.0').should.equal(-32767.999); - hessian.decode(utils.bytes('v1/double/32767'), '1.0').should.equal(32767); - hessian.decode(utils.bytes('v1/double/32766.99999'), '1.0').should.equal(32766.99999); - hessian.decode(utils.bytes('v1/double/32768'), '1.0').should.equal(32768); - hessian.decode(utils.bytes('v1/double/32767.99999'), '1.0').should.equal(32767.99999); - hessian.decode(utils.bytes('v1/double/-2147483649'), '1.0').should.equal(-2147483649); - hessian.decode(utils.bytes('v1/double/-2147483648'), '1.0').should.equal(-2147483648); - hessian.decode(utils.bytes('v1/double/-2147483647'), '1.0').should.equal(-2147483647); - hessian.decode(utils.bytes('v1/double/-2147483610.123'), '1.0').should.equal(-2147483610.123); - hessian.decode(utils.bytes('v1/double/2147483648'), '1.0').should.equal(2147483648); - hessian.decode(utils.bytes('v1/double/2147483647'), '1.0').should.equal(2147483647); - hessian.decode(utils.bytes('v1/double/2147483646'), '1.0').should.equal(2147483646); - hessian.decode(utils.bytes('v1/double/2147483646.456'), '1.0').should.equal(2147483646.456); + assert(hessian.decode(utils.bytes('v1/double/0'), '1.0') === 0); + assert(hessian.decode(utils.bytes('v1/double/1'), '1.0') === 1); + assert(hessian.decode(utils.bytes('v1/double/10'), '1.0') === 10); + assert(hessian.decode(utils.bytes('v1/double/10.123'), '1.0') === 10.123); + assert(hessian.decode(utils.bytes('v1/double/10.1'), '1.0') === 10.1); + assert(hessian.decode(utils.bytes('v1/double/-128'), '1.0') === -128); + assert(hessian.decode(utils.bytes('v1/double/-127.9999'), '1.0') === -127.9999); + assert(hessian.decode(utils.bytes('v1/double/127'), '1.0') === 127); + assert(hessian.decode(utils.bytes('v1/double/126.9989'), '1.0') === 126.9989); + assert(hessian.decode(utils.bytes('v1/double/-32768'), '1.0') === -32768); + assert(hessian.decode(utils.bytes('v1/double/-32767.999'), '1.0') === -32767.999); + assert(hessian.decode(utils.bytes('v1/double/32767'), '1.0') === 32767); + assert( + hessian.decode(utils.bytes('v1/double/32766.99999'), '1.0') === 32766.99999 + ); + assert(hessian.decode(utils.bytes('v1/double/32768'), '1.0') === 32768); + assert( + hessian.decode(utils.bytes('v1/double/32767.99999'), '1.0') === 32767.99999 + ); + assert( + hessian.decode(utils.bytes('v1/double/-2147483649'), '1.0') === -2147483649 + ); + assert( + hessian.decode(utils.bytes('v1/double/-2147483648'), '1.0') === -2147483648 + ); + assert( + hessian.decode(utils.bytes('v1/double/-2147483647'), '1.0') === -2147483647 + ); + assert( + hessian.decode(utils.bytes('v1/double/-2147483610.123'), '1.0') === -2147483610.123 + ); + assert(hessian.decode(utils.bytes('v1/double/2147483648'), '1.0') === 2147483648); + assert(hessian.decode(utils.bytes('v1/double/2147483647'), '1.0') === 2147483647); + assert(hessian.decode(utils.bytes('v1/double/2147483646'), '1.0') === 2147483646); + assert( + hessian.decode(utils.bytes('v1/double/2147483646.456'), '1.0') === 2147483646.456 + ); }); it('should decode with type', function () { - hessian.decode(utils.bytes('v1/double/0'), '1.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/double/0'), '1.0', true), { $class: 'double', $: 0, }); - hessian.decode(utils.bytes('v1/double/-127.9999'), '1.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/double/-127.9999'), '1.0', true), { $class: 'double', $: -127.9999, }); - hessian.decode(utils.bytes('v1/double/-2147483647'), '1.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/double/-2147483647'), '1.0', true), { $class: 'double', $: -2147483647, }); @@ -121,148 +180,224 @@ describe('double.test.js', function () { describe('v2.0', function () { it('should read 0.0 and 1.0', function () { - hessian.decode(new Buffer([0x67]), '2.0').should.equal(0.0); - hessian.decode(new Buffer([0x68]), '2.0').should.equal(1.0); + assert(hessian.decode(new Buffer([0x67]), '2.0') === 0.0); + assert(hessian.decode(new Buffer([0x68]), '2.0') === 1.0); }); it('should read 8 bits double', function () { - hessian.decode(new Buffer([0x69, 0x00]), '2.0').should.equal(0.0); - hessian.decode(new Buffer([0x69, 0x01]), '2.0').should.equal(1.0); - hessian.decode(new Buffer([0x69, 0x80]), '2.0').should.equal(-128.0); - hessian.decode(new Buffer([0x69, 0x7f]), '2.0').should.equal(127.0); + assert(hessian.decode(new Buffer([0x69, 0x00]), '2.0') === 0.0); + assert(hessian.decode(new Buffer([0x69, 0x01]), '2.0') === 1.0); + assert(hessian.decode(new Buffer([0x69, 0x80]), '2.0') === -128.0); + assert(hessian.decode(new Buffer([0x69, 0x7f]), '2.0') === 127.0); }); it('should read 16 bits double', function () { - hessian.decode(new Buffer([0x6a, 0x00, 0x00]), '2.0').should.equal(0.0); - hessian.decode(new Buffer([0x6a, 0x00, 0x01]), '2.0').should.equal(1.0); - hessian.decode(new Buffer([0x6a, 0x00, 0x80]), '2.0').should.equal(128.0); - hessian.decode(new Buffer([0x6a, 0x00, 0x7f]), '2.0').should.equal(127.0); - hessian.decode(new Buffer([0x6a, 0x80, 0x00]), '2.0').should.equal(-32768.0); - hessian.decode(new Buffer([0x6a, 0x7f, 0xff]), '2.0').should.equal(32767.0); + assert(hessian.decode(new Buffer([0x6a, 0x00, 0x00]), '2.0') === 0.0); + assert(hessian.decode(new Buffer([0x6a, 0x00, 0x01]), '2.0') === 1.0); + assert(hessian.decode(new Buffer([0x6a, 0x00, 0x80]), '2.0') === 128.0); + assert(hessian.decode(new Buffer([0x6a, 0x00, 0x7f]), '2.0') === 127.0); + assert(hessian.decode(new Buffer([0x6a, 0x80, 0x00]), '2.0') === -32768.0); + assert(hessian.decode(new Buffer([0x6a, 0x7f, 0xff]), '2.0') === 32767.0); }); it('should read 32 bits float double', function () { - hessian.decode(new Buffer([0x6b, 0x00, 0x00, 0x00, 0x00]), '2.0').should.equal(0.0); - hessian.decode(new Buffer([0x6b, 0x41, 0x44, 0x00, 0x00]), '2.0').should.equal(12.25); + assert(hessian.decode(new Buffer([0x6b, 0x00, 0x00, 0x00, 0x00]), '2.0') === 0.0); + assert( + hessian.decode(new Buffer([0x6b, 0x41, 0x44, 0x00, 0x00]), '2.0') === 12.25 + ); }); it('should read normal double', function () { - hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0').should.equal(10.0); + assert( + hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0') === 10.0 + ); }); it('should decode with type', function () { - hessian.decode(new Buffer([0x69, 0x01]), '2.0', true).should.eql({ + assert.deepEqual(hessian.decode(new Buffer([0x69, 0x01]), '2.0', true), { $class: 'double', $: 1.0, }); - hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0', true).should.eql({ - $class: 'double', - $: 10.0, - }); + assert.deepEqual( + hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0', true), + { + $class: 'double', + $: 10.0, + } + ); - hessian.decode(new Buffer([0x6a, 0x00, 0x80]), '2.0', true).should.eql({ + assert.deepEqual(hessian.decode(new Buffer([0x6a, 0x00, 0x80]), '2.0', true), { $class: 'double', $: 128.0, }); }); it('should write 0.0 and 1.0', function () { - hessian.encode(java.double(0), '2.0').should.eql(new Buffer([0x67])); - hessian.encode(java.double(0.0), '2.0').should.eql(new Buffer([0x67])); + assert.deepEqual(hessian.encode(java.double(0), '2.0'), new Buffer([0x67])); + assert.deepEqual(hessian.encode(java.double(0.0), '2.0'), new Buffer([0x67])); - hessian.encode(java.double(1), '2.0').should.eql(new Buffer([0x68])); - hessian.encode(java.double(1.0), '2.0').should.eql(new Buffer([0x68])); + assert.deepEqual(hessian.encode(java.double(1), '2.0'), new Buffer([0x68])); + assert.deepEqual(hessian.encode(java.double(1.0), '2.0'), new Buffer([0x68])); }); it('should write as java impl', function () { - hessian.encode(java.double(0), '2.0').should.eql(utils.bytes('v2/double/0')); - hessian.encode(java.double(0.0), '2.0').should.eql(utils.bytes('v2/double/0')); - hessian.encode(java.double(1), '2.0').should.eql(utils.bytes('v2/double/1')); - hessian.encode(java.double(1.0), '2.0').should.eql(utils.bytes('v2/double/1')); - hessian.encode(java.double(10), '2.0').should.eql(utils.bytes('v2/double/10')); - hessian.encode(java.double(10.123), '2.0').should.eql(utils.bytes('v2/double/10.123')); - hessian.encode(java.double(10.1), '2.0').should.eql(utils.bytes('v2/double/10.1')); - hessian.encode(java.double(-128), '2.0').should.eql(utils.bytes('v2/double/-128')); - hessian.encode(java.double(-127.9999), '2.0').should.eql(utils.bytes('v2/double/-127.9999')); - hessian.encode(java.double(127), '2.0').should.eql(utils.bytes('v2/double/127')); - hessian.encode(java.double(126.9989), '2.0').should.eql(utils.bytes('v2/double/126.9989')); - hessian.encode(java.double(-32768), '2.0').should.eql(utils.bytes('v2/double/-32768')); - hessian.encode(java.double(-32767.999), '2.0').should.eql(utils.bytes('v2/double/-32767.999')); - hessian.encode(java.double(32767), '2.0').should.eql(utils.bytes('v2/double/32767')); - hessian.encode(java.double(32766.99999), '2.0').should.eql(utils.bytes('v2/double/32766.99999')); - hessian.encode(java.double(32768), '2.0').should.eql(utils.bytes('v2/double/32768')); - hessian.encode(java.double(32767.99999), '2.0').should.eql(utils.bytes('v2/double/32767.99999')); + assert.deepEqual(hessian.encode(java.double(0), '2.0'), utils.bytes('v2/double/0')); + assert.deepEqual(hessian.encode(java.double(0.0), '2.0'), utils.bytes('v2/double/0')); + assert.deepEqual(hessian.encode(java.double(1), '2.0'), utils.bytes('v2/double/1')); + assert.deepEqual(hessian.encode(java.double(1.0), '2.0'), utils.bytes('v2/double/1')); + assert.deepEqual(hessian.encode(java.double(10), '2.0'), utils.bytes('v2/double/10')); + assert.deepEqual( + hessian.encode(java.double(10.123), '2.0'), + utils.bytes('v2/double/10.123') + ); + assert.deepEqual(hessian.encode(java.double(10.1), '2.0'), utils.bytes('v2/double/10.1')); + assert.deepEqual(hessian.encode(java.double(-128), '2.0'), utils.bytes('v2/double/-128')); + assert.deepEqual( + hessian.encode(java.double(-127.9999), '2.0'), + utils.bytes('v2/double/-127.9999') + ); + assert.deepEqual(hessian.encode(java.double(127), '2.0'), utils.bytes('v2/double/127')); + assert.deepEqual( + hessian.encode(java.double(126.9989), '2.0'), + utils.bytes('v2/double/126.9989') + ); + assert.deepEqual( + hessian.encode(java.double(-32768), '2.0'), + utils.bytes('v2/double/-32768') + ); + assert.deepEqual( + hessian.encode(java.double(-32767.999), '2.0'), + utils.bytes('v2/double/-32767.999') + ); + assert.deepEqual(hessian.encode(java.double(32767), '2.0'), utils.bytes('v2/double/32767')); + assert.deepEqual( + hessian.encode(java.double(32766.99999), '2.0'), + utils.bytes('v2/double/32766.99999') + ); + assert.deepEqual(hessian.encode(java.double(32768), '2.0'), utils.bytes('v2/double/32768')); + assert.deepEqual( + hessian.encode(java.double(32767.99999), '2.0'), + utils.bytes('v2/double/32767.99999') + ); // float byte - hessian.encode(java.double(-0x800000), '2.0').should.length(5); - hessian.encode(java.double(-0x800000), '2.0').should.eql(utils.bytes('v2/double/-0x800000')); - hessian.encode(java.double(-0x80000000), '2.0').should.length(5); - hessian.encode(java.double(-0x80000000), '2.0').should.eql(utils.bytes('v2/double/-0x80000000')); - hessian.encode(java.double(-2147483649), '2.0').should.eql(utils.bytes('v2/double/-2147483649')); - hessian.encode(java.double(-2147483648), '2.0').should.eql(utils.bytes('v2/double/-2147483648')); + assert(hessian.encode(java.double(-0x800000), '2.0').length === 5); + assert.deepEqual( + hessian.encode(java.double(-0x800000), '2.0'), + utils.bytes('v2/double/-0x800000') + ); + assert(hessian.encode(java.double(-0x80000000), '2.0').length === 5); + assert.deepEqual( + hessian.encode(java.double(-0x80000000), '2.0'), + utils.bytes('v2/double/-0x80000000') + ); + assert.deepEqual( + hessian.encode(java.double(-2147483649), '2.0'), + utils.bytes('v2/double/-2147483649') + ); + assert.deepEqual( + hessian.encode(java.double(-2147483648), '2.0'), + utils.bytes('v2/double/-2147483648') + ); // hessian.encode(java.double(-2147483647), '2.0').should.eql(utils.bytes('v2/double/-2147483647')); - hessian.encode(java.double(-2147483610.123), '2.0').should.eql(utils.bytes('v2/double/-2147483610.123')); + assert.deepEqual( + hessian.encode(java.double(-2147483610.123), '2.0'), + utils.bytes('v2/double/-2147483610.123') + ); // hessian.encode(java.double(2147483648), '2.0').should.eql(utils.bytes('v2/double/2147483648')); // hessian.encode(java.double(2147483647), '2.0').should.eql(utils.bytes('v2/double/2147483647')); // hessian.encode(java.double(2147483646), '2.0').should.eql(utils.bytes('v2/double/2147483646')); - hessian.encode(java.double(2147483646.456), '2.0').should.eql(utils.bytes('v2/double/2147483646.456')); + assert.deepEqual( + hessian.encode(java.double(2147483646.456), '2.0'), + utils.bytes('v2/double/2147483646.456') + ); }); it('should read java bin format', function () { - hessian.decode(utils.bytes('v2/double/0'), '2.0').should.equal(0); - hessian.decode(utils.bytes('v2/double/1'), '2.0').should.equal(1); - hessian.decode(utils.bytes('v2/double/10'), '2.0').should.equal(10); - hessian.decode(utils.bytes('v2/double/10.123'), '2.0').should.equal(10.123); - hessian.decode(utils.bytes('v2/double/10.1'), '2.0').should.equal(10.1); - hessian.decode(utils.bytes('v2/double/-128'), '2.0').should.equal(-128); - hessian.decode(utils.bytes('v2/double/-127.9999'), '2.0').should.equal(-127.9999); - hessian.decode(utils.bytes('v2/double/127'), '2.0').should.equal(127); - hessian.decode(utils.bytes('v2/double/126.9989'), '2.0').should.equal(126.9989); - hessian.decode(utils.bytes('v2/double/-32768'), '2.0').should.equal(-32768); - hessian.decode(utils.bytes('v2/double/-32767.999'), '2.0').should.equal(-32767.999); - hessian.decode(utils.bytes('v2/double/32767'), '2.0').should.equal(32767); - hessian.decode(utils.bytes('v2/double/32766.99999'), '2.0').should.equal(32766.99999); - hessian.decode(utils.bytes('v2/double/32768'), '2.0').should.equal(32768); - hessian.decode(utils.bytes('v2/double/32767.99999'), '2.0').should.equal(32767.99999); + assert(hessian.decode(utils.bytes('v2/double/0'), '2.0') === 0); + assert(hessian.decode(utils.bytes('v2/double/1'), '2.0') === 1); + assert(hessian.decode(utils.bytes('v2/double/10'), '2.0') === 10); + assert(hessian.decode(utils.bytes('v2/double/10.123'), '2.0') === 10.123); + assert(hessian.decode(utils.bytes('v2/double/10.1'), '2.0') === 10.1); + assert(hessian.decode(utils.bytes('v2/double/-128'), '2.0') === -128); + assert(hessian.decode(utils.bytes('v2/double/-127.9999'), '2.0') === -127.9999); + assert(hessian.decode(utils.bytes('v2/double/127'), '2.0') === 127); + assert(hessian.decode(utils.bytes('v2/double/126.9989'), '2.0') === 126.9989); + assert(hessian.decode(utils.bytes('v2/double/-32768'), '2.0') === -32768); + assert(hessian.decode(utils.bytes('v2/double/-32767.999'), '2.0') === -32767.999); + assert(hessian.decode(utils.bytes('v2/double/32767'), '2.0') === 32767); + assert( + hessian.decode(utils.bytes('v2/double/32766.99999'), '2.0') === 32766.99999 + ); + assert(hessian.decode(utils.bytes('v2/double/32768'), '2.0') === 32768); + assert( + hessian.decode(utils.bytes('v2/double/32767.99999'), '2.0') === 32767.99999 + ); // float byte - hessian.decode(utils.bytes('v2/double/-0x800000'), '2.0').should.equal(-0x800000); - hessian.decode(utils.bytes('v2/double/-0x80000000'), '2.0').should.equal(-0x80000000); - hessian.decode(utils.bytes('v2/double/-2147483649'), '2.0').should.equal(-2147483649); - hessian.decode(utils.bytes('v2/double/-2147483648'), '2.0').should.equal(-2147483648); - hessian.decode(utils.bytes('v2/double/-2147483647'), '2.0').should.equal(-2147483647); - hessian.decode(utils.bytes('v2/double/-2147483610.123'), '2.0').should.equal(-2147483610.123); - hessian.decode(utils.bytes('v2/double/2147483648'), '2.0').should.equal(2147483648); - hessian.decode(utils.bytes('v2/double/2147483647'), '2.0').should.equal(2147483647); - hessian.decode(utils.bytes('v2/double/2147483646'), '2.0').should.equal(2147483646); - hessian.decode(utils.bytes('v2/double/2147483646.456'), '2.0').should.equal(2147483646.456); + assert(hessian.decode(utils.bytes('v2/double/-0x800000'), '2.0') === -0x800000); + assert( + hessian.decode(utils.bytes('v2/double/-0x80000000'), '2.0') === -0x80000000 + ); + assert( + hessian.decode(utils.bytes('v2/double/-2147483649'), '2.0') === -2147483649 + ); + assert( + hessian.decode(utils.bytes('v2/double/-2147483648'), '2.0') === -2147483648 + ); + assert( + hessian.decode(utils.bytes('v2/double/-2147483647'), '2.0') === -2147483647 + ); + assert( + hessian.decode(utils.bytes('v2/double/-2147483610.123'), '2.0') === -2147483610.123 + ); + assert(hessian.decode(utils.bytes('v2/double/2147483648'), '2.0') === 2147483648); + assert(hessian.decode(utils.bytes('v2/double/2147483647'), '2.0') === 2147483647); + assert(hessian.decode(utils.bytes('v2/double/2147483646'), '2.0') === 2147483646); + assert( + hessian.decode(utils.bytes('v2/double/2147483646.456'), '2.0') === 2147483646.456 + ); }); it('should read java hessian 1.0 bin format', function () { - hessian.decode(utils.bytes('v1/double/0'), '2.0').should.equal(0); - hessian.decode(utils.bytes('v1/double/1'), '2.0').should.equal(1); - hessian.decode(utils.bytes('v1/double/10'), '2.0').should.equal(10); - hessian.decode(utils.bytes('v1/double/10.123'), '2.0').should.equal(10.123); - hessian.decode(utils.bytes('v1/double/10.1'), '2.0').should.equal(10.1); - hessian.decode(utils.bytes('v1/double/-128'), '2.0').should.equal(-128); - hessian.decode(utils.bytes('v1/double/-127.9999'), '2.0').should.equal(-127.9999); - hessian.decode(utils.bytes('v1/double/127'), '2.0').should.equal(127); - hessian.decode(utils.bytes('v1/double/126.9989'), '2.0').should.equal(126.9989); - hessian.decode(utils.bytes('v1/double/-32768'), '2.0').should.equal(-32768); - hessian.decode(utils.bytes('v1/double/-32767.999'), '2.0').should.equal(-32767.999); - hessian.decode(utils.bytes('v1/double/32767'), '2.0').should.equal(32767); - hessian.decode(utils.bytes('v1/double/32766.99999'), '2.0').should.equal(32766.99999); - hessian.decode(utils.bytes('v1/double/32768'), '2.0').should.equal(32768); - hessian.decode(utils.bytes('v1/double/32767.99999'), '2.0').should.equal(32767.99999); - hessian.decode(utils.bytes('v1/double/-2147483649'), '2.0').should.equal(-2147483649); - hessian.decode(utils.bytes('v1/double/-2147483648'), '2.0').should.equal(-2147483648); - hessian.decode(utils.bytes('v1/double/-2147483647'), '2.0').should.equal(-2147483647); - hessian.decode(utils.bytes('v1/double/-2147483610.123'), '2.0').should.equal(-2147483610.123); - hessian.decode(utils.bytes('v1/double/2147483648'), '2.0').should.equal(2147483648); - hessian.decode(utils.bytes('v1/double/2147483647'), '2.0').should.equal(2147483647); - hessian.decode(utils.bytes('v1/double/2147483646'), '2.0').should.equal(2147483646); - hessian.decode(utils.bytes('v1/double/2147483646.456'), '2.0').should.equal(2147483646.456); + assert(hessian.decode(utils.bytes('v1/double/0'), '2.0') === 0); + assert(hessian.decode(utils.bytes('v1/double/1'), '2.0') === 1); + assert(hessian.decode(utils.bytes('v1/double/10'), '2.0') === 10); + assert(hessian.decode(utils.bytes('v1/double/10.123'), '2.0') === 10.123); + assert(hessian.decode(utils.bytes('v1/double/10.1'), '2.0') === 10.1); + assert(hessian.decode(utils.bytes('v1/double/-128'), '2.0') === -128); + assert(hessian.decode(utils.bytes('v1/double/-127.9999'), '2.0') === -127.9999); + assert(hessian.decode(utils.bytes('v1/double/127'), '2.0') === 127); + assert(hessian.decode(utils.bytes('v1/double/126.9989'), '2.0') === 126.9989); + assert(hessian.decode(utils.bytes('v1/double/-32768'), '2.0') === -32768); + assert(hessian.decode(utils.bytes('v1/double/-32767.999'), '2.0') === -32767.999); + assert(hessian.decode(utils.bytes('v1/double/32767'), '2.0') === 32767); + assert( + hessian.decode(utils.bytes('v1/double/32766.99999'), '2.0') === 32766.99999 + ); + assert(hessian.decode(utils.bytes('v1/double/32768'), '2.0') === 32768); + assert( + hessian.decode(utils.bytes('v1/double/32767.99999'), '2.0') === 32767.99999 + ); + assert( + hessian.decode(utils.bytes('v1/double/-2147483649'), '2.0') === -2147483649 + ); + assert( + hessian.decode(utils.bytes('v1/double/-2147483648'), '2.0') === -2147483648 + ); + assert( + hessian.decode(utils.bytes('v1/double/-2147483647'), '2.0') === -2147483647 + ); + assert( + hessian.decode(utils.bytes('v1/double/-2147483610.123'), '2.0') === -2147483610.123 + ); + assert(hessian.decode(utils.bytes('v1/double/2147483648'), '2.0') === 2147483648); + assert(hessian.decode(utils.bytes('v1/double/2147483647'), '2.0') === 2147483647); + assert(hessian.decode(utils.bytes('v1/double/2147483646'), '2.0') === 2147483646); + assert( + hessian.decode(utils.bytes('v1/double/2147483646.456'), '2.0') === 2147483646.456 + ); }); }); }); diff --git a/test/exception.test.js b/test/exception.test.js index ee65aa1..24f9704 100644 --- a/test/exception.test.js +++ b/test/exception.test.js @@ -10,11 +10,7 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); var utils = require('./utils'); @@ -22,96 +18,120 @@ describe('exception.test.js', function () { describe('v1.0', function () { it('should read java exception as js error', function () { var ioe = hessian.decode(utils.bytes('v1/exception/IOException')); - ioe.should.be.an.Error; - ioe.name.should.equal('java.io.IOException'); - ioe.message.should.equal('this is a java IOException instance'); - ioe.stack.should.equal('java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)'); + assert(ioe instanceof Error); + assert(ioe.name === 'java.io.IOException'); + assert(ioe.message === 'this is a java IOException instance'); + assert( + ioe.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' + ); var ioe = hessian.decode(utils.bytes('v1/exception/IOException'), true); - ioe.$.should.be.an.Error; - ioe.$.name.should.equal('java.io.IOException'); - ioe.$.message.should.equal('this is a java IOException instance'); - ioe.$.stack.should.equal('java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)'); + assert(ioe.$ instanceof Error); + assert(ioe.$.name === 'java.io.IOException'); + assert(ioe.$.message === 'this is a java IOException instance'); + assert( + ioe.$.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' + ); var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException')); - e.should.be.an.Error; - should.ok((e instanceof Error) === true); - e.name.should.equal('java.io.IOException'); - e.message.should.equal('this is a java IOException instance'); - e.stack.should.equal('java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)'); - should.exist(e.cause); - e.cause.detailMessage.should.equal('this is a java IOException instance'); + assert(e instanceof Error); + assert((e instanceof Error) === true); + assert(e.name === 'java.io.IOException'); + assert(e.message === 'this is a java IOException instance'); + assert( + e.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' + ); + assert(e.cause); + assert(e.cause.detailMessage === 'this is a java IOException instance'); var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException'), true); - e.$.should.be.an.Error; - e.$.name.should.equal('java.io.IOException'); - e.$.message.should.equal('this is a java IOException instance'); - e.$.stack.should.equal('java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)'); - should.exist(e.$.cause); - e.$.cause.$class.should.equal('java.io.IOException'); - e.$.cause.$.should.be.an.Error; - e.$.cause.$.name.should.equal('java.io.IOException'); + assert(e.$ instanceof Error); + assert(e.$.name === 'java.io.IOException'); + assert(e.$.message === 'this is a java IOException instance'); + assert( + e.$.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' + ); + assert(e.$.cause); + assert(e.$.cause.$class === 'java.io.IOException'); + assert(e.$.cause.$ instanceof Error); + assert(e.$.cause.$.name === 'java.io.IOException'); var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException2')); - e.should.be.an.Error; - should.ok((e instanceof Error) === true); - e.name.should.equal('java.io.IOException'); - e.message.should.equal('模拟测试异常; this is a java IOException instance'); - e.stack.should.equal('java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)'); + assert(e instanceof Error); + assert((e instanceof Error) === true); + assert(e.name === 'java.io.IOException'); + assert(e.message === '模拟测试异常; this is a java IOException instance'); + assert( + e.stack === 'java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)' + ); var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException2'), true); - e.$.should.be.an.Error; - e.$.name.should.equal('java.io.IOException'); - e.$.message.should.equal('模拟测试异常; this is a java IOException instance'); - e.$.stack.should.equal('java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)'); + assert(e.$ instanceof Error); + assert(e.$.name === 'java.io.IOException'); + assert(e.$.message === '模拟测试异常; this is a java IOException instance'); + assert( + e.$.stack === 'java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)' + ); }); }); describe('v2.0', function () { it('should read java exception as js error', function () { var ioe = hessian.decode(utils.bytes('v2/exception/IOException'), '2.0'); - ioe.should.be.an.Error; - should.ok((ioe instanceof Error) === true); - ioe.name.should.equal('java.io.IOException'); - ioe.message.should.equal('this is a java IOException instance'); - ioe.stack.should.equal('java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)'); + assert(ioe instanceof Error); + assert((ioe instanceof Error) === true); + assert(ioe.name === 'java.io.IOException'); + assert(ioe.message === 'this is a java IOException instance'); + assert( + ioe.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' + ); var e = hessian.decode(utils.bytes('v2/exception/UndeclaredThrowableException'), '2.0'); - e.should.be.an.Error; - should.ok((e instanceof Error) === true); - e.name.should.equal('java.io.IOException'); - e.message.should.equal('this is a java IOException instance'); - e.stack.should.equal('java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)'); + assert(e instanceof Error); + assert((e instanceof Error) === true); + assert(e.name === 'java.io.IOException'); + assert(e.message === 'this is a java IOException instance'); + assert( + e.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' + ); var e = hessian.decode(utils.bytes('v2/exception/UndeclaredThrowableException2'), '2.0'); - e.should.be.an.Error; - should.ok((e instanceof Error) === true); - e.name.should.equal('java.io.IOException'); - e.message.should.equal('模拟测试异常; this is a java IOException instance'); - e.stack.should.equal('java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)'); + assert(e instanceof Error); + assert((e instanceof Error) === true); + assert(e.name === 'java.io.IOException'); + assert(e.message === '模拟测试异常; this is a java IOException instance'); + assert( + e.stack === 'java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)' + ); }); it('should read hessian 1.0 exception', function () { var ioe = hessian.decode(utils.bytes('v1/exception/IOException'), '2.0'); - ioe.should.be.an.Error; - should.ok((ioe instanceof Error) === true); - ioe.name.should.equal('java.io.IOException'); - ioe.message.should.equal('this is a java IOException instance'); - ioe.stack.should.equal('java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)'); + assert(ioe instanceof Error); + assert((ioe instanceof Error) === true); + assert(ioe.name === 'java.io.IOException'); + assert(ioe.message === 'this is a java IOException instance'); + assert( + ioe.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' + ); var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException'), '2.0'); - e.should.be.an.Error; - should.ok((e instanceof Error) === true); - e.name.should.equal('java.io.IOException'); - e.message.should.equal('this is a java IOException instance'); - e.stack.should.equal('java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)'); + assert(e instanceof Error); + assert((e instanceof Error) === true); + assert(e.name === 'java.io.IOException'); + assert(e.message === 'this is a java IOException instance'); + assert( + e.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' + ); var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException2'), '2.0'); - e.should.be.an.Error; - should.ok((e instanceof Error) === true); - e.name.should.equal('java.io.IOException'); - e.message.should.equal('模拟测试异常; this is a java IOException instance'); - e.stack.should.equal('java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)'); + assert(e instanceof Error); + assert((e instanceof Error) === true); + assert(e.name === 'java.io.IOException'); + assert(e.message === '模拟测试异常; this is a java IOException instance'); + assert( + e.stack === 'java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)' + ); }); }); }); diff --git a/test/int.test.js b/test/int.test.js index c841212..b15faf8 100644 --- a/test/int.test.js +++ b/test/int.test.js @@ -10,233 +10,238 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); var utils = require('./utils'); describe('int.test.js', function () { it('should read integer 300', function () { - hessian.decode(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c])).should.equal(300); + assert( + hessian.decode(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c])) === 300 + ); }); it('should write integer 300', function () { - hessian.encode(300).should.eql(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c])); + assert.deepEqual( + hessian.encode(300), + new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c]) + ); }); it('should write integer 0', function () { - hessian.encode(0).should.eql(new Buffer(['I'.charCodeAt(0), 0, 0, 0, 0])); + assert.deepEqual(hessian.encode(0), new Buffer(['I'.charCodeAt(0), 0, 0, 0, 0])); }); it('should write number as java write', function () { - hessian.encode(0, '1.0').should.eql(utils.bytes('v1/number/0')); - hessian.encode(1).should.eql(utils.bytes('v1/number/1')); - hessian.encode(10).should.eql(utils.bytes('v1/number/10')); - hessian.encode(16).should.eql(utils.bytes('v1/number/16')); - hessian.encode(2047).should.eql(utils.bytes('v1/number/2047')); - hessian.encode(255, '1.0').should.eql(utils.bytes('v1/number/255')); - hessian.encode(256, '1.0').should.eql(utils.bytes('v1/number/256')); - hessian.encode(262143, '1.0').should.eql(utils.bytes('v1/number/262143')); - hessian.encode(262144, '1.0').should.eql(utils.bytes('v1/number/262144')); - hessian.encode(46, '1.0').should.eql(utils.bytes('v1/number/46')); - hessian.encode(47, '1.0').should.eql(utils.bytes('v1/number/47')); - - hessian.encode(-16, '1.0').should.eql(utils.bytes('v1/number/-16')); - hessian.encode(-2048, '1.0').should.eql(utils.bytes('v1/number/-2048')); - hessian.encode(-256).should.eql(utils.bytes('v1/number/-256')); - hessian.encode(-262144, '1.0').should.eql(utils.bytes('v1/number/-262144')); - hessian.encode(-262145, '1.0').should.eql(utils.bytes('v1/number/-262145')); + assert.deepEqual(hessian.encode(0, '1.0'), utils.bytes('v1/number/0')); + assert.deepEqual(hessian.encode(1), utils.bytes('v1/number/1')); + assert.deepEqual(hessian.encode(10), utils.bytes('v1/number/10')); + assert.deepEqual(hessian.encode(16), utils.bytes('v1/number/16')); + assert.deepEqual(hessian.encode(2047), utils.bytes('v1/number/2047')); + assert.deepEqual(hessian.encode(255, '1.0'), utils.bytes('v1/number/255')); + assert.deepEqual(hessian.encode(256, '1.0'), utils.bytes('v1/number/256')); + assert.deepEqual(hessian.encode(262143, '1.0'), utils.bytes('v1/number/262143')); + assert.deepEqual(hessian.encode(262144, '1.0'), utils.bytes('v1/number/262144')); + assert.deepEqual(hessian.encode(46, '1.0'), utils.bytes('v1/number/46')); + assert.deepEqual(hessian.encode(47, '1.0'), utils.bytes('v1/number/47')); + + assert.deepEqual(hessian.encode(-16, '1.0'), utils.bytes('v1/number/-16')); + assert.deepEqual(hessian.encode(-2048, '1.0'), utils.bytes('v1/number/-2048')); + assert.deepEqual(hessian.encode(-256), utils.bytes('v1/number/-256')); + assert.deepEqual(hessian.encode(-262144, '1.0'), utils.bytes('v1/number/-262144')); + assert.deepEqual(hessian.encode(-262145, '1.0'), utils.bytes('v1/number/-262145')); }); it('should read java number bin', function () { - hessian.decode(utils.bytes('v1/number/0'), '1.0').should.equal(0); - hessian.decode(utils.bytes('v1/number/1'), '1.0').should.equal(1); - hessian.decode(utils.bytes('v1/number/10'), '1.0').should.equal(10); - hessian.decode(utils.bytes('v1/number/16'), '1.0').should.equal(16); - hessian.decode(utils.bytes('v1/number/2047'), '1.0').should.equal(2047); - hessian.decode(utils.bytes('v1/number/255'), '1.0').should.equal(255); - hessian.decode(utils.bytes('v1/number/256'), '1.0').should.equal(256); - hessian.decode(utils.bytes('v1/number/262143'), '1.0').should.equal(262143); - hessian.decode(utils.bytes('v1/number/262144'), '1.0').should.equal(262144); - hessian.decode(utils.bytes('v1/number/46'), '1.0').should.equal(46); - hessian.decode(utils.bytes('v1/number/47'), '1.0').should.equal(47); - hessian.decode(utils.bytes('v1/number/-16'), '1.0').should.equal(-16); - hessian.decode(utils.bytes('v1/number/-2048'), '1.0').should.equal(-2048); - hessian.decode(utils.bytes('v1/number/-256'), '1.0').should.equal(-256); - hessian.decode(utils.bytes('v1/number/-262144'), '1.0').should.equal(-262144); - hessian.decode(utils.bytes('v1/number/-262145'), '1.0').should.equal(-262145); + assert(hessian.decode(utils.bytes('v1/number/0'), '1.0') === 0); + assert(hessian.decode(utils.bytes('v1/number/1'), '1.0') === 1); + assert(hessian.decode(utils.bytes('v1/number/10'), '1.0') === 10); + assert(hessian.decode(utils.bytes('v1/number/16'), '1.0') === 16); + assert(hessian.decode(utils.bytes('v1/number/2047'), '1.0') === 2047); + assert(hessian.decode(utils.bytes('v1/number/255'), '1.0') === 255); + assert(hessian.decode(utils.bytes('v1/number/256'), '1.0') === 256); + assert(hessian.decode(utils.bytes('v1/number/262143'), '1.0') === 262143); + assert(hessian.decode(utils.bytes('v1/number/262144'), '1.0') === 262144); + assert(hessian.decode(utils.bytes('v1/number/46'), '1.0') === 46); + assert(hessian.decode(utils.bytes('v1/number/47'), '1.0') === 47); + assert(hessian.decode(utils.bytes('v1/number/-16'), '1.0') === -16); + assert(hessian.decode(utils.bytes('v1/number/-2048'), '1.0') === -2048); + assert(hessian.decode(utils.bytes('v1/number/-256'), '1.0') === -256); + assert(hessian.decode(utils.bytes('v1/number/-262144'), '1.0') === -262144); + assert(hessian.decode(utils.bytes('v1/number/-262145'), '1.0') === -262145); }); describe('v2.0', function () { it('should read compact integers', function () { - hessian.decode(new Buffer([0x90]), '2.0').should.equal(0); - hessian.decode(new Buffer([0x80]), '2.0').should.equal(-16); - hessian.decode(new Buffer([0xbf]), '2.0').should.equal(47); - - hessian.decode(new Buffer([0xc8, 0x00]), '2.0').should.equal(0); - hessian.decode(new Buffer([0xc0, 0x00]), '2.0').should.equal(-2048); - hessian.decode(new Buffer([0xc7, 0x00]), '2.0').should.equal(-256); - hessian.decode(new Buffer([0xcf, 0xff]), '2.0').should.equal(2047); - - hessian.decode(new Buffer([0xd4, 0x00, 0x00]), '2.0').should.equal(0); - hessian.decode(new Buffer([0xd0, 0x00, 0x00]), '2.0').should.equal(-262144); - hessian.decode(new Buffer([0xd7, 0xff, 0xff]), '2.0').should.equal(262143); - - hessian.decode(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x00, 0x00]), '2.0').should.equal(0); - hessian.decode(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c]), '2.0').should.equal(300); + assert(hessian.decode(new Buffer([0x90]), '2.0') === 0); + assert(hessian.decode(new Buffer([0x80]), '2.0') === -16); + assert(hessian.decode(new Buffer([0xbf]), '2.0') === 47); + + assert(hessian.decode(new Buffer([0xc8, 0x00]), '2.0') === 0); + assert(hessian.decode(new Buffer([0xc0, 0x00]), '2.0') === -2048); + assert(hessian.decode(new Buffer([0xc7, 0x00]), '2.0') === -256); + assert(hessian.decode(new Buffer([0xcf, 0xff]), '2.0') === 2047); + + assert(hessian.decode(new Buffer([0xd4, 0x00, 0x00]), '2.0') === 0); + assert(hessian.decode(new Buffer([0xd0, 0x00, 0x00]), '2.0') === -262144); + assert(hessian.decode(new Buffer([0xd7, 0xff, 0xff]), '2.0') === 262143); + + assert( + hessian.decode(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x00, 0x00]), '2.0') === 0 + ); + assert( + hessian.decode(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c]), '2.0') === 300 + ); }); it('should write number as java write', function () { - hessian.encode(0, '2.0').should.eql(utils.bytes('v2/number/0')); - hessian.encode(1, '2.0').should.eql(utils.bytes('v2/number/1')); - hessian.encode(10, '2.0').should.eql(utils.bytes('v2/number/10')); - hessian.encode(16, '2.0').should.eql(utils.bytes('v2/number/16')); - hessian.encode(2047, '2.0').should.eql(utils.bytes('v2/number/2047')); - hessian.encode(255, '2.0').should.eql(utils.bytes('v2/number/255')); - hessian.encode(256, '2.0').should.eql(utils.bytes('v2/number/256')); - hessian.encode(262143, '2.0').should.eql(utils.bytes('v2/number/262143')); - hessian.encode(262144, '2.0').should.eql(utils.bytes('v2/number/262144')); - hessian.encode(46, '2.0').should.eql(utils.bytes('v2/number/46')); - hessian.encode(47, '2.0').should.eql(utils.bytes('v2/number/47')); - hessian.encode(-16, '2.0').should.eql(utils.bytes('v2/number/-16')); - hessian.encode(-2048, '2.0').should.eql(utils.bytes('v2/number/-2048')); - hessian.encode(-256, '2.0').should.eql(utils.bytes('v2/number/-256')); - hessian.encode(-262144, '2.0').should.eql(utils.bytes('v2/number/-262144')); - hessian.encode(-262145, '2.0').should.eql(utils.bytes('v2/number/-262145')); + assert.deepEqual(hessian.encode(0, '2.0'), utils.bytes('v2/number/0')); + assert.deepEqual(hessian.encode(1, '2.0'), utils.bytes('v2/number/1')); + assert.deepEqual(hessian.encode(10, '2.0'), utils.bytes('v2/number/10')); + assert.deepEqual(hessian.encode(16, '2.0'), utils.bytes('v2/number/16')); + assert.deepEqual(hessian.encode(2047, '2.0'), utils.bytes('v2/number/2047')); + assert.deepEqual(hessian.encode(255, '2.0'), utils.bytes('v2/number/255')); + assert.deepEqual(hessian.encode(256, '2.0'), utils.bytes('v2/number/256')); + assert.deepEqual(hessian.encode(262143, '2.0'), utils.bytes('v2/number/262143')); + assert.deepEqual(hessian.encode(262144, '2.0'), utils.bytes('v2/number/262144')); + assert.deepEqual(hessian.encode(46, '2.0'), utils.bytes('v2/number/46')); + assert.deepEqual(hessian.encode(47, '2.0'), utils.bytes('v2/number/47')); + assert.deepEqual(hessian.encode(-16, '2.0'), utils.bytes('v2/number/-16')); + assert.deepEqual(hessian.encode(-2048, '2.0'), utils.bytes('v2/number/-2048')); + assert.deepEqual(hessian.encode(-256, '2.0'), utils.bytes('v2/number/-256')); + assert.deepEqual(hessian.encode(-262144, '2.0'), utils.bytes('v2/number/-262144')); + assert.deepEqual(hessian.encode(-262145, '2.0'), utils.bytes('v2/number/-262145')); }); it('should read java number bin', function () { - hessian.decode(utils.bytes('v2/number/0'), '2.0').should.equal(0); - hessian.decode(utils.bytes('v2/number/1'), '2.0').should.equal(1); - hessian.decode(utils.bytes('v2/number/10'), '2.0').should.equal(10); - hessian.decode(utils.bytes('v2/number/16'), '2.0').should.equal(16); - hessian.decode(utils.bytes('v2/number/2047'), '2.0').should.equal(2047); - hessian.decode(utils.bytes('v2/number/255'), '2.0').should.equal(255); - hessian.decode(utils.bytes('v2/number/256'), '2.0').should.equal(256); - hessian.decode(utils.bytes('v2/number/262143'), '2.0').should.equal(262143); - hessian.decode(utils.bytes('v2/number/262144'), '2.0').should.equal(262144); - hessian.decode(utils.bytes('v2/number/46'), '2.0').should.equal(46); - hessian.decode(utils.bytes('v2/number/47'), '2.0').should.equal(47); - hessian.decode(utils.bytes('v2/number/-16'), '2.0').should.equal(-16); - hessian.decode(utils.bytes('v2/number/-2048'), '2.0').should.equal(-2048); - hessian.decode(utils.bytes('v2/number/-256'), '2.0').should.equal(-256); - hessian.decode(utils.bytes('v2/number/-262144'), '2.0').should.equal(-262144); - hessian.decode(utils.bytes('v2/number/-262145'), '2.0').should.equal(-262145); + assert(hessian.decode(utils.bytes('v2/number/0'), '2.0') === 0); + assert(hessian.decode(utils.bytes('v2/number/1'), '2.0') === 1); + assert(hessian.decode(utils.bytes('v2/number/10'), '2.0') === 10); + assert(hessian.decode(utils.bytes('v2/number/16'), '2.0') === 16); + assert(hessian.decode(utils.bytes('v2/number/2047'), '2.0') === 2047); + assert(hessian.decode(utils.bytes('v2/number/255'), '2.0') === 255); + assert(hessian.decode(utils.bytes('v2/number/256'), '2.0') === 256); + assert(hessian.decode(utils.bytes('v2/number/262143'), '2.0') === 262143); + assert(hessian.decode(utils.bytes('v2/number/262144'), '2.0') === 262144); + assert(hessian.decode(utils.bytes('v2/number/46'), '2.0') === 46); + assert(hessian.decode(utils.bytes('v2/number/47'), '2.0') === 47); + assert(hessian.decode(utils.bytes('v2/number/-16'), '2.0') === -16); + assert(hessian.decode(utils.bytes('v2/number/-2048'), '2.0') === -2048); + assert(hessian.decode(utils.bytes('v2/number/-256'), '2.0') === -256); + assert(hessian.decode(utils.bytes('v2/number/-262144'), '2.0') === -262144); + assert(hessian.decode(utils.bytes('v2/number/-262145'), '2.0') === -262145); }); it('should read hessian 1.0 number bin', function () { - hessian.decode(utils.bytes('v1/number/0'), '2.0').should.equal(0); - hessian.decode(utils.bytes('v1/number/1'), '2.0').should.equal(1); - hessian.decode(utils.bytes('v1/number/10'), '2.0').should.equal(10); - hessian.decode(utils.bytes('v1/number/16'), '2.0').should.equal(16); - hessian.decode(utils.bytes('v1/number/2047'), '2.0').should.equal(2047); - hessian.decode(utils.bytes('v1/number/255'), '2.0').should.equal(255); - hessian.decode(utils.bytes('v1/number/256'), '2.0').should.equal(256); - hessian.decode(utils.bytes('v1/number/262143'), '2.0').should.equal(262143); - hessian.decode(utils.bytes('v1/number/262144'), '2.0').should.equal(262144); - hessian.decode(utils.bytes('v1/number/46'), '2.0').should.equal(46); - hessian.decode(utils.bytes('v1/number/47'), '2.0').should.equal(47); - hessian.decode(utils.bytes('v1/number/-16'), '2.0').should.equal(-16); - hessian.decode(utils.bytes('v1/number/-2048'), '2.0').should.equal(-2048); - hessian.decode(utils.bytes('v1/number/-256'), '2.0').should.equal(-256); - hessian.decode(utils.bytes('v1/number/-262144'), '2.0').should.equal(-262144); - hessian.decode(utils.bytes('v1/number/-262145'), '2.0').should.equal(-262145); + assert(hessian.decode(utils.bytes('v1/number/0'), '2.0') === 0); + assert(hessian.decode(utils.bytes('v1/number/1'), '2.0') === 1); + assert(hessian.decode(utils.bytes('v1/number/10'), '2.0') === 10); + assert(hessian.decode(utils.bytes('v1/number/16'), '2.0') === 16); + assert(hessian.decode(utils.bytes('v1/number/2047'), '2.0') === 2047); + assert(hessian.decode(utils.bytes('v1/number/255'), '2.0') === 255); + assert(hessian.decode(utils.bytes('v1/number/256'), '2.0') === 256); + assert(hessian.decode(utils.bytes('v1/number/262143'), '2.0') === 262143); + assert(hessian.decode(utils.bytes('v1/number/262144'), '2.0') === 262144); + assert(hessian.decode(utils.bytes('v1/number/46'), '2.0') === 46); + assert(hessian.decode(utils.bytes('v1/number/47'), '2.0') === 47); + assert(hessian.decode(utils.bytes('v1/number/-16'), '2.0') === -16); + assert(hessian.decode(utils.bytes('v1/number/-2048'), '2.0') === -2048); + assert(hessian.decode(utils.bytes('v1/number/-256'), '2.0') === -256); + assert(hessian.decode(utils.bytes('v1/number/-262144'), '2.0') === -262144); + assert(hessian.decode(utils.bytes('v1/number/-262145'), '2.0') === -262145); }); it('should write compact integers', function () { // -16 ~ 47 var buf = hessian.encode(0, '2.0'); - buf.should.length(1); - buf[0].should.equal(0x90); - buf.should.eql(new Buffer([0x90])); + assert(buf.length === 1); + assert(buf[0] === 0x90); + assert.deepEqual(buf, new Buffer([0x90])); buf = hessian.encode(1, '2.0'); - buf.should.length(1); - buf[0].should.equal(0x91); - buf.should.eql(new Buffer([0x91])); + assert(buf.length === 1); + assert(buf[0] === 0x91); + assert.deepEqual(buf, new Buffer([0x91])); buf = hessian.encode(-16, '2.0'); - buf.should.length(1); - buf[0].should.equal(0x80); - buf.should.eql(new Buffer([0x80])); + assert(buf.length === 1); + assert(buf[0] === 0x80); + assert.deepEqual(buf, new Buffer([0x80])); buf = hessian.encode(46, '2.0'); - buf.should.length(1); - buf[0].should.equal(0xbe); - buf.should.eql(new Buffer([0xbe])); + assert(buf.length === 1); + assert(buf[0] === 0xbe); + assert.deepEqual(buf, new Buffer([0xbe])); buf = hessian.encode(47, '2.0'); - buf.should.length(1); - buf[0].should.equal(0xbf); - buf.should.eql(new Buffer([0xbf])); + assert(buf.length === 1); + assert(buf[0] === 0xbf); + assert.deepEqual(buf, new Buffer([0xbf])); // -2048 ~ 2047 buf = hessian.encode(-2048, '2.0'); - buf.should.length(2); - buf[0].should.equal(0xc0); - buf[1].should.equal(0x00); - buf.should.eql(new Buffer([0xc0, 0x00])); + assert(buf.length === 2); + assert(buf[0] === 0xc0); + assert(buf[1] === 0x00); + assert.deepEqual(buf, new Buffer([0xc0, 0x00])); buf = hessian.encode(-256, '2.0'); - buf.should.length(2); - buf[0].should.equal(0xc7); - buf[1].should.equal(0x00); - buf.should.eql(new Buffer([0xc7, 0x00])); + assert(buf.length === 2); + assert(buf[0] === 0xc7); + assert(buf[1] === 0x00); + assert.deepEqual(buf, new Buffer([0xc7, 0x00])); buf = hessian.encode(255, '2.0'); - buf.should.length(2); - buf[0].should.equal(0xc8); - buf[1].should.equal(0xff); - buf.should.eql(new Buffer([0xc8, 0xff])); + assert(buf.length === 2); + assert(buf[0] === 0xc8); + assert(buf[1] === 0xff); + assert.deepEqual(buf, new Buffer([0xc8, 0xff])); buf = hessian.encode(256, '2.0'); - buf.should.length(2); - buf[0].should.equal(0xc9); - buf[1].should.equal(0x00); - buf.should.eql(new Buffer([0xc9, 0x00])); + assert(buf.length === 2); + assert(buf[0] === 0xc9); + assert(buf[1] === 0x00); + assert.deepEqual(buf, new Buffer([0xc9, 0x00])); buf = hessian.encode(2047, '2.0'); - buf.should.length(2); - buf[0].should.equal(0xcf); - buf[1].should.equal(0xff); - buf.should.eql(new Buffer([0xcf, 0xff])); + assert(buf.length === 2); + assert(buf[0] === 0xcf); + assert(buf[1] === 0xff); + assert.deepEqual(buf, new Buffer([0xcf, 0xff])); // -262144 ~ 262143 buf = hessian.encode(-262144, '2.0'); - buf.should.length(3); - buf[0].should.equal(0xd0); - buf[1].should.equal(0x00); - buf[2].should.equal(0x00); - buf.should.eql(new Buffer([0xd0, 0x00, 0x00])); + assert(buf.length === 3); + assert(buf[0] === 0xd0); + assert(buf[1] === 0x00); + assert(buf[2] === 0x00); + assert.deepEqual(buf, new Buffer([0xd0, 0x00, 0x00])); buf = hessian.encode(262143, '2.0'); - buf.should.length(3); - buf[0].should.equal(0xd7); - buf[1].should.equal(0xff); - buf[2].should.equal(0xff); - buf.should.eql(new Buffer([0xd7, 0xff, 0xff])); + assert(buf.length === 3); + assert(buf[0] === 0xd7); + assert(buf[1] === 0xff); + assert(buf[2] === 0xff); + assert.deepEqual(buf, new Buffer([0xd7, 0xff, 0xff])); // 262144 buf = hessian.encode(262144, '2.0'); - buf.should.length(5); - buf[0].should.equal('I'.charCodeAt(0)); - buf[1].should.equal(0x00); - buf[2].should.equal(0x04); - buf[3].should.equal(0x00); - buf[4].should.equal(0x00); - buf.should.eql(new Buffer(['I'.charCodeAt(0), 0x00, 0x04, 0x00, 0x00])); + assert(buf.length === 5); + assert(buf[0] === 'I'.charCodeAt(0)); + assert(buf[1] === 0x00); + assert(buf[2] === 0x04); + assert(buf[3] === 0x00); + assert(buf[4] === 0x00); + assert.deepEqual(buf, new Buffer(['I'.charCodeAt(0), 0x00, 0x04, 0x00, 0x00])); buf = hessian.encode(-262145, '2.0'); - buf.should.length(5); - buf[0].should.equal('I'.charCodeAt(0)); - buf[1].should.equal(0xff); - buf[2].should.equal(0xfb); - buf[3].should.equal(0xff); - buf[4].should.equal(0xff); - buf.should.eql(new Buffer(['I'.charCodeAt(0), 0xff, 0xfb, 0xff, 0xff])); + assert(buf.length === 5); + assert(buf[0] === 'I'.charCodeAt(0)); + assert(buf[1] === 0xff); + assert(buf[2] === 0xfb); + assert(buf[3] === 0xff); + assert(buf[4] === 0xff); + assert.deepEqual(buf, new Buffer(['I'.charCodeAt(0), 0xff, 0xfb, 0xff, 0xff])); }); }); }); diff --git a/test/list.test.js b/test/list.test.js index 81ac26b..7590d0e 100644 --- a/test/list.test.js +++ b/test/list.test.js @@ -10,11 +10,7 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var java = require('js-to-java'); var hessian = require('../'); var utils = require('./utils'); @@ -35,8 +31,8 @@ describe('list.test.js', function () { ]); it('should read int[] = {0, 1}', function () { - hessian.decode(intListBuffer).should.eql([0, 1]); - hessian.decode(intListBuffer, true).should.eql({ + assert.deepEqual(hessian.decode(intListBuffer), [0, 1]); + assert.deepEqual(hessian.decode(intListBuffer, true), { '$class': '[int', '$': [ { '$class': 'int', '$': 0 }, { '$class': 'int', '$': 1 } ] }); @@ -44,14 +40,14 @@ describe('list.test.js', function () { it('should write int[] = {0, 1}', function () { var buf = hessian.encode(java.array.int([0, 1])); - buf.should.be.a.Buffer; - buf.should.length(intListBuffer.length); - buf.should.eql(intListBuffer); + assert(Buffer.isBuffer(buf)); + assert(buf.length === intListBuffer.length); + assert.deepEqual(buf, intListBuffer); - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: '[int', $: [0, 1] - }).should.eql(intListBuffer); + }), intListBuffer); // empty list var empty = Buffer.concat([ @@ -65,7 +61,7 @@ describe('list.test.js', function () { 'z'.charCodeAt(0) ]) ]); - hessian.decode(empty).should.eql([]); + assert.deepEqual(hessian.decode(empty), []); }); it('should read write anonymous variable-length list = {0, null, "foobar"}', function () { @@ -80,22 +76,22 @@ describe('list.test.js', function () { new Buffer('z'), ]); - hessian.decode(anonymousList).should.eql([0, null, 'foobar']); + assert.deepEqual(hessian.decode(anonymousList), [0, null, 'foobar']); // empty var emptyList = Buffer.concat([ new Buffer('V'), new Buffer('z'), ]); - hessian.decode(emptyList).should.eql([]); + assert.deepEqual(hessian.decode(emptyList), []); }); it('should write and read untyped list', function () { - hessian.encode([1, 2, 'foo'], '1.0').should.eql(utils.bytes('v1/list/untyped_list')); - hessian.encode([], '1.0').should.eql(utils.bytes('v1/list/untyped_[]')); + assert.deepEqual(hessian.encode([1, 2, 'foo'], '1.0'), utils.bytes('v1/list/untyped_list')); + assert.deepEqual(hessian.encode([], '1.0'), utils.bytes('v1/list/untyped_[]')); - hessian.decode(utils.bytes('v1/list/untyped_list'), '1.0').should.eql([1, 2, 'foo']); - hessian.decode(utils.bytes('v1/list/untyped_list'), '1.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_list'), '1.0'), [1, 2, 'foo']); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_list'), '1.0', true), { $class: 'java.util.ArrayList', $: [ { '$class': 'int', '$': 1 }, @@ -103,29 +99,31 @@ describe('list.test.js', function () { { '$class': 'java.lang.String', '$': 'foo' } ] }); - hessian.decode(utils.bytes('v1/list/untyped_[]'), '1.0').should.eql([]); - hessian.decode(utils.bytes('v1/list/untyped_[foo,bar]'), '1.0', true).should.eql( { - $class: 'java.util.ArrayList', - $: [ - { '$class': 'java.lang.String', '$': 'foo' }, - { '$class': 'java.lang.String', '$': 'bar' } - ] - }); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_[]'), '1.0'), []); + assert.deepEqual( + hessian.decode(utils.bytes('v1/list/untyped_[foo,bar]'), '1.0', true), + { + $class: 'java.util.ArrayList', + $: [ + { '$class': 'java.lang.String', '$': 'foo' }, + { '$class': 'java.lang.String', '$': 'bar' } + ] + } + ); // java.util.ArrayList as simple - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'java.util.ArrayList', $: [1, 2, 'foo'] - }, '1.0').should.eql(utils.bytes('v1/list/untyped_list')); + }, '1.0'), utils.bytes('v1/list/untyped_list')); }); it('should write and read typed fixed-length list', function () { - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'hessian.demo.SomeArrayList', $: ['ok', 'some list'] - }, '1.0').should.eql(utils.bytes('v1/list/typed_list')); - hessian.decode(utils.bytes('v1/list/typed_list'), '1.0', true) - .should.eql({ + }, '1.0'), utils.bytes('v1/list/typed_list')); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/typed_list'), '1.0', true), { '$class': 'hessian.demo.SomeArrayList', '$': [ { '$class': 'java.lang.String', '$': 'ok' }, @@ -133,16 +131,18 @@ describe('list.test.js', function () { ] }); - hessian.decode(utils.bytes('v1/list/typed_list'), '1.0') - .should.eql([ 'ok', 'some list' ]); + assert.deepEqual( + hessian.decode(utils.bytes('v1/list/typed_list'), '1.0'), + [ 'ok', 'some list' ] + ); var list = { $class: '[int', $: [1, 2, 3] }; - hessian.encode(list, '1.0').should.eql(utils.bytes('v1/list/[int')); - hessian.decode(utils.bytes('v1/list/[int'), '1.0').should.eql([1, 2, 3]); - hessian.decode(utils.bytes('v1/list/[int'), '1.0', true).should.eql({ + assert.deepEqual(hessian.encode(list, '1.0'), utils.bytes('v1/list/[int')); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/[int'), '1.0'), [1, 2, 3]); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/[int'), '1.0', true), { '$class': '[int', '$': [ { '$class': 'int', '$': 1 }, @@ -155,8 +155,8 @@ describe('list.test.js', function () { $class: '[string', $: ['1', '@', '3'] }; - hessian.encode(strs, '1.0').should.eql(utils.bytes('v1/list/[string')); - hessian.decode(utils.bytes('v1/list/[string'), '1.0', true).should.eql({ + assert.deepEqual(hessian.encode(strs, '1.0'), utils.bytes('v1/list/[string')); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/[string'), '1.0', true), { '$class': '[string', '$': [ { '$class': 'java.lang.String', '$': '1' }, @@ -168,70 +168,83 @@ describe('list.test.js', function () { describe('v2.0', function () { it('should write and read untyped list', function () { - hessian.encode([1, 2, 'foo'], '2.0').should.eql(utils.bytes('v2/list/untyped_list')); - hessian.encode([], '2.0').should.eql(utils.bytes('v2/list/untyped_[]')); - - hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0').should.eql([1, 2, 'foo']); - hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true).should.eql([1, 2, 'foo']); - hessian.decode(utils.bytes('v2/list/untyped_[]'), '2.0').should.eql([]); - hessian.decode(utils.bytes('v2/list/untyped_[foo,bar]'), '2.0', true).should.eql(['foo', 'bar']); + assert.deepEqual(hessian.encode([1, 2, 'foo'], '2.0'), utils.bytes('v2/list/untyped_list')); + assert.deepEqual(hessian.encode([], '2.0'), utils.bytes('v2/list/untyped_[]')); + + assert.deepEqual(hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0'), [1, 2, 'foo']); + assert.deepEqual( + hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true), + [1, 2, 'foo'] + ); + assert.deepEqual(hessian.decode(utils.bytes('v2/list/untyped_[]'), '2.0'), []); + assert.deepEqual( + hessian.decode(utils.bytes('v2/list/untyped_[foo,bar]'), '2.0', true), + ['foo', 'bar'] + ); // java.util.ArrayList as simple - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'java.util.ArrayList', $: [1, 2, 'foo'] - }, '2.0').should.eql(utils.bytes('v2/list/untyped_list')); + }, '2.0'), utils.bytes('v2/list/untyped_list')); // encode again should cache class - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'java.util.ArrayList', $: [1, 2, 'foo'] - }, '2.0').should.eql(utils.bytes('v2/list/untyped_list')); + }, '2.0'), utils.bytes('v2/list/untyped_list')); }); it('should write and read typed fixed-length list', function () { - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'hessian.demo.SomeArrayList', $: ['ok', 'some list'] - }, '2.0').should.eql(utils.bytes('v2/list/typed_list')); + }, '2.0'), utils.bytes('v2/list/typed_list')); // encode again should use type cache - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'hessian.demo.SomeArrayList', $: ['ok', 'some list'] - }, '2.0').should.eql(utils.bytes('v2/list/typed_list')); + }, '2.0'), utils.bytes('v2/list/typed_list')); - hessian.decode(utils.bytes('v2/list/typed_list'), '2.0', true) - .should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v2/list/typed_list'), '2.0', true), { '$class': 'hessian.demo.SomeArrayList', '$': [ 'ok', 'some list' ] }); - hessian.decode(utils.bytes('v2/list/typed_list'), '2.0') - .should.eql([ 'ok', 'some list' ]); + assert.deepEqual( + hessian.decode(utils.bytes('v2/list/typed_list'), '2.0'), + [ 'ok', 'some list' ] + ); var list = { $class: '[int', $: [1, 2, 3] }; - hessian.encode(list, '2.0').should.eql(utils.bytes('v2/list/[int')); - hessian.encode(list, '2.0').should.eql(utils.bytes('v2/list/[int')); + assert.deepEqual(hessian.encode(list, '2.0'), utils.bytes('v2/list/[int')); + assert.deepEqual(hessian.encode(list, '2.0'), utils.bytes('v2/list/[int')); - hessian.decode(utils.bytes('v2/list/[int'), '2.0').should.eql([1, 2, 3]); + assert.deepEqual(hessian.decode(utils.bytes('v2/list/[int'), '2.0'), [1, 2, 3]); // encode again should use type cache - hessian.decode(utils.bytes('v2/list/[int'), '2.0', true).should.eql(list); + assert.deepEqual(hessian.decode(utils.bytes('v2/list/[int'), '2.0', true), list); var strs = { $class: '[string', $: ['1', '@', '3'] }; - hessian.encode(strs, '2.0').should.eql(utils.bytes('v2/list/[string')); - hessian.decode(utils.bytes('v2/list/[string'), '2.0', true).should.eql(strs); + assert.deepEqual(hessian.encode(strs, '2.0'), utils.bytes('v2/list/[string')); + assert.deepEqual(hessian.decode(utils.bytes('v2/list/[string'), '2.0', true), strs); }); it('should read hessian 1.0 untyped list', function () { - hessian.decode(utils.bytes('v1/list/untyped_list'), '2.0').should.eql([1, 2, 'foo']); - hessian.decode(utils.bytes('v1/list/untyped_list'), '2.0', true).should.eql([1, 2, 'foo']); - hessian.decode(utils.bytes('v1/list/untyped_[]'), '2.0').should.eql([]); - hessian.decode(utils.bytes('v1/list/untyped_[foo,bar]'), '2.0', true).should.eql(['foo', 'bar']); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_list'), '2.0'), [1, 2, 'foo']); + assert.deepEqual( + hessian.decode(utils.bytes('v1/list/untyped_list'), '2.0', true), + [1, 2, 'foo'] + ); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_[]'), '2.0'), []); + assert.deepEqual( + hessian.decode(utils.bytes('v1/list/untyped_[foo,bar]'), '2.0', true), + ['foo', 'bar'] + ); }); }); }); diff --git a/test/long.test.js b/test/long.test.js index f2b5405..045556d 100644 --- a/test/long.test.js +++ b/test/long.test.js @@ -10,11 +10,7 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var java = require('js-to-java'); var Long = require('long'); var hessian = require('../'); @@ -27,82 +23,99 @@ describe('long.test.js', function () { var obj = hessian.decode(hessian.encode({ $class: "java.lang.Long", $: null })); - should.ok(obj === null); + assert(obj === null); }); it('should read long 300', function () { - hessian.decode(longBuffer).should.equal(300); + assert(hessian.decode(longBuffer) === 300); }); it('should write long 300', function () { - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'long', $: 300 - }).should.eql(longBuffer); - hessian.encode(java.long(300)).should.eql(longBuffer); - hessian.encode(java.long(300)).should.eql(longBuffer); - hessian.encode(java.long(Long.fromNumber(300))).should.eql(longBuffer); - hessian.encode(Long.fromNumber(300)).should.eql(longBuffer); + }), longBuffer); + assert.deepEqual(hessian.encode(java.long(300)), longBuffer); + assert.deepEqual(hessian.encode(java.long(300)), longBuffer); + assert.deepEqual(hessian.encode(java.long(Long.fromNumber(300))), longBuffer); + assert.deepEqual(hessian.encode(Long.fromNumber(300)), longBuffer); }); it('should write long 0', function () { - hessian.encode(java.long(0)).should.eql( - new Buffer(['L'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0])); + assert.deepEqual( + hessian.encode(java.long(0)), + new Buffer(['L'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]) + ); }); it('should write and read equal java impl', function () { - hessian.encode(java.long(0), '1.0').should.eql(utils.bytes('v1/long/0')); - hessian.decode(utils.bytes('v1/long/0')).should.equal(0); - hessian.encode(java.long(-8), '1.0').should.eql(utils.bytes('v1/long/-8')); - hessian.decode(utils.bytes('v1/long/-8')).should.equal(-8); - hessian.encode(java.long(-7), '1.0').should.eql(utils.bytes('v1/long/-7')); - hessian.decode(utils.bytes('v1/long/-7')).should.equal(-7); - hessian.encode(java.long(15), '1.0').should.eql(utils.bytes('v1/long/15')); - hessian.decode(utils.bytes('v1/long/15')).should.equal(15); - hessian.encode(java.long(14), '1.0').should.eql(utils.bytes('v1/long/14')); - hessian.decode(utils.bytes('v1/long/14')).should.equal(14); - hessian.encode(java.long(-9), '1.0').should.eql(utils.bytes('v1/long/-9')); - hessian.decode(utils.bytes('v1/long/-9')).should.equal(-9); - hessian.encode(java.long(16), '1.0').should.eql(utils.bytes('v1/long/16')); - hessian.decode(utils.bytes('v1/long/16')).should.equal(16); - hessian.encode(java.long(255), '1.0').should.eql(utils.bytes('v1/long/255')); - hessian.decode(utils.bytes('v1/long/255')).should.equal(255); - hessian.encode(java.long(-2048), '1.0').should.eql(utils.bytes('v1/long/-2048')); - hessian.decode(utils.bytes('v1/long/-2048')).should.equal(-2048); - hessian.encode(java.long(2047), '1.0').should.eql(utils.bytes('v1/long/2047')); - hessian.decode(utils.bytes('v1/long/2047')).should.equal(2047); - hessian.encode(java.long(262143), '1.0').should.eql(utils.bytes('v1/long/262143')); - hessian.decode(utils.bytes('v1/long/262143')).should.equal(262143); - hessian.encode(java.long(-262144), '1.0').should.eql(utils.bytes('v1/long/-262144')); - hessian.decode(utils.bytes('v1/long/-262144')).should.equal(-262144); - hessian.encode(java.long(2048), '1.0').should.eql(utils.bytes('v1/long/2048')); - hessian.decode(utils.bytes('v1/long/2048')).should.equal(2048); - hessian.encode(java.long(-2049), '1.0').should.eql(utils.bytes('v1/long/-2049')); - hessian.decode(utils.bytes('v1/long/-2049')).should.equal(-2049); - hessian.encode(java.long(-2147483648), '1.0').should.eql(utils.bytes('v1/long/-2147483648')); - hessian.decode(utils.bytes('v1/long/-2147483648')).should.equal(-2147483648); - hessian.encode(java.long(-2147483647), '1.0').should.eql(utils.bytes('v1/long/-2147483647')); - hessian.decode(utils.bytes('v1/long/-2147483647')).should.equal(-2147483647); - hessian.encode(java.long(2147483647), '1.0').should.eql(utils.bytes('v1/long/2147483647')); - hessian.decode(utils.bytes('v1/long/2147483647')).should.equal(2147483647); - hessian.encode(java.long(2147483646), '1.0').should.eql(utils.bytes('v1/long/2147483646')); - hessian.decode(utils.bytes('v1/long/2147483646')).should.equal(2147483646); - hessian.encode(java.long(2147483648), '1.0').should.eql(utils.bytes('v1/long/2147483648')); - hessian.decode(utils.bytes('v1/long/2147483648')).should.equal(2147483648); + assert.deepEqual(hessian.encode(java.long(0), '1.0'), utils.bytes('v1/long/0')); + assert(hessian.decode(utils.bytes('v1/long/0')) === 0); + assert.deepEqual(hessian.encode(java.long(-8), '1.0'), utils.bytes('v1/long/-8')); + assert(hessian.decode(utils.bytes('v1/long/-8')) === -8); + assert.deepEqual(hessian.encode(java.long(-7), '1.0'), utils.bytes('v1/long/-7')); + assert(hessian.decode(utils.bytes('v1/long/-7')) === -7); + assert.deepEqual(hessian.encode(java.long(15), '1.0'), utils.bytes('v1/long/15')); + assert(hessian.decode(utils.bytes('v1/long/15')) === 15); + assert.deepEqual(hessian.encode(java.long(14), '1.0'), utils.bytes('v1/long/14')); + assert(hessian.decode(utils.bytes('v1/long/14')) === 14); + assert.deepEqual(hessian.encode(java.long(-9), '1.0'), utils.bytes('v1/long/-9')); + assert(hessian.decode(utils.bytes('v1/long/-9')) === -9); + assert.deepEqual(hessian.encode(java.long(16), '1.0'), utils.bytes('v1/long/16')); + assert(hessian.decode(utils.bytes('v1/long/16')) === 16); + assert.deepEqual(hessian.encode(java.long(255), '1.0'), utils.bytes('v1/long/255')); + assert(hessian.decode(utils.bytes('v1/long/255')) === 255); + assert.deepEqual(hessian.encode(java.long(-2048), '1.0'), utils.bytes('v1/long/-2048')); + assert(hessian.decode(utils.bytes('v1/long/-2048')) === -2048); + assert.deepEqual(hessian.encode(java.long(2047), '1.0'), utils.bytes('v1/long/2047')); + assert(hessian.decode(utils.bytes('v1/long/2047')) === 2047); + assert.deepEqual(hessian.encode(java.long(262143), '1.0'), utils.bytes('v1/long/262143')); + assert(hessian.decode(utils.bytes('v1/long/262143')) === 262143); + assert.deepEqual(hessian.encode(java.long(-262144), '1.0'), utils.bytes('v1/long/-262144')); + assert(hessian.decode(utils.bytes('v1/long/-262144')) === -262144); + assert.deepEqual(hessian.encode(java.long(2048), '1.0'), utils.bytes('v1/long/2048')); + assert(hessian.decode(utils.bytes('v1/long/2048')) === 2048); + assert.deepEqual(hessian.encode(java.long(-2049), '1.0'), utils.bytes('v1/long/-2049')); + assert(hessian.decode(utils.bytes('v1/long/-2049')) === -2049); + assert.deepEqual( + hessian.encode(java.long(-2147483648), '1.0'), + utils.bytes('v1/long/-2147483648') + ); + assert(hessian.decode(utils.bytes('v1/long/-2147483648')) === -2147483648); + assert.deepEqual( + hessian.encode(java.long(-2147483647), '1.0'), + utils.bytes('v1/long/-2147483647') + ); + assert(hessian.decode(utils.bytes('v1/long/-2147483647')) === -2147483647); + assert.deepEqual( + hessian.encode(java.long(2147483647), '1.0'), + utils.bytes('v1/long/2147483647') + ); + assert(hessian.decode(utils.bytes('v1/long/2147483647')) === 2147483647); + assert.deepEqual( + hessian.encode(java.long(2147483646), '1.0'), + utils.bytes('v1/long/2147483646') + ); + assert(hessian.decode(utils.bytes('v1/long/2147483646')) === 2147483646); + assert.deepEqual( + hessian.encode(java.long(2147483648), '1.0'), + utils.bytes('v1/long/2147483648') + ); + assert(hessian.decode(utils.bytes('v1/long/2147483648')) === 2147483648); }); it('should decode with type', function () { - hessian.decode(utils.bytes('v1/long/-7'), true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/long/-7'), true), { $class: 'long', $: -7, }); - hessian.decode(utils.bytes('v1/long/262143'), true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/long/262143'), true), { $class: 'long', $: 262143, }); - hessian.decode(utils.bytes('v1/long/2147483648'), true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/long/2147483648'), true), { $class: 'long', $: 2147483648, }); @@ -110,221 +123,245 @@ describe('long.test.js', function () { describe('v2.0', function () { it('should read compact long', function () { - hessian.decode(new Buffer([0xe0]), '2.0').should.equal(0); - hessian.decode(new Buffer([0xd8]), '2.0').should.equal(-8); - hessian.decode(new Buffer([0xef]), '2.0').should.equal(15); + assert(hessian.decode(new Buffer([0xe0]), '2.0') === 0); + assert(hessian.decode(new Buffer([0xd8]), '2.0') === -8); + assert(hessian.decode(new Buffer([0xef]), '2.0') === 15); - hessian.decode(new Buffer([0xf8, 0x00]), '2.0').should.equal(0); - hessian.decode(new Buffer([0xf0, 0x00]), '2.0').should.equal(-2048); - hessian.decode(new Buffer([0xf7, 0x00]), '2.0').should.equal(-256); - hessian.decode(new Buffer([0xff, 0xff]), '2.0').should.equal(2047); + assert(hessian.decode(new Buffer([0xf8, 0x00]), '2.0') === 0); + assert(hessian.decode(new Buffer([0xf0, 0x00]), '2.0') === -2048); + assert(hessian.decode(new Buffer([0xf7, 0x00]), '2.0') === -256); + assert(hessian.decode(new Buffer([0xff, 0xff]), '2.0') === 2047); - hessian.decode(new Buffer([0x3c, 0x00, 0x00]), '2.0').should.equal(0); - hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0').should.equal(-262144); - hessian.decode(new Buffer([0x3f, 0xff, 0xff]), '2.0').should.equal(262143); + assert(hessian.decode(new Buffer([0x3c, 0x00, 0x00]), '2.0') === 0); + assert(hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0') === -262144); + assert(hessian.decode(new Buffer([0x3f, 0xff, 0xff]), '2.0') === 262143); // four octet longs - hessian.decode(new Buffer([0x77, 0x00, 0x00, 0x00, 0x00]), '2.0').should.equal(0); - hessian.decode(new Buffer([0x77, 0x00, 0x00, 0x01, 0x2c]), '2.0').should.equal(300); - hessian.decode(new Buffer([0x77, 0x7f, 0xff, 0xff, 0xff]), '2.0').should.equal(2147483647); - hessian.decode(new Buffer([0x77, 0x80, 0x00, 0x00, 0x00]), '2.0').should.equal(-2147483648); + assert(hessian.decode(new Buffer([0x77, 0x00, 0x00, 0x00, 0x00]), '2.0') === 0); + assert(hessian.decode(new Buffer([0x77, 0x00, 0x00, 0x01, 0x2c]), '2.0') === 300); + assert( + hessian.decode(new Buffer([0x77, 0x7f, 0xff, 0xff, 0xff]), '2.0') === 2147483647 + ); + assert( + hessian.decode(new Buffer([0x77, 0x80, 0x00, 0x00, 0x00]), '2.0') === -2147483648 + ); }); it('should read normal long', function () { - hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00]), '2.0').should.equal(2147483648); + assert( + hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00]), '2.0') === 2147483648 + ); }); it('should write compact long', function () { // -8 ~ 15 var buf = hessian.encode(java.long(0), '2.0'); - buf.should.length(1); - buf[0].should.equal(0xe0); - buf.should.eql(new Buffer([0xe0])); + assert(buf.length === 1); + assert(buf[0] === 0xe0); + assert.deepEqual(buf, new Buffer([0xe0])); buf = hessian.encode(java.long(-8), '2.0'); - buf.should.length(1); - buf[0].should.equal(0xd8); - buf.should.eql(new Buffer([0xd8])); + assert(buf.length === 1); + assert(buf[0] === 0xd8); + assert.deepEqual(buf, new Buffer([0xd8])); buf = hessian.encode(java.long(-7), '2.0'); - buf.should.length(1); - buf[0].should.equal(0xd9); - buf.should.eql(new Buffer([0xd9])); + assert(buf.length === 1); + assert(buf[0] === 0xd9); + assert.deepEqual(buf, new Buffer([0xd9])); buf = hessian.encode(java.long(15), '2.0'); - buf.should.length(1); - buf[0].should.equal(0xef); - buf.should.eql(new Buffer([0xef])); + assert(buf.length === 1); + assert(buf[0] === 0xef); + assert.deepEqual(buf, new Buffer([0xef])); buf = hessian.encode(java.long(14), '2.0'); - buf.should.length(1); - buf[0].should.equal(0xee); - buf.should.eql(new Buffer([0xee])); + assert(buf.length === 1); + assert(buf[0] === 0xee); + assert.deepEqual(buf, new Buffer([0xee])); // -2048 ~ 2047 buf = hessian.encode(java.long(-9), '2.0'); - buf.should.length(2); - buf[0].should.equal(0xf7); - buf[1].should.equal(0xf7); - buf.should.eql(new Buffer([0xf7, 0xf7])); + assert(buf.length === 2); + assert(buf[0] === 0xf7); + assert(buf[1] === 0xf7); + assert.deepEqual(buf, new Buffer([0xf7, 0xf7])); buf = hessian.encode(java.long(16), '2.0'); - buf.should.length(2); - buf[0].should.equal(0xf8); - buf[1].should.equal(0x10); - buf.should.eql(new Buffer([0xf8, 0x10])); + assert(buf.length === 2); + assert(buf[0] === 0xf8); + assert(buf[1] === 0x10); + assert.deepEqual(buf, new Buffer([0xf8, 0x10])); buf = hessian.encode(java.long(255), '2.0'); - buf.should.length(2); - buf[0].should.equal(0xf8); - buf[1].should.equal(0xff); - buf.should.eql(new Buffer([0xf8, 0xff])); + assert(buf.length === 2); + assert(buf[0] === 0xf8); + assert(buf[1] === 0xff); + assert.deepEqual(buf, new Buffer([0xf8, 0xff])); buf = hessian.encode(java.long(-2048), '2.0'); - buf.should.length(2); - buf[0].should.equal(0xf0); - buf[1].should.equal(0); - buf.should.eql(new Buffer([0xf0, 0x00])); + assert(buf.length === 2); + assert(buf[0] === 0xf0); + assert(buf[1] === 0); + assert.deepEqual(buf, new Buffer([0xf0, 0x00])); buf = hessian.encode(java.long(2047), '2.0'); - buf.should.length(2); - buf[0].should.equal(0xff); - buf[1].should.equal(0xff); - buf.should.eql(new Buffer([0xff, 0xff])); + assert(buf.length === 2); + assert(buf[0] === 0xff); + assert(buf[1] === 0xff); + assert.deepEqual(buf, new Buffer([0xff, 0xff])); // -262144 ~ 262143 buf = hessian.encode(java.long(262143), '2.0'); - buf.should.length(3); - buf[0].should.equal(0x3f); - buf[1].should.equal(0xff); - buf[2].should.equal(0xff); - buf.should.eql(new Buffer([0x3f, 0xff, 0xff])); + assert(buf.length === 3); + assert(buf[0] === 0x3f); + assert(buf[1] === 0xff); + assert(buf[2] === 0xff); + assert.deepEqual(buf, new Buffer([0x3f, 0xff, 0xff])); buf = hessian.encode(java.long(-262144), '2.0'); - buf.should.length(3); - buf[0].should.equal(0x38); - buf[1].should.equal(0); - buf[2].should.equal(0); - buf.should.eql(new Buffer([0x38, 0x00, 0x00])); + assert(buf.length === 3); + assert(buf[0] === 0x38); + assert(buf[1] === 0); + assert(buf[2] === 0); + assert.deepEqual(buf, new Buffer([0x38, 0x00, 0x00])); buf = hessian.encode(java.long(2048), '2.0'); - buf.should.length(3); - buf[0].should.equal(0x3c); - buf[1].should.equal(0x08); - buf[2].should.equal(0x00); - buf.should.eql(new Buffer([0x3c, 0x08, 0x00])); + assert(buf.length === 3); + assert(buf[0] === 0x3c); + assert(buf[1] === 0x08); + assert(buf[2] === 0x00); + assert.deepEqual(buf, new Buffer([0x3c, 0x08, 0x00])); buf = hessian.encode(java.long(-2049), '2.0'); - buf.should.length(3); - buf[0].should.equal(0x3b); - buf[1].should.equal(0xf7); - buf[2].should.equal(0xff); - buf.should.eql(new Buffer([0x3b, 0xf7, 0xff])); + assert(buf.length === 3); + assert(buf[0] === 0x3b); + assert(buf[1] === 0xf7); + assert(buf[2] === 0xff); + assert.deepEqual(buf, new Buffer([0x3b, 0xf7, 0xff])); // -2147483648 ~ 2147483647 buf = hessian.encode(java.long(-2147483648), '2.0'); - buf.should.length(5); - buf[0].should.equal(0x77); - buf[1].should.equal(0x80); - buf[2].should.equal(0x00); - buf[3].should.equal(0x00); - buf[4].should.equal(0x00); - buf.should.eql(new Buffer([0x77, 0x80, 0x00, 0x00, 0x00])); + assert(buf.length === 5); + assert(buf[0] === 0x77); + assert(buf[1] === 0x80); + assert(buf[2] === 0x00); + assert(buf[3] === 0x00); + assert(buf[4] === 0x00); + assert.deepEqual(buf, new Buffer([0x77, 0x80, 0x00, 0x00, 0x00])); buf = hessian.encode(java.long(2147483647), '2.0'); - buf.should.length(5); - buf[0].should.equal(0x77); - buf[1].should.equal(0x7f); - buf[2].should.equal(0xff); - buf[3].should.equal(0xff); - buf[4].should.equal(0xff); - buf.should.eql(new Buffer([0x77, 0x7f, 0xff, 0xff, 0xff])); + assert(buf.length === 5); + assert(buf[0] === 0x77); + assert(buf[1] === 0x7f); + assert(buf[2] === 0xff); + assert(buf[3] === 0xff); + assert(buf[4] === 0xff); + assert.deepEqual(buf, new Buffer([0x77, 0x7f, 0xff, 0xff, 0xff])); // L buf = hessian.encode(java.long(2147483648), '2.0'); - buf.should.length(9); - buf.should.eql(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00])); + assert(buf.length === 9); + assert.deepEqual(buf, new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00])); }); it('should write and read equal java impl', function () { - hessian.encode(java.long(0), '2.0').should.eql(utils.bytes('v2/long/0')); - hessian.decode(utils.bytes('v2/long/0'), '2.0').should.equal(0); - hessian.encode(java.long(-8), '2.0').should.eql(utils.bytes('v2/long/-8')); - hessian.decode(utils.bytes('v2/long/-8'), '2.0').should.equal(-8); - hessian.encode(java.long(-7), '2.0').should.eql(utils.bytes('v2/long/-7')); - hessian.decode(utils.bytes('v2/long/-7'), '2.0').should.equal(-7); - hessian.encode(java.long(15), '2.0').should.eql(utils.bytes('v2/long/15')); - hessian.decode(utils.bytes('v2/long/15'), '2.0').should.equal(15); - hessian.encode(java.long(14), '2.0').should.eql(utils.bytes('v2/long/14')); - hessian.decode(utils.bytes('v2/long/14'), '2.0').should.equal(14); - hessian.encode(java.long(-9), '2.0').should.eql(utils.bytes('v2/long/-9')); - hessian.decode(utils.bytes('v2/long/-9'), '2.0').should.equal(-9); - hessian.encode(java.long(16), '2.0').should.eql(utils.bytes('v2/long/16')); - hessian.decode(utils.bytes('v2/long/16'), '2.0').should.equal(16); - hessian.encode(java.long(255), '2.0').should.eql(utils.bytes('v2/long/255')); - hessian.encode(java.long(Long.fromNumber(255)), '2.0').should.eql(utils.bytes('v2/long/255')); - hessian.encode(Long.fromNumber(255), '2.0').should.eql(utils.bytes('v2/long/255')); - - hessian.decode(utils.bytes('v2/long/255'), '2.0').should.equal(255); - hessian.encode(java.long(-2048), '2.0').should.eql(utils.bytes('v2/long/-2048')); - hessian.decode(utils.bytes('v2/long/-2048'), '2.0').should.equal(-2048); - hessian.encode(java.long(2047), '2.0').should.eql(utils.bytes('v2/long/2047')); - hessian.decode(utils.bytes('v2/long/2047'), '2.0').should.equal(2047); - hessian.encode(java.long(262143), '2.0').should.eql(utils.bytes('v2/long/262143')); - hessian.decode(utils.bytes('v2/long/262143'), '2.0').should.equal(262143); - hessian.encode(java.long(-262144), '2.0').should.eql(utils.bytes('v2/long/-262144')); - hessian.decode(utils.bytes('v2/long/-262144'), '2.0').should.equal(-262144); - hessian.encode(java.long(2048), '2.0').should.eql(utils.bytes('v2/long/2048')); - hessian.decode(utils.bytes('v2/long/2048'), '2.0').should.equal(2048); - hessian.encode(java.long(-2049), '2.0').should.eql(utils.bytes('v2/long/-2049')); - hessian.decode(utils.bytes('v2/long/-2049'), '2.0').should.equal(-2049); - hessian.encode(java.long(-2147483648), '2.0').should.eql(utils.bytes('v2/long/-2147483648')); - hessian.decode(utils.bytes('v2/long/-2147483648'), '2.0').should.equal(-2147483648); - hessian.encode(java.long(-2147483647), '2.0').should.eql(utils.bytes('v2/long/-2147483647')); - hessian.decode(utils.bytes('v2/long/-2147483647'), '2.0').should.equal(-2147483647); - hessian.encode(java.long(2147483647), '2.0').should.eql(utils.bytes('v2/long/2147483647')); - hessian.decode(utils.bytes('v2/long/2147483647'), '2.0').should.equal(2147483647); - hessian.encode(java.long(2147483646), '2.0').should.eql(utils.bytes('v2/long/2147483646')); - hessian.decode(utils.bytes('v2/long/2147483646'), '2.0').should.equal(2147483646); - hessian.encode(java.long(2147483648), '2.0').should.eql(utils.bytes('v2/long/2147483648')); - hessian.decode(utils.bytes('v2/long/2147483648'), '2.0').should.equal(2147483648); + assert.deepEqual(hessian.encode(java.long(0), '2.0'), utils.bytes('v2/long/0')); + assert(hessian.decode(utils.bytes('v2/long/0'), '2.0') === 0); + assert.deepEqual(hessian.encode(java.long(-8), '2.0'), utils.bytes('v2/long/-8')); + assert(hessian.decode(utils.bytes('v2/long/-8'), '2.0') === -8); + assert.deepEqual(hessian.encode(java.long(-7), '2.0'), utils.bytes('v2/long/-7')); + assert(hessian.decode(utils.bytes('v2/long/-7'), '2.0') === -7); + assert.deepEqual(hessian.encode(java.long(15), '2.0'), utils.bytes('v2/long/15')); + assert(hessian.decode(utils.bytes('v2/long/15'), '2.0') === 15); + assert.deepEqual(hessian.encode(java.long(14), '2.0'), utils.bytes('v2/long/14')); + assert(hessian.decode(utils.bytes('v2/long/14'), '2.0') === 14); + assert.deepEqual(hessian.encode(java.long(-9), '2.0'), utils.bytes('v2/long/-9')); + assert(hessian.decode(utils.bytes('v2/long/-9'), '2.0') === -9); + assert.deepEqual(hessian.encode(java.long(16), '2.0'), utils.bytes('v2/long/16')); + assert(hessian.decode(utils.bytes('v2/long/16'), '2.0') === 16); + assert.deepEqual(hessian.encode(java.long(255), '2.0'), utils.bytes('v2/long/255')); + assert.deepEqual( + hessian.encode(java.long(Long.fromNumber(255)), '2.0'), + utils.bytes('v2/long/255') + ); + assert.deepEqual(hessian.encode(Long.fromNumber(255), '2.0'), utils.bytes('v2/long/255')); + + assert(hessian.decode(utils.bytes('v2/long/255'), '2.0') === 255); + assert.deepEqual(hessian.encode(java.long(-2048), '2.0'), utils.bytes('v2/long/-2048')); + assert(hessian.decode(utils.bytes('v2/long/-2048'), '2.0') === -2048); + assert.deepEqual(hessian.encode(java.long(2047), '2.0'), utils.bytes('v2/long/2047')); + assert(hessian.decode(utils.bytes('v2/long/2047'), '2.0') === 2047); + assert.deepEqual(hessian.encode(java.long(262143), '2.0'), utils.bytes('v2/long/262143')); + assert(hessian.decode(utils.bytes('v2/long/262143'), '2.0') === 262143); + assert.deepEqual(hessian.encode(java.long(-262144), '2.0'), utils.bytes('v2/long/-262144')); + assert(hessian.decode(utils.bytes('v2/long/-262144'), '2.0') === -262144); + assert.deepEqual(hessian.encode(java.long(2048), '2.0'), utils.bytes('v2/long/2048')); + assert(hessian.decode(utils.bytes('v2/long/2048'), '2.0') === 2048); + assert.deepEqual(hessian.encode(java.long(-2049), '2.0'), utils.bytes('v2/long/-2049')); + assert(hessian.decode(utils.bytes('v2/long/-2049'), '2.0') === -2049); + assert.deepEqual( + hessian.encode(java.long(-2147483648), '2.0'), + utils.bytes('v2/long/-2147483648') + ); + assert(hessian.decode(utils.bytes('v2/long/-2147483648'), '2.0') === -2147483648); + assert.deepEqual( + hessian.encode(java.long(-2147483647), '2.0'), + utils.bytes('v2/long/-2147483647') + ); + assert(hessian.decode(utils.bytes('v2/long/-2147483647'), '2.0') === -2147483647); + assert.deepEqual( + hessian.encode(java.long(2147483647), '2.0'), + utils.bytes('v2/long/2147483647') + ); + assert(hessian.decode(utils.bytes('v2/long/2147483647'), '2.0') === 2147483647); + assert.deepEqual( + hessian.encode(java.long(2147483646), '2.0'), + utils.bytes('v2/long/2147483646') + ); + assert(hessian.decode(utils.bytes('v2/long/2147483646'), '2.0') === 2147483646); + assert.deepEqual( + hessian.encode(java.long(2147483648), '2.0'), + utils.bytes('v2/long/2147483648') + ); + assert(hessian.decode(utils.bytes('v2/long/2147483648'), '2.0') === 2147483648); }); it('should read 1.0 bin as well', function () { - hessian.decode(utils.bytes('v1/long/0'), '2.0').should.equal(0); - hessian.decode(utils.bytes('v1/long/-8'), '2.0').should.equal(-8); - hessian.decode(utils.bytes('v1/long/-7'), '2.0').should.equal(-7); - hessian.decode(utils.bytes('v1/long/15'), '2.0').should.equal(15); - hessian.decode(utils.bytes('v1/long/14'), '2.0').should.equal(14); - hessian.decode(utils.bytes('v1/long/-9'), '2.0').should.equal(-9); - hessian.decode(utils.bytes('v1/long/16'), '2.0').should.equal(16); - hessian.decode(utils.bytes('v1/long/255'), '2.0').should.equal(255); - hessian.decode(utils.bytes('v1/long/-2048'), '2.0').should.equal(-2048); - hessian.decode(utils.bytes('v1/long/2047'), '2.0').should.equal(2047); - hessian.decode(utils.bytes('v1/long/262143'), '2.0').should.equal(262143); - hessian.decode(utils.bytes('v1/long/-262144'), '2.0').should.equal(-262144); - hessian.decode(utils.bytes('v1/long/2048'), '2.0').should.equal(2048); - hessian.decode(utils.bytes('v1/long/-2049'), '2.0').should.equal(-2049); - hessian.decode(utils.bytes('v1/long/-2147483648'), '2.0').should.equal(-2147483648); - hessian.decode(utils.bytes('v1/long/-2147483647'), '2.0').should.equal(-2147483647); - hessian.decode(utils.bytes('v1/long/2147483647'), '2.0').should.equal(2147483647); - hessian.decode(utils.bytes('v1/long/2147483646'), '2.0').should.equal(2147483646); - hessian.decode(utils.bytes('v1/long/2147483648'), '2.0').should.equal(2147483648); + assert(hessian.decode(utils.bytes('v1/long/0'), '2.0') === 0); + assert(hessian.decode(utils.bytes('v1/long/-8'), '2.0') === -8); + assert(hessian.decode(utils.bytes('v1/long/-7'), '2.0') === -7); + assert(hessian.decode(utils.bytes('v1/long/15'), '2.0') === 15); + assert(hessian.decode(utils.bytes('v1/long/14'), '2.0') === 14); + assert(hessian.decode(utils.bytes('v1/long/-9'), '2.0') === -9); + assert(hessian.decode(utils.bytes('v1/long/16'), '2.0') === 16); + assert(hessian.decode(utils.bytes('v1/long/255'), '2.0') === 255); + assert(hessian.decode(utils.bytes('v1/long/-2048'), '2.0') === -2048); + assert(hessian.decode(utils.bytes('v1/long/2047'), '2.0') === 2047); + assert(hessian.decode(utils.bytes('v1/long/262143'), '2.0') === 262143); + assert(hessian.decode(utils.bytes('v1/long/-262144'), '2.0') === -262144); + assert(hessian.decode(utils.bytes('v1/long/2048'), '2.0') === 2048); + assert(hessian.decode(utils.bytes('v1/long/-2049'), '2.0') === -2049); + assert(hessian.decode(utils.bytes('v1/long/-2147483648'), '2.0') === -2147483648); + assert(hessian.decode(utils.bytes('v1/long/-2147483647'), '2.0') === -2147483647); + assert(hessian.decode(utils.bytes('v1/long/2147483647'), '2.0') === 2147483647); + assert(hessian.decode(utils.bytes('v1/long/2147483646'), '2.0') === 2147483646); + assert(hessian.decode(utils.bytes('v1/long/2147483648'), '2.0') === 2147483648); }); }); it('should decode with type', function () { - hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0', true).should.eql({ + assert.deepEqual(hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0', true), { $class: 'long', $: -262144, }); - hessian.decode(utils.bytes('v2/long/-2048'), '2.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v2/long/-2048'), '2.0', true), { $class: 'long', $: -2048, }); - hessian.decode(utils.bytes('v2/long/2147483646'), '2.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v2/long/2147483646'), '2.0', true), { $class: 'long', $: 2147483646, }); diff --git a/test/map.test.js b/test/map.test.js index 4480c09..f539d9a 100644 --- a/test/map.test.js +++ b/test/map.test.js @@ -10,11 +10,7 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); var utils = require('./utils'); @@ -74,13 +70,13 @@ describe('map.test.js', function () { new Buffer('z'), ]); - hessian.decode(mapBuffer).should.eql({ + assert.deepEqual(hessian.decode(mapBuffer), { model: 'Beetle', color: 'aquamarine', mileage: 65536 }); - hessian.decode(mapBuffer, true).should.eql({ + assert.deepEqual(hessian.decode(mapBuffer, true), { '$class': 'com.caucho.test.Car', '$': { model: { '$class': 'java.lang.String', '$': 'Beetle' }, @@ -93,8 +89,8 @@ describe('map.test.js', function () { it('should write {hasOwnProperty: 1, a: 0, b: false, c: null} obj', function () { /* jshint -W001 */ var buf = hessian.encode({hasOwnProperty: 1, a: 0, b: false, c: null}); - buf.should.be.a.Buffer; - hessian.decode(buf).should.eql({hasOwnProperty: 1, a: 0, b: false, c: null}); + assert(Buffer.isBuffer(buf)); + assert.deepEqual(hessian.decode(buf), {hasOwnProperty: 1, a: 0, b: false, c: null}); }); it('should read A sparse array', function () { @@ -131,7 +127,7 @@ describe('map.test.js', function () { new Buffer('z'), ]); - hessian.decode(mapBuffer).should.eql({ + assert.deepEqual(hessian.decode(mapBuffer), { '1': 'fee', '16': 'fie', '256': 'foe', @@ -140,20 +136,20 @@ describe('map.test.js', function () { it('should write js object to no type hash map', function () { var buf = hessian.encode({ foo: '' }); - buf.should.eql(utils.bytes('v1/map/foo_empty')); - hessian.decode(utils.bytes('v1/map/foo_empty'), '1.0').should.eql({ + assert.deepEqual(buf, utils.bytes('v1/map/foo_empty')); + assert.deepEqual(hessian.decode(utils.bytes('v1/map/foo_empty'), '1.0'), { foo: '' }); - hessian.encode({ + assert.deepEqual(hessian.encode({ '123': 456, foo: 'bar', zero: 0, '中文key': '中文哈哈value', - }).should.eql(utils.bytes('v1/map/foo_bar')); + }), utils.bytes('v1/map/foo_bar')); // read it - hessian.decode(utils.bytes('v1/map/foo_bar'), '1.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/foo_bar'), '1.0'), { foo: 'bar', '中文key': '中文哈哈value', '123': 456, @@ -171,13 +167,13 @@ describe('map.test.js', function () { map.set({ '$class': 'java.lang.Long', '$': 123 }, 123456); map.set({ '$class': 'java.lang.Long', '$': 123456 }, 123); var buf = hessian.encode(map); - buf.should.eql(utils.bytes('v1/map/generic')); + assert.deepEqual(buf, utils.bytes('v1/map/generic')); buf = hessian.encode({ '$class': 'java.util.HashMap', '$': map }); - buf.should.eql(utils.bytes('v1/map/generic')); + assert.deepEqual(buf, utils.bytes('v1/map/generic')); // decode auto transfer key to string - hessian.decode(utils.bytes('v1/map/generic'), '1.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/generic'), '1.0'), { '123': 123456, '123456': 123 }); @@ -195,15 +191,15 @@ describe('map.test.js', function () { map.set({ '$class': 'java.lang.Long', '$': 123456 }, 123); var encoder = new hessian.EncoderV2(); var buf = encoder.write(map).get(); - buf.should.eql(generic); + assert.deepEqual(buf, generic); encoder.reset(); buf = encoder.write({ '$class': 'java.util.HashMap', '$': map }).get(); - buf.should.eql(generic); + assert.deepEqual(buf, generic); // decode auto transfer key to string - hessian.decode(generic, '2.0').should.eql({ + assert.deepEqual(hessian.decode(generic, '2.0'), { '123': 123456, '123456': 123 }); @@ -247,11 +243,11 @@ describe('map.test.js', function () { } }; var buf = encoder.write(car).get(); - buf.should.eql(utils.bytes('v2/map/car')); + assert.deepEqual(buf, utils.bytes('v2/map/car')); }); it('should read java hash map', function () { - hessian.decode(hashmapBuffer, '2.0').should.eql({ + assert.deepEqual(hessian.decode(hashmapBuffer, '2.0'), { 1: 'fee', 16: 'fie', 256: 'foe' @@ -260,7 +256,7 @@ describe('map.test.js', function () { it('should read a Circular java Object', function () { var car = hessian.decode(utils.bytes('v2/map/car'), '2.0'); - car.should.eql({ + assert.deepEqual(car, { a: 'a', c: 'c', b: 'b', @@ -270,18 +266,22 @@ describe('map.test.js', function () { }); var obj = hessian.decode(utils.bytes('v2/map/car1'), '2.0'); - obj.should.have.keys('color', 'model', 'mileage', 'self', 'prev'); - obj.self.should.equal(obj); - should.not.exist(obj.prev); - obj.self.should.have.keys('color', 'model', 'mileage', 'self', 'prev'); + [ 'color', 'model', 'mileage', 'self', 'prev' ].forEach(function(p) { + assert(Object.prototype.hasOwnProperty.call(obj, p)); + }); + assert(obj.self === obj); + assert(!obj.prev); + [ 'color', 'model', 'mileage', 'self', 'prev' ].forEach(function(p) { + assert(Object.prototype.hasOwnProperty.call(obj.self, p)); + }); }); it('should write js object to no type hash map', function () { var encoder = new hessian.EncoderV2(); var fooEmpty = new Buffer('4d03666f6f007a', 'hex'); var buf = encoder.write({ foo: '' }).get(); - buf.should.eql(fooEmpty); - hessian.decode(fooEmpty, '2.0').should.eql({ + assert.deepEqual(buf, fooEmpty); + assert.deepEqual(hessian.decode(fooEmpty, '2.0'), { foo: '' }); @@ -293,10 +293,10 @@ describe('map.test.js', function () { '123': 456, zero: 0, }).get(); - buf.should.eql(fooBar); + assert.deepEqual(buf, fooBar); // read it - hessian.decode(fooBar, '2.0').should.eql({ + assert.deepEqual(hessian.decode(fooBar, '2.0'), { foo: 'bar', '中文key': '中文哈哈value', '123': 456, @@ -305,10 +305,10 @@ describe('map.test.js', function () { }); it('should read hessian 1.0 hash map', function () { - hessian.decode(utils.bytes('v1/map/foo_empty'), '2.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/foo_empty'), '2.0'), { foo: '' }); - hessian.decode(utils.bytes('v1/map/foo_bar'), '2.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/foo_bar'), '2.0'), { foo: 'bar', '中文key': '中文哈哈value', '123': 456, @@ -341,25 +341,25 @@ describe('map.test.js', function () { }; var encoder = new hessian.EncoderV2(); var buf = encoder.write(map).get(); - hessian.decode(buf, '2.0').should.eql(map); + assert.deepEqual(hessian.decode(buf, '2.0'), map); // writeRef var bufRef = encoder.write(map).get(); - hessian.decode(bufRef, '2.0').should.eql(map); - bufRef.slice(buf.length).should.eql(new Buffer([0x4a, 0x00])); + assert.deepEqual(hessian.decode(bufRef, '2.0'), map); + assert.deepEqual(bufRef.slice(buf.length), new Buffer([0x4a, 0x00])); var buf2 = hessian.encode({ $class: 'java.util.HashMap', $: map }, '2.0'); - buf2.should.eql(buf); - hessian.decode(buf2, '2.0').should.eql(map); + assert.deepEqual(buf2, buf); + assert.deepEqual(hessian.decode(buf2, '2.0'), map); }); }); it('should decode successful when key is null', function () { var data = new Buffer([77, 116, 0, 0, 78, 83, 0, 4, 110, 117, 108, 108, 122]); var rv = hessian.decode(data); - rv.should.eql({null: 'null'}); + assert.deepEqual(rv, {null: 'null'}); }); }); diff --git a/test/null.test.js b/test/null.test.js index 7088133..bcb5c15 100644 --- a/test/null.test.js +++ b/test/null.test.js @@ -10,27 +10,23 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); describe('null.test.js', function () { it('should read null', function () { var a = hessian.decode(new Buffer('N')); - should.ok(a === null); + assert(a === null); }); it('should write null', function () { - hessian.encode(null).should.eql(new Buffer('N')); + assert.deepEqual(hessian.encode(null), new Buffer('N')); }); describe('v2.0', function () { it('should read write as 1.0', function () { - hessian.encode(null, '2.0').should.eql(new Buffer('N')); - should.ok(hessian.decode(new Buffer('N'), '2.0') === null); + assert.deepEqual(hessian.encode(null, '2.0'), new Buffer('N')); + assert(hessian.decode(new Buffer('N'), '2.0') === null); }); }); }); diff --git a/test/object.test.js b/test/object.test.js index 3a19cda..468334e 100644 --- a/test/object.test.js +++ b/test/object.test.js @@ -10,24 +10,28 @@ "use strict"; -/** - * Module dependencies. - */ -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); var utils = require('./utils'); describe('object.test.js', function () { describe('v1.0', function () { + it('should skip function', function() { + var o = { foo: 'bar', fn: function() {} }; + var buf = hessian.encode(o, '1.0'); + var output = hessian.decode(buf, '1.0'); + assert.deepEqual(output, { foo: 'bar', fn: null }); + }); + it('should write null for property like: { a: { "$class": "yyy.yyy", "$": null } }', function () { var o = { '$class': 'xxx.xxx', '$': { a: { '$class': 'yyy.yyy', '$': null } } }; var rightBuf = new Buffer('4d7400077878782e787878530001614e7a', 'hex'); var buf = hessian.encode(o, '1.0'); - buf.should.length(rightBuf.length); - buf.should.eql(rightBuf); + assert(buf.length === rightBuf.length); + assert.deepEqual(buf, rightBuf); }); it('should write object for property like: { a: { "$class": "yyy.yyy", "$": {} } }', function () { @@ -36,8 +40,8 @@ describe('object.test.js', function () { var rightBuf = new Buffer('4d7400077878782e787878530001614d7400077979792e7979797a7a', 'hex'); var buf = hessian.encode(o, '1.0'); - buf.should.length(rightBuf.length); - buf.should.eql(rightBuf); + assert(buf.length === rightBuf.length); + assert.deepEqual(buf, rightBuf); }); it('should _assertType error when encode wrong object', function () { @@ -52,9 +56,9 @@ describe('object.test.js', function () { } catch (err) { rs = err; } - should.exist(rs); - rs.message.should.containEql('com.alipay.x.biz.User'); - should.not.exist(buf); + assert(rs); + assert(rs.message.indexOf('com.alipay.x.biz.User') >= 0); + assert(!buf); }); it('should decode and encode ConnectionRequest', function () { @@ -82,30 +86,30 @@ describe('object.test.js', function () { // jsbuf.should.eql(javabuf); var jsbuf2Again = hessian.encode(jsconnreq, '1.0'); - jsbuf2Again.should.eql(jsbuf2); + assert.deepEqual(jsbuf2Again, jsbuf2); }); it('should write enum Color', function () { - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { name: 'RED' } - }, '1.0').should.eql(utils.bytes('v1/enum/red')); + }, '1.0'), utils.bytes('v1/enum/red')); - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { name: 'GREEN' } - }, '1.0').should.eql(utils.bytes('v1/enum/green')); + }, '1.0'), utils.bytes('v1/enum/green')); - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { name: 'BLUE' } - }, '1.0').should.eql(utils.bytes('v1/enum/blue')); + }, '1.0'), utils.bytes('v1/enum/blue')); }); it('should write enum with ref', function () { @@ -113,7 +117,7 @@ describe('object.test.js', function () { // list.add(Color.BLUE); // list.add(Color.RED); // list.add(Color.GREEN); - hessian.encode([ + assert.deepEqual(hessian.encode([ { $class: 'hessian.Main$Color', $: { @@ -132,7 +136,7 @@ describe('object.test.js', function () { name: 'GREEN' } }, - ], '1.0').should.eql(utils.bytes('v1/enum/lists')); + ], '1.0'), utils.bytes('v1/enum/lists')); }); it('should read enum Color', function () { @@ -142,37 +146,38 @@ describe('object.test.js', function () { // BLUE, // } - hessian.decode(utils.bytes('v1/enum/red'), '1.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/enum/red'), '1.0'), { name: 'RED' }); - hessian.decode(utils.bytes('v1/enum/green'), '1.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/enum/green'), '1.0', true), { '$class': 'hessian.Main$Color', '$': { name: { '$class': 'java.lang.String', '$': 'GREEN' } } }); - hessian.decode(utils.bytes('v1/enum/blue'), '1.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/enum/blue'), '1.0'), { name: 'BLUE' }); - hessian.decode(utils.bytes('v1/enum/green'), '1.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/enum/green'), '1.0'), { name: 'GREEN' }); - hessian.decode(utils.bytes('v1/enum/red'), '1.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/enum/red'), '1.0', true), { '$class': 'hessian.Main$Color', '$': { name: { '$class': 'java.lang.String', '$': 'RED' } } }); - hessian.decode(utils.bytes('v1/enum/lists'), '1.0').should.eql( + assert.deepEqual( + hessian.decode(utils.bytes('v1/enum/lists'), '1.0'), [ { name: 'BLUE' }, { name: 'RED' }, { name: 'GREEN' } ] ); - hessian.decode(utils.bytes('v1/enum/lists'), '1.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/enum/lists'), '1.0', true), { $class: 'java.util.ArrayList', $: [ { '$class': 'hessian.Main$Color', '$': { name: { '$class': 'java.lang.String', '$': 'BLUE' } } }, @@ -188,9 +193,9 @@ describe('object.test.js', function () { $: {a: 1, b: 'map'} }; var buf = hessian.encode(obj, '1.0'); - buf[0].should.equal(0x4d); // 'M' - hessian.decode(buf, '1.0').should.eql(obj.$); - hessian.decode(buf, '1.0', true).should.eql( { + assert(buf[0] === 0x4d); // 'M' + assert.deepEqual(hessian.decode(buf, '1.0'), obj.$); + assert.deepEqual(hessian.decode(buf, '1.0', true), { '$class': 'hessian.test.demo.Car', '$': { a: { '$class': 'int', '$': 1 }, @@ -200,7 +205,7 @@ describe('object.test.js', function () { }); it('should read and write one car list', function () { - hessian.decode(utils.bytes('v1/map/one_car_list'), '1.0').should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/one_car_list'), '1.0'), [ { a: 'a', c: 'c', b: 'b', @@ -210,7 +215,7 @@ describe('object.test.js', function () { ]); var cars = hessian.decode(utils.bytes('v1/map/one_car_list'), '1.0', true); - cars.should.eql({ + assert.deepEqual(cars, { $class: 'java.util.ArrayList', $: [ { @@ -227,11 +232,11 @@ describe('object.test.js', function () { ] }); - hessian.encode(cars, '1.0').should.eql(utils.bytes('v1/map/one_car_list')); + assert.deepEqual(hessian.encode(cars, '1.0'), utils.bytes('v1/map/one_car_list')); }); it('should read and write two car list', function () { - hessian.decode(utils.bytes('v1/map/two_car_list'), '1.0').should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/two_car_list'), '1.0'), [ { a: 'a', c: 'c', b: 'b', @@ -247,7 +252,7 @@ describe('object.test.js', function () { ]); var cars = hessian.decode(utils.bytes('v1/map/two_car_list'), '1.0', true); - cars.should.eql({ + assert.deepEqual(cars, { $class: 'java.util.ArrayList', $: [ { @@ -275,8 +280,8 @@ describe('object.test.js', function () { }); var buf = hessian.encode(cars, '1.0'); - buf.should.length(utils.bytes('v1/map/two_car_list').length); - buf.should.eql(utils.bytes('v1/map/two_car_list')); + assert(buf.length === utils.bytes('v1/map/two_car_list').length); + assert.deepEqual(buf, utils.bytes('v1/map/two_car_list')); }); it('should read and write many cars', function () { @@ -284,7 +289,7 @@ describe('object.test.js', function () { // list.add(new Car("model 1")); // list.add(new Car("model 2")); // list.add(new Car("model 3")); - hessian.decode(utils.bytes('v1/map/car_list'), '1.0').should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/car_list'), '1.0'), [ { a: 'a', c: 'c', b: 'b', @@ -306,7 +311,7 @@ describe('object.test.js', function () { ]); var cars = hessian.decode(utils.bytes('v1/map/car_list'), '1.0', true); - cars.should.eql({ + assert.deepEqual(cars, { $class: 'java.util.ArrayList', $: [ { @@ -343,16 +348,16 @@ describe('object.test.js', function () { ] }); - hessian.encode(cars, '1.0').should.eql(utils.bytes('v1/map/car_list')); + assert.deepEqual(hessian.encode(cars, '1.0'), utils.bytes('v1/map/car_list')); }); describe('java.util.concurrent.atomic.AtomicLong', function () { it('should read and write', function () { var javabuf = utils.bytes('v1/object/AtomicLong0'); var a0 = hessian.decode(javabuf); - a0.should.eql({value: 0}); + assert.deepEqual(a0, {value: 0}); var a0 = hessian.decode(javabuf, true); - a0.should.eql({ + assert.deepEqual(a0, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { @@ -361,13 +366,13 @@ describe('object.test.js', function () { } } }); - hessian.encode(a0).should.eql(javabuf); + assert.deepEqual(hessian.encode(a0), javabuf); javabuf = utils.bytes('v1/object/AtomicLong1'); var a1 = hessian.decode(javabuf); - a1.should.eql({value: 1}); + assert.deepEqual(a1, {value: 1}); var a1 = hessian.decode(javabuf, true); - a1.should.eql({ + assert.deepEqual(a1, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { @@ -376,17 +381,24 @@ describe('object.test.js', function () { } } }); - hessian.encode(a1).should.eql(javabuf); + assert.deepEqual(hessian.encode(a1), javabuf); }); }); }); describe('v2.0', function () { + it('should skip function', function() { + var o = { foo: 'bar', fn: function() {} }; + var buf = hessian.encode(o, '2.0'); + var output = hessian.decode(buf, '2.0'); + assert.deepEqual(output, { foo: 'bar', fn: null }); + }); + it('should decode and encode ConnectionRequest', function () { var javabuf = utils.bytes('v2/object/ConnectionRequest'); var connreq1 = hessian.decode(javabuf, '2.0'); - connreq1.should.have.keys('ctx'); - connreq1.ctx.id.should.equal(101); + assert(connreq1.ctx); + assert(connreq1.ctx.id === 101); var connreq = hessian.decode(javabuf, '2.0', true); var jsconnreq = { @@ -413,46 +425,46 @@ describe('object.test.js', function () { it('should decode hessian 1.0 ConnectionRequest', function () { var javabuf = utils.bytes('v1/object/ConnectionRequest'); var connreq = hessian.decode(javabuf, '1.0', true); - connreq.$class.should.equal('hessian.ConnectionRequest'); - connreq.$.ctx.$class.should.equal('hessian.ConnectionRequest$RequestContext'); + assert(connreq.$class === 'hessian.ConnectionRequest'); + assert(connreq.$.ctx.$class === 'hessian.ConnectionRequest$RequestContext'); }); it('should write enum Color', function () { - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { name: 'RED' } - }, '2.0').should.eql(utils.bytes('v2/enum/red')); + }, '2.0'), utils.bytes('v2/enum/red')); - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { name: 'GREEN' } - }, '2.0').should.eql(utils.bytes('v2/enum/green')); + }, '2.0'), utils.bytes('v2/enum/green')); - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { name: 'BLUE' } - }, '2.0').should.eql(utils.bytes('v2/enum/blue')); + }, '2.0'), utils.bytes('v2/enum/blue')); }); it('should read hessian 1.0 enum Color', function () { - hessian.decode(utils.bytes('v1/enum/red'), '2.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/enum/red'), '2.0', true), { $class: 'hessian.Main$Color', $: { name: 'RED' } }); - hessian.decode(utils.bytes('v1/enum/green'), '2.0', false).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/enum/green'), '2.0', false), { name: 'GREEN' }); - hessian.decode(utils.bytes('v1/enum/blue'), '2.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v1/enum/blue'), '2.0', true), { $class: 'hessian.Main$Color', $: { name: 'BLUE' @@ -465,7 +477,7 @@ describe('object.test.js', function () { // list.add(Color.BLUE); // list.add(Color.RED); // list.add(Color.GREEN); - hessian.encode([ + assert.deepEqual(hessian.encode([ { $class: 'hessian.Main$Color', $: { @@ -484,7 +496,7 @@ describe('object.test.js', function () { name: 'GREEN' } }, - ], '2.0').should.eql(utils.bytes('v2/enum/lists')); + ], '2.0'), utils.bytes('v2/enum/lists')); }); it('should read enum Color', function () { @@ -496,37 +508,38 @@ describe('object.test.js', function () { // enum format: // O type 1 "name" o ref name-value - hessian.decode(utils.bytes('v2/enum/red'), '2.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v2/enum/red'), '2.0'), { name: 'RED' }); - hessian.decode(utils.bytes('v2/enum/green'), '2.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v2/enum/green'), '2.0', true), { $class: 'hessian.Main$Color', $: { name: 'GREEN' } }); - hessian.decode(utils.bytes('v2/enum/blue'), '2.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v2/enum/blue'), '2.0'), { name: 'BLUE' }); - hessian.decode(utils.bytes('v2/enum/green'), '2.0').should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v2/enum/green'), '2.0'), { name: 'GREEN' }); - hessian.decode(utils.bytes('v2/enum/red'), '2.0', true).should.eql({ + assert.deepEqual(hessian.decode(utils.bytes('v2/enum/red'), '2.0', true), { $class: 'hessian.Main$Color', $: { name: 'RED' } }); - hessian.decode(utils.bytes('v2/enum/lists'), '2.0').should.eql( + assert.deepEqual( + hessian.decode(utils.bytes('v2/enum/lists'), '2.0'), [ { name: 'BLUE' }, { name: 'RED' }, { name: 'GREEN' } ] ); - hessian.decode(utils.bytes('v2/enum/lists'), '2.0', true).should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v2/enum/lists'), '2.0', true), [ { '$class': 'hessian.Main$Color', '$': { name: 'BLUE' } }, { '$class': 'hessian.Main$Color', '$': { name: 'RED' } }, { '$class': 'hessian.Main$Color', '$': { name: 'GREEN' } } @@ -539,13 +552,13 @@ describe('object.test.js', function () { $: {a: 1, b: 'map'} }; var buf = hessian.encode(obj, '2.0'); - buf[0].should.equal(0x4f); - hessian.decode(buf, '2.0').should.eql(obj.$); - hessian.decode(buf, '2.0', true).should.eql(obj); + assert(buf[0] === 0x4f); + assert.deepEqual(hessian.decode(buf, '2.0'), obj.$); + assert.deepEqual(hessian.decode(buf, '2.0', true), obj); }); it('should read one car list', function () { - hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0').should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0'), [ { a: 'a', c: 'c', b: 'b', @@ -555,7 +568,7 @@ describe('object.test.js', function () { ]); var cars = hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0', true); - cars.should.eql([ + assert.deepEqual(cars, [ { $class: 'hessian.demo.Car', $: { @@ -568,11 +581,11 @@ describe('object.test.js', function () { } ]); - hessian.encode(cars, '2.0').should.eql(utils.bytes('v2/map/one_car_list')); + assert.deepEqual(hessian.encode(cars, '2.0'), utils.bytes('v2/map/one_car_list')); }); it('should read two car list', function () { - hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0').should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0'), [ { a: 'a', c: 'c', b: 'b', @@ -588,7 +601,7 @@ describe('object.test.js', function () { ]); var cars = hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0', true); - cars.should.eql([ + assert.deepEqual(cars, [ { $class: 'hessian.demo.Car', $: { @@ -612,8 +625,8 @@ describe('object.test.js', function () { ]); var buf = hessian.encode(cars, '2.0'); - buf.should.length(utils.bytes('v2/map/two_car_list').length); - buf.should.eql(utils.bytes('v2/map/two_car_list')); + assert(buf.length === utils.bytes('v2/map/two_car_list').length); + assert.deepEqual(buf, utils.bytes('v2/map/two_car_list')); }); it('should read many cars', function () { @@ -621,7 +634,7 @@ describe('object.test.js', function () { // list.add(new Car("model 1")); // list.add(new Car("model 2")); // list.add(new Car("model 3")); - hessian.decode(utils.bytes('v2/map/car_list'), '2.0').should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v2/map/car_list'), '2.0'), [ { a: 'a', c: 'c', b: 'b', @@ -643,7 +656,7 @@ describe('object.test.js', function () { ]); var cars = hessian.decode(utils.bytes('v2/map/car_list'), '2.0', true); - cars.should.eql([ + assert.deepEqual(cars, [ { '$class': 'hessian.demo.Car', '$': { a: 'a', @@ -670,11 +683,11 @@ describe('object.test.js', function () { mileage: 65536 } } ]); - hessian.encode(cars, '2.0').should.eql(utils.bytes('v2/map/car_list')); + assert.deepEqual(hessian.encode(cars, '2.0'), utils.bytes('v2/map/car_list')); }); it('should read hessian 1.0 one car list', function () { - hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0').should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0'), [ { a: 'a', c: 'c', b: 'b', @@ -684,7 +697,7 @@ describe('object.test.js', function () { ]); var cars = hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0', true); - cars.should.eql([ + assert.deepEqual(cars, [ { $class: 'hessian.demo.Car', $: { @@ -699,7 +712,7 @@ describe('object.test.js', function () { }); it('should read hessian 1.0 two car list', function () { - hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0').should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0'), [ { a: 'a', c: 'c', b: 'b', @@ -715,7 +728,7 @@ describe('object.test.js', function () { ]); var cars = hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0', true); - cars.should.eql([ + assert.deepEqual(cars, [ { $class: 'hessian.demo.Car', $: { @@ -744,7 +757,7 @@ describe('object.test.js', function () { // list.add(new Car("model 1")); // list.add(new Car("model 2")); // list.add(new Car("model 3")); - hessian.decode(utils.bytes('v1/map/car_list'), '2.0').should.eql([ + assert.deepEqual(hessian.decode(utils.bytes('v1/map/car_list'), '2.0'), [ { a: 'a', c: 'c', b: 'b', @@ -766,7 +779,7 @@ describe('object.test.js', function () { ]); var cars = hessian.decode(utils.bytes('v1/map/car_list'), '2.0', true); - cars.should.eql([ + assert.deepEqual(cars, [ { '$class': 'hessian.demo.Car', '$': { a: 'a', @@ -798,9 +811,9 @@ describe('object.test.js', function () { it('should read and write', function () { var javabuf = utils.bytes('v2/object/AtomicLong0'); var a0 = hessian.decode(javabuf, '2.0'); - a0.should.eql({value: 0}); + assert.deepEqual(a0, {value: 0}); var a0 = hessian.decode(javabuf, '2.0', true); - a0.should.eql({ + assert.deepEqual(a0, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { @@ -809,7 +822,7 @@ describe('object.test.js', function () { }, } }); - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { @@ -817,13 +830,13 @@ describe('object.test.js', function () { $: 0 } } - }, '2.0').should.eql(javabuf); + }, '2.0'), javabuf); javabuf = utils.bytes('v2/object/AtomicLong1'); var a1 = hessian.decode(javabuf, '2.0'); - a1.should.eql({value: 1}); + assert.deepEqual(a1, {value: 1}); var a1 = hessian.decode(javabuf, '2.0', true); - a1.should.eql({ + assert.deepEqual(a1, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { @@ -832,7 +845,7 @@ describe('object.test.js', function () { }, } }); - hessian.encode({ + assert.deepEqual(hessian.encode({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { @@ -840,7 +853,7 @@ describe('object.test.js', function () { $: 1 } } - }, '2.0').should.eql(javabuf); + }, '2.0'), javabuf); }); }); }); diff --git a/test/string.test.js b/test/string.test.js index cdd5a5f..28590d0 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -10,11 +10,7 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var hessian = require('../'); var utils = require('./utils'); @@ -25,22 +21,22 @@ describe('string.test.js', function () { new Buffer('hello')]); it('should read string', function () { - hessian.decode(helloBuffer).should.equal('hello'); - hessian.decode(Buffer.concat([new Buffer(['s'.charCodeAt(0), 0x00, 0x07]), + assert(hessian.decode(helloBuffer) === 'hello'); + assert(hessian.decode(Buffer.concat([new Buffer(['s'.charCodeAt(0), 0x00, 0x07]), new Buffer('hello, '), new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), - new Buffer('world')])).should.equal('hello, world'); + new Buffer('world')])) === 'hello, world'); }); it('should write string', function () { var s = hessian.encode('hello'); - s.should.be.a.Buffer; - s.should.length(8); - s.should.eql(helloBuffer); + assert(Buffer.isBuffer(s)); + assert(s.length === 8); + assert.deepEqual(s, helloBuffer); }); it('should read write empty string', function () { - hessian.decode(new Buffer(['S'.charCodeAt(0), 0, 0])).should.equal(''); - hessian.encode('').should.eql(new Buffer(['S'.charCodeAt(0), 0, 0])); + assert(hessian.decode(new Buffer(['S'.charCodeAt(0), 0, 0])) === ''); + assert.deepEqual(hessian.encode(''), new Buffer(['S'.charCodeAt(0), 0, 0])); }); it('should read and write utf8 string as java', function () { @@ -48,94 +44,129 @@ describe('string.test.js', function () { for (var i = 0; i < 32767; i++) { str += '锋'; } - hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_32767')); - hessian.decode(utils.bytes('v1/string/utf8_32767')).should.equal(str); + assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_32767')); + assert(hessian.decode(utils.bytes('v1/string/utf8_32767')) === str); var str = ''; for (var i = 0; i < 32768; i++) { str += '锋'; } - hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_32768')); - hessian.decode(utils.bytes('v1/string/utf8_32768')).should.equal(str); + assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_32768')); + assert(hessian.decode(utils.bytes('v1/string/utf8_32768')) === str); var str = ''; for (var i = 0; i < 32769; i++) { str += '锋'; } - hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_32769')); - hessian.decode(utils.bytes('v1/string/utf8_32769')).should.equal(str); + assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_32769')); + assert(hessian.decode(utils.bytes('v1/string/utf8_32769')) === str); var str = ''; for (var i = 0; i < 65534; i++) { str += '锋'; } - hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_65534')); - hessian.decode(utils.bytes('v1/string/utf8_65534')).should.equal(str); + assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_65534')); + assert(hessian.decode(utils.bytes('v1/string/utf8_65534')) === str); var str = ''; for (var i = 0; i < 65535; i++) { str += '锋'; } - hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_65535')); - hessian.decode(utils.bytes('v1/string/utf8_65535')).should.equal(str); + assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_65535')); + assert(hessian.decode(utils.bytes('v1/string/utf8_65535')) === str); var str = ''; for (var i = 0; i < 65536; i++) { str += '锋'; } - hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_65536')); - hessian.decode(utils.bytes('v1/string/utf8_65536')).should.equal(str); + assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_65536')); + assert(hessian.decode(utils.bytes('v1/string/utf8_65536')) === str); var str = ''; for (var i = 0; i < 65537; i++) { str += '锋'; } - hessian.encode(str, '1.0').should.eql(utils.bytes('v1/string/utf8_65537')); - hessian.decode(utils.bytes('v1/string/utf8_65537')).should.equal(str); + assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_65537')); + assert(hessian.decode(utils.bytes('v1/string/utf8_65537')) === str); }); it('should write string same as java write', function () { - hessian.encode('', '1.0').should.eql(utils.bytes('v1/string/empty')); - hessian.encode('foo').should.eql(utils.bytes('v1/string/foo')); - hessian.encode('中文 Chinese', '1.0').should.eql(utils.bytes('v1/string/chinese')); + assert.deepEqual(hessian.encode('', '1.0'), utils.bytes('v1/string/empty')); + assert.deepEqual(hessian.encode('foo'), utils.bytes('v1/string/foo')); + assert.deepEqual(hessian.encode('中文 Chinese', '1.0'), utils.bytes('v1/string/chinese')); var text4k = utils.string('4k'); - hessian.encode(text4k, '1.0').should.eql(utils.bytes('v1/string/text4k')); - hessian.decode(utils.bytes('v1/string/text4k')).should.equal(text4k); + assert.deepEqual(hessian.encode(text4k, '1.0'), utils.bytes('v1/string/text4k')); + assert(hessian.decode(utils.bytes('v1/string/text4k')) === text4k); var largeBuf = new Buffer(32767); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_32767')); - hessian.decode(utils.bytes('v1/string/large_string_32767')).should.equal(largeBuf.toString()); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '1.0'), + utils.bytes('v1/string/large_string_32767') + ); + assert( + hessian.decode(utils.bytes('v1/string/large_string_32767')) === largeBuf.toString() + ); var largeBuf = new Buffer(32768); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_32768')); - hessian.decode(utils.bytes('v1/string/large_string_32768')).should.equal(largeBuf.toString()); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '1.0'), + utils.bytes('v1/string/large_string_32768') + ); + assert( + hessian.decode(utils.bytes('v1/string/large_string_32768')) === largeBuf.toString() + ); var largeBuf = new Buffer(32769); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_32769')); - hessian.decode(utils.bytes('v1/string/large_string_32769')).should.equal(largeBuf.toString()); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '1.0'), + utils.bytes('v1/string/large_string_32769') + ); + assert( + hessian.decode(utils.bytes('v1/string/large_string_32769')) === largeBuf.toString() + ); var largeBuf = new Buffer(65534); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_65534')); - hessian.decode(utils.bytes('v1/string/large_string_65534')).should.equal(largeBuf.toString()); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '1.0'), + utils.bytes('v1/string/large_string_65534') + ); + assert( + hessian.decode(utils.bytes('v1/string/large_string_65534')) === largeBuf.toString() + ); var largeBuf = new Buffer(65535); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_65535')); - hessian.decode(utils.bytes('v1/string/large_string_65535')).should.equal(largeBuf.toString()); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '1.0'), + utils.bytes('v1/string/large_string_65535') + ); + assert( + hessian.decode(utils.bytes('v1/string/large_string_65535')) === largeBuf.toString() + ); var largeBuf = new Buffer(65536); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_65536')); - hessian.decode(utils.bytes('v1/string/large_string_65536')).should.equal(largeBuf.toString()); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '1.0'), + utils.bytes('v1/string/large_string_65536') + ); + assert( + hessian.decode(utils.bytes('v1/string/large_string_65536')) === largeBuf.toString() + ); var largeBuf = new Buffer(65537); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '1.0').should.eql(utils.bytes('v1/string/large_string_65537')); - hessian.decode(utils.bytes('v1/string/large_string_65537')).should.equal(largeBuf.toString()); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '1.0'), + utils.bytes('v1/string/large_string_65537') + ); + assert( + hessian.decode(utils.bytes('v1/string/large_string_65537')) === largeBuf.toString() + ); }); @@ -150,57 +181,55 @@ describe('string.test.js', function () { } } largeString = largeString.join(''); - hessian.encode(largeString, '1.0').should.eql(utils.bytes('v1/string/large_string_chars')); + assert.deepEqual( + hessian.encode(largeString, '1.0'), + utils.bytes('v1/string/large_string_chars') + ); // read it - hessian.decode(utils.bytes('v1/string/large_string_chars')).should.equal(largeString); + assert( + hessian.decode(utils.bytes('v1/string/large_string_chars')) === largeString + ); }); describe('v2.0', function () { it('should read short strings', function () { - hessian.decode(new Buffer([0x00]), '2.0').should.equal(''); - hessian.decode(new Buffer([0x00]), '2.0', true).should.equal(''); - hessian.decode(Buffer.concat([new Buffer([0x05]), - new Buffer('hello')]), '2.0').should.equal('hello'); - hessian.decode(new Buffer([0x01, 0xc3, 0x83]), '2.0').should.equal('\u00c3'); - hessian.decode(Buffer.concat([new Buffer([0x09]), - new Buffer('hello, 中文')]), '2.0').should.equal('hello, 中文'); + assert(hessian.decode(new Buffer([0x00]), '2.0') === ''); + assert(hessian.decode(new Buffer([0x00]), '2.0', true) === ''); + assert(hessian.decode(Buffer.concat([new Buffer([0x05]), + new Buffer('hello')]), '2.0') === 'hello'); + assert(hessian.decode(new Buffer([0x01, 0xc3, 0x83]), '2.0') === '\u00c3'); + assert(hessian.decode(Buffer.concat([new Buffer([0x09]), + new Buffer('hello, 中文')]), '2.0') === 'hello, 中文'); }); it('should read "hello" in long form', function () { - hessian.decode(Buffer.concat([new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), - new Buffer('hello')]), '2.0').should.equal('hello'); + assert(hessian.decode(Buffer.concat([new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), + new Buffer('hello')]), '2.0') === 'hello'); }); it('should read split into two chunks: s and short strings', function () { - hessian.decode(Buffer.concat([new Buffer(['s'.charCodeAt(0), 0x00, 0x07]), - new Buffer('hello, '), new Buffer([0x05]), new Buffer('world')]), '2.0') - .should.equal('hello, world'); + assert(hessian.decode(Buffer.concat([new Buffer(['s'.charCodeAt(0), 0x00, 0x07]), + new Buffer('hello, '), new Buffer([0x05]), new Buffer('world')]), '2.0') === 'hello, world'); }); it('should write short strings', function () { - hessian.encode('', '2.0').should.eql(new Buffer([0x00])); - hessian.encode('foo', '2.0').should.eql( - Buffer.concat([ - new Buffer([0x03]), - new Buffer('foo') - ]) - ); - hessian.encode('0123456789012345678901234567890', '2.0').should.eql( - Buffer.concat([ - new Buffer([0x1f]), - new Buffer('0123456789012345678901234567890') - ]) - ); + assert.deepEqual(hessian.encode('', '2.0'), new Buffer([0x00])); + assert.deepEqual(hessian.encode('foo', '2.0'), Buffer.concat([ + new Buffer([0x03]), + new Buffer('foo') + ])); + assert.deepEqual(hessian.encode('0123456789012345678901234567890', '2.0'), Buffer.concat([ + new Buffer([0x1f]), + new Buffer('0123456789012345678901234567890') + ])); var len32Buf = new Buffer(2); len32Buf.writeInt16BE(32, 0); - hessian.encode('01234567890123456789012345678901', '2.0').should.eql( - Buffer.concat([ - new Buffer([0x53]), - len32Buf, - new Buffer('01234567890123456789012345678901') - ]) - ); + assert.deepEqual(hessian.encode('01234567890123456789012345678901', '2.0'), Buffer.concat([ + new Buffer([0x53]), + len32Buf, + new Buffer('01234567890123456789012345678901') + ])); var largeBuf = new Buffer(65535); largeBuf.fill(0x41); @@ -212,72 +241,109 @@ describe('string.test.js', function () { }); it('should read java string', function () { - hessian.decode(utils.bytes('v2/string/empty'), '2.0').should.equal(''); - hessian.decode(utils.bytes('v2/string/foo'), '2.0').should.equal('foo'); - hessian.decode(utils.bytes('v2/string/chinese'), '2.0').should.equal('中文 Chinese'); - hessian.decode(utils.bytes('v2/string/text4k'), '2.0').should.equal(utils.string('4k')); + assert(hessian.decode(utils.bytes('v2/string/empty'), '2.0') === ''); + assert(hessian.decode(utils.bytes('v2/string/foo'), '2.0') === 'foo'); + assert(hessian.decode(utils.bytes('v2/string/chinese'), '2.0') === '中文 Chinese'); + assert( + hessian.decode(utils.bytes('v2/string/text4k'), '2.0') === utils.string('4k') + ); var largeBuf = new Buffer(32767); largeBuf.fill(0x41); - hessian.decode(utils.bytes('v2/string/large_string_32767'), '2.0').should.equal(largeBuf.toString()); + assert( + hessian.decode(utils.bytes('v2/string/large_string_32767'), '2.0') === largeBuf.toString() + ); var largeBuf = new Buffer(32768); largeBuf.fill(0x41); - hessian.decode(utils.bytes('v2/string/large_string_32768'), '2.0').should.equal(largeBuf.toString()); + assert( + hessian.decode(utils.bytes('v2/string/large_string_32768'), '2.0') === largeBuf.toString() + ); var largeBuf = new Buffer(32769); largeBuf.fill(0x41); - hessian.decode(utils.bytes('v2/string/large_string_32769'), '2.0').should.equal(largeBuf.toString()); + assert( + hessian.decode(utils.bytes('v2/string/large_string_32769'), '2.0') === largeBuf.toString() + ); var largeBuf = new Buffer(65534); largeBuf.fill(0x41); - hessian.decode(utils.bytes('v2/string/large_string_65534'), '2.0').should.equal(largeBuf.toString()); + assert( + hessian.decode(utils.bytes('v2/string/large_string_65534'), '2.0') === largeBuf.toString() + ); var largeBuf = new Buffer(65535); largeBuf.fill(0x41); - hessian.decode(utils.bytes('v2/string/large_string_65535'), '2.0').should.equal(largeBuf.toString()); + assert( + hessian.decode(utils.bytes('v2/string/large_string_65535'), '2.0') === largeBuf.toString() + ); var largeBuf = new Buffer(65536); largeBuf.fill(0x41); - hessian.decode(utils.bytes('v2/string/large_string_65536'), '2.0').should.equal(largeBuf.toString()); + assert( + hessian.decode(utils.bytes('v2/string/large_string_65536'), '2.0') === largeBuf.toString() + ); var largeBuf = new Buffer(65537); largeBuf.fill(0x41); - hessian.decode(utils.bytes('v2/string/large_string_65537'), '2.0').should.equal(largeBuf.toString()); + assert( + hessian.decode(utils.bytes('v2/string/large_string_65537'), '2.0') === largeBuf.toString() + ); }); it('should write string same as java write', function () { - hessian.encode('', '2.0').should.eql(utils.bytes('v2/string/empty')); - hessian.encode('foo', '2.0').should.eql(utils.bytes('v2/string/foo')); - hessian.encode('中文 Chinese', '2.0').should.eql(utils.bytes('v2/string/chinese')); + assert.deepEqual(hessian.encode('', '2.0'), utils.bytes('v2/string/empty')); + assert.deepEqual(hessian.encode('foo', '2.0'), utils.bytes('v2/string/foo')); + assert.deepEqual(hessian.encode('中文 Chinese', '2.0'), utils.bytes('v2/string/chinese')); var text4k = utils.string('4k'); - hessian.encode(text4k, '2.0').should.eql(utils.bytes('v2/string/text4k')); + assert.deepEqual(hessian.encode(text4k, '2.0'), utils.bytes('v2/string/text4k')); var largeBuf = new Buffer(32767); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '2.0').should.eql(utils.bytes('v2/string/large_string_32767')); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '2.0'), + utils.bytes('v2/string/large_string_32767') + ); var largeBuf = new Buffer(32768); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '2.0').should.eql(utils.bytes('v2/string/large_string_32768')); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '2.0'), + utils.bytes('v2/string/large_string_32768') + ); var largeBuf = new Buffer(32769); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '2.0').should.eql(utils.bytes('v2/string/large_string_32769')); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '2.0'), + utils.bytes('v2/string/large_string_32769') + ); var largeBuf = new Buffer(65534); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '2.0').should.eql(utils.bytes('v2/string/large_string_65534')); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '2.0'), + utils.bytes('v2/string/large_string_65534') + ); var largeBuf = new Buffer(65535); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '2.0').should.eql(utils.bytes('v2/string/large_string_65535')); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '2.0'), + utils.bytes('v2/string/large_string_65535') + ); var largeBuf = new Buffer(65536); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '2.0').should.eql(utils.bytes('v2/string/large_string_65536')); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '2.0'), + utils.bytes('v2/string/large_string_65536') + ); var largeBuf = new Buffer(65537); largeBuf.fill(0x41); - hessian.encode(largeBuf.toString(), '2.0').should.eql(utils.bytes('v2/string/large_string_65537')); + assert.deepEqual( + hessian.encode(largeBuf.toString(), '2.0'), + utils.bytes('v2/string/large_string_65537') + ); }); @@ -290,7 +356,10 @@ describe('string.test.js', function () { } } largeString = largeString.join(''); - hessian.encode(largeString, '2.0').should.eql(utils.bytes('v2/string/large_string_chars')); + assert.deepEqual( + hessian.encode(largeString, '2.0'), + utils.bytes('v2/string/large_string_chars') + ); }); it('should read and write utf8 string as java', function () { @@ -298,57 +367,57 @@ describe('string.test.js', function () { for (var i = 0; i < 32767; i++) { str += '锋'; } - hessian.encode(str, '2.0').should.eql(utils.bytes('v2/string/utf8_32767')); - hessian.decode(utils.bytes('v2/string/utf8_32767'), '2.0').should.equal(str); - hessian.decode(utils.bytes('v1/string/utf8_32767'), '2.0').should.equal(str); + assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_32767')); + assert(hessian.decode(utils.bytes('v2/string/utf8_32767'), '2.0') === str); + assert(hessian.decode(utils.bytes('v1/string/utf8_32767'), '2.0') === str); var str = ''; for (var i = 0; i < 32768; i++) { str += '锋'; } - hessian.encode(str, '2.0').should.eql(utils.bytes('v2/string/utf8_32768')); - hessian.decode(utils.bytes('v2/string/utf8_32768'), '2.0').should.equal(str); - hessian.decode(utils.bytes('v1/string/utf8_32768'), '2.0').should.equal(str); + assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_32768')); + assert(hessian.decode(utils.bytes('v2/string/utf8_32768'), '2.0') === str); + assert(hessian.decode(utils.bytes('v1/string/utf8_32768'), '2.0') === str); var str = ''; for (var i = 0; i < 32769; i++) { str += '锋'; } - hessian.encode(str, '2.0').should.eql(utils.bytes('v2/string/utf8_32769')); - hessian.decode(utils.bytes('v2/string/utf8_32769'), '2.0').should.equal(str); - hessian.decode(utils.bytes('v1/string/utf8_32769'), '2.0').should.equal(str); + assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_32769')); + assert(hessian.decode(utils.bytes('v2/string/utf8_32769'), '2.0') === str); + assert(hessian.decode(utils.bytes('v1/string/utf8_32769'), '2.0') === str); var str = ''; for (var i = 0; i < 65534; i++) { str += '锋'; } - hessian.encode(str, '2.0').should.eql(utils.bytes('v1/string/utf8_65534')); - hessian.decode(utils.bytes('v1/string/utf8_65534'), '2.0').should.equal(str); - hessian.decode(utils.bytes('v1/string/utf8_65534'), '2.0').should.equal(str); + assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v1/string/utf8_65534')); + assert(hessian.decode(utils.bytes('v1/string/utf8_65534'), '2.0') === str); + assert(hessian.decode(utils.bytes('v1/string/utf8_65534'), '2.0') === str); var str = ''; for (var i = 0; i < 65535; i++) { str += '锋'; } - hessian.encode(str, '2.0').should.eql(utils.bytes('v2/string/utf8_65535')); - hessian.decode(utils.bytes('v2/string/utf8_65535'), '2.0').should.equal(str); - hessian.decode(utils.bytes('v1/string/utf8_65535'), '2.0').should.equal(str); + assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_65535')); + assert(hessian.decode(utils.bytes('v2/string/utf8_65535'), '2.0') === str); + assert(hessian.decode(utils.bytes('v1/string/utf8_65535'), '2.0') === str); var str = ''; for (var i = 0; i < 65536; i++) { str += '锋'; } - hessian.encode(str, '2.0').should.eql(utils.bytes('v2/string/utf8_65536')); - hessian.decode(utils.bytes('v2/string/utf8_65536'), '2.0').should.equal(str); - hessian.decode(utils.bytes('v1/string/utf8_65536'), '2.0').should.equal(str); + assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_65536')); + assert(hessian.decode(utils.bytes('v2/string/utf8_65536'), '2.0') === str); + assert(hessian.decode(utils.bytes('v1/string/utf8_65536'), '2.0') === str); var str = ''; for (var i = 0; i < 65537; i++) { str += '锋'; } - hessian.encode(str, '2.0').should.eql(utils.bytes('v2/string/utf8_65537')); - hessian.decode(utils.bytes('v2/string/utf8_65537'), '2.0').should.equal(str); - hessian.decode(utils.bytes('v1/string/utf8_65537'), '2.0').should.equal(str); + assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_65537')); + assert(hessian.decode(utils.bytes('v2/string/utf8_65537'), '2.0') === str); + assert(hessian.decode(utils.bytes('v1/string/utf8_65537'), '2.0') === str); }); }); }); diff --git a/test/utils.test.js b/test/utils.test.js index 8d23055..927e61d 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -10,21 +10,17 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); +var assert = require('assert'); var utils = require('../lib/utils'); describe('utils.test.js', function () { describe('getSerializer()', function () { it('should [int get writeArray', function () { - utils.getSerializer('[int').should.equal('writeArray'); + assert(utils.getSerializer('[int') === 'writeArray'); }); it('should [string get writeArray', function () { - utils.getSerializer('[string').should.equal('writeArray'); + assert(utils.getSerializer('[string') === 'writeArray'); }); }); }); diff --git a/test/v1.test.js b/test/v1.test.js index 54286e7..0390053 100644 --- a/test/v1.test.js +++ b/test/v1.test.js @@ -10,9 +10,9 @@ 'use strict'; +var assert = require('assert'); var fs = require('fs'); var path = require('path'); -var should = require('should'); var hessian = require('../'); var Encoder = hessian.Encoder; var Decoder = hessian.Decoder; @@ -33,26 +33,26 @@ describe('hessian v1', function () { describe('null', function () { it('should write and read null ok', function () { var buf = encoder.writeNull().get(); - buf.should.eql(new Buffer('N')); + assert.deepEqual(buf, new Buffer('N')); decoder.init(buf); - (decoder.readNull() === null).should.be.ok; + assert(decoder.readNull() === null); }); }); describe('bool', function () { it('should write and read true ok', function () { var buf = encoder.writeBool(true).get(); - buf.should.eql(new Buffer('T')); + assert.deepEqual(buf, new Buffer('T')); - decoder.init(buf).readBool().should.eql(true); + assert(decoder.init(buf).readBool() === true); }); it('should write and read false ok', function () { var buf = encoder.writeBool(false).get(); - buf.should.eql(new Buffer('F')); + assert.deepEqual(buf, new Buffer('F')); - decoder.init(buf).readBool().should.eql(false); + assert(decoder.init(buf).readBool() === false); }); }); @@ -68,9 +68,9 @@ describe('hessian v1', function () { tests.forEach(function (t) { var buf = encoder.writeInt(t[0]).get(); - buf.inspect().should.equal(t[1]); + assert(buf.inspect() === t[1]); decoder.init(buf); - decoder.readInt().should.equal(t[0]); + assert(decoder.readInt() === t[0]); encoder.clean(); decoder.clean(); }); @@ -84,9 +84,9 @@ describe('hessian v1', function () { ]; tests.forEach(function (t, idx) { - (function () { + assert.throws(function () { var buf = encoder.writeInt(t); - }).should.throw('hessian writeInt expect input type is `int32`, but got `number` : ' + tests[idx] + ' '); + }, 'hessian writeInt expect input type is `int32`, but got `number` : ' + tests[idx] + ' '); }); }); @@ -95,9 +95,9 @@ describe('hessian v1', function () { [new Buffer([0x48, 0x00, 0x00, 0x00, 0x00]), 'hessian readInt only accept label `I` but got unexpect label `H`'], ]; tests.forEach(function (t) { - (function () { + assert.throws(function () { decoder.init(t[0]).readInt(); - }).should.throw(t[1]); + }, t[1]); }); }); }); @@ -117,8 +117,8 @@ describe('hessian v1', function () { tests.forEach(function (t) { var buf = encoder.writeLong(t[0]).get(); - buf.inspect().should.equal(t[1]); - decoder.init(buf).readLong().should.eql(t[0]); + assert(buf.inspect() === t[1]); + assert.deepEqual(decoder.init(buf).readLong(), t[0]); encoder.clean(); decoder.clean(); }); @@ -132,7 +132,7 @@ describe('hessian v1', function () { tests.forEach(function (t) { var buf = encoder.writeLong(t).get(); - decoder.init(buf).readLong().should.not.eql(t); + assert.notStrictEqual(decoder.init(buf).readLong(), t); }); }); @@ -142,9 +142,9 @@ describe('hessian v1', function () { 'hessian readLong only accept label `L` but got unexpect label `K`'] ]; tests.forEach(function (t) { - (function () { + assert.throws(function () { decoder.init(t[0]).readLong(); - }).should.throw(t[1]); + }, t[1]); }); }); }); @@ -164,8 +164,8 @@ describe('hessian v1', function () { tests.forEach(function (t) { var buf = encoder.writeDouble(t[0]).get(); - buf.inspect().should.equal(t[1]); - decoder.init(buf).readDouble().should.equal(t[0]); + assert(buf.inspect() === t[1]); + assert(decoder.init(buf).readDouble() === t[0]); encoder.clean(); decoder.clean(); }); @@ -177,9 +177,9 @@ describe('hessian v1', function () { 'hessian readDouble only accept label `D` but got unexpect label `E`'] ]; tests.forEach(function (t) { - (function () { + assert.throws(function () { decoder.init(t[0]).readDouble(); - }).should.throw(t[1]); + }, t[1]); }); }); }); @@ -193,8 +193,8 @@ describe('hessian v1', function () { tests.forEach(function (t) { var buf = encoder.writeDate(t[0]).get(); - buf.inspect().should.equal(t[1]); - decoder.init(buf).readDate().should.eql(t[0]); + assert(buf.inspect() === t[1]); + assert.deepEqual(decoder.init(buf).readDate(), t[0]); encoder.clean(); decoder.clean(); }); @@ -206,9 +206,9 @@ describe('hessian v1', function () { 'hessian readDate only accept label `d` but got unexpect label `e`'] ]; tests.forEach(function (t) { - (function () { + assert.throws(function () { decoder.init(t[0]).readDate(); - }).should.throw(t[1]); + }, t[1]); }); }); }); @@ -217,17 +217,17 @@ describe('hessian v1', function () { it('should write and read small bytes ok', function () { var inputBuffer = new Buffer([1, 2, 3, 4, 5]); var buf = encoder.writeBytes(inputBuffer).get(); - buf.inspect().should.equal(''); - decoder.init(buf).readBytes().should.eql(inputBuffer); + assert(buf.inspect() === ''); + assert.deepEqual(decoder.init(buf).readBytes(), inputBuffer); }); it('should write and read big bytes ok', function () { var inputBuffer = fixtureBytes; var inputLength = inputBuffer.length; var buf = encoder.writeBytes(inputBuffer).get(); - buf.length.should.equal(inputLength + + assert(buf.length === inputLength + Math.ceil(inputLength / utils.MAX_BYTE_TRUNK_SIZE) * 3); - decoder.init(buf).readBytes().should.eql(inputBuffer); + assert.deepEqual(decoder.init(buf).readBytes(), inputBuffer); }); it('should read bytes error', function () { @@ -237,41 +237,41 @@ describe('hessian v1', function () { ]; tests.forEach(function (t) { - (function () { + assert.throws(function () { var buf = decoder.init(t[0]).readBytes(); - }).should.throw(t[1]); + }, t[1]); }); }); it('should bytes length equal MAX_BYTE_TRUNK_SIZE work', function () { var oneTrunkBuf = new Buffer(utils.MAX_BYTE_TRUNK_SIZE); var buf = encoder.writeBytes(oneTrunkBuf).get(); - decoder.init(buf).readBytes().should.eql(oneTrunkBuf); + assert.deepEqual(decoder.init(buf).readBytes(), oneTrunkBuf); encoder.clean(); var twoTrunkBuf = new Buffer(utils.MAX_BYTE_TRUNK_SIZE * 2); buf = encoder.writeBytes(twoTrunkBuf).get(); - decoder.init(buf).readBytes().should.eql(twoTrunkBuf); + assert.deepEqual(decoder.init(buf).readBytes(), twoTrunkBuf); }); it('should write type error', function () { - (function () { + assert.throws(function () { encoder.writeBytes(); - }).should.throw('hessian writeBytes expect input type is `buffer`, but got `undefined` : undefined '); - (function () { + }, 'hessian writeBytes expect input type is `buffer`, but got `undefined` : undefined '); + assert.throws(function () { encoder.writeBytes(''); - }).should.throw('hessian writeBytes expect input type is `buffer`, but got `string` : "" '); - (function () { + }, 'hessian writeBytes expect input type is `buffer`, but got `string` : "" '); + assert.throws(function () { encoder.writeBytes(null); - }).should.throw('hessian writeBytes expect input type is `buffer`, but got `object` : null '); - (function () { + }, 'hessian writeBytes expect input type is `buffer`, but got `object` : null '); + assert.throws(function () { encoder.writeBytes(100); - }).should.throw('hessian writeBytes expect input type is `buffer`, but got `number` : 100 '); + }, 'hessian writeBytes expect input type is `buffer`, but got `number` : 100 '); }); it('should write and read empty bytes', function () { var buf = encoder.writeBytes(new Buffer('')).get(); - decoder.init(buf).readBytes().should.eql(new Buffer('')); + assert.deepEqual(decoder.init(buf).readBytes(), new Buffer('')); }); }); @@ -279,17 +279,19 @@ describe('hessian v1', function () { it('should write and read small string ok', function () { var inputStr = '你好,hessian.∆∆˚œø∂πß∂µ'; var buf = encoder.writeString(inputStr).get(); - buf.inspect().should.equal(''); - decoder.init(buf).readString().should.equal(inputStr); + assert( + buf.inspect() === '' + ); + assert(decoder.init(buf).readString() === inputStr); }); it('should write and read big string ok', function () { var inputStr = fixtureString; var inputStrLength = inputStr.length; var buf = encoder.writeString(inputStr).get(); - buf.length.should.equal(new Buffer(inputStr).length + + assert(buf.length === new Buffer(inputStr).length + Math.ceil(inputStrLength / utils.MAX_CHAR_TRUNK_SIZE) * 3); - decoder.init(buf).readString().should.equal(inputStr); + assert(decoder.init(buf).readString() === inputStr); }); it('should read string error', function () { @@ -300,26 +302,26 @@ describe('hessian v1', function () { ]; tests.forEach(function (t) { - (function () { + assert.throws(function () { var buf = decoder.init(t[0]).readString(); - }).should.throw(t[1]); + }, t[1]); }); }); it('should write type error', function () { - (function () { + assert.throws(function () { encoder.writeString(); - }).should.throw('hessian writeString expect input type is `string`, but got `undefined` : undefined '); + }, 'hessian writeString expect input type is `string`, but got `undefined` : undefined '); // v0.10.28 return [1,2,3,4,5] // (function () { // encoder.writeString(new Buffer([1,2,3,4,5])); // }).should.throw('hessian writeString expect input type is `string`, but got `object` : {"type":"Buffer","data":[1,2,3,4,5]} '); - (function () { + assert.throws(function () { encoder.writeString(null); - }).should.throw('hessian writeString expect input type is `string`, but got `object` : null '); - (function () { + }, 'hessian writeString expect input type is `string`, but got `object` : null '); + assert.throws(function () { encoder.writeString(100); - }).should.throw('hessian writeString expect input type is `string`, but got `number` : 100 '); + }, 'hessian writeString expect input type is `string`, but got `number` : 100 '); }); it('should string length equal MAX_CHAR_TRUNK_SIZE work', function () { @@ -327,19 +329,19 @@ describe('hessian v1', function () { oneTrunkString.fill(0x41); oneTrunkString = oneTrunkString.toString(); var buf = encoder.writeString(oneTrunkString).get(); - decoder.init(buf).readString().should.eql(oneTrunkString); + assert.deepEqual(decoder.init(buf).readString(), oneTrunkString); encoder.clean(); var twoTrunkString = new Buffer(utils.MAX_CHAR_TRUNK_SIZE * 2); twoTrunkString.fill(0x41); twoTrunkString = twoTrunkString.toString(); buf = encoder.writeString(twoTrunkString).get(); - decoder.init(buf).read().should.eql(twoTrunkString); + assert.deepEqual(decoder.init(buf).read(), twoTrunkString); }); it('should write and read empty string', function () { var buf = encoder.writeString('').get(); - decoder.init(buf).read().should.eql(''); + assert(decoder.init(buf).read() === ''); }); }); @@ -355,13 +357,13 @@ describe('hessian v1', function () { g: {a: 1, b: true, c: 'string'} }; var buf = encoder.writeObject(testObject).get(); - decoder.init(buf).readObject().should.eql(testObject); + assert.deepEqual(decoder.init(buf).readObject(), testObject); }); it('should write null obejct ok', function () { var nullObject = null; var nullBuf = encoder.writeObject(nullObject).get(); - (decoder.init(nullBuf).read() === null).should.be.ok; + assert(decoder.init(nullBuf).read() === null); }); it('should write and read object with circular ok', function () { @@ -376,12 +378,12 @@ describe('hessian v1', function () { var buf = encoder.writeObject(testObject).get(); var res = decoder.init(buf).readObject(); - res.a.should.equal(testObject.a); - res.b.should.equal(testObject.b); - res.c.a.a.should.equal(testObject.a); - res.c.a.b.should.equal(testObject.b); - res.d[2].a.a.should.equal(testObject.a); - res.d[2].a.b.should.equal(testObject.b); + assert(res.a === testObject.a); + assert(res.b === testObject.b); + assert(res.c.a.a === testObject.a); + assert(res.c.a.b === testObject.b); + assert(res.d[2].a.a === testObject.a); + assert(res.d[2].a.b === testObject.b); }); it('should write and read complex object ok', function () { @@ -395,9 +397,9 @@ describe('hessian v1', function () { var buf = encoder.writeObject(testObject).get(); var res = decoder.init(buf).readObject(); - res.should.eql({a:1, b:[1, 2, 3]}); + assert.deepEqual(res, {a:1, b:[1, 2, 3]}); var resWithType = decoder.init(buf).readObject(true); - resWithType.should.eql( { + assert.deepEqual(resWithType, { '$class': 'com.hessian.TestObject', '$': { a: { '$class': 'int', '$': 1 }, @@ -424,11 +426,11 @@ describe('hessian v1', function () { var buf = encoder.writeObject(testObject).get(); decoder.init(buf); - decoder.position().should.equal(0); + assert(decoder.position() === 0); decoder.position(1); //skip 'M' - decoder.readType().should.equal('com.hessian.TestObject'); - decoder.position().should.equal(26); - decoder.position(0).readObject(true).should.eql({ + assert(decoder.readType() === 'com.hessian.TestObject'); + assert(decoder.position() === 26); + assert.deepEqual(decoder.position(0).readObject(true), { '$class': 'com.hessian.TestObject', '$': { a: { '$class': 'int', '$': 1 }, @@ -452,9 +454,9 @@ describe('hessian v1', function () { var buf = encoder.writeObject(testObject).get(); encoder.clean(); - buf.should.eql(encoder.writeObject({foo: 'bar'}).get()); - decoder.init(buf).read().should.eql({foo: 'bar'}); - decoder.init(buf).read(true).should.eql({ + assert.deepEqual(buf, encoder.writeObject({foo: 'bar'}).get()); + assert.deepEqual(decoder.init(buf).read(), {foo: 'bar'}); + assert.deepEqual(decoder.init(buf).read(true), { '$class': 'java.util.HashMap', '$': { foo: { '$class': 'java.lang.String', '$': 'bar' } @@ -463,15 +465,15 @@ describe('hessian v1', function () { }); it('should write type error', function () { - (function () { + assert.throws(function () { encoder.writeObject('123'); - }).should.throw('hessian writeObject / writeMap expect input type is `object`, but got `string` : "123" '); - (function () { + }, 'hessian writeObject / writeMap expect input type is `object`, but got `string` : "123" '); + assert.throws(function () { encoder.writeObject(1.111); - }).should.throw('hessian writeObject / writeMap expect input type is `object`, but got `number` : 1.111 '); - (function () { + }, 'hessian writeObject / writeMap expect input type is `object`, but got `number` : 1.111 '); + assert.throws(function () { encoder.writeObject(100); - }).should.throw('hessian writeObject / writeMap expect input type is `object`, but got `number` : 100 '); + }, 'hessian writeObject / writeMap expect input type is `object`, but got `number` : 100 '); }); }); @@ -479,7 +481,7 @@ describe('hessian v1', function () { it('should write and read simple array ok', function () { var testArray = [1, true, 'string', 1.1, new Date()]; var buf = encoder.writeArray(testArray).get(); - decoder.init(buf).readArray().should.eql(testArray); + assert.deepEqual(decoder.init(buf).readArray(), testArray); }); it('should write circular array ok', function () { @@ -487,8 +489,8 @@ describe('hessian v1', function () { testArray.push(testArray); var buf = encoder.writeArray(testArray).get(); var res = decoder.init(buf).readArray(); - res[0].should.equal(testArray[0]); - res[1][1][1][0].should.equal(testArray[0]); + assert(res[0] === testArray[0]); + assert(res[1][1][1][0] === testArray[0]); }); it('should write and read complex array ok', function () { @@ -500,8 +502,8 @@ describe('hessian v1', function () { }] }; var buf = encoder.writeArray(testArray).get(); - decoder.init(buf).readArray().should.eql([[1, 2, 3]]); - decoder.init(buf).readArray(true).should.eql({ + assert.deepEqual(decoder.init(buf).readArray(), [[1, 2, 3]]); + assert.deepEqual(decoder.init(buf).readArray(true), { '$class': 'java.util.Set', '$': [ { @@ -524,9 +526,9 @@ describe('hessian v1', function () { var buf = encoder.writeArray(testArray).get(); encoder.clean(); - buf.should.eql(encoder.writeArray([1, 2, 3]).get()); - decoder.init(buf).read().should.eql([1, 2, 3]); - decoder.init(buf).read(true).should.eql({ + assert.deepEqual(buf, encoder.writeArray([1, 2, 3]).get()); + assert.deepEqual(decoder.init(buf).read(), [1, 2, 3]); + assert.deepEqual(decoder.init(buf).read(true), { $class: 'java.util.ArrayList', $: [ { '$class': 'int', '$': 1 }, @@ -539,24 +541,24 @@ describe('hessian v1', function () { it('should read unexpect end label', function () { var buf = encoder.writeArray([1, 2, 3]).get(); buf[buf.length - 1] = 40; - (function () { + assert.throws(function () { decoder.init(buf).read('hessian readArray error, unexpect end label: ('); - }).should.throw(); + }); }); it('should write type error', function () { - (function () { + assert.throws(function () { encoder.writeArray(); - }).should.throw('hessian writeArray input type invalid'); - (function () { + }, 'hessian writeArray input type invalid'); + assert.throws(function () { encoder.writeArray('123'); - }).should.throw('hessian writeArray input type invalid'); - (function () { + }, 'hessian writeArray input type invalid'); + assert.throws(function () { encoder.writeArray(1.111); - }).should.throw('hessian writeArray input type invalid'); - (function () { + }, 'hessian writeArray input type invalid'); + assert.throws(function () { encoder.writeArray(100); - }).should.throw('hessian writeArray input type invalid'); + }, 'hessian writeArray input type invalid'); }); }); @@ -572,7 +574,7 @@ describe('hessian v1', function () { ].forEach(function(val) { var buf = encoder.write({$class:'java.lang.Object', $: val}).get(); encoder.clean(); - decoder.init(buf).read().should.eql(val); + assert.deepEqual(decoder.init(buf).read(), val); }); }); }); @@ -611,19 +613,19 @@ describe('hessian v1', function () { var buf = hessian.encode(t[0]); var res = hessian.decode(buf, true); if (res) { - res.should.eql(t[1] || t[0]); + assert.deepEqual(res, t[1] || t[0]); } else { /* jshint eqeqeq: false */ - should.ok(res == t[0]); + assert(res == t[0]); } }); }); it('should decode error', function () { var buf = new Buffer([0x50, 0x11]); - (function() { + assert.throws(function() { hessian.decode(buf); - }).should.throw('hessian read got an unexpect code: 0x50'); + }, 'hessian read got an unexpect code: 0x50'); }); }); });