blob: 4a532e28942339c3340d96f6942844fb2f10a01f [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test-pre.js"></script>
</head>
<body>
<script>
description('Tests for .valueAsNumber with &lt;input type=number>.');
var input = document.createElement('input');
input.type = 'number';
function valueAsNumberFor(stringValue) {
input.value = stringValue;
return input.valueAsNumber;
}
function setValueAsNumberAndGetValue(num) {
input.valueAsNumber = num;
return input.value;
}
shouldBe('valueAsNumberFor("0")', '0');
shouldBe('valueAsNumberFor("0.2")', '0.2');
shouldBe('valueAsNumberFor(".2")', '0.2');
shouldBe('valueAsNumberFor("10")', '10');
shouldBe('valueAsNumberFor("01")', '1');
shouldBe('valueAsNumberFor("-0")', '0'); // "-0" is 0 in HTML5.
shouldBe('valueAsNumberFor("-1.2")', '-1.2');
shouldBe('valueAsNumberFor("1.2E10")', '1.2E10');
shouldBe('valueAsNumberFor("1.2E-10")', '1.2E-10');
shouldBe('valueAsNumberFor("1.2E+10")', '1.2E10');
shouldBe('valueAsNumberFor("123456789012345678901234567890123456789")', '1.2345678901234568E+38');
shouldBe('valueAsNumberFor("0.12345678901234567890123456789012345678901234567890")', '0.123456789012345678');
debug('valueAsNumber for invalid string values:');
shouldBeTrue('isNaN(valueAsNumberFor(""))');
shouldBeTrue('isNaN(valueAsNumberFor("abc"))');
shouldBeTrue('isNaN(valueAsNumberFor("0xff"))');
shouldBeTrue('isNaN(valueAsNumberFor("+1"))');
shouldBeTrue('isNaN(valueAsNumberFor(" 10"))');
shouldBeTrue('isNaN(valueAsNumberFor("10 "))');
shouldBeTrue('isNaN(valueAsNumberFor("."))');
shouldBeTrue('isNaN(valueAsNumberFor("1E"))');
shouldBeTrue('isNaN(valueAsNumberFor("NaN"))');
shouldBeTrue('isNaN(valueAsNumberFor("nan"))');
shouldBeTrue('isNaN(valueAsNumberFor("Inf"))');
shouldBeTrue('isNaN(valueAsNumberFor("inf"))');
shouldBeTrue('isNaN(valueAsNumberFor("Infinity"))');
shouldBeTrue('isNaN(valueAsNumberFor("infinity"))');
debug('Too huge exponent to support');
shouldBeTrue('isNaN(valueAsNumberFor("1.2E65535"))');
debug('Tests for the valueAsNumber setter:');
shouldBe('setValueAsNumberAndGetValue(0)', '"0"');
shouldBe('setValueAsNumberAndGetValue(10)', '"10"');
shouldBe('setValueAsNumberAndGetValue(01)', '"1"');
shouldBe('setValueAsNumberAndGetValue(-0)', '"0"');
shouldBe('setValueAsNumberAndGetValue(-1.2)', '"-1.2"');
shouldBe('setValueAsNumberAndGetValue(1.2e10)', '"12000000000"');
shouldBe('setValueAsNumberAndGetValue(1.2e-10)', '"1.2e-10"');
shouldBe('setValueAsNumberAndGetValue(1.2345678901234567e+38)', '"1.2345678901234567e+38"');
shouldBe('setValueAsNumberAndGetValue("-3.40282346e+38")', '"-3.40282346e+38"');
shouldThrowErrorName('setValueAsNumberAndGetValue("-3.40282348e+38")', 'InvalidStateError');
shouldBe('setValueAsNumberAndGetValue("3.40282346e+38")', '"3.40282346e+38"');
shouldThrowErrorName('setValueAsNumberAndGetValue("3.40282348e+38")', 'InvalidStateError');
debug('Tests to set invalid values to valueAsNumber:');
shouldBe('setValueAsNumberAndGetValue(null)', '"0"');
shouldThrowErrorName('setValueAsNumberAndGetValue("foo")', 'NotSupportedError');
shouldThrowErrorName('setValueAsNumberAndGetValue(NaN)', 'NotSupportedError');
shouldThrowErrorName('setValueAsNumberAndGetValue(Number.NaN)', 'NotSupportedError');
shouldThrowErrorName('setValueAsNumberAndGetValue(Infinity)', 'NotSupportedError');
shouldThrowErrorName('setValueAsNumberAndGetValue(Number.POSITIVE_INFINITY)', 'NotSupportedError');
shouldThrowErrorName('setValueAsNumberAndGetValue(Number.NEGATIVE_INFINITY)', 'NotSupportedError');
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>