Skip to content

Commit 450dfca

Browse files
committed
fix: print createFromBase64 regardless of position
1 parent 9e0a86b commit 450dfca

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

src/binary.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,6 @@ export class Binary extends BSONValue {
302302
}
303303

304304
inspect(): string {
305-
if (this.position === 0) {
306-
return this.sub_type !== Binary.BSON_BINARY_SUBTYPE_DEFAULT
307-
? `new Binary(undefined, ${this.sub_type})`
308-
: 'new Binary()';
309-
}
310305
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
311306
return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
312307
}

test/node/binary.test.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,38 +81,53 @@ describe('class Binary', () => {
8181
});
8282

8383
context('inspect()', () => {
84-
it('when value is default returns "new Binary()"', () => {
85-
expect(new Binary().inspect()).to.equal('new Binary()');
84+
it('when value is default returns "Binary.createFromBase64("", 0)"', () => {
85+
expect(new Binary().inspect()).to.equal('Binary.createFromBase64("", 0)');
8686
});
8787

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

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

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

100-
it('when value is empty returns "Binary.createFromBase64("", 0)"', () => {
102+
it('when value has utf8 "abcdef" encoded returns "Binary.createFromBase64("YWJjZGVm", 0)"', () => {
101103
expect(new Binary(Buffer.from('abcdef', 'utf8')).inspect()).to.equal(
102104
'Binary.createFromBase64("YWJjZGVm", 0)'
103105
);
104106
});
105107

106108
context('when result is executed', () => {
107-
it('is deep equal with a Binary that has no data', () => {
109+
it('has a position of zero when constructed with default space', () => {
108110
const bsonValue = new Binary();
109111
const ctx = { ...BSON, module: { exports: { result: null } } };
110112
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
113+
expect(ctx.module.exports.result).to.have.property('position', 0);
114+
expect(ctx.module.exports.result).to.have.property('sub_type', 0);
115+
116+
// While the default Binary has 256 bytes the newly constructed one will have 0
117+
// both will have a position of zero so when serialized to BSON they are the equivalent.
118+
expect(ctx.module.exports.result).to.have.nested.property('buffer.byteLength', 0);
119+
expect(bsonValue).to.have.nested.property('buffer.byteLength', 256);
120+
});
121+
122+
it('is deep equal with a Binary that has no data', () => {
123+
const bsonValue = new Binary(new Uint8Array(0));
124+
const ctx = { ...BSON, module: { exports: { result: null } } };
125+
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
111126
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
112127
});
113128

114129
it('is deep equal with a Binary that has a subtype but no data', () => {
115-
const bsonValue = new Binary(undefined, 0x23);
130+
const bsonValue = new Binary(new Uint8Array(0), 0x23);
116131
const ctx = { ...BSON, module: { exports: { result: null } } };
117132
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
118133
expect(ctx.module.exports.result).to.deep.equal(bsonValue);

test/node/bson_type_classes.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const BSONTypeClasses = [
3737
];
3838

3939
const BSONTypeClassCtors = new Map<string, () => BSONValue>([
40-
['Binary', () => new Binary()],
40+
['Binary', () => new Binary(new Uint8Array(0), 0)],
4141
['Code', () => new Code('function () {}')],
4242
['DBRef', () => new DBRef('name', new ObjectId('00'.repeat(12)))],
4343
['Decimal128', () => new Decimal128('1.23')],

0 commit comments

Comments
 (0)