Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/GlobalParseXTest.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /**
6 *
7 */
8 package org.mozilla.javascript.tests;
9
10 import junit.framework.TestCase;
11
12 import org.mozilla.javascript.Context;
13 import org.mozilla.javascript.ContextAction;
14 import org.mozilla.javascript.Scriptable;
15
16 /**
17 * Tests for global functions parseFloat and parseInt.
18 * @author Marc Guillemot
19 */
20 public class GlobalParseXTest extends TestCase {
21
22 /**
23 * Test for bug #501972
24 * https://bugzilla.mozilla.org/show_bug.cgi?id=501972
25 * Leading whitespaces should be ignored with following white space chars
26 * (see ECMA spec 15.1.2.3)
27 * <TAB>, <SP>, <NBSP>, <FF>, <VT>, <CR>, <LF>, <LS>, <PS>, <USP>
28 */
29 public void testParseFloatAndIntWhiteSpaces() {
30 testParseFloatWhiteSpaces("\\u00A0 "); // <NBSP>
31
32 testParseFloatWhiteSpaces("\\t ");
33 testParseFloatWhiteSpaces("\\u00A0 "); // <NBSP>
34 testParseFloatWhiteSpaces("\\u000C "); // <FF>
35 testParseFloatWhiteSpaces("\\u000B "); // <VT>
36 testParseFloatWhiteSpaces("\\u000D "); // <CR>
37 testParseFloatWhiteSpaces("\\u000A "); // <LF>
38 testParseFloatWhiteSpaces("\\u2028 "); // <LS>
39 testParseFloatWhiteSpaces("\\u2029 "); // <PS>
40 }
41
42 private void testParseFloatWhiteSpaces(final String prefix) {
43 assertEvaluates("789", "String(parseInt('" + prefix + "789 '))");
44 assertEvaluates("7.89", "String(parseFloat('" + prefix + "7.89 '))");
45 }
46
47 /**
48 * Test for bug #531436
49 * https://bugzilla.mozilla.org/show_bug.cgi?id=531436
50 * Trailing noise should be ignored
51 * (see ECMA spec 15.1.2.3)
52 */
53 public void testParseFloatTrailingNoise() {
54 testParseFloat("7890", "789e1");
55 testParseFloat("7890", "789E1");
56 testParseFloat("7890", "789E+1");
57 testParseFloat("7890", "789E+1e");
58 testParseFloat("789", "7890E-1");
59 testParseFloat("789", "7890E-1e");
60
61 testParseFloat("789", "789hello");
62 testParseFloat("789", "789e");
63 testParseFloat("789", "789E");
64 testParseFloat("789", "789e+");
65 testParseFloat("789", "789Efgh");
66 testParseFloat("789", "789efgh");
67 testParseFloat("789", "789e-");
68 testParseFloat("789", "789e-hello");
69 testParseFloat("789", "789e+hello");
70 testParseFloat("789", "789+++hello");
71 testParseFloat("789", "789-e-+hello");
72 testParseFloat("789", "789e+e++hello");
73 testParseFloat("789", "789e-e++hello");
74 }
75
76 private void testParseFloat(final String expected, final String value) {
77 assertEvaluates(expected, "String(parseFloat('" + value + "'))");
78 }
79
80 private void assertEvaluates(final Object expected, final String source) {
81 final ContextAction action = new ContextAction() {
82 public Object run(Context cx) {
83 final Scriptable scope = cx.initStandardObjects();
84 final Object rep = cx.evaluateString(scope, source, "test.js",
85 0, null);
86 assertEquals(expected, rep);
87 return null;
88 }
89 };
90 Utils.runWithAllOptimizationLevels(action);
91 }
92 }