forked from mcollina/msgpack5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support encoding of native
Map
object types
this supports the ES2015 Map object as a msgpack `fixmap`, `map16`, or `map32`. Some tests "documenting" the oddities of this implementation have been added as well to ensure consistency of the oddities are kept.
- Loading branch information
1 parent
005eceb
commit a41ec94
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict' | ||
|
||
var test = require('tape').test | ||
var msgpack = require('../') | ||
|
||
test('decoding a native Map object', function (t) { | ||
var map = new Map([ | ||
[ 'hello', 'world' ] | ||
]) | ||
|
||
var expected = { hello: 'world' } | ||
|
||
var pack = msgpack() | ||
|
||
t.deepEqual(pack.decode(pack.encode(map)), expected) | ||
t.end() | ||
}) | ||
|
||
test('decoding a native Map object with Map objects inside it', function (t) { | ||
var map = new Map([ | ||
[ 'hello', 'world' ], | ||
[ 'foo', new Map([[ 'bar', 'baz' ]]) ] | ||
]) | ||
|
||
var expected = { 'hello': 'world', 'foo': { 'bar': 'baz' } } | ||
|
||
var pack = msgpack() | ||
|
||
t.deepEqual(pack.decode(pack.encode(map)), expected) | ||
t.end() | ||
}) | ||
|
||
// The decoding done in this test is not exactly correct.. | ||
// The key is decoded as a `string` rather than a `number`, but | ||
// the test has been added to preserve consistency. | ||
test('decoding a native Map object with a numeric key', function (t) { | ||
var map = new Map([ | ||
[ 1, 'world' ] | ||
]) | ||
|
||
var obj = { '1': 'world' } | ||
|
||
var pack = msgpack() | ||
|
||
t.deepEqual(pack.decode(pack.encode(map)), obj) | ||
t.end() | ||
}) | ||
|
||
// The decoding done in this next test is DEFINITELY not correct! | ||
// The two keys are encoded as two different things but are decoded | ||
// as one key, causing an overwrite. | ||
// Again, this test has been added to preserve consistency! | ||
test('decoding a native Map object with a numeric key and a string number representation', function (t) { | ||
var map = new Map([ | ||
[ 1, 'world' ], | ||
[ '1', 'hello' ] | ||
]) | ||
|
||
var obj = { '1': 'hello' } | ||
|
||
var pack = msgpack() | ||
|
||
t.notEqual(pack.encode(map), pack.encode(obj)) | ||
t.deepEqual(pack.decode(pack.encode(map)), obj) | ||
t.end() | ||
}) |