| 1 |
// Copyright 2009 the Sputnik authors. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
info: | |
| 6 |
Result of number conversion from object value is the result |
| 7 |
of conversion from primitive value |
| 8 |
es5id: 9.3_A5_T1 |
| 9 |
description: > |
| 10 |
new Number(), new Number(0), new Number(Number.NaN), new |
| 11 |
Number(null), new Number(void 0) and others convert to Number by |
| 12 |
explicit transformation |
| 13 |
---*/ |
| 14 |
|
| 15 |
// CHECK#1 |
| 16 |
if (Number(new Number()) !== 0) { |
| 17 |
$ERROR('#1: Number(new Number()) === 0. Actual: ' + (Number(new Number()))); |
| 18 |
} |
| 19 |
|
| 20 |
// CHECK#2 |
| 21 |
if (Number(new Number(0)) !== 0) { |
| 22 |
$ERROR('#2: Number(new Number(0)) === 0. Actual: ' + (Number(new Number(0)))); |
| 23 |
} |
| 24 |
|
| 25 |
// CHECK#3 |
| 26 |
assert.sameValue(Number(new Number(NaN)), NaN, "Number(new Number(NaN)"); |
| 27 |
|
| 28 |
// CHECK#4 |
| 29 |
if (Number(new Number(null)) !== 0) { |
| 30 |
$ERROR('#4.1: Number(new Number(null)) === 0. Actual: ' + (Number(new Number(null)))); |
| 31 |
} else { |
| 32 |
if (1 / Number(new Number(null)) !== Number.POSITIVE_INFINITY) { |
| 33 |
$ERROR('#4.2: Number(new Number(null)) === +0. Actual: -0'); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// CHECK#5 |
| 38 |
assert.sameValue(Number(new Number(void 0)), NaN, "Number(new Number(void 0)"); |
| 39 |
|
| 40 |
// CHECK#6 |
| 41 |
if (Number(new Number(true)) !== 1) { |
| 42 |
$ERROR('#6: Number(new Number(true)) === 1. Actual: ' + (Number(new Number(true)))); |
| 43 |
} |
| 44 |
|
| 45 |
// CHECK#7 |
| 46 |
if (Number(new Number(false)) !== +0) { |
| 47 |
$ERROR('#7.1: Number(new Number(false)) === 0. Actual: ' + (Number(new Number(false)))); |
| 48 |
} else { |
| 49 |
if (1 / Number(new Number(false)) !== Number.POSITIVE_INFINITY) { |
| 50 |
$ERROR('#7.2: Number(new Number(false)) === +0. Actual: -0'); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// CHECK#8 |
| 55 |
if (Number(new Boolean(true)) !== 1) { |
| 56 |
$ERROR('#8: Number(new Boolean(true)) === 1. Actual: ' + (Number(new Boolean(true)))); |
| 57 |
} |
| 58 |
|
| 59 |
// CHECK#9 |
| 60 |
if (Number(new Boolean(false)) !== +0) { |
| 61 |
$ERROR('#9.1: Number(new Boolean(false)) === 0. Actual: ' + (Number(new Boolean(false)))); |
| 62 |
} else { |
| 63 |
if (1 / Number(new Boolean(false)) !== Number.POSITIVE_INFINITY) { |
| 64 |
$ERROR('#9.2: Number(new Boolean(false)) === +0. Actual: -0'); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// CHECK#10 |
| 69 |
assert.sameValue(Number(new Array(2, 4, 8, 16, 32)), NaN, "Number(new Array(2,4,8,16,32))"); |
| 70 |
|
| 71 |
// CHECK#11 |
| 72 |
var myobj1 = { |
| 73 |
ToNumber: function() { |
| 74 |
return 12345; |
| 75 |
}, |
| 76 |
toString: function() { |
| 77 |
return "67890"; |
| 78 |
}, |
| 79 |
valueOf: function() { |
| 80 |
return "[object MyObj]"; |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
assert.sameValue(Number(myobj1), NaN, "Number(myobj1)"); |
| 85 |
|
| 86 |
// CHECK#12 |
| 87 |
var myobj2 = { |
| 88 |
ToNumber: function() { |
| 89 |
return 12345; |
| 90 |
}, |
| 91 |
toString: function() { |
| 92 |
return "67890"; |
| 93 |
}, |
| 94 |
valueOf: function() { |
| 95 |
return "9876543210"; |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
if (Number(myobj2) !== 9876543210) { |
| 100 |
$ERROR("#12: Number(myobj2) calls ToPrimitive with hint Number. Exptected: 9876543210. Actual: " + (Number(myobj2))); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
// CHECK#13 |
| 105 |
var myobj3 = { |
| 106 |
ToNumber: function() { |
| 107 |
return 12345; |
| 108 |
}, |
| 109 |
toString: function() { |
| 110 |
return "[object MyObj]"; |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
assert.sameValue(Number(myobj3), NaN, "Number(myobj3)"); |
| 115 |
|
| 116 |
// CHECK#14 |
| 117 |
var myobj4 = { |
| 118 |
ToNumber: function() { |
| 119 |
return 12345; |
| 120 |
}, |
| 121 |
toString: function() { |
| 122 |
return "67890"; |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
if (Number(myobj4) !== 67890) { |
| 127 |
$ERROR("#14: Number(myobj4) calls ToPrimitive with hint Number. Exptected: 67890. Actual: " + (Number(myobj4))); |
| 128 |
} |
| 129 |
|
| 130 |
// CHECK#15 |
| 131 |
var myobj5 = { |
| 132 |
ToNumber: function() { |
| 133 |
return 12345; |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
assert.sameValue(Number(myobj5), NaN, "Number(myobj5)"); |