@@ -201,12 +201,53 @@ test('don\'t stringify non-enumerable symbols', t => {
201
201
202
202
t . is ( stringifyObject ( object ) , '{\n\t[Symbol(\'for enumerable key\')]: undefined\n}' ) ;
203
203
} ) ;
204
+
204
205
test ( 'handle symbols' , t => {
205
206
const object = {
206
207
[ Symbol ( 'unique' ) ] : Symbol ( 'unique' ) ,
207
208
[ Symbol . for ( 'registry' ) ] : [ Symbol . for ( 'registry' ) , 2 ] ,
208
209
[ Symbol . iterator ] : { k : Symbol . iterator } ,
209
- [ Symbol ( ) ] : 'undef'
210
+ [ Symbol ( ) ] : 'undef' , // eslint-disable-line symbol-description
210
211
} ;
211
212
t . is ( stringifyObject ( object ) , '{\n\t[Symbol(\'unique\')]: Symbol(\'unique\'),\n\t[Symbol.for(\'registry\')]: [\n\t\tSymbol.for(\'registry\'),\n\t\t2\n\t],\n\t[Symbol.iterator]: {\n\t\tk: Symbol.iterator\n\t},\n\t[Symbol()]: \'undef\'\n}' ) ;
213
+
214
+ // Anonymous symbol (no description)
215
+ t . is ( stringifyObject ( Symbol ( ) ) , 'Symbol()' ) ; // eslint-disable-line symbol-description
216
+
217
+ // Symbol with empty string description
218
+ t . is ( stringifyObject ( Symbol ( '' ) ) , 'Symbol(\'\')' ) ;
219
+
220
+ // Symbol.for with empty string
221
+ t . is ( stringifyObject ( Symbol . for ( '' ) ) , 'Symbol.for(\'\')' ) ;
222
+
223
+ // Test as object keys
224
+ const emptySymbolKeys = {
225
+ [ Symbol ( ) ] : 'anonymous' , // eslint-disable-line symbol-description
226
+ [ Symbol ( '' ) ] : 'empty string' ,
227
+ [ Symbol . for ( '' ) ] : 'empty for' ,
228
+ } ;
229
+ t . regex ( stringifyObject ( emptySymbolKeys ) , / \[ S y m b o l \( \) ] / ) ;
230
+ t . regex ( stringifyObject ( emptySymbolKeys ) , / \[ S y m b o l \( ' ' \) ] / ) ;
231
+ t . regex ( stringifyObject ( emptySymbolKeys ) , / \[ S y m b o l \. f o r \( ' ' \) ] / ) ;
232
+
233
+ // Symbol escaping with special characters
234
+ const symbolWithSpecialChars = Symbol ( 'a"b\\c\n' ) ;
235
+ t . is ( stringifyObject ( symbolWithSpecialChars ) , 'Symbol(\'a"b\\\\c\\n\')' ) ;
236
+ t . is ( stringifyObject ( symbolWithSpecialChars , { singleQuotes : false } ) , 'Symbol("a\\"b\\\\c\\n")' ) ;
237
+
238
+ const specialCharKey = {
239
+ [ Symbol ( 'a"b\\c\n' ) ] : 'value' ,
240
+ } ;
241
+ t . regex ( stringifyObject ( specialCharKey ) , / \[ S y m b o l \( ' a " b \\ \\ c \\ n ' \) ] / ) ;
242
+
243
+ // Well-known symbols
244
+ t . is ( stringifyObject ( Symbol . iterator ) , 'Symbol.iterator' ) ;
245
+ t . is ( stringifyObject ( Symbol . hasInstance ) , 'Symbol.hasInstance' ) ;
246
+ t . is ( stringifyObject ( Symbol . toStringTag ) , 'Symbol.toStringTag' ) ;
247
+
248
+ // Look-alike symbols (not real well-known symbols)
249
+ t . is ( stringifyObject ( Symbol ( 'Symbol.iterator' ) ) , 'Symbol(\'Symbol.iterator\')' ) ;
250
+ t . is ( stringifyObject ( Symbol ( 'Symbol.hasInstance' ) ) , 'Symbol(\'Symbol.hasInstance\')' ) ;
251
+ t . is ( stringifyObject ( Symbol ( 'Symbol.toStringTag' ) ) , 'Symbol(\'Symbol.toStringTag\')' ) ;
252
+ } ) ;
212
253
} ) ;
0 commit comments