Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Break MsgPack support out; refactor some.

  • Loading branch information...
commit 41aa4fd7efc46f621c6b04bdc5fb12bcb5decd24 1 parent a825a6b
@pgriess authored
View
2  Makefile
@@ -1,7 +1,7 @@
.PHONY: test
test:
- for f in `ls -1 test/test-*.js` ; do \
+ for f in `find . -name 'test*.js'` ; do \
echo ">>> Testing $$f" ; \
node $$f ; \
done
View
181 test/test-msgpack.js → examples/msgpack/msgpack.js
@@ -1,177 +1,11 @@
// Verify that we can read a MsgPack stream.
var assert = require('assert');
-var TestStream = require('./util').TestStream;
-var strtok = require('../lib/strtok');
-
-var data = '\x25' + // fixnum(37)
- '\x6f' + // fixnum(111)
- '\xff' + // negative_fixnum(-32)
- '\xeb' + // negative_fixnum(-12)
- '\xcc\xff' + // uint8(255)
- '\xcd\x01\x01' + // uint16(257)
- '\xce\x01\x01\x01\x01' + // uint32(16843009)
- '\x93\x78\x0a\xcc\xef' + // fixarray([120, 10, 239])
- '\xc0' + // nil
- '\xc3' + // true
- '\xc2' + // false
- '\xd0\xff' + // int8(-1)
- '\xd0\x80' + // int8(-128)
- '\xd0\x70' + // int8(112)
- '\xd1\xff\xff' + // int16(-1)
- '\xd1\x80\x00' + // int16(-32768)
- '\xd1\x01\x00' + // int16(256)
- '\xd2\x00\x00\x00\x00' + // int32(0)
- '\xd2\x00\x00\xff\xff' + // int32(65535)
- '\xd2\x80\x00\x00\x00' + // int32(-2147483648)
- '\xd2\x80\xff\xff\xff' + // int32(-2130706433)
- '\xdc\x00\x01\x25' + // array16([37])
- '\xdc\x00\x03\xff\x25\xcc\xff' + // array16([-32, 37, 255])
- '\xdd\x00\x00\x00\x01\x25' + // array32([37])
- '\xdd\x00\x00\x00\x03\xff\x25\xcc\xff' + // array32([-32, 37, 255])
- '\xa3\x01\x02\x03' + // fixraw([0x01, 0x02, 0x03])
- '\xda\x00\x03\x01\x02\x03' + // raw16([0x01, 0x02, 0x03])
- '\xdb\x00\x00\x00\x03\x01\x02\x03' + // raw32([0x01, 0x02, 0x03])
- '\x81\x25\x6f' + // fixmap({37 : 111})
- '\xde\x00\x01\x25\x6f' + // map16({37 : 111})
- '\xdf\x00\x00\x00\x01\x25\x6f' + // map32({37 : 111})
- '';
-
-// Accumulate a top-level MsgPack value
-var valuesSeen = 0;
-var accMsgPack = function(v) {
- switch (valuesSeen++) {
- case 0:
- assert.strictEqual(v, 37);
- break;
-
- case 1:
- assert.strictEqual(v, 111);
- break;
-
- case 2:
- assert.strictEqual(v, -32);
- break;
-
- case 3:
- assert.strictEqual(v, -12);
- break;
-
- case 4:
- assert.strictEqual(v, 255);
- break;
-
- case 5:
- assert.strictEqual(v, 257);
- break;
-
- case 6:
- assert.strictEqual(v, 16843009);
- break;
-
- case 7:
- assert.ok(Array.isArray(v));
- assert.equal(v.length, 3);
- assert.deepEqual(v, [120, 10, 239]);
- break;
-
- case 8:
- assert.strictEqual(v, undefined);
- break;
-
- case 9:
- assert.strictEqual(v, true);
- break;
-
- case 10:
- assert.strictEqual(v, false);
- break;
-
- case 11:
- assert.strictEqual(v, -1);
- break;
-
- case 12:
- assert.strictEqual(v, -128);
- break;
-
- case 13:
- assert.strictEqual(v, 112);
- break;
-
- case 14:
- assert.strictEqual(v, -1);
- break;
-
- case 15:
- assert.strictEqual(v, -32768);
- break;
-
- case 16:
- assert.strictEqual(v, 256);
- break;
-
- case 17:
- assert.strictEqual(v, 0);
- break;
-
- case 18:
- assert.strictEqual(v, 65535);
- break;
-
- case 19:
- assert.strictEqual(v, -2147483648);
- break;
-
- case 20:
- assert.strictEqual(v, -2130706433);
- break;
-
- case 21:
- assert.ok(Array.isArray(v));
- assert.equal(v.length, 1);
- assert.deepEqual(v, [37]);
- break;
-
- case 22:
- assert.ok(Array.isArray(v));
- assert.equal(v.length, 3);
- assert.deepEqual(v, [-32, 37, 255]);
- break;
-
- case 23:
- assert.ok(Array.isArray(v));
- assert.equal(v.length, 1);
- assert.deepEqual(v, [37]);
- break;
-
- case 24:
- assert.ok(Array.isArray(v));
- assert.equal(v.length, 3);
- assert.deepEqual(v, [-32, 37, 255]);
- break;
-
- case 25:
- case 26:
- case 27:
- assert.strictEqual(typeof v, 'object');
- assert.equal(v.length, 3);
- assert.deepEqual(v.toString('binary'), '\x01\x02\x03');
- break;
-
- case 28:
- case 29:
- case 30:
- assert.strictEqual(typeof v, 'object');
- assert.deepEqual(v, {37 : 111});
- break;
-
- default:
- console.error('unexpected value: ' + JSON.stringify(v));
- }
-};
+var strtok = require('../../lib/strtok');
-strtok.parse(new TestStream(data), (function(acc) {
+// Generator function for handing to strtok.parse(); takes an accumulator
+// callback to invoke when a top-level type is read from the stream
+var parser = function(acc) {
// Type that we're in when reading a primitive; MSGPACK_* values
var type = undefined;
@@ -406,8 +240,5 @@ strtok.parse(new TestStream(data), (function(acc) {
// We're reading a new primitive; go get it
return strtok.UINT8;
};
-})(accMsgPack));
-
-process.on('exit', function() {
- assert.equal(valuesSeen, 31);
-});
+};
+exports.parser = parser;
View
77 examples/msgpack/test.js
@@ -0,0 +1,77 @@
+var assert = require('assert');
+var Buffer = require('buffer').Buffer;
+var msgpack = require('./msgpack');
+var strtok = require('../../lib/strtok');
+var util = require('../../test/util');
+
+var TESTS = [
+ ['\xc0', undefined], // nil
+ ['\xc3', true], // true
+ ['\xc2', false], // false
+ ['\x25', 37 ], // positive fixnum
+ ['\x6f', 111], // positive fixnum
+ ['\xff', -32], // negative fixnum
+ ['\xeb', -12], // negative _fixnum
+ ['\xcc\xff', 255], // uint8
+ ['\xcd\x01\x01', 257], // uint16
+ ['\xce\x01\x01\x01\x01', 16843009], // uint32
+ ['\xd0\xff', -1], // int8
+ ['\xd0\x80', -128], // int8
+ ['\xd0\x70', 112], // int8
+ ['\xd1\xff\xff', -1], // int16
+ ['\xd1\x80\x00', -32768], // int16
+ ['\xd1\x01\x00', 256], // int16
+ ['\xd2\x00\x00\x00\x00', 0], // int32
+ ['\xd2\x00\x00\xff\xff', 65535], // int32
+ ['\xd2\x80\x00\x00\x00', -2147483648], // int32
+ ['\xd2\x80\xff\xff\xff', -2130706433], // int32
+ ['\x93\x78\x0a\xcc\xef', [120, 10, 239]], // fixarray
+ ['\xdc\x00\x01\x25', [37]], // array16
+ [ // array16
+ '\xdc\x00\x03\xff\x25\xcc\xff',
+ [-32, 37, 255]
+ ],
+ ['\xdd\x00\x00\x00\x01\x25', [37]], // array32
+ [ // array32
+ '\xdd\x00\x00\x00\x03\xff\x25\xcc\xff',
+ [-32, 37, 255]
+ ],
+ [ // fixraw
+ '\xa3\x01\x02\x03',
+ new Buffer('\x01\x02\x03', 'binary')
+ ],
+ [ // raw16
+ '\xda\x00\x03\x01\x02\x03',
+ new Buffer('\x01\x02\x03', 'binary')
+ ],
+ [ // raw32
+ '\xdb\x00\x00\x00\x03\x01\x02\x03',
+ new Buffer('\x01\x02\x03', 'binary')
+ ],
+ ['\x81\x25\x6f', {37 : 111}], // fixmap
+ ['\xde\x00\x01\x25\x6f', {37 : 111}], // map16
+ ['\xdf\x00\x00\x00\x01\x25\x6f', {37 : 111}] // map32
+];
+
+var f = function() {
+ if (TESTS.length === 0) {
+ return;
+ }
+
+ var t = TESTS.shift();
+
+ strtok.parse(
+ new util.TestStream(t[0]),
+ msgpack.parser(function(v) {
+ assert.deepEqual(v, t[1]);
+
+ f();
+ })
+ );
+};
+
+f();
+
+process.on('exit', function() {
+ assert.equal(TESTS.length, 0);
+});
Please sign in to comment.
Something went wrong with that request. Please try again.