2011-09-07 08:35:18 +02:00
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
2014-07-22 01:09:02 +02:00
/ * - - -
info : Operator use ToNumber
2017-06-29 17:10:43 +02:00
esid : sec - parseint - string - radix
2014-07-22 01:09:02 +02:00
description : If Type ( value ) is Object , evaluate ToPrimitive ( value , Number )
-- - * /
2011-09-07 08:35:18 +02:00
//CHECK#1
2018-02-15 21:12:07 +01:00
var object = {
valueOf : function ( ) {
return 2
}
} ;
2020-09-18 18:06:50 +02:00
assert . sameValue (
parseInt ( "11" , object ) ,
parseInt ( "11" , 2 ) ,
'parseInt("11", {valueOf: function() {return 2}}) must return the same value returned by parseInt("11", 2)'
) ;
2011-09-07 08:35:18 +02:00
//CHECK#2
2018-02-15 21:12:07 +01:00
var object = {
valueOf : function ( ) {
return 2
} ,
toString : function ( ) {
return 1
}
} ;
2020-09-18 18:06:50 +02:00
assert . sameValue (
parseInt ( "11" , object ) ,
parseInt ( "11" , 2 ) ,
'parseInt("11", {valueOf: function() {return 2}, toString: function() {return 1}}) must return the same value returned by parseInt("11", 2)'
) ;
2011-09-07 08:35:18 +02:00
//CHECK#3
2018-02-15 21:12:07 +01:00
var object = {
valueOf : function ( ) {
return 2
} ,
toString : function ( ) {
return { }
}
} ;
2020-09-18 18:06:50 +02:00
assert . sameValue (
parseInt ( "11" , object ) ,
parseInt ( "11" , 2 ) ,
'parseInt("11", {valueOf: function() {return 2}, toString: function() {return {}}}) must return the same value returned by parseInt("11", 2)'
) ;
2011-09-07 08:35:18 +02:00
//CHECK#4
try {
2018-02-15 21:12:07 +01:00
var object = {
valueOf : function ( ) {
return 2
} ,
toString : function ( ) {
throw "error"
}
} ;
2020-09-18 18:06:50 +02:00
assert . sameValue (
parseInt ( "11" , object ) ,
parseInt ( "11" , 2 ) ,
'parseInt( "11", {valueOf: function() {return 2}, toString: function() {throw \\"error\\"}} ) must return the same value returned by parseInt("11", 2)'
) ;
2011-09-07 08:35:18 +02:00
}
catch ( e ) {
2020-09-18 18:06:50 +02:00
assert . notSameValue ( e , "error" , 'The value of `e` is not "error"' ) ;
2011-09-07 08:35:18 +02:00
}
//CHECK#5
2018-02-15 21:12:07 +01:00
var object = {
toString : function ( ) {
return 2
}
} ;
2020-09-18 18:06:50 +02:00
assert . sameValue (
parseInt ( "11" , object ) ,
parseInt ( "11" , 2 ) ,
'parseInt("11", {toString: function() {return 2}}) must return the same value returned by parseInt("11", 2)'
) ;
2011-09-07 08:35:18 +02:00
//CHECK#6
2018-02-15 21:12:07 +01:00
var object = {
valueOf : function ( ) {
return { }
} ,
toString : function ( ) {
return 2
}
}
2020-09-18 18:06:50 +02:00
assert . sameValue (
parseInt ( "11" , object ) ,
parseInt ( "11" , 2 ) ,
'parseInt("11", {valueOf: function() {return {}}, toString: function() {return 2}}) must return the same value returned by parseInt("11", 2)'
) ;
2011-09-07 08:35:18 +02:00
//CHECK#7
try {
2018-02-15 21:12:07 +01:00
var object = {
valueOf : function ( ) {
throw "error"
} ,
toString : function ( ) {
return 2
}
} ;
2011-09-07 08:35:18 +02:00
parseInt ( "11" , object ) ;
2020-09-18 18:06:50 +02:00
Test262Error . thrower ( '#7.1: var object = {valueOf: function() {throw "error"}, toString: function() {return 2}}; parseInt("11", object) throw "error". Actual: ' + ( parseInt ( "11" , object ) ) ) ;
2017-06-29 17:10:43 +02:00
}
2011-09-07 08:35:18 +02:00
catch ( e ) {
2020-09-18 18:06:50 +02:00
assert . sameValue ( e , "error" , 'The value of `e` is "error"' ) ;
2011-09-07 08:35:18 +02:00
}
//CHECK#8
try {
2018-02-15 21:12:07 +01:00
var object = {
valueOf : function ( ) {
return { }
} ,
toString : function ( ) {
return { }
}
} ;
2011-09-07 08:35:18 +02:00
parseInt ( "11" , object ) ;
2020-09-18 18:06:50 +02:00
Test262Error . thrower ( '#8.1: var object = {valueOf: function() {return {}}, toString: function() {return {}}}; parseInt("11", object) throw TypeError. Actual: ' + ( parseInt ( "11" , object ) ) ) ;
2017-06-29 17:10:43 +02:00
}
2011-09-07 08:35:18 +02:00
catch ( e ) {
2020-09-18 18:06:50 +02:00
assert . sameValue ( e instanceof TypeError , true , 'The result of `(e instanceof TypeError)` is true' ) ;
2011-09-07 08:35:18 +02:00
}