@@ -24,6 +24,24 @@ test('nested circular reference to root', function (assert) {
2424 assert . end ( )
2525} )
2626
27+ test ( 'throw if circularValue is set to TypeError' , function ( assert ) {
28+ const noCircularStringify = stringify . configure ( { circularValue : TypeError } )
29+ const object = { number : 42 , boolean : true , string : 'Yes!' }
30+ object . circular = object
31+
32+ assert . throws ( ( ) => noCircularStringify ( object ) , TypeError )
33+ assert . end ( )
34+ } )
35+
36+ test ( 'throw if circularValue is set to Error' , function ( assert ) {
37+ const noCircularStringify = stringify . configure ( { circularValue : Error } )
38+ const object = { number : 42 , boolean : true , string : 'Yes!' }
39+ object . circular = object
40+
41+ assert . throws ( ( ) => noCircularStringify ( object ) , TypeError )
42+ assert . end ( )
43+ } )
44+
2745test ( 'child circular reference' , function ( assert ) {
2846 const fixture = { name : 'Tywin Lannister' , child : { name : 'Tyrion\n\t"Lannister' . repeat ( 20 ) } }
2947 fixture . child . dinklage = fixture . child
@@ -1013,7 +1031,7 @@ test('should throw when maximumBreadth receives malformed input', (assert) => {
10131031 assert . end ( )
10141032} )
10151033
1016- test ( 'check for well formed stringify implementation ' , ( assert ) => {
1034+ test ( 'check that all single characters are identical to JSON.stringify ' , ( assert ) => {
10171035 for ( let i = 0 ; i < 2 ** 16 ; i ++ ) {
10181036 const string = String . fromCharCode ( i )
10191037 const actual = stringify ( string )
@@ -1030,3 +1048,45 @@ test('check for well formed stringify implementation', (assert) => {
10301048 assert . equal ( longStringEscape , `"${ 'a' . repeat ( 100 ) } \\ud800"` )
10311049 assert . end ( )
10321050} )
1051+
1052+ test ( 'check for lone surrogate pairs' , ( assert ) => {
1053+ const edgeChar = String . fromCharCode ( 0xd799 )
1054+
1055+ for ( let charCode = 0xD800 ; charCode < 0xDFFF ; charCode ++ ) {
1056+ const surrogate = String . fromCharCode ( charCode )
1057+
1058+ assert . equal (
1059+ stringify ( surrogate ) ,
1060+ `"\\u${ charCode . toString ( 16 ) } "`
1061+ )
1062+ assert . equal (
1063+ stringify ( `${ 'a' . repeat ( 200 ) } ${ surrogate } ` ) ,
1064+ `"${ 'a' . repeat ( 200 ) } \\u${ charCode . toString ( 16 ) } "`
1065+ )
1066+ assert . equal (
1067+ stringify ( `${ surrogate } ${ 'a' . repeat ( 200 ) } ` ) ,
1068+ `"\\u${ charCode . toString ( 16 ) } ${ 'a' . repeat ( 200 ) } "`
1069+ )
1070+ if ( charCode < 0xdc00 ) {
1071+ const highSurrogate = surrogate
1072+ const lowSurrogate = String . fromCharCode ( charCode + 1024 )
1073+ assert . notOk (
1074+ stringify (
1075+ `${ edgeChar } ${ highSurrogate } ${ lowSurrogate } ${ edgeChar } `
1076+ ) . includes ( '\\u' )
1077+ )
1078+ assert . equal (
1079+ ( stringify (
1080+ `${ highSurrogate } ${ highSurrogate } ${ lowSurrogate } `
1081+ ) . match ( / \\ u / g) || [ ] ) . length ,
1082+ 1
1083+ )
1084+ } else {
1085+ assert . equal (
1086+ stringify ( `${ edgeChar } ${ surrogate } ${ edgeChar } ` ) ,
1087+ `"${ edgeChar } \\u${ charCode . toString ( 16 ) } ${ edgeChar } "`
1088+ )
1089+ }
1090+ }
1091+ assert . end ( )
1092+ } )
0 commit comments