@@ -73,6 +73,17 @@ test.each([
7373 expect ( result ) . toBe ( `${ a } ${ b } test ${ c } ` ) ;
7474} ) ;
7575
76+ test . each ( [
77+ { input : "abcd" , index : 3 } ,
78+ { input : "abcde" , index : 3 } ,
79+ { input : "abcde" , index : 0 } ,
80+ { input : "a" , index : 0 } ,
81+ ] ) ( "string index (%p)" , ( { input, index } ) => {
82+ const result = util . transpileAndExecute ( `return "${ input } "[${ index } ];` ) ;
83+
84+ expect ( result ) . toBe ( input [ index ] ) ;
85+ } ) ;
86+
7687test . each ( [
7788 { inp : "hello test" , searchValue : "" , replaceValue : "" } ,
7889 { inp : "hello test" , searchValue : " " , replaceValue : "" } ,
@@ -290,7 +301,7 @@ test.each<{ inp: string; args: Parameters<string["startsWith"]> }>([
290301 { inp : "hello test" , args : [ "test" ] } ,
291302 { inp : "hello test" , args : [ "test" , 6 ] } ,
292303] ) ( "string.startsWith (%p)" , ( { inp, args } ) => {
293- const argsString = args . map ( arg => JSON . stringify ( arg ) ) . join ( ", " ) ;
304+ const argsString = util . valuesToString ( args ) ;
294305 const result = util . transpileAndExecute ( `return "${ inp } ".startsWith(${ argsString } )` ) ;
295306
296307 expect ( result ) . toBe ( inp . startsWith ( ...args ) ) ;
@@ -302,19 +313,46 @@ test.each<{ inp: string; args: Parameters<string["endsWith"]> }>([
302313 { inp : "hello test" , args : [ "hello" ] } ,
303314 { inp : "hello test" , args : [ "hello" , 5 ] } ,
304315] ) ( "string.endsWith (%p)" , ( { inp, args } ) => {
305- const argsString = args . map ( arg => JSON . stringify ( arg ) ) . join ( ", " ) ;
316+ const argsString = util . valuesToString ( args ) ;
306317 const result = util . transpileAndExecute ( `return "${ inp } ".endsWith(${ argsString } )` ) ;
307318
308319 expect ( result ) . toBe ( inp . endsWith ( ...args ) ) ;
309320} ) ;
310321
311322test . each ( [
312- { input : "abcd" , index : 3 } ,
313- { input : "abcde" , index : 3 } ,
314- { input : "abcde" , index : 0 } ,
315- { input : "a" , index : 0 } ,
316- ] ) ( "string index (%p)" , ( { input, index } ) => {
317- const result = util . transpileAndExecute ( `return "${ input } "[${ index } ];` ) ;
323+ { inp : "hello test" , count : 0 } ,
324+ { inp : "hello test" , count : 1 } ,
325+ { inp : "hello test" , count : 2 } ,
326+ { inp : "hello test" , count : 1.1 } ,
327+ { inp : "hello test" , count : 1.5 } ,
328+ { inp : "hello test" , count : 1.9 } ,
329+ ] ) ( "string.repeat (%p)" , ( { inp, count } ) => {
330+ const result = util . transpileAndExecute ( `return "${ inp } ".repeat(${ count } )` ) ;
331+
332+ expect ( result ) . toBe ( inp . repeat ( count ) ) ;
333+ } ) ;
318334
319- expect ( result ) . toBe ( input [ index ] ) ;
335+ const padCases = [
336+ { inp : "foo" , maxLength : 0 } ,
337+ { inp : "foo" , maxLength : 3 } ,
338+ { inp : "foo" , maxLength : 5 } ,
339+ { inp : "foo" , maxLength : 4 , fillString : " " } ,
340+ { inp : "foo" , maxLength : 10 , fillString : " " } ,
341+ { inp : "foo" , maxLength : 5 , fillString : "1234" } ,
342+ { inp : "foo" , maxLength : 5.9 , fillString : "1234" } ,
343+ { inp : "foo" , maxLength : NaN } ,
344+ ] ;
345+
346+ test . each ( padCases ) ( "string.padStart (%p)" , ( { inp, maxLength, fillString } ) => {
347+ const argsString = util . valuesToString ( [ maxLength , fillString ] ) ;
348+ const result = util . transpileAndExecute ( `return "${ inp } ".padStart(${ argsString } )` ) ;
349+
350+ expect ( result ) . toBe ( inp . padStart ( maxLength , fillString ) ) ;
351+ } ) ;
352+
353+ test . each ( padCases ) ( "string.padEnd (%p)" , ( { inp, maxLength, fillString } ) => {
354+ const argsString = util . valuesToString ( [ maxLength , fillString ] ) ;
355+ const result = util . transpileAndExecute ( `return "${ inp } ".padEnd(${ argsString } )` ) ;
356+
357+ expect ( result ) . toBe ( inp . padEnd ( maxLength , fillString ) ) ;
320358} ) ;
0 commit comments