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