test: complete coverage of buffer#12831
Closed
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 missingbreakcases and oneutf-8case 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
BufferAPI treats the invalid value of theencodingparameter seems quite inconsistent, for instance:Buffer.fromtreats all JavaScript falsy values and all other non-string values asutf8Buffer.from('foo', false)equalsBuffer.from('foo', 'utf8')Buffer.from('foo', 123)equalsBuffer.from('foo', 'utf8')buf.toStringonly treatsundefinedasutf8, 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.writejust 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