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