Skip to content

Commit

Permalink
chore: add symbol coverage and error case
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Dec 16, 2024
1 parent 3f22a32 commit 4443177
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/parser/calculate_size.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Binary } from '../binary';
import type { Document } from '../bson';
import { BSONVersionError } from '../error';
import { BSONError, BSONVersionError } from '../error';
import * as constants from '../constants';
import { ByteUtils } from '../utils/byte_utils';
import { isAnyArrayBuffer, isDate, isRegExp } from './utils';
Expand Down Expand Up @@ -208,6 +208,10 @@ function calculateElement(
return 0;
case 'bigint':
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
case 'symbol':
return 0;
default:
throw new BSONError(`Unrecognized JS type: ${typeof value}`);
}

return 0;
Expand Down
8 changes: 7 additions & 1 deletion test/node/parser/calculate_size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ describe('calculateSize()', () => {
).to.throw(BSONVersionError, /Unsupported BSON version/i);
});

it('returns the correct size for a bigint value', function () {
it('returns 8 bytes (+7 meta bytes) size for a bigint value', function () {
const doc = { a: BigInt(1) };
expect(BSON.calculateObjectSize(doc)).to.equal(16);
expect(BSON.calculateObjectSize(doc)).to.equal(BSON.serialize(doc).byteLength);
});

it('returns 0 bytes (+5 meta bytes) for a symbol value', function () {
const doc = { a: Symbol() };
expect(BSON.calculateObjectSize(doc)).to.equal(5);
expect(BSON.calculateObjectSize(doc)).to.equal(BSON.serialize(doc).byteLength);
});
});

0 comments on commit 4443177

Please sign in to comment.