From 476497e3386a4ad0b38aa3d1dd6bcd592aafecab Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Wed, 20 Mar 2024 17:02:06 -0400 Subject: [PATCH 1/5] added unit tests and fixed functionality --- src/binary.ts | 4 ++-- test/node/binary.test.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/binary.ts b/src/binary.ts index 84182fb2..b93b17f8 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -193,8 +193,8 @@ export class Binary extends BSONValue { if (encoding === 'hex') return ByteUtils.toHex(this.buffer); if (encoding === 'base64') return ByteUtils.toBase64(this.buffer); if (encoding === 'utf8' || encoding === 'utf-8') - return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false); - return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false); + return ByteUtils.toUTF8(this.buffer, 0, this.position, false); + return ByteUtils.toUTF8(this.buffer, 0, this.position, false); } /** @internal */ diff --git a/test/node/binary.test.ts b/test/node/binary.test.ts index 1e0b2bd6..b5abe5c8 100644 --- a/test/node/binary.test.ts +++ b/test/node/binary.test.ts @@ -144,4 +144,20 @@ describe('class Binary', () => { }); }); }); + + context('toString()', () => { + it('should respect position when converting toUTF8 (default)', () => { + const bin = new Binary(); + expect(bin.toString()).to.equal(''); + bin.put(1); + expect(bin.toString()).to.equal('\u0001'); + }); + it('should remain same after round trip', () => { + const bin = new BSON.Binary(); + bin.toString() + const serializedBin = BSON.serialize({ bin }); + const roundTrippedBin = BSON.deserialize(serializedBin); + expect(roundTrippedBin.bin.toString()).to.equal(bin.toString()); + }); + }); }); From 5d2ed5ced6f30e2dd8b2daeced22d1144e7ab03d Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Wed, 20 Mar 2024 17:08:52 -0400 Subject: [PATCH 2/5] lint fix --- test/node/binary.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/node/binary.test.ts b/test/node/binary.test.ts index b5abe5c8..7433f65b 100644 --- a/test/node/binary.test.ts +++ b/test/node/binary.test.ts @@ -154,7 +154,7 @@ describe('class Binary', () => { }); it('should remain same after round trip', () => { const bin = new BSON.Binary(); - bin.toString() + bin.toString(); const serializedBin = BSON.serialize({ bin }); const roundTrippedBin = BSON.deserialize(serializedBin); expect(roundTrippedBin.bin.toString()).to.equal(bin.toString()); From 71b278584c7ad1392b36631c9167db8ed47a4fda Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Mon, 25 Mar 2024 15:41:43 -0400 Subject: [PATCH 3/5] expanded cases and toJSON --- src/binary.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/binary.ts b/src/binary.ts index b93b17f8..6a892b00 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -186,12 +186,12 @@ export class Binary extends BSONValue { } toJSON(): string { - return ByteUtils.toBase64(this.buffer); + return ByteUtils.toBase64(this.buffer.subarray(0, this.position)); } toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string { - if (encoding === 'hex') return ByteUtils.toHex(this.buffer); - if (encoding === 'base64') return ByteUtils.toBase64(this.buffer); + if (encoding === 'hex') return ByteUtils.toHex(this.buffer.subarray(0, this.position)); + if (encoding === 'base64') return ByteUtils.toBase64(this.buffer.subarray(0, this.position)); if (encoding === 'utf8' || encoding === 'utf-8') return ByteUtils.toUTF8(this.buffer, 0, this.position, false); return ByteUtils.toUTF8(this.buffer, 0, this.position, false); From 26e8b88157a4dfe9d92cf48888aff999a0797508 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Mon, 25 Mar 2024 15:48:12 -0400 Subject: [PATCH 4/5] added unit tests for expanded cases --- test/node/binary.test.ts | 60 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/test/node/binary.test.ts b/test/node/binary.test.ts index 7433f65b..d5abfb2f 100644 --- a/test/node/binary.test.ts +++ b/test/node/binary.test.ts @@ -145,19 +145,67 @@ describe('class Binary', () => { }); }); - context('toString()', () => { - it('should respect position when converting toUTF8 (default)', () => { + context.only('toString()', () => { + context('when case is UTF8 (default)', () => { + it('should respect position when converting to string', () => { + const bin = new Binary(); + expect(bin.toString()).to.equal(''); + bin.put(1); + expect(bin.toString()).to.equal('\u0001'); + }); + it('should remain same after round trip', () => { + const bin = new BSON.Binary(); + const serializedBin = BSON.serialize({ bin }); + const roundTrippedBin = BSON.deserialize(serializedBin); + expect(roundTrippedBin.bin.toString()).to.equal(bin.toString()); + }); + }); + + context('when case is hex', () => { + it('should respect position when converting to string', () => { + const bin = new Binary(); + expect(bin.toString('hex')).to.equal(''); + bin.put(1); + expect(bin.toString('hex')).to.equal('01'); + }); + it('should remain same after round trip', () => { + const bin = new BSON.Binary(); + const serializedBin = BSON.serialize({ bin }); + const roundTrippedBin = BSON.deserialize(serializedBin); + expect(roundTrippedBin.bin.toString('hex')).to.equal(bin.toString('hex')); + }); + }); + + context('when case is base64', () => { + it('should respect position when converting to string', () => { + const bin = new Binary(); + expect(bin.toString('base64')).to.equal(''); + bin.put(1); + expect(bin.toString('base64')).to.equal('AQ=='); + }); + it('should remain same after round trip', () => { + const bin = new BSON.Binary(); + const serializedBin = BSON.serialize({ bin }); + const roundTrippedBin = BSON.deserialize(serializedBin); + expect(roundTrippedBin.bin.toString('base64')).to.equal(bin.toString()); + }); + }); + }); + + context.only('toJSON()', () => { + it('should respect position when converting to JSON', () => { const bin = new Binary(); - expect(bin.toString()).to.equal(''); + expect(bin.toJSON()).to.equal(''); bin.put(1); - expect(bin.toString()).to.equal('\u0001'); + // toJSON uses base64 + expect(bin.toJSON()).to.equal('AQ=='); }); + it('should remain same after round trip', () => { const bin = new BSON.Binary(); - bin.toString(); const serializedBin = BSON.serialize({ bin }); const roundTrippedBin = BSON.deserialize(serializedBin); - expect(roundTrippedBin.bin.toString()).to.equal(bin.toString()); + expect(roundTrippedBin.bin.toJSON()).to.equal(bin.toJSON()); }); }); }); From 39a2003465048906b70ccc2e1e4bb2f731b28475 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Mon, 25 Mar 2024 15:49:33 -0400 Subject: [PATCH 5/5] lint fix --- test/node/binary.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/node/binary.test.ts b/test/node/binary.test.ts index d5abfb2f..2adffc79 100644 --- a/test/node/binary.test.ts +++ b/test/node/binary.test.ts @@ -145,7 +145,7 @@ describe('class Binary', () => { }); }); - context.only('toString()', () => { + context('toString()', () => { context('when case is UTF8 (default)', () => { it('should respect position when converting to string', () => { const bin = new Binary(); @@ -192,7 +192,7 @@ describe('class Binary', () => { }); }); - context.only('toJSON()', () => { + context('toJSON()', () => { it('should respect position when converting to JSON', () => { const bin = new Binary(); expect(bin.toJSON()).to.equal('');