Skip to content

Commit f3dc6a3

Browse files
committed
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: nodejs#10979 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 56f4f09 commit f3dc6a3

2 files changed

Lines changed: 36 additions & 24 deletions

File tree

test/parallel/test-buffer-alloc.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -769,30 +769,6 @@ assert.strictEqual(Buffer.from('13.37').length, 5);
769769
// issue GH-3416
770770
Buffer.from(Buffer.allocUnsafe(0), 0, 0);
771771

772-
// GH-5110
773-
{
774-
const buffer = Buffer.from('test');
775-
const string = JSON.stringify(buffer);
776-
777-
assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}');
778-
779-
assert.deepStrictEqual(buffer, JSON.parse(string, (key, value) => {
780-
return value && value.type === 'Buffer' ?
781-
Buffer.from(value.data) :
782-
value;
783-
}));
784-
}
785-
786-
// issue GH-7849
787-
{
788-
const buf = Buffer.from('test');
789-
const json = JSON.stringify(buf);
790-
const obj = JSON.parse(json);
791-
const copy = Buffer.from(obj);
792-
793-
assert(buf.equals(copy));
794-
}
795-
796772
// issue GH-5587
797773
assert.throws(() => Buffer.alloc(8).writeFloatLE(0, 5), RangeError);
798774
assert.throws(() => Buffer.alloc(16).writeDoubleLE(0, 9), RangeError);
Lines changed: 36 additions & 0 deletions
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)