1'
2' Copyright 2013 Piotr Caban for CodeWeavers
3'
4' This library is free software; you can redistribute it and/or
5' modify it under the terms of the GNU Lesser General Public
6' License as published by the Free Software Foundation; either
7' version 2.1 of the License, or (at your option) any later version.
8'
9' This library is distributed in the hope that it will be useful,
10' but WITHOUT ANY WARRANTY; without even the implied warranty of
11' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12' Lesser General Public License for more details.
13'
14' You should have received a copy of the GNU Lesser General Public
15' License along with this library; if not, write to the Free Software
16' Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17'
18
19Option Explicit
20
21Dim x, matches, match, submatch
22
23Set x = CreateObject("vbscript.regexp")
24Call ok(getVT(x.Pattern) = "VT_BSTR", "getVT(RegExp.Pattern) = " & getVT(x.Pattern))
25Call ok(x.Pattern = "", "RegExp.Pattern = " & x.Pattern)
26Call ok(getVT(x.IgnoreCase) = "VT_BOOL", "getVT(RegExp.IgnoreCase) = " & getVT(x.IgnoreCase))
27Call ok(x.IgnoreCase = false, "RegExp.IgnoreCase = " & x.IgnoreCase)
28Call ok(getVT(x.Global) = "VT_BOOL", "getVT(RegExp.Global) = " & getVT(x.Global))
29Call ok(x.Global = false, "RegExp.Global = " & x.Global)
30Call ok(getVT(x.Multiline) = "VT_BOOL", "getVT(RegExp.Multiline) = " & getVT(x.Multiline))
31Call ok(x.Multiline = false, "RegExp.Multiline = " & x.Multiline)
32
33x.Pattern = "a+"
34matches = x.Test(" aabaaa")
35Call ok(matches = true, "RegExp.Test returned: " & matches)
36Set matches = x.Execute(" aabaaa")
37Call ok(getVT(matches.Count) = "VT_I4", "getVT(matches.Count) = " & getVT(matches.Count))
38Call ok(matches.Count = 1, "matches.Count = " & matches.Count)
39Set match = matches.Item(0)
40Call ok(match.Value = "aa", "match.Value = " & match.Value)
41Call ok(match.FirstIndex = 1, "match.FirstIndex = " & match.FirstIndex)
42Call ok(match.Length = 2, "match.Length = " & match.Length)
43Set submatch = match.SubMatches
44Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
45
46x.Global = true
47Set matches = x.Execute(" aabaaa")
48Call ok(matches.Count = 2, "matches.Count = " & matches.Count)
49Set match = matches.Item(0)
50Call ok(match.Value = "aa", "match.Value = " & match.Value)
51Call ok(match.FirstIndex = 1, "match.FirstIndex = " & match.FirstIndex)
52Call ok(match.Length = 2, "match.Length = " & match.Length)
53Set submatch = match.SubMatches
54Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
55Set match = matches.Item(1)
56Call ok(match.Value = "aaa", "match.Value = " & match.Value)
57Call ok(match.FirstIndex = 4, "match.FirstIndex = " & match.FirstIndex)
58Call ok(match.Length = 3, "match.Length = " & match.Length)
59Set submatch = match.SubMatches
60Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
61
62Set matches = x.Execute(" aabaaa")
63Call ok(matches.Count = 2, "matches.Count = " & matches.Count)
64Set match = matches.Item(0)
65Call ok(match.Value = "aa", "match.Value = " & match.Value)
66Call ok(match.FirstIndex = 1, "match.FirstIndex = " & match.FirstIndex)
67Call ok(match.Length = 2, "match.Length = " & match.Length)
68Set submatch = match.SubMatches
69Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
70
71x.Pattern = "^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$"
72x.Global = false
73Set matches = x.Execute("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
74Call ok(matches.Count = 0, "matches.Count = " & matches.Count)
75Set submatch = match.SubMatches
76Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
77
78x.Pattern = "(a|b)+|(c)"
79Set matches = x.Execute("aa")
80Call ok(matches.Count = 1, "matches.Count = " & matches.Count)
81Set match = matches.Item(0)
82Call ok(match.Value = "aa", "match.Value = " & match.Value)
83Call ok(match.FirstIndex = 0, "match.FirstIndex = " & match.FirstIndex)
84Call ok(match.Length = 2, "match.Length = " & match.Length)
85Set submatch = match.SubMatches
86Call ok(submatch.Count = 2, "submatch.Count = " & submatch.Count)
87Call ok(getVT(submatch.Item(0)) = "VT_BSTR", "getVT(submatch.Item(0)) = " & getVT(submatch.Item(0)))
88Call ok(submatch.Item(0) = "a", "submatch.Item(0) = " & submatch.Item(0))
89Call ok(getVT(submatch.Item(1)) = "VT_EMPTY", "getVT(submatch.Item(1)) = " & getVT(submatch.Item(1)))
90Call ok(submatch.Item(1) = "", "submatch.Item(0) = " & submatch.Item(1))
91
92matches = x.Test("  a ")
93Call ok(matches = true, "RegExp.Test returned: " & matches)
94matches = x.Test("  a ")
95Call ok(matches = true, "RegExp.Test returned: " & matches)
96
97x.Pattern = "\[([^\[]+)\]"
98x.Global = true
99Set matches = x.Execute(" [test]  ")
100Call ok(matches.Count = 1, "matches.Count = " & matches.Count)
101Set match = matches.Item(0)
102Call ok(match.Value = "[test]", "match.Value = " & match.Value)
103Call ok(match.FirstIndex = 1, "match.FirstIndex = " & match.FirstIndex)
104Call ok(match.Length = 6, "match.Length = " & match.Length)
105Set submatch = match.SubMatches
106Call ok(submatch.Count = 1, "submatch.Count = " & submatch.Count)
107Call ok(submatch.Item(0) = "test", "submatch.Item(0) = " & submatch.Item(0))
108
109x.Pattern = "Ab"
110x.IgnoreCase = true
111Set matches = x.Execute("abcaBc")
112Call ok(matches.Count = 2, "matches.Count = " & matches.Count)
113Set match = matches.Item(0)
114Call ok(match.Value = "ab", "match.Value = " & match.Value)
115Call ok(match.FirstIndex = 0, "match.FirstIndex = " & match.FirstIndex)
116Call ok(match.Length = 2, "match.Length = " & match.Length)
117Set submatch = match.SubMatches
118Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
119Set match = matches.Item(1)
120Call ok(match.Value = "aB", "match.Value = " & match.Value)
121Call ok(match.FirstIndex = 3, "match.FirstIndex = " & match.FirstIndex)
122Call ok(match.Length = 2, "match.Length = " & match.Length)
123Set submatch = match.SubMatches
124Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
125
126x.Pattern = "a+b"
127x.IgnoreCase = false
128Set matches = x.Execute("aaabcabc")
129Call ok(matches.Count = 2, "matches.Count = " & matches.Count)
130Set match = matches.Item(0)
131Call ok(match.Value = "aaab", "match.Value = " & match.Value)
132Call ok(match.FirstIndex = 0, "match.FirstIndex = " & match.FirstIndex)
133Call ok(match.Length = 4, "match.Length = " & match.Length)
134Set submatch = match.SubMatches
135Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
136Set match = matches.Item(1)
137Call ok(match.Value = "ab", "match.Value = " & match.Value)
138Call ok(match.FirstIndex = 5, "match.FirstIndex = " & match.FirstIndex)
139Call ok(match.Length = 2, "match.Length = " & match.Length)
140Set submatch = match.SubMatches
141Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
142
143x.Pattern = "\\"
144Set matches = x.Execute("aaa\\cabc")
145Call ok(matches.Count = 2, "matches.Count = " & matches.Count)
146Set match = matches.Item(0)
147Call ok(match.Value = "\", "match.Value = " & match.Value)
148Call ok(match.FirstIndex = 3, "match.FirstIndex = " & match.FirstIndex)
149Call ok(match.Length = 1, "match.Length = " & match.Length)
150Set submatch = match.SubMatches
151Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
152Set match = matches.Item(1)
153Call ok(match.Value = "\", "match.Value = " & match.Value)
154Call ok(match.FirstIndex = 4, "match.FirstIndex = " & match.FirstIndex)
155Call ok(match.Length = 1, "match.Length = " & match.Length)
156Set submatch = match.SubMatches
157Call ok(submatch.Count = 0, "submatch.Count = " & submatch.Count)
158
159x.Pattern = "(a)(b)cabc"
160Set matches = x.Execute("abcabc")
161Call ok(matches.Count = 1, "matches.Count = " & matches.Count)
162Set match = matches.Item(0)
163Call ok(match.Value = "abcabc", "match.Value = " & match.Value)
164Call ok(match.FirstIndex = 0, "match.FirstIndex = " & match.FirstIndex)
165Call ok(match.Length = 6, "match.Length = " & match.Length)
166Set submatch = match.SubMatches
167Call ok(submatch.Count = 2, "submatch.Count = " & submatch.Count)
168Call ok(submatch.Item(0) = "a", "submatch.Item(0) = " & submatch.Item(0))
169Call ok(submatch.Item(1) = "b", "submatch.Item(0) = " & submatch.Item(1))
170
171Set x = new regexp
172Call ok(x.Pattern = "", "RegExp.Pattern = " & x.Pattern)
173Call ok(x.IgnoreCase = false, "RegExp.IgnoreCase = " & x.IgnoreCase)
174Call ok(x.Global = false, "RegExp.Global = " & x.Global)
175Call ok(x.Multiline = false, "RegExp.Multiline = " & x.Multiline)
176
177set matches = x.execute("test")
178Call ok(matches.Count = 1, "matches.Count = " & matches.Count)
179x.pattern = ""
180set matches = x.execute("test")
181Call ok(matches.Count = 1, "matches.Count = " & matches.Count)
182set match = matches.item(0)
183Call ok(match.Value = "", "match.Value = " & match.Value)
184x.global = true
185set matches = x.execute("test")
186Call ok(matches.Count = 5, "matches.Count = " & matches.Count)
187set match = matches.item(0)
188Call ok(match.Value = "", "match.Value = " & match.Value)
189set match = matches.item(4)
190Call ok(match.Value = "", "match.Value = " & match.Value)
191matches = x.test("test")
192Call ok(matches = true, "matches = " & matches)
193
194Call reportSuccess()
195