-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase coverage of lib/buffer.js. PR-URL: #12714 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
1 parent
133fb0c
commit 04796ee
Showing
3 changed files
with
95 additions
and
6 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,25 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
// utf8, ucs2, ascii, latin1, utf16le | ||
const encodings = ['utf8', 'ucs2', 'ucs-2', 'ascii', 'latin1', 'binary', | ||
'utf16le', 'utf-16le']; | ||
|
||
encodings | ||
.reduce((es, e) => es.concat(e, e.toUpperCase()), []) | ||
.forEach((encoding) => { | ||
assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo'); | ||
}); | ||
|
||
// base64 | ||
['base64', 'BASE64'].forEach((encoding) => { | ||
assert.strictEqual(Buffer.from('Zm9v', encoding).toString(encoding), 'Zm9v'); | ||
}); | ||
|
||
// hex | ||
['hex', 'HEX'].forEach((encoding) => { | ||
assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding), | ||
'666f6f'); | ||
}); |
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,55 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const outsideBounds = /^RangeError: Attempt to write outside buffer bounds$/; | ||
|
||
assert.throws(() => Buffer.alloc(9).write('foo', -1), outsideBounds); | ||
assert.throws(() => Buffer.alloc(9).write('foo', 10), outsideBounds); | ||
|
||
const resultMap = new Map([ | ||
['utf8', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], | ||
['ucs2', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])], | ||
['ascii', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], | ||
['latin1', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], | ||
['binary', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], | ||
['utf16le', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])], | ||
['base64', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])], | ||
['hex', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])] | ||
]); | ||
|
||
// utf8, ucs2, ascii, latin1, utf16le | ||
const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', | ||
'binary', 'utf16le', 'utf-16le']; | ||
|
||
encodings | ||
.reduce((es, e) => es.concat(e, e.toUpperCase()), []) | ||
.forEach((encoding) => { | ||
const buf = Buffer.alloc(9); | ||
const len = Buffer.byteLength('foo', encoding); | ||
assert.strictEqual(buf.write('foo', 0, len, encoding), len); | ||
|
||
if (encoding.indexOf('-') !== -1) | ||
encoding = encoding.replace('-', ''); | ||
|
||
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); | ||
}); | ||
|
||
// base64 | ||
['base64', 'BASE64'].forEach((encoding) => { | ||
const buf = Buffer.alloc(9); | ||
const len = Buffer.byteLength('Zm9v', encoding); | ||
|
||
assert.strictEqual(buf.write('Zm9v', 0, len, encoding), len); | ||
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); | ||
}); | ||
|
||
// hex | ||
['hex', 'HEX'].forEach((encoding) => { | ||
const buf = Buffer.alloc(9); | ||
const len = Buffer.byteLength('666f6f', encoding); | ||
|
||
assert.strictEqual(buf.write('666f6f', 0, len, encoding), len); | ||
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); | ||
}); |