@@ -144,4 +144,68 @@ describe('class Binary', () => {
144
144
} ) ;
145
145
} ) ;
146
146
} ) ;
147
+
148
+ context ( 'toString()' , ( ) => {
149
+ context ( 'when case is UTF8 (default)' , ( ) => {
150
+ it ( 'should respect position when converting to string' , ( ) => {
151
+ const bin = new Binary ( ) ;
152
+ expect ( bin . toString ( ) ) . to . equal ( '' ) ;
153
+ bin . put ( 1 ) ;
154
+ expect ( bin . toString ( ) ) . to . equal ( '\u0001' ) ;
155
+ } ) ;
156
+ it ( 'should remain same after round trip' , ( ) => {
157
+ const bin = new BSON . Binary ( ) ;
158
+ const serializedBin = BSON . serialize ( { bin } ) ;
159
+ const roundTrippedBin = BSON . deserialize ( serializedBin ) ;
160
+ expect ( roundTrippedBin . bin . toString ( ) ) . to . equal ( bin . toString ( ) ) ;
161
+ } ) ;
162
+ } ) ;
163
+
164
+ context ( 'when case is hex' , ( ) => {
165
+ it ( 'should respect position when converting to string' , ( ) => {
166
+ const bin = new Binary ( ) ;
167
+ expect ( bin . toString ( 'hex' ) ) . to . equal ( '' ) ;
168
+ bin . put ( 1 ) ;
169
+ expect ( bin . toString ( 'hex' ) ) . to . equal ( '01' ) ;
170
+ } ) ;
171
+ it ( 'should remain same after round trip' , ( ) => {
172
+ const bin = new BSON . Binary ( ) ;
173
+ const serializedBin = BSON . serialize ( { bin } ) ;
174
+ const roundTrippedBin = BSON . deserialize ( serializedBin ) ;
175
+ expect ( roundTrippedBin . bin . toString ( 'hex' ) ) . to . equal ( bin . toString ( 'hex' ) ) ;
176
+ } ) ;
177
+ } ) ;
178
+
179
+ context ( 'when case is base64' , ( ) => {
180
+ it ( 'should respect position when converting to string' , ( ) => {
181
+ const bin = new Binary ( ) ;
182
+ expect ( bin . toString ( 'base64' ) ) . to . equal ( '' ) ;
183
+ bin . put ( 1 ) ;
184
+ expect ( bin . toString ( 'base64' ) ) . to . equal ( 'AQ==' ) ;
185
+ } ) ;
186
+ it ( 'should remain same after round trip' , ( ) => {
187
+ const bin = new BSON . Binary ( ) ;
188
+ const serializedBin = BSON . serialize ( { bin } ) ;
189
+ const roundTrippedBin = BSON . deserialize ( serializedBin ) ;
190
+ expect ( roundTrippedBin . bin . toString ( 'base64' ) ) . to . equal ( bin . toString ( ) ) ;
191
+ } ) ;
192
+ } ) ;
193
+ } ) ;
194
+
195
+ context ( 'toJSON()' , ( ) => {
196
+ it ( 'should respect position when converting to JSON' , ( ) => {
197
+ const bin = new Binary ( ) ;
198
+ expect ( bin . toJSON ( ) ) . to . equal ( '' ) ;
199
+ bin . put ( 1 ) ;
200
+ // toJSON uses base64
201
+ expect ( bin . toJSON ( ) ) . to . equal ( 'AQ==' ) ;
202
+ } ) ;
203
+
204
+ it ( 'should remain same after round trip' , ( ) => {
205
+ const bin = new BSON . Binary ( ) ;
206
+ const serializedBin = BSON . serialize ( { bin } ) ;
207
+ const roundTrippedBin = BSON . deserialize ( serializedBin ) ;
208
+ expect ( roundTrippedBin . bin . toJSON ( ) ) . to . equal ( bin . toJSON ( ) ) ;
209
+ } ) ;
210
+ } ) ;
147
211
} ) ;
0 commit comments