forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.useraw.js
More file actions
executable file
·92 lines (72 loc) · 2.45 KB
/
15.useraw.js
File metadata and controls
executable file
·92 lines (72 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env mocha -R spec
var assert = require("assert");
var msgpackJS = "../index";
var isBrowser = ("undefined" !== typeof window);
var msgpack = isBrowser && window.msgpack || require(msgpackJS);
var TITLE = __filename.replace(/^.*\//, "");
var TESTS = [0, 1, 31, 32, 255, 256, 65535, 65536];
function toArray(array) {
return Array.prototype.slice.call(array);
}
describe(TITLE, function() {
var options;
it("useraw (decode)", function() {
options = {codec: msgpack.createCodec({useraw: true})};
// raw
assert.deepEqual(toArray(msgpack.decode(new Buffer([0xa1, 65]), options)), [65]);
// str
assert.equal(msgpack.decode(new Buffer([0xa1, 65])), "A");
});
it("useraw (encode)", function() {
// raw (String)
assert.deepEqual(toArray(msgpack.encode("A", options)), [0xa1, 65]);
// raw (Buffer)
assert.deepEqual(toArray(msgpack.encode(new Buffer([65]), options)), [0xa1, 65]);
// str
assert.deepEqual(toArray(msgpack.encode("A")), [0xa1, 65]);
});
it("useraw (String)", function() {
TESTS.forEach(test);
function test(length) {
var source = "";
for (var i = 0; i < length; i++) {
source += "a";
}
// encode as raw
var encoded = msgpack.encode(source, options);
assert.ok(encoded.length);
// decode as raw (Buffer)
var buffer = msgpack.decode(encoded, options);
assert.ok(Buffer.isBuffer(buffer));
assert.equal(buffer.length, length);
if (length) assert.equal(buffer[0], 97);
// decode as str (String)
var string = msgpack.decode(encoded);
assert.equal(typeof string, "string");
assert.equal(string.length, length);
assert.equal(string, source);
}
});
it("useraw (Buffer)", function() {
TESTS.forEach(test);
function test(length) {
var source = new Buffer(length);
for (var i = 0; i < length; i++) {
source[i] = 65; // "A"
}
// encode as raw
var encoded = msgpack.encode(source, options);
assert.ok(encoded.length);
// decode as raw (Buffer)
var buffer = msgpack.decode(encoded, options);
assert.ok(Buffer.isBuffer(buffer));
assert.equal(buffer.length, length);
if (length) assert.equal(buffer[0], 65);
// decode as str (String)
var string = msgpack.decode(encoded);
assert.equal(typeof string, "string");
assert.equal(string.length, length);
if (length) assert.equal(string[0], "A");
}
});
});