blob: 8022142cdea1711fb503e9a2b610a187e2e1b99b [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"');
shouldThrow('setValueAsNumberAndGetValue("-3.40282348e+38")', '"Error: InvalidStateError: DOM Exception 11"');
shouldBe('setValueAsNumberAndGetValue("3.40282346e+38")', '"3.40282346e+38"');
shouldThrow('setValueAsNumberAndGetValue("3.40282348e+38")', '"Error: InvalidStateError: DOM Exception 11"');
debug('Tests to set invalid values to valueAsNumber:');
shouldBe('setValueAsNumberAndGetValue(null)', '"0"');
shouldThrow('setValueAsNumberAndGetValue("foo")', '"Error: NotSupportedError: DOM Exception 9"');
shouldThrow('setValueAsNumberAndGetValue(NaN)', '"Error: NotSupportedError: DOM Exception 9"');
shouldThrow('setValueAsNumberAndGetValue(Number.NaN)', '"Error: NotSupportedError: DOM Exception 9"');
shouldThrow('setValueAsNumberAndGetValue(Infinity)', '"Error: NotSupportedError: DOM Exception 9"');
shouldThrow('setValueAsNumberAndGetValue(Number.POSITIVE_INFINITY)', '"Error: NotSupportedError: DOM Exception 9"');
shouldThrow('setValueAsNumberAndGetValue(Number.NEGATIVE_INFINITY)', '"Error: NotSupportedError: DOM Exception 9"');
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>