-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: complete coverage of buffer #12831
Conversation
@@ -23,3 +23,11 @@ encodings | |||
assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding), | |||
'666f6f'); | |||
}); | |||
|
|||
// invalid encodings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer the comment to start with a capital letter, but maybe it's just me, idk :)
test/parallel/test-buffer-write.js
Outdated
@@ -53,3 +53,11 @@ encodings | |||
assert.strictEqual(buf.write('666f6f', 0, len, encoding), len); | |||
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); | |||
}); | |||
|
|||
// invalid encodings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a comment.
for (let i = 1; i < 10; i++) { | ||
const encoding = String(i).repeat(i); | ||
|
||
assert.strictEqual(Buffer.byteLength('foo', encoding), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use Buffer.isEncoding()
here?
8da6576
to
3202a99
Compare
Landed in a710e44 |
PR-URL: #12831 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
PR-URL: nodejs#12831 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Should this be backported to |
Complete coverage of
lib/buffer.js
. According to https://coverage.nodejs.org/coverage-ff001c12b032c33d/root/buffer.js.html , this PR adds the coverage for some missingbreak
cases and oneutf-8
case which are not get covered in #12714 .PS: When i was writing the test cases for those invalid encodings cases, i found that the way each
Buffer
API treats the invalid value of theencoding
parameter seems quite inconsistent, for instance:Buffer.from
treats all JavaScript falsy values and all other non-string values asutf8
Buffer.from('foo', false)
equalsBuffer.from('foo', 'utf8')
Buffer.from('foo', 123)
equalsBuffer.from('foo', 'utf8')
buf.toString
only treatsundefined
asutf8
, and throws an error when comes to all other invalid valuesBuffer.from('foo').toString(undefined)
equalsBuffer.from('foo').toString('utf8')
Buffer.from('foo').toString('u')
andBuffer.from('foo').toString(0)
will throwbuf.write
just treats all JavaScript falsy value asutf8
, and throws an error when comes to all other invalid valuesBuffer.alloc(9).write('foo', 0)
equalsBuffer.alloc(9).write('foo', 'utf8')
So shall we find a consistent way to deal with this invalid encoding parameter situation to avoid some potential confusions?
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
test