Skip to content

Commit b7c3858

Browse files
committed
chore: fix required for getNumber
1 parent 9e9f265 commit b7c3858

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/cmap/wire_protocol/on_demand/document.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,11 @@ export class OnDemandDocument {
261261
return this.bson[offset] ? 1 : 0;
262262
}
263263

264-
return null;
264+
if (required === true) {
265+
throw new BSONError(`BSON element "${name}" does not have numeric type: ${type}`);
266+
} else {
267+
return null;
268+
}
265269
}
266270

267271
/**

test/unit/cmap/wire_protocol/on_demand/document.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ describe('class OnDemandDocument', () => {
185185
int: 1,
186186
long: 2n,
187187
double: 2.3,
188-
bool: false
188+
bool: false,
189+
string: 'abc'
189190
};
190191

191192
beforeEach(async function () {
@@ -202,6 +203,25 @@ describe('class OnDemandDocument', () => {
202203
expect(() => document.getNumber('blah!', true)).to.throw(BSONError);
203204
});
204205

206+
it('throws if required is set to true and element is not numeric', () => {
207+
// just making sure this test does not fail for the non-exist reason
208+
expect(document.hasElement('string')).to.be.true;
209+
expect(() => document.getNumber('string', true)).to.throw(BSONError);
210+
});
211+
212+
it('returns null if required is set to false and element does not exist', () => {
213+
expect(document.getNumber('blah!', false)).to.be.null;
214+
expect(document.getNumber('blah!')).to.be.null;
215+
});
216+
217+
it('returns null if required is set to false and element is not numeric', () => {
218+
// just making sure this test does not fail for the non-exist reason
219+
expect(document.hasElement('string')).to.be.true;
220+
221+
expect(document.getNumber('string', false)).to.be.null;
222+
expect(document.getNumber('string')).to.be.null;
223+
});
224+
205225
it('supports parsing int', () => {
206226
expect(document.getNumber('int')).to.equal(1);
207227
});

0 commit comments

Comments
 (0)