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.regexp.mozilla.js1_2;
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   * Tests originally in '/js/src/tests/js1_2/regexp/string_split.js'.
25   *
26   * @author Ahmed Ashour
27   * @author Frank Danek
28   */
29  @RunWith(BrowserRunner.class)
30  public class StringSplitTest extends WebDriverTestCase {
31  
32      /**
33       * Tests 'a b c de f'.split(/\s/).
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts("a,b,c,de,f")
38      public void test1() throws Exception {
39          test("'a b c de f'.split(/\\s/)");
40      }
41  
42      /**
43       * Tests 'a b c de f'.split(/\s/,3).
44       * @throws Exception if the test fails
45       */
46      @Test
47      @Alerts("a,b,c")
48      public void test2() throws Exception {
49          test("'a b c de f'.split(/\\s/,3)");
50      }
51  
52      /**
53       * Tests 'a b c de f'.split(/X/).
54       * @throws Exception if the test fails
55       */
56      @Test
57      @Alerts("a b c de f")
58      public void test3() throws Exception {
59          test("'a b c de f'.split(/X/)");
60      }
61  
62      /**
63       * Tests 'dfe23iu 34 =+65--'.split(/\d+/).
64       * @throws Exception if the test fails
65       */
66      @Test
67      @Alerts("dfe,iu , =+,--")
68      public void test4() throws Exception {
69          test("'dfe23iu 34 =+65--'.split(/\\d+/)");
70      }
71  
72      /**
73       * Tests 'dfe23iu 34 =+65--'.split(new RegExp('\\d+')).
74       * @throws Exception if the test fails
75       */
76      @Test
77      @Alerts("dfe,iu , =+,--")
78      public void test5() throws Exception {
79          test("'dfe23iu 34 =+65--'.split(new RegExp('\\\\d+'))");
80      }
81  
82      /**
83       * Tests 'abc'.split(/[a-z]/).
84       * @throws Exception if the test fails
85       */
86      @Test
87      @Alerts(",,,")
88      public void test6() throws Exception {
89          test("'abc'.split(/[a-z]/)");
90      }
91  
92      /**
93       * Tests 'abc'.split(/[a-z]/).
94       * @throws Exception if the test fails
95       */
96      @Test
97      @Alerts(",,,")
98      public void test7() throws Exception {
99          test("'abc'.split(/[a-z]/)");
100     }
101 
102     /**
103      * Tests 'abc'.split(new RegExp('[a-z]')).
104      * @throws Exception if the test fails
105      */
106     @Test
107     @Alerts(",,,")
108     public void test8() throws Exception {
109         test("'abc'.split(new RegExp('[a-z]'))");
110     }
111 
112     /**
113      * Tests 'abc'.split(new RegExp('[a-z]')).
114      * @throws Exception if the test fails
115      */
116     @Test
117     @Alerts(",,,")
118     public void test9() throws Exception {
119         test("'abc'.split(new RegExp('[a-z]'))");
120     }
121 
122     private void test(final String script) throws Exception {
123         final String html = "<html><head><script>\n"
124             + LOG_TITLE_FUNCTION
125             + "  log(" + script + ");\n"
126             + "</script></head><body>\n"
127             + "</body></html>";
128         loadPageVerifyTitle2(html);
129     }
130 }