1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.regexp;
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
25
26
27
28
29 @RunWith(BrowserRunner.class)
30 public class HtmlUnitRegExpProxyInstancePropertiesTest extends WebDriverTestCase {
31
32 private void testProperties(final String string, final String regexp) throws Exception {
33 final String html = "<html><head>\n"
34 + "<script>\n"
35 + LOG_TITLE_FUNCTION
36 + " function test() {\n"
37 + " var str = '" + string + "';\n"
38 + " var myRegExp = " + regexp + ";\n"
39 + " log(myRegExp.exec(str));\n"
40 + " log(myRegExp.source);\n"
41 + " log(myRegExp.ignoreCase);\n"
42 + " log(myRegExp.global);\n"
43 + " log(myRegExp.multiline);\n"
44 + " log(myRegExp.lastIndex);\n"
45 + " }\n"
46 + "</script></head><body onload='test()'>\n"
47 + "</body></html>";
48
49 loadPageVerifyTitle2(html);
50 }
51
52
53
54
55 @Test
56 @Alerts({"HtmlUnit", "HtmlUnit", "false", "false", "false", "0"})
57 public void regExpPropertyNone() throws Exception {
58 testProperties("HtmlUnit", "new RegExp('HtmlUnit')");
59 }
60
61
62
63
64 @Test
65 @Alerts({"HtmlUnit", "HtmlUnit", "true", "false", "false", "0"})
66 public void regExpPropertyIgnoreCase() throws Exception {
67 testProperties("HtmlUnit", "new RegExp('HtmlUnit', 'i')");
68 }
69
70
71
72
73 @Test
74 @Alerts({"HtmlUnit", "HtmlUnit", "false", "true", "false", "8"})
75 public void regExpPropertyGlobal() throws Exception {
76 testProperties("HtmlUnit", "new RegExp('HtmlUnit', 'g')");
77 }
78
79
80
81
82 @Test
83 @Alerts({"HtmlUnit", "HtmlUnit", "false", "false", "true", "0"})
84 public void regExpPropertyMultiline() throws Exception {
85 testProperties("HtmlUnit", "new RegExp('HtmlUnit', 'm')");
86 }
87
88
89
90
91 @Test
92 @Alerts({"0", "true", "8", "true", "27", "true", "8", "true", "27", "false", "0"})
93 public void regExpPropertyLastIndexGlobalSetTest() throws Exception {
94 final String html = "<html><head>\n"
95 + "<script>\n"
96 + LOG_TITLE_FUNCTION
97 + " function test() {\n"
98 + " var str = 'HtmlUnit is great (HtmlUnit)';\n"
99 + " var myRegExp = new RegExp('HtmlUnit', 'g');\n"
100 + " log(myRegExp.lastIndex);\n"
101 + " log(myRegExp.test(str));\n"
102 + " log(myRegExp.lastIndex);\n"
103 + " log(myRegExp.test(str));\n"
104 + " log(myRegExp.lastIndex);\n"
105
106 + " myRegExp.lastIndex = 0;\n"
107 + " log(myRegExp.test(str));\n"
108 + " log(myRegExp.lastIndex);\n"
109
110 + " myRegExp.lastIndex = 1;\n"
111 + " log(myRegExp.test(str));\n"
112 + " log(myRegExp.lastIndex);\n"
113 + " myRegExp.lastIndex = 50;\n"
114 + " log(myRegExp.test(str));\n"
115 + " log(myRegExp.lastIndex);\n"
116 + " }\n"
117 + "</script></head><body onload='test()'>\n"
118 + "</body></html>";
119
120 loadPageVerifyTitle2(html);
121 }
122
123
124
125
126 @Test
127 @Alerts({"0", "HtmlUnit", "8"})
128 public void regExpPropertyLastIndexGlobalExec() throws Exception {
129 final String html = "<html><head>\n"
130 + "<script>\n"
131 + LOG_TITLE_FUNCTION
132 + " function test() {\n"
133 + " var str = 'HtmlUnit is great';\n"
134 + " var myRegExp = new RegExp('HtmlUnit', 'g');\n"
135 + " log(myRegExp.lastIndex);\n"
136 + " log(myRegExp.exec(str));\n"
137 + " log(myRegExp.lastIndex);\n"
138 + " }\n"
139 + "</script></head><body onload='test()'>\n"
140 + "</body></html>";
141
142 loadPageVerifyTitle2(html);
143 }
144
145
146
147
148
149 @Test
150 @Alerts({"html,body,div,div,div", "undefined", "undefined", "undefined",
151 "html", "1", "undefined", "/html/body/div[5]/div[1]/div[1]"})
152 public void regExResultProperties() throws Exception {
153 final String html = "<html><head>\n"
154 + "<script>\n"
155 + LOG_TITLE_FUNCTION
156 + " function test() {\n"
157 + " var source = '/html/body/div[5]/div[1]/div[1]';\n"
158 + " var myRegexp = new RegExp(\n"
159 + " '\\\\$?(?:(?![0-9-])[\\\\w-]+:)?(?![0-9-])[\\\\w-]+' ,'g');\n"
160
161 + " var result = source.match(myRegexp);\n"
162 + " log(result);\n"
163 + " if (result) {\n"
164 + " log(result.index);\n"
165 + " log(result.lastIndex);\n"
166 + " log(result.input);\n"
167 + " }\n"
168
169 + " result = myRegexp.exec(source);\n"
170 + " log(result);\n"
171 + " if (result) {\n"
172 + " log(result.index);\n"
173 + " log(result.lastIndex);\n"
174 + " log(result.input);\n"
175 + " }\n"
176 + " }\n"
177 + "</script></head>\n"
178 + "<body onload='test()'>\n"
179 + "</body></html>";
180
181 loadPageVerifyTitle2(html);
182 }
183 }