Skip to content

Commit c723d7a

Browse files
joyeecheungitaloacasas
authored andcommitted
buffer, test: add tests for buffer.toJSON
* Pull out the tests for buffer.toJSON from test-buffer-alloc * Add tests for serializing a 0-length buffer PR-URL: #10979 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e9f6bc6 commit c723d7a

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

test/parallel/test-buffer-alloc.js

-24
Original file line numberDiff line numberDiff line change
@@ -767,30 +767,6 @@ assert.strictEqual(Buffer.from('13.37').length, 5);
767767
// issue GH-3416
768768
Buffer.from(Buffer.allocUnsafe(0), 0, 0);
769769

770-
// GH-5110
771-
{
772-
const buffer = Buffer.from('test');
773-
const string = JSON.stringify(buffer);
774-
775-
assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}');
776-
777-
assert.deepStrictEqual(buffer, JSON.parse(string, (key, value) => {
778-
return value && value.type === 'Buffer' ?
779-
Buffer.from(value.data) :
780-
value;
781-
}));
782-
}
783-
784-
// issue GH-7849
785-
{
786-
const buf = Buffer.from('test');
787-
const json = JSON.stringify(buf);
788-
const obj = JSON.parse(json);
789-
const copy = Buffer.from(obj);
790-
791-
assert(buf.equals(copy));
792-
}
793-
794770
// issue GH-4331
795771
assert.throws(() => Buffer.allocUnsafe(0xFFFFFFFF), RangeError);
796772
assert.throws(() => Buffer.allocUnsafe(0xFFFFFFFFF), RangeError);

test/parallel/test-buffer-tojson.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const Buffer = require('buffer').Buffer;
6+
7+
{
8+
assert.strictEqual(JSON.stringify(Buffer.alloc(0)),
9+
'{"type":"Buffer","data":[]}');
10+
assert.strictEqual(JSON.stringify(Buffer.from([1, 2, 3, 4])),
11+
'{"type":"Buffer","data":[1,2,3,4]}');
12+
}
13+
14+
// issue GH-7849
15+
{
16+
const buf = Buffer.from('test');
17+
const json = JSON.stringify(buf);
18+
const obj = JSON.parse(json);
19+
const copy = Buffer.from(obj);
20+
21+
assert.deepStrictEqual(buf, copy);
22+
}
23+
24+
// GH-5110
25+
{
26+
const buffer = Buffer.from('test');
27+
const string = JSON.stringify(buffer);
28+
29+
assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}');
30+
31+
function receiver(key, value) {
32+
return value && value.type === 'Buffer' ? Buffer.from(value.data) : value;
33+
}
34+
35+
assert.deepStrictEqual(buffer, JSON.parse(string, receiver));
36+
}

0 commit comments

Comments
 (0)