1 |
|
---|
2 | # Constant
|
---|
3 | assert("OnigRegexp::CONSTANT") do
|
---|
4 | OnigRegexp::IGNORECASE == 1 and OnigRegexp::EXTENDED == 2 and OnigRegexp::MULTILINE == 4
|
---|
5 | end
|
---|
6 |
|
---|
7 |
|
---|
8 | # Class method
|
---|
9 | assert('OnigRgexp.compile', '15.2.15.6.2') do
|
---|
10 | assert_equal OnigRegexp.compile('.*'), OnigRegexp.compile('.*')
|
---|
11 | end
|
---|
12 |
|
---|
13 | assert('OnigRegexp.escape', '15.2.15.6.2') do
|
---|
14 | escaping_chars = "\n\t\r\f #$()*+-.?[\\]^{|}"
|
---|
15 | assert_equal '\n\t\r\f\\ \#\$\(\)\*\+\-\.\?\[\\\\\]\^\{\|\}', OnigRegexp.escape(escaping_chars)
|
---|
16 | assert_equal 'cute\nmruby\tcute', OnigRegexp.escape("cute\nmruby\tcute")
|
---|
17 | end
|
---|
18 |
|
---|
19 | assert('OnigRegexp.last_match', '15.2.15.6.3') do
|
---|
20 | OnigRegexp.new('.*') =~ 'ginka'
|
---|
21 | assert_equal 'ginka', OnigRegexp.last_match[0]
|
---|
22 | end
|
---|
23 |
|
---|
24 | assert('OnigRegexp.quote', '15.2.15.6.4') do
|
---|
25 | assert_equal '\n', OnigRegexp.quote("\n")
|
---|
26 | end
|
---|
27 |
|
---|
28 | # Instance method
|
---|
29 | assert('OnigRegexp#initialize', '15.2.15.7.1') do
|
---|
30 | OnigRegexp.new(".*") and OnigRegexp.new(".*", OnigRegexp::MULTILINE)
|
---|
31 | end
|
---|
32 |
|
---|
33 | assert('OnigRegexp#initialize_copy', '15.2.15.7.2') do
|
---|
34 | r1 = OnigRegexp.new(".*")
|
---|
35 | r2 = r1.dup
|
---|
36 | assert_equal r1, r2
|
---|
37 | assert_equal 'kawa', r2.match('kawa')[0]
|
---|
38 | end
|
---|
39 |
|
---|
40 | assert("OnigRegexp#==", '15.2.15.7.3') do
|
---|
41 | reg1 = reg2 = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
42 | reg3 = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
43 | reg4 = OnigRegexp.new("(https://[^/]+)[-a-zA-Z0-9./]+")
|
---|
44 |
|
---|
45 | assert_true(reg1 == reg2 && reg1 == reg3 && !(reg1 == reg4))
|
---|
46 |
|
---|
47 | assert_false(OnigRegexp.new("a") == "a")
|
---|
48 | end
|
---|
49 |
|
---|
50 | assert("OnigRegexp#===", '15.2.15.7.4') do
|
---|
51 | reg = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
52 | assert_true reg === "http://example.com"
|
---|
53 | assert_false reg === "htt://example.com"
|
---|
54 | end
|
---|
55 |
|
---|
56 | assert('OnigRegexp#=~', '15.2.15.7.5') do
|
---|
57 | assert_equal(0) { OnigRegexp.new('.*') =~ 'akari' }
|
---|
58 | assert_equal(nil) { OnigRegexp.new('t') =~ 'akari' }
|
---|
59 | end
|
---|
60 |
|
---|
61 | assert("OnigRegexp#casefold?", '15.2.15.7.6') do
|
---|
62 | assert_false OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+", OnigRegexp::MULTILINE).casefold?
|
---|
63 | assert_true OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+", OnigRegexp::IGNORECASE | OnigRegexp::EXTENDED).casefold?
|
---|
64 | assert_true OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+", OnigRegexp::MULTILINE | OnigRegexp::IGNORECASE).casefold?
|
---|
65 | assert_false OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+").casefold?
|
---|
66 | assert_true OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+", true).casefold?
|
---|
67 | end
|
---|
68 |
|
---|
69 | assert("OnigRegexp#match", '15.2.15.7.7') do
|
---|
70 | reg = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
71 | assert_false reg.match("http://masamitsu-murase.12345/hoge.html").nil?
|
---|
72 | assert_nil reg.match("http:///masamitsu-murase.12345/hoge.html")
|
---|
73 | end
|
---|
74 |
|
---|
75 | assert("OnigRegexp#source", '15.2.15.7.8') do
|
---|
76 | str = "(https?://[^/]+)[-a-zA-Z0-9./]+"
|
---|
77 | reg = OnigRegexp.new(str)
|
---|
78 |
|
---|
79 | reg.source == str
|
---|
80 | end
|
---|
81 |
|
---|
82 | if OnigRegexp.const_defined? :ASCII_RANGE
|
---|
83 | assert('OnigRegexp#options (no options)') do
|
---|
84 | assert_equal OnigRegexp::ASCII_RANGE | OnigRegexp::POSIX_BRACKET_ALL_RANGE | OnigRegexp::WORD_BOUND_ALL_RANGE, OnigRegexp.new(".*").options
|
---|
85 | end
|
---|
86 |
|
---|
87 | assert('OnigRegexp#options (multiline)') do
|
---|
88 | assert_equal OnigRegexp::MULTILINE | OnigRegexp::ASCII_RANGE | OnigRegexp::POSIX_BRACKET_ALL_RANGE | OnigRegexp::WORD_BOUND_ALL_RANGE, OnigRegexp.new(".*", OnigRegexp::MULTILINE).options
|
---|
89 | end
|
---|
90 | end
|
---|
91 |
|
---|
92 | assert("OnigRegexp#inspect") do
|
---|
93 | reg = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
94 |
|
---|
95 | assert_equal '/(https?:\/\/[^\/]+)[-a-zA-Z0-9.\/]+/', reg.inspect
|
---|
96 | assert_equal '/abc\nd\te/mi', OnigRegexp.new("abc\nd\te", OnigRegexp::MULTILINE | OnigRegexp::IGNORECASE).inspect
|
---|
97 | assert_equal '/abc/min', OnigRegexp.new("abc", OnigRegexp::MULTILINE | OnigRegexp::IGNORECASE, "none").inspect
|
---|
98 | end
|
---|
99 |
|
---|
100 | assert("OnigRegexp#to_s") do
|
---|
101 | assert_equal '(?-mix:ab+c)', OnigRegexp.new("ab+c").to_s
|
---|
102 | assert_equal '(?-mix:ab+c)', /ab+c/.to_s
|
---|
103 | assert_equal '(?mx-i:ab+c)', OnigRegexp.new("ab+c", OnigRegexp::MULTILINE | OnigRegexp::EXTENDED).to_s
|
---|
104 | assert_equal '(?mi-x:ab+c)', /ab+c/im.to_s
|
---|
105 | assert_equal '(?mi-x:ab+c)', /ab+c/imn.to_s
|
---|
106 | end
|
---|
107 |
|
---|
108 | assert("OnigRegexp#to_s (composition)") do
|
---|
109 | re1 = OnigRegexp.new("ab+c")
|
---|
110 | re2 = OnigRegexp.new("xy#{re1}z")
|
---|
111 | assert_equal '(?-mix:xy(?-mix:ab+c)z)', re2.to_s
|
---|
112 |
|
---|
113 | re3 = OnigRegexp.new("ab.+c", OnigRegexp::MULTILINE)
|
---|
114 | re4 = OnigRegexp.new("xy#{re3}z", OnigRegexp::IGNORECASE)
|
---|
115 | assert_equal '(?i-mx:xy(?m-ix:ab.+c)z)', re4.to_s
|
---|
116 | end
|
---|
117 |
|
---|
118 | # Extended patterns.
|
---|
119 | assert("OnigRegexp#match (no flags)") do
|
---|
120 | [
|
---|
121 | [ ".*", "abcd\nefg", "abcd" ],
|
---|
122 | [ "^a.", "abcd\naefg", "ab" ],
|
---|
123 | [ "^a.", "bacd\naefg", "ae" ],
|
---|
124 | [ ".$", "bacd\naefg", "d" ]
|
---|
125 | ].each do |reg, str, result|
|
---|
126 | m = OnigRegexp.new(reg).match(str)
|
---|
127 | assert_equal result, m[0] if assert_false m.nil?
|
---|
128 | end
|
---|
129 | end
|
---|
130 |
|
---|
131 | assert("OnigRegexp#match (multiline)") do
|
---|
132 | patterns = [
|
---|
133 | [ OnigRegexp.new(".*", OnigRegexp::MULTILINE), "abcd\nefg", "abcd\nefg" ]
|
---|
134 | ]
|
---|
135 |
|
---|
136 | patterns.all?{ |reg, str, result| reg.match(str)[0] == result }
|
---|
137 | end
|
---|
138 |
|
---|
139 | assert("OnigRegexp#match (ignorecase)") do
|
---|
140 | [
|
---|
141 | [ "aBcD", "00AbcDef", "AbcD" ],
|
---|
142 | [ "0x[a-f]+", "00XaBCdefG", "0XaBCdef" ],
|
---|
143 | [ "0x[^c-f]+", "00XaBCdefG", "0XaB" ]
|
---|
144 | ].each do |reg, str, result|
|
---|
145 | m = OnigRegexp.new(reg, OnigRegexp::IGNORECASE|OnigRegexp::EXTENDED).match(str)
|
---|
146 | assert_equal result, m[0] if assert_false m.nil?
|
---|
147 | end
|
---|
148 | end
|
---|
149 |
|
---|
150 | assert("OnigRegexp#match (none encoding)") do
|
---|
151 | assert_equal 2, /\x82/n =~ "あ"
|
---|
152 | end
|
---|
153 |
|
---|
154 | assert('OnigRegexp.version') do
|
---|
155 | OnigRegexp.version.kind_of? String
|
---|
156 | end
|
---|
157 |
|
---|
158 | def onig_match_data_example
|
---|
159 | OnigRegexp.new('(\w+)(\w)').match('+aaabb-')
|
---|
160 | end
|
---|
161 |
|
---|
162 | assert('OnigMatchData.new') do
|
---|
163 | assert_raise(NoMethodError) { OnigMatchData.new('aaa', 'i') }
|
---|
164 | end
|
---|
165 |
|
---|
166 | assert('OnigMatchData#[]', '15.2.16.3.1') do
|
---|
167 | m = onig_match_data_example
|
---|
168 | assert_equal 'aaabb', m[0]
|
---|
169 | assert_equal 'aaab', m[1]
|
---|
170 | assert_equal 'b', m[2]
|
---|
171 | assert_nil m[3]
|
---|
172 |
|
---|
173 | m = OnigRegexp.new('(?<name>\w\w)').match('aba')
|
---|
174 | assert_raise(TypeError) { m[[]] }
|
---|
175 | assert_raise(IndexError) { m['nam'] }
|
---|
176 | assert_equal 'ab', m[:name]
|
---|
177 | assert_equal 'ab', m['name']
|
---|
178 | assert_equal 'ab', m[1]
|
---|
179 |
|
---|
180 | m = OnigRegexp.new('(\w) (\w) (\w) (\w)').match('a b c d')
|
---|
181 | assert_equal %w(a b c d), m[1..-1]
|
---|
182 | end
|
---|
183 |
|
---|
184 | assert('OnigMatchData#begin', '15.2.16.3.2') do
|
---|
185 | m = onig_match_data_example
|
---|
186 | assert_equal 1, m.begin(0)
|
---|
187 | assert_equal 1, m.begin(1)
|
---|
188 | assert_raise(IndexError) { m.begin 3 }
|
---|
189 | end
|
---|
190 |
|
---|
191 | assert('OnigMatchData#captures', '15.2.16.3.3') do
|
---|
192 | m = onig_match_data_example
|
---|
193 | assert_equal ['aaab', 'b'], m.captures
|
---|
194 |
|
---|
195 | m = OnigRegexp.new('(\w+)(\d)?').match('+aaabb-')
|
---|
196 | assert_equal ['aaabb', nil], m.captures
|
---|
197 | end
|
---|
198 |
|
---|
199 | assert('OnigMatchData#end', '15.2.16.3.4') do
|
---|
200 | m = onig_match_data_example
|
---|
201 | assert_equal 6, m.end(0)
|
---|
202 | assert_equal 5, m.end(1)
|
---|
203 | assert_raise(IndexError) { m.end 3 }
|
---|
204 | end
|
---|
205 |
|
---|
206 | assert('OnigMatchData#initialize_copy', '15.2.16.3.5') do
|
---|
207 | m = onig_match_data_example
|
---|
208 | c = m.dup
|
---|
209 | assert_equal m.to_a, c.to_a
|
---|
210 | end
|
---|
211 |
|
---|
212 | assert('OnigMatchData#length', '15.2.16.3.6') do
|
---|
213 | assert_equal 3, onig_match_data_example.length
|
---|
214 | end
|
---|
215 |
|
---|
216 | assert('OnigMatchData#offset', '15.2.16.3.7') do
|
---|
217 | assert_equal [1, 6], onig_match_data_example.offset(0)
|
---|
218 | assert_equal [1, 5], onig_match_data_example.offset(1)
|
---|
219 | end
|
---|
220 |
|
---|
221 | assert('OnigMatchData#post_match', '15.2.16.3.8') do
|
---|
222 | assert_equal '-', onig_match_data_example.post_match
|
---|
223 | end
|
---|
224 |
|
---|
225 | assert('OnigMatchData#pre_match', '15.2.16.3.9') do
|
---|
226 | assert_equal '+', onig_match_data_example.pre_match
|
---|
227 | end
|
---|
228 |
|
---|
229 | assert('OnigMatchData#size', '15.2.16.3.10') do
|
---|
230 | assert_equal 3, onig_match_data_example.length
|
---|
231 | end
|
---|
232 |
|
---|
233 | assert('OnigMatchData#string', '15.2.16.3.11') do
|
---|
234 | assert_equal '+aaabb-', onig_match_data_example.string
|
---|
235 | end
|
---|
236 |
|
---|
237 | assert('OnigMatchData#to_a', '15.2.16.3.12') do
|
---|
238 | assert_equal ['aaabb', 'aaab', 'b'], onig_match_data_example.to_a
|
---|
239 | end
|
---|
240 |
|
---|
241 | assert('OnigMatchData#to_s', '15.2.16.3.13') do
|
---|
242 | assert_equal 'aaabb', onig_match_data_example.to_s
|
---|
243 | end
|
---|
244 |
|
---|
245 | assert('OnigMatchData#regexp') do
|
---|
246 | assert_equal '(\w+)(\w)', onig_match_data_example.regexp.source
|
---|
247 | end
|
---|
248 |
|
---|
249 | assert('Invalid regexp') do
|
---|
250 | assert_raise(ArgumentError) { OnigRegexp.new '[aio' }
|
---|
251 | end
|
---|
252 |
|
---|
253 | assert('String#onig_regexp_gsub') do
|
---|
254 | test_str = 'hello mruby'
|
---|
255 | assert_equal 'h*ll* mr*by', test_str.onig_regexp_gsub(OnigRegexp.new('[aeiou]'), '*')
|
---|
256 | assert_equal 'h<e>ll<o> mr<u>by', test_str.onig_regexp_gsub(OnigRegexp.new('([aeiou])'), '<\1>')
|
---|
257 | assert_equal 'h e l l o m r u b y ', test_str.onig_regexp_gsub(OnigRegexp.new('\w')) { |v| v + ' ' }
|
---|
258 | assert_equal 'h{e}ll{o} mr{u}by', test_str.onig_regexp_gsub(OnigRegexp.new('(?<hoge>[aeiou])'), '{\k<hoge>}')
|
---|
259 | assert_raise(IndexError) { test_str.onig_regexp_gsub(OnigRegexp.new('(mruby)'), '<\2>') }
|
---|
260 | end
|
---|
261 |
|
---|
262 | assert('String#onig_regexp_scan') do
|
---|
263 | test_str = 'mruby world'
|
---|
264 | assert_equal ['mruby', 'world'], test_str.onig_regexp_scan(OnigRegexp.new('\w+'))
|
---|
265 | assert_equal ['mru', 'by ', 'wor'], test_str.onig_regexp_scan(OnigRegexp.new('...'))
|
---|
266 | assert_equal [['mru'], ['by '], ['wor']], test_str.onig_regexp_scan(OnigRegexp.new('(...)'))
|
---|
267 | assert_equal [['mr', 'ub'], ['y ', 'wo']], test_str.onig_regexp_scan(OnigRegexp.new('(..)(..)'))
|
---|
268 |
|
---|
269 | result = []
|
---|
270 | assert_equal test_str, test_str.onig_regexp_scan(OnigRegexp.new('\w+')) { |v| result << "<<#{v}>>" }
|
---|
271 | assert_equal ['<<mruby>>', '<<world>>'], result
|
---|
272 |
|
---|
273 | result = ''
|
---|
274 | assert_equal test_str, test_str.onig_regexp_scan(OnigRegexp.new('(.)(.)')) { |x, y| result += y; result += x }
|
---|
275 | assert_equal 'rmbu yowlr', result
|
---|
276 | end
|
---|
277 |
|
---|
278 | assert('String#onig_regexp_sub') do
|
---|
279 | test_str = 'hello mruby'
|
---|
280 | assert_equal 'h*llo mruby', test_str.onig_regexp_sub(OnigRegexp.new('[aeiou]'), '*')
|
---|
281 | assert_equal 'h<e>llo mruby', test_str.onig_regexp_sub(OnigRegexp.new('([aeiou])'), '<\1>')
|
---|
282 | assert_equal 'h ello mruby', test_str.onig_regexp_sub(OnigRegexp.new('\w')) { |v| v + ' ' }
|
---|
283 | assert_equal 'h{e}llo mruby', test_str.onig_regexp_sub(OnigRegexp.new('(?<hoge>[aeiou])'), '{\k<hoge>}')
|
---|
284 | end
|
---|
285 |
|
---|
286 | assert('String#onig_regexp_split') do
|
---|
287 | test_str = 'cute mruby cute'
|
---|
288 | assert_equal ['cute', 'mruby', 'cute'], test_str.onig_regexp_split
|
---|
289 | assert_equal ['cute', 'mruby', 'cute'], test_str.onig_regexp_split(OnigRegexp.new(' '))
|
---|
290 |
|
---|
291 | prev_splitter = $;
|
---|
292 | $; = OnigRegexp.new ' \w'
|
---|
293 | assert_equal ['cute', 'ruby', 'ute'], test_str.onig_regexp_split
|
---|
294 | $; = 't'
|
---|
295 | assert_equal ['cu', 'e mruby cu', 'e'], test_str.onig_regexp_split
|
---|
296 | $; = prev_splitter
|
---|
297 |
|
---|
298 | assert_equal ['h', 'e', 'l', 'l', 'o'], 'hello'.onig_regexp_split(OnigRegexp.new(''))
|
---|
299 | assert_equal ['h', 'e', 'llo'], 'hello'.onig_regexp_split(OnigRegexp.new(''), 3)
|
---|
300 | assert_equal ['h', 'i', 'd', 'a', 'd'], 'hi dad'.onig_regexp_split(OnigRegexp.new('\s*'))
|
---|
301 |
|
---|
302 | test_str = '1, 2, 3, 4, 5,, 6'
|
---|
303 | assert_equal ['1', '2', '3', '4', '5', '', '6'], test_str.onig_regexp_split(OnigRegexp.new(',\s*'))
|
---|
304 |
|
---|
305 | test_str = '1,,2,3,,4,,'
|
---|
306 | assert_equal ['1', '', '2', '3', '', '4'], test_str.onig_regexp_split(OnigRegexp.new(','))
|
---|
307 | assert_equal ['1', '', '2', '3,,4,,'], test_str.onig_regexp_split(OnigRegexp.new(','), 4)
|
---|
308 | assert_equal ['1', '', '2', '3', '', '4', '', ''], test_str.onig_regexp_split(OnigRegexp.new(','), -4)
|
---|
309 |
|
---|
310 | assert_equal [], ''.onig_regexp_split(OnigRegexp.new(','), -1)
|
---|
311 | end
|
---|
312 |
|
---|
313 | assert('String#index') do
|
---|
314 | assert_equal 0, 'abc'.index('a')
|
---|
315 | assert_nil 'abc'.index('d')
|
---|
316 | assert_equal 3, 'abcabc'.index('a', 1)
|
---|
317 | assert_equal 1, "hello".index(?e)
|
---|
318 |
|
---|
319 | assert_equal 0, 'abcabc'.index(/a/)
|
---|
320 | assert_nil 'abc'.index(/d/)
|
---|
321 | assert_equal 3, 'abcabc'.index(/a/, 1)
|
---|
322 | assert_equal 4, "hello".index(/[aeiou]/, -3)
|
---|
323 | assert_equal 3, "regexpindex".index(/e.*x/, 2)
|
---|
324 | end
|
---|
325 |
|
---|
326 | prev_regexp = Regexp
|
---|
327 |
|
---|
328 | Regexp = OnigRegexp
|
---|
329 |
|
---|
330 | # global variables
|
---|
331 | assert('$~') do
|
---|
332 | m = onig_match_data_example
|
---|
333 | assert_equal m[0], $~[0]
|
---|
334 | end
|
---|
335 |
|
---|
336 | assert('$&') do
|
---|
337 | m = onig_match_data_example
|
---|
338 | assert_equal m[0], $&
|
---|
339 | end
|
---|
340 |
|
---|
341 | assert('$`') do
|
---|
342 | m = onig_match_data_example
|
---|
343 | assert_equal m.pre_match, $`
|
---|
344 | end
|
---|
345 |
|
---|
346 | assert('$\'') do
|
---|
347 | m = onig_match_data_example
|
---|
348 | assert_equal m.post_match, $'
|
---|
349 | end
|
---|
350 |
|
---|
351 | assert('$+') do
|
---|
352 | m = onig_match_data_example
|
---|
353 | assert_equal m[-1], $+
|
---|
354 | end
|
---|
355 |
|
---|
356 | assert('$1 to $9') do
|
---|
357 | onig_match_data_example
|
---|
358 | assert_equal 'aaab', $1
|
---|
359 | assert_equal 'b', $2
|
---|
360 | assert_nil $3
|
---|
361 | assert_nil $4
|
---|
362 | assert_nil $5
|
---|
363 | assert_nil $6
|
---|
364 | assert_nil $7
|
---|
365 | assert_nil $8
|
---|
366 | assert_nil $9
|
---|
367 | end
|
---|
368 |
|
---|
369 | assert('default OnigRegexp.set_global_variables?') do
|
---|
370 | assert_true OnigRegexp.set_global_variables?
|
---|
371 | end
|
---|
372 |
|
---|
373 | assert('change set_global_variables') do
|
---|
374 | m = onig_match_data_example
|
---|
375 | assert_equal m[0], $~[0]
|
---|
376 |
|
---|
377 | OnigRegexp.set_global_variables = false
|
---|
378 | assert_false OnigRegexp.set_global_variables?
|
---|
379 |
|
---|
380 | # global variables must be cleared when OnigRegexp.set_global_variables gets change
|
---|
381 | assert_nil $~
|
---|
382 |
|
---|
383 | onig_match_data_example
|
---|
384 | assert_nil $~
|
---|
385 |
|
---|
386 | OnigRegexp.set_global_variables = true
|
---|
387 | end
|
---|
388 |
|
---|
389 | Regexp = Object
|
---|
390 |
|
---|
391 | assert('OnigRegexp not default') do
|
---|
392 | onig_match_data_example
|
---|
393 | assert_nil $~
|
---|
394 | end
|
---|
395 |
|
---|
396 | Regexp = prev_regexp
|
---|