@@ -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 ) ;
0 commit comments