View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Number is a native JavaScript object.
25   *
26   * @author Marc Guillemot
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class NativeNumberTest extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts({"false", "false", "false", "true", "true", "false", "false"})
38      public void isFinite() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html>\n"
41              + "<head>\n"
42              + "  <script>\n"
43              + LOG_TITLE_FUNCTION
44              + "    if (Number.isFinite === undefined) {\n"
45              + "      log('no Number.isFinite');\n"
46              + "    } else {\n"
47              + "      log(Number.isFinite(Infinity));\n"
48              + "      log(Number.isFinite(NaN));\n"
49              + "      log(Number.isFinite(-Infinity));\n"
50              + "      log(Number.isFinite(0));\n"
51              + "      log(Number.isFinite(2e64));\n"
52              + "      log(Number.isFinite('0'));\n"
53              + "      log(Number.isFinite(null));\n"
54              + "    }\n"
55              + "  </script>\n"
56              + "</head>\n"
57              + "<body>\n"
58              + "</body>\n"
59              + "</html>";
60          loadPageVerifyTitle2(html);
61      }
62  
63      /**
64       * @throws Exception if the test fails
65       */
66      @Test
67      @Alerts({"true", "true", "true", "false", "false", "false", "false",
68               "false", "false", "false", "false", "false"})
69      public void isInteger() throws Exception {
70          final String html = DOCTYPE_HTML
71              + "<html>\n"
72              + "<head>\n"
73              + "  <script>\n"
74              + LOG_TITLE_FUNCTION
75              + "    if (Number.isInteger === undefined) {\n"
76              + "      log('no Number.isInteger');\n"
77              + "    } else {\n"
78              + "      log(Number.isInteger(0));\n"
79              + "      log(Number.isInteger(1));\n"
80              + "      log(Number.isInteger(-100000));\n"
81  
82              + "      log(Number.isInteger(0.1));\n"
83              + "      log(Number.isInteger(Math.PI));\n"
84  
85              + "      log(Number.isInteger(NaN));\n"
86              + "      log(Number.isInteger(Infinity));\n"
87              + "      log(Number.isInteger(-Infinity));\n"
88              + "      log(Number.isInteger('10'));\n"
89              + "      log(Number.isInteger(true));\n"
90              + "      log(Number.isInteger(false));\n"
91              + "      log(Number.isInteger([1]));\n"
92              + "    }\n"
93              + "  </script>\n"
94              + "</head>\n"
95              + "<body>\n"
96              + "</body>\n"
97              + "</html>";
98          loadPageVerifyTitle2(html);
99      }
100 
101     /**
102      * @throws Exception if the test fails
103      */
104     @Test
105     @Alerts({"true", "true", "true", "false", "false", "false", "false",
106              "false", "false", "false", "false", "false", "false", "false"})
107     public void isNaN() throws Exception {
108         final String html = DOCTYPE_HTML
109             + "<html>\n"
110             + "<head>\n"
111             + "  <script>\n"
112             + LOG_TITLE_FUNCTION
113             + "    if (Number.isNaN === undefined) {\n"
114             + "      log('no Number.isNaN');\n"
115             + "    } else {\n"
116             + "      log(Number.isNaN(NaN));\n"
117             + "      log(Number.isNaN(Number.NaN));\n"
118             + "      log(Number.isNaN(0 / 0));\n"
119 
120             + "      log(Number.isNaN('NaN'));\n"
121             + "      log(Number.isNaN(undefined));\n"
122             + "      log(Number.isNaN({}));\n"
123             + "      log(Number.isNaN('blabla'));\n"
124 
125             + "      log(Number.isNaN(true));\n"
126             + "      log(Number.isNaN(null));\n"
127             + "      log(Number.isNaN(37));\n"
128             + "      log(Number.isNaN('37'));\n"
129             + "      log(Number.isNaN('37.37'));\n"
130             + "      log(Number.isNaN(''));\n"
131             + "      log(Number.isNaN(' '));\n"
132             + "    }\n"
133             + "  </script>\n"
134             + "</head>\n"
135             + "<body>\n"
136             + "</body>\n"
137             + "</html>";
138         loadPageVerifyTitle2(html);
139     }
140 
141     /**
142      * @throws Exception if the test fails
143      */
144     @Test
145     @Alerts({"true", "false", "true", "false", "false", "false", "false", "true"})
146     public void isSafeInteger() throws Exception {
147         final String html = DOCTYPE_HTML
148             + "<html>\n"
149             + "<head>\n"
150             + "  <script>\n"
151             + LOG_TITLE_FUNCTION
152             + "    if (Number.isSafeInteger === undefined) {\n"
153             + "      log('no Number.isSafeInteger');\n"
154             + "    } else {\n"
155             + "      log(Number.isSafeInteger(3));\n"
156             + "      log(Number.isSafeInteger(Math.pow(2, 53)));\n"
157             + "      log(Number.isSafeInteger(Math.pow(2, 53) - 1));\n"
158             + "      log(Number.isSafeInteger(NaN));\n"
159             + "      log(Number.isSafeInteger(Infinity));\n"
160             + "      log(Number.isSafeInteger('3'));\n"
161             + "      log(Number.isSafeInteger(3.1));\n"
162             + "      log(Number.isSafeInteger(3.0));\n"
163             + "    }\n"
164             + "  </script>\n"
165             + "</head>\n"
166             + "<body>\n"
167             + "</body>\n"
168             + "</html>";
169         loadPageVerifyTitle2(html);
170     }
171 
172     /**
173      * @throws Exception if the test fails
174      */
175     @Test
176     @Alerts("3.14")
177     public void parseFloat() throws Exception {
178         final String html = DOCTYPE_HTML
179             + "<html>\n"
180             + "<head>\n"
181             + "  <script>\n"
182             + LOG_TITLE_FUNCTION
183             + "    if (Number.parseFloat === undefined) {\n"
184             + "      log('no Number.parseFloat');\n"
185             + "    } else {\n"
186             + "      log(Number.parseFloat('3.14'));\n"
187             + "    }\n"
188             + "  </script>\n"
189             + "</head>\n"
190             + "<body>\n"
191             + "</body>\n"
192             + "</html>";
193         loadPageVerifyTitle2(html);
194     }
195 
196     /**
197      * @throws Exception if the test fails
198      */
199     @Test
200     @Alerts("4")
201     public void parseInt() throws Exception {
202         final String html = DOCTYPE_HTML
203             + "<html>\n"
204             + "<head>\n"
205             + "  <script>\n"
206             + LOG_TITLE_FUNCTION
207             + "    if (Number.parseInt === undefined) {\n"
208             + "      log('no Number.parseInt');\n"
209             + "    } else {\n"
210             + "      log(Number.parseInt('4'));\n"
211             + "    }\n"
212             + "  </script>\n"
213             + "</head>\n"
214             + "<body>\n"
215             + "</body>\n"
216             + "</html>";
217         loadPageVerifyTitle2(html);
218     }
219 
220     /**
221      * Test for the methods with the same expectations for all browsers.
222      * @throws Exception if the test fails
223      */
224     @Test
225     @Alerts({"toFixed: function", "toExponential: function", "toLocaleString: function", "toPrecision: function",
226         "toString: function", "valueOf: function"})
227     public void methods_common() throws Exception {
228         final String[] methods = {"toFixed", "toExponential", "toLocaleString", "toPrecision", "toString", "valueOf"};
229         final String html = NativeDateTest.createHTMLTestMethods("new Number()", methods);
230         loadPageVerifyTitle2(html);
231     }
232 
233     /**
234      * Test for the methods with the different expectations depending on the browsers.
235      * @throws Exception if the test fails
236      */
237     @Test
238     @Alerts("toSource: undefined")
239     public void methods_different() throws Exception {
240         final String html = NativeDateTest.createHTMLTestMethods("new Number()", "toSource");
241         loadPageVerifyTitle2(html);
242     }
243 
244     /**
245      * Test for Rhino bug 538172.
246      * @throws Exception if the test fails
247      */
248     @Test
249     @Alerts("2.274341322658976e-309")
250     public void toStringRhinoBug538172() throws Exception {
251         final String html = DOCTYPE_HTML
252             + "<html><head><script>\n"
253             + LOG_TITLE_FUNCTION
254             + "log(2.274341322658976E-309);\n"
255             + "</script></head><body>\n"
256             + "</body></html>";
257         loadPageVerifyTitle2(html);
258     }
259 
260     /**
261      * @throws Exception if the test fails
262      */
263     @Test
264     @Alerts("12,345")
265     public void toLocaleString() throws Exception {
266         final String html = DOCTYPE_HTML
267             + "<html><head><script>\n"
268             + LOG_TITLE_FUNCTION
269             + "  log((12345).toLocaleString('en'));\n"
270             + "</script></head><body>\n"
271             + "</body></html>";
272         loadPageVerifyTitle2(html);
273     }
274 
275     /**
276      * @throws Exception if the test fails
277      */
278     @Test
279     @Alerts("12.345")
280     public void toLocaleStringDe() throws Exception {
281         final String html = DOCTYPE_HTML
282             + "<html><head><script>\n"
283             + LOG_TITLE_FUNCTION
284             + "  log((12345).toLocaleString('de'));\n"
285             + "</script></head><body>\n"
286             + "</body></html>";
287         loadPageVerifyTitle2(html);
288     }
289 
290     /**
291      * @throws Exception if the test fails
292      */
293     @Test
294     @Alerts("12,345")
295     public void toLocaleStringEnUS() throws Exception {
296         final String html = DOCTYPE_HTML
297             + "<html><head><script>\n"
298             + LOG_TITLE_FUNCTION
299             + "  log((12345).toLocaleString('en-US'));\n"
300             + "</script></head><body>\n"
301             + "</body></html>";
302         loadPageVerifyTitle2(html);
303     }
304 
305     /**
306      * @throws Exception if the test fails
307      */
308     @Test
309     @Alerts("12,345")
310     public void toLocaleStringNoParam() throws Exception {
311         final String html = DOCTYPE_HTML
312             + "<html><head><script>\n"
313             + LOG_TITLE_FUNCTION
314             + "  try {\n"
315             + "    log((12345).toLocaleString());\n"
316             + "  } catch(e) { log(e); }\n"
317             + "</script></head><body>\n"
318             + "</body></html>";
319         loadPageVerifyTitle2(html);
320     }
321 
322     /**
323      * @throws Exception if the test fails
324      */
325     @Test
326     @Alerts("RangeError")
327     public void toLocaleStringHintertupfingen() throws Exception {
328         final String html = DOCTYPE_HTML
329             + "<html><head><script>\n"
330             + LOG_TITLE_FUNCTION
331             + "  try {\n"
332             + "    log((12345).toLocaleString('Hintertupfingen'));\n"
333             + "  } catch(e) { logEx(e); }\n"
334             + "</script></head><body>\n"
335             + "</body></html>";
336         loadPageVerifyTitle2(html);
337     }
338 }