@@ -125,6 +125,120 @@ describe('serialize( obj )', function () {
125
125
126
126
expect ( obj . hello ( ) ) . to . equal ( true ) ;
127
127
} ) ;
128
+
129
+ it ( 'should serialize functions that contain dates' , function ( ) {
130
+ function fn ( arg1 ) { return new Date ( '2016-04-28T22:02:17.156Z' ) } ;
131
+ expect ( serialize ( fn ) ) . to . be . a ( 'string' ) . equal ( 'function fn(arg1) {return new Date(\'2016-04-28T22:02:17.156Z\')}' ) ;
132
+ } ) ;
133
+
134
+ it ( 'should deserialize functions that contain dates' , function ( ) {
135
+ var fn ; eval ( 'fn = ' + serialize ( function ( ) { return new Date ( '2016-04-28T22:02:17.156Z' ) } ) ) ;
136
+ expect ( fn ) . to . be . a ( 'function' ) ;
137
+ expect ( fn ( ) . getTime ( ) ) . to . equal ( new Date ( '2016-04-28T22:02:17.156Z' ) . getTime ( ) ) ;
138
+ } ) ;
139
+
140
+ it ( 'should serialize functions that return other functions' , function ( ) {
141
+ function fn ( ) { return function ( arg1 ) { return arg1 + 5 } } ;
142
+ expect ( serialize ( fn ) ) . to . be . a ( 'string' ) . equal ( 'function fn() {return function(arg1) {return arg1 + 5}}' ) ;
143
+ } ) ;
144
+
145
+ it ( 'should deserialize functions that return other functions' , function ( ) {
146
+ var fn ; eval ( 'fn = ' + serialize ( function ( ) { return function ( arg1 ) { return arg1 + 5 } } ) ) ;
147
+ expect ( fn ) . to . be . a ( 'function' ) ;
148
+ expect ( fn ( ) ( 7 ) ) . to . equal ( 12 ) ;
149
+ } ) ;
150
+ } ) ;
151
+
152
+ describe ( 'arrow-functions' , function ( ) {
153
+ it ( 'should serialize arrow functions' , function ( ) {
154
+ var fn = ( ) => { } ;
155
+ expect ( serialize ( fn ) ) . to . be . a ( 'string' ) . equal ( '() => {}' ) ;
156
+ } ) ;
157
+
158
+ it ( 'should deserialize arrow functions' , function ( ) {
159
+ var fn ; eval ( 'fn = ' + serialize ( ( ) => true ) ) ;
160
+ expect ( fn ) . to . be . a ( 'function' ) ;
161
+ expect ( fn ( ) ) . to . equal ( true ) ;
162
+ } ) ;
163
+
164
+ it ( 'should serialize arrow functions with one argument' , function ( ) {
165
+ var fn = arg1 => { }
166
+ expect ( serialize ( fn ) ) . to . be . a ( 'string' ) . equal ('arg1 => {}' ) ;
167
+ } ) ;
168
+
169
+ it ( 'should deserialize arrow functions with one argument' , function ( ) {
170
+ var fn ; eval ( 'fn = ' + serialize ( arg1 => { } ) ) ;
171
+ expect ( fn ) . to . be . a ( 'function' ) ;
172
+ expect ( fn . length ) . to . equal ( 1 ) ;
173
+ } ) ;
174
+
175
+ it ( 'should serialize arrow functions with multiple arguments' , function ( ) {
176
+ var fn = ( arg1 , arg2 ) => { }
177
+ expect ( serialize ( fn ) ) . to . equal ( '(arg1, arg2) => {}' ) ;
178
+ } ) ;
179
+
180
+ it ( 'should deserialize arrow functions with multiple arguments' , function ( ) {
181
+ var fn ; eval ( 'fn = ' + serialize ( ( arg1 , arg2 ) => { } ) ) ;
182
+ expect ( fn ) . to . be . a ( 'function' ) ;
183
+ expect ( fn . length ) . to . equal ( 2 ) ;
184
+ } ) ;
185
+
186
+ it ( 'should serialize arrow functions with bodies' , function ( ) {
187
+ var fn = ( ) => { return true ; }
188
+ expect ( serialize ( fn ) ) . to . equal ( '() => { return true; }' ) ;
189
+ } ) ;
190
+
191
+ it ( 'should deserialize arrow functions with bodies' , function ( ) {
192
+ var fn ; eval ( 'fn = ' + serialize ( ( ) => { return true ; } ) ) ;
193
+ expect ( fn ) . to . be . a ( 'function' ) ;
194
+ expect ( fn ( ) ) . to . equal ( true ) ;
195
+ } ) ;
196
+
197
+ it ( 'should serialize enhanced literal objects' , function ( ) {
198
+ var obj = {
199
+ foo : ( ) => { return true ; } ,
200
+ bar : arg1 => { return true ; } ,
201
+ baz : ( arg1 , arg2 ) => { return true ; }
202
+ } ;
203
+
204
+ expect ( serialize ( obj ) ) . to . equal ( '{"foo":() => { return true; },"bar":arg1 => { return true; },"baz":(arg1, arg2) => { return true; }}' ) ;
205
+ } ) ;
206
+
207
+ it ( 'should deserialize enhanced literal objects' , function ( ) {
208
+ var obj ;
F438
tr>
209
+ eval ( 'obj = ' + serialize ( { foo : ( ) => { return true ; } ,
210
+ foo : ( ) => { return true ; } ,
211
+ bar : arg1 => { return true ; } ,
212
+ baz : ( arg1 , arg2 ) => { return true ; }
213
+ } ) ) ;
214
+
215
+ expect ( obj . foo ( ) ) . to . equal ( true ) ;
216
+ expect ( obj . bar ( 'arg1' ) ) . to . equal ( true ) ;
217
+ expect ( obj . baz ( 'arg1' , 'arg1' ) ) . to . equal ( true ) ;
218
+ } ) ;
219
+
220
+ it ( 'should serialize arrow functions with added properties' , function ( ) {
221
+ var fn = ( ) => { } ;
222
+ fn . property1 = 'a string'
223
+ expect ( serialize ( fn ) ) . to . be . a ( 'string' ) . equal ( '() => {}' ) ;
224
+ } ) ;
225
+
226
+ it ( 'should deserialize arrow functions with added properties' , function ( ) {
227
+ var fn ; eval ( 'fn = ' + serialize ( ( ) => { this . property1 = 'a string' ; return 5 } ) ) ;
228
+ expect ( fn ) . to . be . a ( 'function' ) ;
229
+ expect ( fn ( ) ) . to . equal ( 5 ) ;
230
+ } ) ;
231
+
232
+ it ( 'should serialize arrow functions that return other functions' , function ( ) {
233
+ var fn = arg1 => { return arg2 => arg1 + arg2 } ;
234
+ expect ( serialize ( fn ) ) . to . be . a ( 'string' ) . equal ( 'arg1 => { return arg2 => arg1 + arg2 }' ) ;
235
+ } ) ;
236
+
237
+ it ( 'should deserialize arrow functions that return other functions' , function ( ) {
238
+ var fn ; eval ( 'fn = ' + serialize ( arg1 => { return arg2 => arg1 + arg2 } ) ) ;
239
+ expect ( fn ) . to . be . a ( 'function' ) ;
240
+ expect ( fn ( 2 ) ( 3 ) ) . to . equal ( 5 ) ;
241
+ } ) ;
128
242
} ) ;
129
243
130
244
describe ( 'regexps' , function ( ) {
@@ -196,6 +310,1
8302
2 @@ describe('serialize( obj )', function () {
196
310
expect ( d ) . to . be . a ( 'string' ) ;
197
311
expect ( d ) . to . equal ( '2016-04-28T25:02:17.156Z' ) ;
198
312
} ) ;
313
+
314
+ it ( 'should serialize dates within objects' , function ( ) {
315
+ var d = { foo : new Date ( '2016-04-28T22:02:17.156Z' ) } ;
316
+ expect ( serialize ( d ) ) . to . be . a ( 'string' ) . equal ( '{"foo":new Date("2016-04-28T22:02:17.156Z")}' ) ;
317
+ expect ( serialize ( { t : [ d ] } ) ) . to . be . a ( 'string' ) . equal ( '{"t":[{"foo":new Date("2016-04-28T22:02:17.156Z")}]}' ) ;
318
+ } ) ;
199
319
} ) ;
200
320
201
321
describe ( 'maps' , function ( ) {
0 commit comments