Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
♻️ Fix test format and message check
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Feb 22, 2018
1 parent 11e2f25 commit 457b7e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cryptography/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const bufferToHex = buffer => naclInstance.to_hex(buffer);
const hexRegex = /^[0-9a-f]+/i;
export const hexToBuffer = hex => {
if (typeof hex !== 'string') {
throw new TypeError('Argument must be a string');
throw new TypeError('Argument must be a string.');
}
const matchedHex = (hex.match(hexRegex) || [])[0];
if (!matchedHex) {
Expand Down
19 changes: 13 additions & 6 deletions test/cryptography/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,39 @@ describe('convert', () => {
const buffer = hexToBuffer(defaultHex);
return buffer.should.be.eql(defaultBuffer);
});

it('should throw TypeError with number', () => {
return (() => hexToBuffer(123)).should.throw(
new TypeError('argument must be string'),
);
return hexToBuffer
.bind(null, 123)
.should.throw(TypeError, { message: 'Argument must be a string.' });
});

it('should throw TypeError with object', () => {
return (() => hexToBuffer({})).should.throw(
new TypeError('argument must be string'),
);
return hexToBuffer
.bind(null, {})
.should.throw(TypeError, { message: 'Argument must be a string.' });
});

it('should create empty Buffer from non hex string', () => {
const buffer = hexToBuffer('yKJh');
return buffer.should.be.eql(Buffer.alloc(0));
});

it('should create partial Buffer from partially non hex string', () => {
const buffer = hexToBuffer('Abxzzzz');
return buffer.should.be.eql(Buffer.from('Ab', 'hex'));
});

it('should create partial Buffer with only first valid hex string', () => {
const buffer = hexToBuffer('Abxzzab');
return buffer.should.be.eql(Buffer.from('Ab', 'hex'));
});

it('should create even numbered Buffer from odd number hex string with invalid hex', () => {
const buffer = hexToBuffer('123xxxx');
return buffer.should.be.eql(Buffer.from('12', 'hex'));
});

it('should create even numbered Buffer from odd number hex string', () => {
const buffer = hexToBuffer('c3a5c3a4c3b6a');
return buffer.should.be.eql(Buffer.from('c3a5c3a4c3b6', 'hex'));
Expand Down

0 comments on commit 457b7e7

Please sign in to comment.