Skip to content

Commit

Permalink
fix: print createFromBase64 regardless of position
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Apr 3, 2023
1 parent 9e0a86b commit 450dfca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
5 changes: 0 additions & 5 deletions src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,6 @@ export class Binary extends BSONValue {
}

inspect(): string {
if (this.position === 0) {
return this.sub_type !== Binary.BSON_BINARY_SUBTYPE_DEFAULT
? `new Binary(undefined, ${this.sub_type})`
: 'new Binary()';
}
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
}
Expand Down
37 changes: 26 additions & 11 deletions test/node/binary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,38 +81,53 @@ describe('class Binary', () => {
});

context('inspect()', () => {
it('when value is default returns "new Binary()"', () => {
expect(new Binary().inspect()).to.equal('new Binary()');
it('when value is default returns "Binary.createFromBase64("", 0)"', () => {
expect(new Binary().inspect()).to.equal('Binary.createFromBase64("", 0)');
});

it('when value is empty returns "new Binary()"', () => {
expect(new Binary(new Uint8Array(0)).inspect()).to.equal('new Binary()');
it('when value is empty returns "Binary.createFromBase64("", 0)"', () => {
expect(new Binary(new Uint8Array(0)).inspect()).to.equal('Binary.createFromBase64("", 0)');
});

it('when value is default with a subtype returns "new Binary()"', () => {
expect(new Binary(null, 0x23).inspect()).to.equal('new Binary(undefined, 35)');
it('when value is default with a subtype returns "Binary.createFromBase64("", 35)"', () => {
expect(new Binary(null, 0x23).inspect()).to.equal('Binary.createFromBase64("", 35)');
});

it('when value is empty with a subtype returns "new Binary(undefined, 35)"', () => {
expect(new Binary(new Uint8Array(0), 0x23).inspect()).to.equal('new Binary(undefined, 35)');
it('when value is empty with a subtype returns "Binary.createFromBase64("", 35)"', () => {
expect(new Binary(new Uint8Array(0), 0x23).inspect()).to.equal(
'Binary.createFromBase64("", 35)'
);
});

it('when value is empty returns "Binary.createFromBase64("", 0)"', () => {
it('when value has utf8 "abcdef" encoded returns "Binary.createFromBase64("YWJjZGVm", 0)"', () => {
expect(new Binary(Buffer.from('abcdef', 'utf8')).inspect()).to.equal(
'Binary.createFromBase64("YWJjZGVm", 0)'
);
});

context('when result is executed', () => {
it('is deep equal with a Binary that has no data', () => {
it('has a position of zero when constructed with default space', () => {
const bsonValue = new Binary();
const ctx = { ...BSON, module: { exports: { result: null } } };
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
expect(ctx.module.exports.result).to.have.property('position', 0);
expect(ctx.module.exports.result).to.have.property('sub_type', 0);

// While the default Binary has 256 bytes the newly constructed one will have 0
// both will have a position of zero so when serialized to BSON they are the equivalent.
expect(ctx.module.exports.result).to.have.nested.property('buffer.byteLength', 0);
expect(bsonValue).to.have.nested.property('buffer.byteLength', 256);
});

it('is deep equal with a Binary that has no data', () => {
const bsonValue = new Binary(new Uint8Array(0));
const ctx = { ...BSON, module: { exports: { result: null } } };
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
});

it('is deep equal with a Binary that has a subtype but no data', () => {
const bsonValue = new Binary(undefined, 0x23);
const bsonValue = new Binary(new Uint8Array(0), 0x23);
const ctx = { ...BSON, module: { exports: { result: null } } };
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
Expand Down
2 changes: 1 addition & 1 deletion test/node/bson_type_classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const BSONTypeClasses = [
];

const BSONTypeClassCtors = new Map<string, () => BSONValue>([
['Binary', () => new Binary()],
['Binary', () => new Binary(new Uint8Array(0), 0)],
['Code', () => new Code('function () {}')],
['DBRef', () => new DBRef('name', new ObjectId('00'.repeat(12)))],
['Decimal128', () => new Decimal128('1.23')],
Expand Down

0 comments on commit 450dfca

Please sign in to comment.