Skip to content

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 as utf8
    • Buffer.from('foo', false) equals Buffer.from('foo', 'utf8')
    • Buffer.from('foo', 123) equals Buffer.from('foo', 'utf8')
  • buf.toString only treats undefined as utf8, and throws an error when comes to all other invalid values
    • Buffer.from('foo').toString(undefined) equals Buffer.from('foo').toString('utf8')
    • Buffer.from('foo').toString('u') and Buffer.from('foo').toString(0) will throw
  • buf.write just treats all JavaScript falsy value as utf8, and throws an error when comes to all other invalid values
    • Buffer.alloc(9).write('foo', 0) equals Buffer.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), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

test

Merge request reports

Loading