UNPKG

4.33 kBJavaScriptView Raw
1#!/usr/bin/env mocha -R spec
2
3var assert = require("assert");
4
5var msgpackJS = "../index";
6var isBrowser = ("undefined" !== typeof window);
7var msgpack = isBrowser && window.msgpack || require(msgpackJS);
8var TITLE = __filename.replace(/^.*\//, "") + ":";
9
10describe(TITLE, function() {
11
12 // positive fixint -- 0x00 - 0x7f
13 it("00-7f: positive fixint", function() {
14 assert.deepEqual(toArray(msgpack.encode(0x00)), [0x00]);
15 assert.deepEqual(toArray(msgpack.encode(0x7F)), [0x7F]);
16 });
17
18 // fixmap -- 0x80 - 0x8f
19 it("80-8f: fixmap", function() {
20 assert.deepEqual(toArray(msgpack.encode({})), [0x80]);
21
22 var map = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15};
23 var array = [0x8F];
24 Object.keys(map).forEach(function(key) {
25 array.push(0xa1, key.charCodeAt(0), map[key]);
26 });
27 assert.deepEqual(toArray(msgpack.encode(map)), array);
28 });
29
30 // fixarray -- 0x90 - 0x9f
31 it("90-9f: fixarray", function() {
32 assert.deepEqual(toArray(msgpack.encode([])), [0x90]);
33
34 var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
35 assert.deepEqual(toArray(msgpack.encode(array)), concat([0x9F], array));
36 });
37
38 // fixstr -- 0xa0 - 0xbf
39 it("a0-bf: fixstr", function() {
40 assert.deepEqual(toArray(msgpack.encode("")), [0xa0]);
41
42 var str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
43 assert.deepEqual(toArray(msgpack.encode(str)), concat([0xBF], Buffer(str)));
44 });
45
46 // nil -- 0xc0
47 it("c0: nil", function() {
48 assert.deepEqual(toArray(msgpack.encode(null)), [0xc0]);
49 });
50
51 // false -- 0xc2
52 // true -- 0xc3
53 it("c2-c3: boolean", function() {
54 assert.deepEqual(toArray(msgpack.encode(false)), [0xc2]);
55 assert.deepEqual(toArray(msgpack.encode(true)), [0xc3]);
56 });
57
58 // bin 8 -- 0xc4
59 // bin 16 -- 0xc5
60 // bin 32 -- 0xc6
61 it("c4-c6: bin 8/16/32", function() {
62 var bin;
63 bin = Buffer(1);
64 bin.fill(0);
65 assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc4, 1], bin));
66
67 bin = Buffer(256);
68 bin.fill(0);
69 assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc5, 1, 0], bin));
70
71 bin = Buffer(65536);
72 bin.fill(0);
73 assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc6, 0, 1, 0, 0], bin));
74 });
75
76 // float 32 -- 0xca -- NOT SUPPORTED
77 // float 64 -- 0xcb
78 it("ca-cb: float 32/64", function() {
79 assert.deepEqual(toArray(msgpack.encode(0.5)), [0xcb, 63, 224, 0, 0, 0, 0, 0, 0]);
80 });
81
82 // uint 8 -- 0xcc
83 // uint 16 -- 0xcd
84 // uint 32 -- 0xce
85 // uint 64 -- 0xcf -- NOT SUPPORTED
86 it("cc-cf: uint 8/16/32/64", function() {
87 assert.deepEqual(toArray(msgpack.encode(0xFF)), [0xcc, 0xFF]);
88 assert.deepEqual(toArray(msgpack.encode(0xFFFF)), [0xcd, 0xFF, 0xFF]);
89 assert.deepEqual(toArray(msgpack.encode(0x7FFFFFFF)), [0xce, 0x7F, 0xFF, 0xFF, 0xFF]);
90 });
91
92 // int 8 -- 0xd0
93 // int 16 -- 0xd1
94 // int 32 -- 0xd2
95 // int 64 -- 0xd3 -- NOT SUPPORTED
96 it("d0-d3: int 8/16/32/64", function() {
97 assert.deepEqual(toArray(msgpack.encode(-0x80)), [0xd0, 0x80]);
98 assert.deepEqual(toArray(msgpack.encode(-0x8000)), [0xd1, 0x80, 0x00]);
99 assert.deepEqual(toArray(msgpack.encode(-0x80000000)), [0xd2, 0x80, 0x00, 0x00, 0x00]);
100 });
101
102 // str 8 -- 0xd9
103 // str 16 -- 0xda
104 // str 32 -- 0xdb
105 it("d9-db: str 8/16/32", function() {
106 this.timeout(10000);
107 var str, src = "a";
108 for (var i = 0; i < 17; i++) src += src;
109
110 str = src.substr(0, 0xFF);
111 assert.deepEqual(toArray(msgpack.encode(str)), concat([0xd9, 0xFF], Buffer(str)));
112
113 str = src.substr(0, 0x0100);
114 assert.deepEqual(toArray(msgpack.encode(str)), concat([0xda, 0x01, 0x00], Buffer(str)));
115
116 str = src.substr(0, 0xFFFF);
117 assert.deepEqual(toArray(msgpack.encode(str)), concat([0xda, 0xFF, 0xFF], Buffer(str)));
118
119 str = src.substr(0, 0x010000);
120 assert.deepEqual(toArray(msgpack.encode(str)), concat([0xdb, 0x00, 0x01, 0x00, 0x00], Buffer(str)));
121 });
122
123 // negative fixint -- 0xe0 - 0xff
124 it("e0-ff: negative fixint", function() {
125 assert.deepEqual(toArray(msgpack.encode(-1)), [0xFF]);
126 assert.deepEqual(toArray(msgpack.encode(-32)), [0xE0]);
127 });
128});
129
130function toArray(buffer) {
131 return Array.prototype.slice.call(buffer);
132}
133
134function concat(buf) {
135 return Array.prototype.concat.apply([], Array.prototype.map.call(arguments, toArray));
136}
\No newline at end of file