@@ -78,3 +78,57 @@ test("number intersected method", () => {
7878test ( "numbers overflowing the float limit become math.huge" , ( ) => {
7979 util . testExpression `1e309` . expectToMatchJsResult ( ) ;
8080} ) ;
81+
82+ describe . each ( [ "parseInt" , "parseFloat" ] ) ( "parse numbers with %s" , parseFunction => {
83+ const numberStrings = [ "3" , "3.0" , "9" , "42" , "239810241" , "-20391" , "3.1415" , "2.7182" , "-34910.3" ] ;
84+
85+ test . each ( numberStrings ) ( "parses (%s)" , numberString => {
86+ util . testExpression `${ parseFunction } ("${ numberString } ")` . expectToMatchJsResult ( ) ;
87+ } ) ;
88+
89+ test ( "empty string" , ( ) => {
90+ util . testExpression `${ parseFunction } ("")` . expectToMatchJsResult ( ) ;
91+ } ) ;
92+
93+ test ( "invalid string" , ( ) => {
94+ util . testExpression `${ parseFunction } ("bla")` . expectToMatchJsResult ( ) ;
95+ } ) ;
96+
97+ test . each ( [ "1px" , "2300m" , "3,4" , "452adkfl" ] ) ( "trailing text (%s)" , numberString => {
98+ util . testExpression `${ parseFunction } ("${ numberString } ")` . expectToMatchJsResult ( ) ;
99+ } ) ;
100+
101+ test . each ( [ " 3" , " 4" , " -231" , " 1px" ] ) ( "leading whitespace (%s)" , numberString => {
102+ util . testExpression `${ parseFunction } ("${ numberString } ")` . expectToMatchJsResult ( ) ;
103+ } ) ;
104+ } ) ;
105+
106+ test . each ( [ "Infinity" , "-Infinity" , " -Infinity" ] ) ( "parseFloat handles Infinity" , numberString => {
107+ util . testExpression `parseFloat("${ numberString } ")` . expectToMatchJsResult ( ) ;
108+ } ) ;
109+
110+ test . each ( [
111+ { numberString : "36" , base : 8 } ,
112+ { numberString : "-36" , base : 8 } ,
113+ { numberString : "100010101101" , base : 2 } ,
114+ { numberString : "-100010101101" , base : 2 } ,
115+ { numberString : "3F" , base : 16 } ,
116+ ] ) ( "parseInt with base (%p)" , ( { numberString, base } ) => {
117+ util . testExpression `parseInt("${ numberString } ", ${ base } )` . expectToMatchJsResult ( ) ;
118+ } ) ;
119+
120+ test . each ( [ "0x4A" , "-0x42" , "0X42" , " 0x391" , " -0x8F" ] ) ( "parseInt detects hexadecimal" , numberString => {
121+ util . testExpression `parseInt("${ numberString } ")` . expectToMatchJsResult ( ) ;
122+ } ) ;
123+
124+ test . each ( [ 1 , 37 , - 100 ] ) ( "parseInt with invalid base (%p)" , base => {
125+ util . testExpression `parseInt("11111", ${ base } )` . expectToMatchJsResult ( ) ;
126+ } ) ;
127+
128+ test . each ( [
129+ { numberString : "36px" , base : 8 } ,
130+ { numberString : "10001010110231" , base : 2 } ,
131+ { numberString : "3Fcolor" , base : 16 } ,
132+ ] ) ( "parseInt with base and trailing text (%p)" , ( { numberString, base } ) => {
133+ util . testExpression `parseInt("${ numberString } ", ${ base } )` . expectToMatchJsResult ( ) ;
134+ } ) ;
0 commit comments