Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

DavidCai1111
Copy link
Member

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 #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

@nodejs-github-bot nodejs-github-bot added the test Issues and PRs related to the tests. label May 4, 2017
@DavidCai1111 DavidCai1111 added the buffer Issues and PRs related to the buffer subsystem. label May 4, 2017
@@ -23,3 +23,11 @@ encodings
assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding),
'666f6f');
});

// invalid encodings
Copy link
Contributor

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 :)

@@ -53,3 +53,11 @@ encodings
assert.strictEqual(buf.write('666f6f', 0, len, encoding), len);
assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
});

// invalid encodings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Copy link
Contributor

@cjihrig cjihrig left a 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),
Copy link
Contributor

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?

@DavidCai1111
Copy link
Member Author

@aqrln @cjihrig Updated, PTAL :=)

@DavidCai1111
Copy link
Member Author

@addaleax
Copy link
Member

addaleax commented May 7, 2017

Landed in a710e44

@addaleax addaleax closed this May 7, 2017
addaleax pushed a commit that referenced this pull request May 7, 2017
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>
anchnk pushed a commit to anchnk/node that referenced this pull request May 19, 2017
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>
@jasnell jasnell mentioned this pull request May 11, 2017
@gibfahn gibfahn mentioned this pull request Jun 15, 2017
3 tasks
@gibfahn
Copy link
Member

gibfahn commented Jun 20, 2017

Should this be backported to v6.x-staging? If yes please follow the guide and raise a backport PR, if no let me know or add the dont-land-on label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants