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