test: complete coverage of buffer
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 missing break
cases and one utf-8
case which are not get covered in https://github.com/nodejs/node/pull/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 the encoding
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 values-
Buffer.from('foo').toString(undefined)
equalsBuffer.from('foo').toString('utf8')
-
Buffer.from('foo').toString('u')
andBuffer.from('foo').toString(0)
will throw
-
-
buf.write
just treats all JavaScript falsy value asutf8
, and throws an error when comes to all other invalid values-
Buffer.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) passes -
tests and/or benchmarks are included -
commit message follows commit guidelines
Affected core subsystem(s)
test