1 |
|
---|
2 | # Constant
|
---|
3 | assert("OnigRegexp::CONSTANT") do
|
---|
4 | assert_equal 1, OnigRegexp::IGNORECASE
|
---|
5 | assert_equal 2, OnigRegexp::EXTENDED
|
---|
6 | assert_equal 4, OnigRegexp::MULTILINE
|
---|
7 | end
|
---|
8 |
|
---|
9 |
|
---|
10 | # Class method
|
---|
11 | assert('OnigRgexp.compile', '15.2.15.6.2') do
|
---|
12 | assert_equal OnigRegexp.compile('.*'), OnigRegexp.compile('.*')
|
---|
13 | end
|
---|
14 |
|
---|
15 | assert('OnigRegexp.escape', '15.2.15.6.2') do
|
---|
16 | escaping_chars = "\n\t\r\f #$()*+-.?[\\]^{|}"
|
---|
17 | assert_equal '\n\t\r\f\\ \#\$\(\)\*\+\-\.\?\[\\\\\]\^\{\|\}', OnigRegexp.escape(escaping_chars)
|
---|
18 | assert_equal 'cute\nmruby\tcute', OnigRegexp.escape("cute\nmruby\tcute")
|
---|
19 | end
|
---|
20 |
|
---|
21 | assert('OnigRegexp.last_match', '15.2.15.6.3') do
|
---|
22 | OnigRegexp.new('.*') =~ 'ginka'
|
---|
23 | assert_equal 'ginka', OnigRegexp.last_match[0]
|
---|
24 |
|
---|
25 | OnigRegexp.new('zzz') =~ 'ginka'
|
---|
26 | assert_nil OnigRegexp.last_match
|
---|
27 | end
|
---|
28 |
|
---|
29 | assert('OnigRegexp.quote', '15.2.15.6.4') do
|
---|
30 | assert_equal '\n', OnigRegexp.quote("\n")
|
---|
31 | end
|
---|
32 |
|
---|
33 | # Instance method
|
---|
34 | assert('OnigRegexp#initialize', '15.2.15.7.1') do
|
---|
35 | assert_equal OnigRegexp, OnigRegexp.new(".*").class
|
---|
36 | assert_equal OnigRegexp, OnigRegexp.new(".*", OnigRegexp::MULTILINE).class
|
---|
37 | end
|
---|
38 |
|
---|
39 | assert('OnigRegexp#initialize_copy', '15.2.15.7.2') do
|
---|
40 | r1 = OnigRegexp.new(".*")
|
---|
41 | r2 = r1.dup
|
---|
42 | assert_equal r1, r2
|
---|
43 | assert_equal 'kawa', r2.match('kawa')[0]
|
---|
44 | end
|
---|
45 |
|
---|
46 | assert("OnigRegexp#==", '15.2.15.7.3') do
|
---|
47 | reg1 = reg2 = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
48 | reg3 = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
49 | reg4 = OnigRegexp.new("(https://[^/]+)[-a-zA-Z0-9./]+")
|
---|
50 |
|
---|
51 | assert_true(reg1 == reg2 && reg1 == reg3 && !(reg1 == reg4))
|
---|
52 |
|
---|
53 | assert_false(OnigRegexp.new("a") == "a")
|
---|
54 | end
|
---|
55 |
|
---|
56 | assert("OnigRegexp#===", '15.2.15.7.4') do
|
---|
57 | reg = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
58 | assert_true reg === "http://example.com"
|
---|
59 | assert_false reg === "htt://example.com"
|
---|
60 |
|
---|
61 | assert_false /a/ === Object.new
|
---|
62 | end
|
---|
63 |
|
---|
64 | assert('OnigRegexp#=~', '15.2.15.7.5') do
|
---|
65 | assert_equal(0) { OnigRegexp.new('.*') =~ 'akari' }
|
---|
66 | assert_equal(nil) { OnigRegexp.new('t') =~ 'akari' }
|
---|
67 | end
|
---|
68 |
|
---|
69 | assert("OnigRegexp#casefold?", '15.2.15.7.6') do
|
---|
70 | assert_false OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+", OnigRegexp::MULTILINE).casefold?
|
---|
71 | assert_true OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+", OnigRegexp::IGNORECASE | OnigRegexp::EXTENDED).casefold?
|
---|
72 | assert_true OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+", OnigRegexp::MULTILINE | OnigRegexp::IGNORECASE).casefold?
|
---|
73 | assert_false OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+").casefold?
|
---|
74 | assert_true OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+", true).casefold?
|
---|
75 | end
|
---|
76 |
|
---|
77 | assert("OnigRegexp#match", '15.2.15.7.7') do
|
---|
78 | reg = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
79 | assert_false reg.match("http://masamitsu-murase.12345/hoge.html").nil?
|
---|
80 | assert_nil reg.match("http:///masamitsu-murase.12345/hoge.html")
|
---|
81 |
|
---|
82 | reg = OnigRegexp.new('def')
|
---|
83 | assert_equal "def", reg.match('abcdef', 3)[0]
|
---|
84 | assert_nil reg.match('abcdef', -1)
|
---|
85 | assert_nil reg.match('abcdef', 4)
|
---|
86 | assert_nil reg.match(nil, 3)
|
---|
87 | end
|
---|
88 |
|
---|
89 | assert("OnigRegexp#source", '15.2.15.7.8') do
|
---|
90 | str = "(https?://[^/]+)[-a-zA-Z0-9./]+"
|
---|
91 | reg = OnigRegexp.new(str)
|
---|
92 |
|
---|
93 | assert_equal str, reg.source
|
---|
94 | end
|
---|
95 |
|
---|
96 | if OnigRegexp.const_defined? :ASCII_RANGE
|
---|
97 | assert('OnigRegexp#options (no options)') do
|
---|
98 | assert_equal OnigRegexp::ASCII_RANGE | OnigRegexp::POSIX_BRACKET_ALL_RANGE | OnigRegexp::WORD_BOUND_ALL_RANGE, OnigRegexp.new(".*").options
|
---|
99 | end
|
---|
100 |
|
---|
101 | assert('OnigRegexp#options (multiline)') do
|
---|
102 | assert_equal OnigRegexp::MULTILINE | OnigRegexp::ASCII_RANGE | OnigRegexp::POSIX_BRACKET_ALL_RANGE | OnigRegexp::WORD_BOUND_ALL_RANGE, OnigRegexp.new(".*", OnigRegexp::MULTILINE).options
|
---|
103 | end
|
---|
104 | end
|
---|
105 |
|
---|
106 | assert("OnigRegexp#inspect") do
|
---|
107 | reg = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
108 |
|
---|
109 | assert_equal '/(https?:\/\/[^\/]+)[-a-zA-Z0-9.\/]+/', reg.inspect
|
---|
110 | assert_equal '/abc\nd\te/mi', OnigRegexp.new("abc\nd\te", OnigRegexp::MULTILINE | OnigRegexp::IGNORECASE).inspect
|
---|
111 | assert_equal '/abc/min', OnigRegexp.new("abc", OnigRegexp::MULTILINE | OnigRegexp::IGNORECASE, "none").inspect
|
---|
112 | assert_equal "/\\\\\\+\\//", /\\\+\//.inspect
|
---|
113 | end
|
---|
114 |
|
---|
115 | assert("OnigRegexp#to_s") do
|
---|
116 | assert_equal '(?-mix:ab+c)', OnigRegexp.new("ab+c").to_s
|
---|
117 | assert_equal '(?-mix:ab+c)', /ab+c/.to_s
|
---|
118 | assert_equal '(?mx-i:ab+c)', OnigRegexp.new("ab+c", OnigRegexp::MULTILINE | OnigRegexp::EXTENDED).to_s
|
---|
119 | assert_equal '(?mi-x:ab+c)', /ab+c/im.to_s
|
---|
120 | assert_equal '(?mi-x:ab+c)', /ab+c/imn.to_s
|
---|
121 | assert_equal "(?-mix:\\\\\\+)", /\\\+/.to_s
|
---|
122 | end
|
---|
123 |
|
---|
124 | assert("OnigRegexp#to_s (composition)") do
|
---|
125 | re1 = OnigRegexp.new("ab+c")
|
---|
126 | re2 = OnigRegexp.new("xy#{re1}z")
|
---|
127 | assert_equal '(?-mix:xy(?-mix:ab+c)z)', re2.to_s
|
---|
128 |
|
---|
129 | re3 = OnigRegexp.new("ab.+c", OnigRegexp::MULTILINE)
|
---|
130 | re4 = OnigRegexp.new("xy#{re3}z", OnigRegexp::IGNORECASE)
|
---|
131 | assert_equal '(?i-mx:xy(?m-ix:ab.+c)z)', re4.to_s
|
---|
132 |
|
---|
133 | re5 = /\\\+/
|
---|
134 | re6 = /xy#{re5}z/
|
---|
135 | assert_equal "(?-mix:xy(?-mix:\\\\\\+)z)", re6.to_s
|
---|
136 | end
|
---|
137 |
|
---|
138 | # Extended patterns.
|
---|
139 | assert("OnigRegexp#match (no flags)") do
|
---|
140 | [
|
---|
141 | [ ".*", "abcd\nefg", "abcd" ],
|
---|
142 | [ "^a.", "abcd\naefg", "ab" ],
|
---|
143 | [ "^a.", "bacd\naefg", "ae" ],
|
---|
144 | [ ".$", "bacd\naefg", "d" ],
|
---|
145 | [ "bc", :abc, "bc"],
|
---|
146 | [ "bj", "obj", "bj"],
|
---|
147 | ].each do |reg, str, result|
|
---|
148 | m = OnigRegexp.new(reg).match(str)
|
---|
149 | assert_equal result, m[0] if assert_false m.nil?
|
---|
150 | end
|
---|
151 |
|
---|
152 | assert_raise(TypeError) { /a/.match(Object.new) }
|
---|
153 | end
|
---|
154 |
|
---|
155 | assert("OnigRegexp#match (multiline)") do
|
---|
156 | patterns = [
|
---|
157 | [ OnigRegexp.new(".*", OnigRegexp::MULTILINE), "abcd\nefg", "abcd\nefg" ]
|
---|
158 | ]
|
---|
159 |
|
---|
160 | patterns.each{ |reg, str, result| assert_equal result, reg.match(str)[0] }
|
---|
161 | end
|
---|
162 |
|
---|
163 | assert("OnigRegexp#match (ignorecase)") do
|
---|
164 | [
|
---|
165 | [ "aBcD", "00AbcDef", "AbcD" ],
|
---|
166 | [ "0x[a-f]+", "00XaBCdefG", "0XaBCdef" ],
|
---|
167 | [ "0x[^c-f]+", "00XaBCdefG", "0XaB" ]
|
---|
168 | ].each do |reg, str, result|
|
---|
169 | m = OnigRegexp.new(reg, OnigRegexp::IGNORECASE|OnigRegexp::EXTENDED).match(str)
|
---|
170 | assert_equal result, m[0] if assert_false m.nil?
|
---|
171 | end
|
---|
172 | end
|
---|
173 |
|
---|
174 | assert("OnigRegexp#match (none encoding)") do
|
---|
175 | assert_equal 2, /\x82/n =~ "あ"
|
---|
176 | end
|
---|
177 |
|
---|
178 | assert("OnigRegexp#match (with block)") do
|
---|
179 | reg = OnigRegexp.new("(https?://[^/]+)[-a-zA-Z0-9./]+")
|
---|
180 | reg.match("http://masamitsu-murase.12345/hoge.html") do |m|
|
---|
181 | assert_true m.is_a?(OnigMatchData)
|
---|
182 | assert_equal "http://masamitsu-murase.12345/hoge.html", m[0]
|
---|
183 | end
|
---|
184 | end
|
---|
185 |
|
---|
186 | assert('OnigRegexp.version') do
|
---|
187 | assert_kind_of String, OnigRegexp.version
|
---|
188 | end
|
---|
189 |
|
---|
190 | def onig_match_data_example
|
---|
191 | OnigRegexp.new('(\w+)(\w)').match('+aaabb-')
|
---|
192 | end
|
---|
193 |
|
---|
194 | def onig_mismatch_data_example
|
---|
195 | OnigRegexp.new('abc').match('z')
|
---|
196 | end
|
---|
197 |
|
---|
198 | assert('OnigMatchData.new') do
|
---|
199 | assert_raise(NoMethodError) { OnigMatchData.new('aaa', 'i') }
|
---|
200 | end
|
---|
201 |
|
---|
202 | assert('OnigMatchData#[]', '15.2.16.3.1') do
|
---|
203 | m = onig_match_data_example
|
---|
204 | assert_equal 'aaabb', m[0]
|
---|
205 | assert_equal 'aaab', m[1]
|
---|
206 | assert_equal 'b', m[2]
|
---|
207 | assert_nil m[3]
|
---|
208 |
|
---|
209 | m = OnigRegexp.new('(?<name>\w\w)').match('aba')
|
---|
210 | assert_raise(TypeError) { m[[]] }
|
---|
211 | assert_raise(IndexError) { m['nam'] }
|
---|
212 | assert_equal 'ab', m[:name]
|
---|
213 | assert_equal 'ab', m['name']
|
---|
214 | assert_equal 'ab', m[1]
|
---|
215 |
|
---|
216 | m = OnigRegexp.new('(\w) (\w) (\w) (\w)').match('a b c d')
|
---|
217 | assert_equal %w(a b c d), m[1..-1]
|
---|
218 | end
|
---|
219 |
|
---|
220 | assert('OnigMatchData#begin', '15.2.16.3.2') do
|
---|
221 | m = onig_match_data_example
|
---|
222 | assert_equal 1, m.begin(0)
|
---|
223 | assert_equal 1, m.begin(1)
|
---|
224 | assert_raise(IndexError) { m.begin 3 }
|
---|
225 | end
|
---|
226 |
|
---|
227 | assert('OnigMatchData#captures', '15.2.16.3.3') do
|
---|
228 | m = onig_match_data_example
|
---|
229 | assert_equal ['aaab', 'b'], m.captures
|
---|
230 |
|
---|
231 | m = OnigRegexp.new('(\w+)(\d)?').match('+aaabb-')
|
---|
232 | assert_equal ['aaabb', nil], m.captures
|
---|
233 | end
|
---|
234 |
|
---|
235 | assert('OnigMatchData#end', '15.2.16.3.4') do
|
---|
236 | m = onig_match_data_example
|
---|
237 | assert_equal 6, m.end(0)
|
---|
238 | assert_equal 5, m.end(1)
|
---|
239 | assert_raise(IndexError) { m.end 3 }
|
---|
240 | end
|
---|
241 |
|
---|
242 | assert('OnigMatchData#initialize_copy', '15.2.16.3.5') do
|
---|
243 | m = onig_match_data_example
|
---|
244 | c = m.dup
|
---|
245 | assert_equal m.to_a, c.to_a
|
---|
246 | end
|
---|
247 |
|
---|
248 | assert('OnigMatchData#length', '15.2.16.3.6') do
|
---|
249 | assert_equal 3, onig_match_data_example.length
|
---|
250 | end
|
---|
251 |
|
---|
252 | assert('OnigMatchData#offset', '15.2.16.3.7') do
|
---|
253 | assert_equal [1, 6], onig_match_data_example.offset(0)
|
---|
254 | assert_equal [1, 5], onig_match_data_example.offset(1)
|
---|
255 | end
|
---|
256 |
|
---|
257 | assert('OnigMatchData#post_match', '15.2.16.3.8') do
|
---|
258 | assert_equal '-', onig_match_data_example.post_match
|
---|
259 | end
|
---|
260 |
|
---|
261 | assert('OnigMatchData#pre_match', '15.2.16.3.9') do
|
---|
262 | assert_equal '+', onig_match_data_example.pre_match
|
---|
263 | end
|
---|
264 |
|
---|
265 | assert('OnigMatchData#size', '15.2.16.3.10') do
|
---|
266 | assert_equal 3, onig_match_data_example.length
|
---|
267 | end
|
---|
268 |
|
---|
269 | assert('OnigMatchData#string', '15.2.16.3.11') do
|
---|
270 | assert_equal '+aaabb-', onig_match_data_example.string
|
---|
271 | end
|
---|
272 |
|
---|
273 | assert('OnigMatchData#to_a', '15.2.16.3.12') do
|
---|
274 | assert_equal ['aaabb', 'aaab', 'b'], onig_match_data_example.to_a
|
---|
275 | end
|
---|
276 |
|
---|
277 | assert('OnigMatchData#to_s', '15.2.16.3.13') do
|
---|
278 | assert_equal 'aaabb', onig_match_data_example.to_s
|
---|
279 | end
|
---|
280 |
|
---|
281 | assert('OnigMatchData#regexp') do
|
---|
282 | assert_equal '(\w+)(\w)', onig_match_data_example.regexp.source
|
---|
283 | end
|
---|
284 |
|
---|
285 | assert('Invalid regexp') do
|
---|
286 | assert_raise(RegexpError) { OnigRegexp.new '[aio' }
|
---|
287 | end
|
---|
288 |
|
---|
289 | assert('Invalid argument') do
|
---|
290 | assert_raise(ArgumentError) { "".sub(//) }
|
---|
291 | assert_raise(ArgumentError) { "".onig_regexp_sub(OnigRegexp.new('')) }
|
---|
292 | assert_raise(ArgumentError) { "\xf0".gsub(/[^a]/,"X") }
|
---|
293 | end
|
---|
294 |
|
---|
295 | assert('String#onig_regexp_gsub') do
|
---|
296 | test_str = 'hello mruby'
|
---|
297 | assert_equal 'h*ll* mr*by', test_str.onig_regexp_gsub(OnigRegexp.new('[aeiou]'), '*')
|
---|
298 | assert_equal 'h<e>ll<o> mr<u>by', test_str.onig_regexp_gsub(OnigRegexp.new('([aeiou])'), '<\1>')
|
---|
299 | assert_equal 'h e l l o m r u b y ', test_str.onig_regexp_gsub(OnigRegexp.new('\w')) { |v| v + ' ' }
|
---|
300 | assert_equal 'h{e}ll{o} mr{u}by', test_str.onig_regexp_gsub(OnigRegexp.new('(?<hoge>[aeiou])'), '{\k<hoge>}')
|
---|
301 | assert_equal '.h.e.l.l.o. .m.r.u.b.y.', test_str.onig_regexp_gsub(OnigRegexp.new(''), '.')
|
---|
302 | assert_equal " hello\n mruby", "hello\nmruby".onig_regexp_gsub(OnigRegexp.new('^'), ' ')
|
---|
303 | assert_equal "he<l><><l><>o mruby", test_str.onig_regexp_gsub(OnigRegexp.new('(l)'), '<\1><\2>')
|
---|
304 | end
|
---|
305 |
|
---|
306 | assert('String#onig_regexp_gsub with hash') do
|
---|
307 | assert_equal('azc', 'abc'.gsub(/b/, "b" => "z"))
|
---|
308 | assert_equal('ac', 'abc'.gsub(/b/, {}))
|
---|
309 | assert_equal('a1c', 'abc'.gsub(/b/, "b" => 1))
|
---|
310 | assert_equal('aBc', 'abc'.gsub(/b/, Hash.new {|h, k| k.upcase }))
|
---|
311 | assert_equal('a[\&]c', 'abc'.gsub(/b/, "b" => '[\&]'))
|
---|
312 | assert_equal('aBcaBc', 'abcabc'.gsub(/b/, Hash.new {|h, k| h[k] = k.upcase }))
|
---|
313 | assert_equal('aBcDEf', 'abcdef'.gsub(/de|b/, "b" => "B", "de" => "DE"))
|
---|
314 | end
|
---|
315 |
|
---|
316 | assert('String#onig_regexp_scan') do
|
---|
317 | test_str = 'mruby world'
|
---|
318 | assert_equal ['mruby', 'world'], test_str.onig_regexp_scan(OnigRegexp.new('\w+'))
|
---|
319 | assert_equal ['mru', 'by ', 'wor'], test_str.onig_regexp_scan(OnigRegexp.new('...'))
|
---|
320 | assert_equal [['mru'], ['by '], ['wor']], test_str.onig_regexp_scan(OnigRegexp.new('(...)'))
|
---|
321 | assert_equal [['mr', 'ub'], ['y ', 'wo']], test_str.onig_regexp_scan(OnigRegexp.new('(..)(..)'))
|
---|
322 |
|
---|
323 | result = []
|
---|
324 | assert_equal test_str, test_str.onig_regexp_scan(OnigRegexp.new('\w+')) { |v| result << "<<#{v}>>" }
|
---|
325 | assert_equal ['<<mruby>>', '<<world>>'], result
|
---|
326 |
|
---|
327 | result = ''
|
---|
328 | assert_equal test_str, test_str.onig_regexp_scan(OnigRegexp.new('(.)(.)')) { |x, y| result += y; result += x }
|
---|
329 | assert_equal 'rmbu yowlr', result
|
---|
330 |
|
---|
331 | assert_equal [""] * (test_str.length + 1), test_str.onig_regexp_scan(OnigRegexp.new(''))
|
---|
332 | assert_equal [""], "".onig_regexp_scan(OnigRegexp.new(''))
|
---|
333 | end
|
---|
334 |
|
---|
335 | assert('String#onig_regexp_sub') do
|
---|
336 | test_str = 'hello mruby'
|
---|
337 | assert_equal 'h*llo mruby', test_str.onig_regexp_sub(OnigRegexp.new('[aeiou]'), '*')
|
---|
338 | assert_equal 'h<e>llo mruby', test_str.onig_regexp_sub(OnigRegexp.new('([aeiou])'), '<\1>')
|
---|
339 | assert_equal 'h ello mruby', test_str.onig_regexp_sub(OnigRegexp.new('\w')) { |v| v + ' ' }
|
---|
340 | assert_equal 'h{e}llo mruby', test_str.onig_regexp_sub(OnigRegexp.new('(?<hoge>[aeiou])'), '{\k<hoge>}')
|
---|
341 | assert_equal "heOlo mruby", test_str.onig_regexp_sub(OnigRegexp.new("l"), "O") { "X" }
|
---|
342 | end
|
---|
343 |
|
---|
344 | assert('String#onig_regexp_sub with hash') do
|
---|
345 | assert_equal('azc', 'abc'.sub(/b/, "b" => "z"))
|
---|
346 | assert_equal('ac', 'abc'.sub(/b/, {}))
|
---|
347 | assert_equal('a1c', 'abc'.sub(/b/, "b" => 1))
|
---|
348 | assert_equal('aBc', 'abc'.sub(/b/, Hash.new {|h, k| k.upcase }))
|
---|
349 | assert_equal('a[\&]c', 'abc'.sub(/b/, "b" => '[\&]'))
|
---|
350 | assert_equal('aBcabc', 'abcabc'.sub(/b/, Hash.new {|h, k| h[k] = k.upcase }))
|
---|
351 | assert_equal('aBcdef', 'abcdef'.sub(/de|b/, "b" => "B", "de" => "DE"))
|
---|
352 | end
|
---|
353 |
|
---|
354 | assert('String#onig_regexp_split') do
|
---|
355 | test_str = 'cute mruby cute'
|
---|
356 | assert_equal ['cute', 'mruby', 'cute'], test_str.onig_regexp_split
|
---|
357 | assert_equal ['cute', 'mruby', 'cute'], test_str.onig_regexp_split(OnigRegexp.new(' '))
|
---|
358 |
|
---|
359 | prev_splitter = $;
|
---|
360 | $; = OnigRegexp.new ' \w'
|
---|
361 | assert_equal ['cute', 'ruby', 'ute'], test_str.onig_regexp_split
|
---|
362 | assert_equal ['cute', 'ruby', 'ute'], test_str.onig_regexp_split(nil)
|
---|
363 | assert_equal ['cute', 'ruby cute'], test_str.onig_regexp_split(nil, 2)
|
---|
364 | $; = 't'
|
---|
365 | assert_equal ['cu', 'e mruby cu', 'e'], test_str.onig_regexp_split
|
---|
366 | assert_equal ['cu', 'e mruby cu', 'e'], test_str.onig_regexp_split(nil)
|
---|
367 | assert_equal ['cu', 'e mruby cute'], test_str.onig_regexp_split(nil, 2)
|
---|
368 | $; = nil
|
---|
369 | assert_equal ['cute', 'mruby', 'cute'], test_str.onig_regexp_split
|
---|
370 | assert_equal ['cute', 'mruby', 'cute'], test_str.onig_regexp_split(nil)
|
---|
371 | assert_equal ['cute', 'mruby cute'], test_str.onig_regexp_split(nil, 2)
|
---|
372 | $; = 1
|
---|
373 | assert_raise(TypeError) { "".onig_regexp_split }
|
---|
374 | $; = prev_splitter
|
---|
375 |
|
---|
376 | assert_equal ['h', 'e', 'l', 'l', 'o'], 'hello'.onig_regexp_split(OnigRegexp.new(''))
|
---|
377 | assert_equal ["h", "e", "l", "l", "o", ""], 'hello'.onig_regexp_split(OnigRegexp.new(''), -1)
|
---|
378 | assert_equal ["h", "", "i", "", "!", "", ""], "hi!".onig_regexp_split(OnigRegexp.new('()'), -1)
|
---|
379 | assert_equal ["h", "el", "lo"], "hello".onig_regexp_split(OnigRegexp.new('(el)|(xx)'))
|
---|
380 |
|
---|
381 | assert_equal ['あ', 'い', 'う', 'え', 'お'], 'あいうえお'.onig_regexp_split('')
|
---|
382 | assert_equal ['あ', 'い', 'う', 'え', 'お', ''], 'あいうえお'.onig_regexp_split('', -1)
|
---|
383 | assert_equal ['あいうえお'], 'あいうえお'.onig_regexp_split('', 1)
|
---|
384 | assert_equal ['あ', 'いうえお'], 'あいうえお'.onig_regexp_split('', 2)
|
---|
385 | assert_equal ['あ', 'い', 'うえお'], 'あいうえお'.onig_regexp_split('', 3)
|
---|
386 |
|
---|
387 | assert_equal ['あ', 'い', 'う', 'え', 'お'], 'あいうえお'.onig_regexp_split(OnigRegexp.new(''))
|
---|
388 | assert_equal ['あ', 'い', 'う', 'え', 'お', ''], 'あいうえお'.onig_regexp_split(OnigRegexp.new(''), -1)
|
---|
389 | assert_equal ['あいうえお'], 'あいうえお'.onig_regexp_split(OnigRegexp.new(''), 1)
|
---|
390 | assert_equal ['あ', 'いうえお'], 'あいうえお'.onig_regexp_split(OnigRegexp.new(''), 2)
|
---|
391 | assert_equal ['あ', 'い', 'うえお'], 'あいうえお'.onig_regexp_split(OnigRegexp.new(''), 3)
|
---|
392 |
|
---|
393 | assert_equal ['h', 'e', 'llo'], 'hello'.onig_regexp_split(OnigRegexp.new(''), 3)
|
---|
394 | assert_equal ['h', 'i', 'd', 'a', 'd'], 'hi dad'.onig_regexp_split(OnigRegexp.new('\s*'))
|
---|
395 |
|
---|
396 | test_str = '1, 2, 3, 4, 5,, 6'
|
---|
397 | assert_equal ['1', '2', '3', '4', '5', '', '6'], test_str.onig_regexp_split(OnigRegexp.new(',\s*'))
|
---|
398 |
|
---|
399 | test_str = '1,,2,3,,4,,'
|
---|
400 | assert_equal ['1', '', '2', '3', '', '4'], test_str.onig_regexp_split(OnigRegexp.new(','))
|
---|
401 | assert_equal ['1', '', '2', '3,,4,,'], test_str.onig_regexp_split(OnigRegexp.new(','), 4)
|
---|
402 | assert_equal ['1', '', '2', '3', '', '4', '', ''], test_str.onig_regexp_split(OnigRegexp.new(','), -4)
|
---|
403 |
|
---|
404 | assert_equal [], ''.onig_regexp_split(OnigRegexp.new(','), -1)
|
---|
405 |
|
---|
406 | assert_equal ["こ", "に", "ち", "わ"], "こ,に,ち,わ".onig_regexp_split(",")
|
---|
407 | assert_raise(TypeError) { "".onig_regexp_split(1) }
|
---|
408 | end
|
---|
409 |
|
---|
410 | assert('String#onig_regexp_match') do
|
---|
411 | reg = OnigRegexp.new('d(e)f')
|
---|
412 | assert_equal ['def', 'e'], 'abcdef'.match(reg).to_a
|
---|
413 | assert_nil 'abcdef'.match(reg, 4)
|
---|
414 | match_data = nil
|
---|
415 | 'abcdef'.match(reg) do |m|
|
---|
416 | match_data = m
|
---|
417 | end
|
---|
418 | assert_equal ['def', 'e'], match_data.to_a
|
---|
419 | end
|
---|
420 |
|
---|
421 | assert('String#index') do
|
---|
422 | assert_equal 0, 'abc'.index('a')
|
---|
423 | assert_nil 'abc'.index('d')
|
---|
424 | assert_equal 3, 'abcabc'.index('a', 1)
|
---|
425 | assert_equal 1, "hello".index(?e)
|
---|
426 |
|
---|
427 | assert_equal 0, 'abcabc'.index(/a/)
|
---|
428 | assert_nil 'abc'.index(/d/)
|
---|
429 | assert_equal 3, 'abcabc'.index(/a/, 1)
|
---|
430 | assert_equal 4, "hello".index(/[aeiou]/, -3)
|
---|
431 | assert_equal 3, "regexpindex".index(/e.*x/, 2)
|
---|
432 | end
|
---|
433 |
|
---|
434 | prev_regexp = Regexp
|
---|
435 |
|
---|
436 | Regexp = OnigRegexp
|
---|
437 |
|
---|
438 | # global variables
|
---|
439 | assert('$~') do
|
---|
440 | m = onig_match_data_example
|
---|
441 | assert_equal m[0], $~[0]
|
---|
442 |
|
---|
443 | onig_mismatch_data_example
|
---|
444 | assert_nil $~
|
---|
445 | end
|
---|
446 |
|
---|
447 | assert('$&') do
|
---|
448 | m = onig_match_data_example
|
---|
449 | assert_equal m[0], $&
|
---|
450 |
|
---|
451 | onig_mismatch_data_example
|
---|
452 | assert_nil $&
|
---|
453 | end
|
---|
454 |
|
---|
455 | assert('$`') do
|
---|
456 | m = onig_match_data_example
|
---|
457 | assert_equal m.pre_match, $`
|
---|
458 |
|
---|
459 | onig_mismatch_data_example
|
---|
460 | assert_nil $`
|
---|
461 | end
|
---|
462 |
|
---|
463 | assert('$\'') do
|
---|
464 | m = onig_match_data_example
|
---|
465 | assert_equal m.post_match, $'
|
---|
466 |
|
---|
467 | onig_mismatch_data_example
|
---|
468 | assert_nil $'
|
---|
469 | end
|
---|
470 |
|
---|
471 | assert('$+') do
|
---|
472 | m = onig_match_data_example
|
---|
473 | assert_equal m[-1], $+
|
---|
474 |
|
---|
475 | onig_mismatch_data_example
|
---|
476 | assert_nil $+
|
---|
477 | end
|
---|
478 |
|
---|
479 | assert('$1 to $9') do
|
---|
480 | onig_match_data_example
|
---|
481 | assert_equal 'aaab', $1
|
---|
482 | assert_equal 'b', $2
|
---|
483 | assert_nil $3
|
---|
484 | assert_nil $4
|
---|
485 | assert_nil $5
|
---|
486 | assert_nil $6
|
---|
487 | assert_nil $7
|
---|
488 | assert_nil $8
|
---|
489 | assert_nil $9
|
---|
490 |
|
---|
491 | onig_mismatch_data_example
|
---|
492 | assert_nil $1
|
---|
493 | assert_nil $2
|
---|
494 | assert_nil $3
|
---|
495 | assert_nil $4
|
---|
496 | assert_nil $5
|
---|
497 | assert_nil $6
|
---|
498 | assert_nil $7
|
---|
499 | assert_nil $8
|
---|
500 | assert_nil $9
|
---|
501 | end
|
---|
502 |
|
---|
503 | assert('default OnigRegexp.set_global_variables?') do
|
---|
504 | assert_true OnigRegexp.set_global_variables?
|
---|
505 | end
|
---|
506 |
|
---|
507 | assert('change set_global_variables') do
|
---|
508 | m = onig_match_data_example
|
---|
509 | assert_equal m[0], $~[0]
|
---|
510 |
|
---|
511 | OnigRegexp.set_global_variables = false
|
---|
512 | assert_false OnigRegexp.set_global_variables?
|
---|
513 |
|
---|
514 | # global variables must be cleared when OnigRegexp.set_global_variables gets change
|
---|
515 | assert_nil $~
|
---|
516 |
|
---|
517 | onig_match_data_example
|
---|
518 | assert_nil $~
|
---|
519 |
|
---|
520 | OnigRegexp.set_global_variables = true
|
---|
521 | end
|
---|
522 |
|
---|
523 | assert('OnigRegexp#match?') do
|
---|
524 | assert_false OnigRegexp.new('^[123]+$').match?('abc')
|
---|
525 | assert_false OnigRegexp.new('^[123]+$').match?(:abc)
|
---|
526 | assert_true OnigRegexp.new('^[123]+$').match?('321')
|
---|
527 | assert_true OnigRegexp.new('webp').match?('text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8')
|
---|
528 | assert_true(/webp/.match?('text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'))
|
---|
529 |
|
---|
530 | reg = OnigRegexp.new('def')
|
---|
531 | assert_true reg.match?('abcdef', 3)
|
---|
532 | assert_false reg.match?('abcdef', -1)
|
---|
533 | assert_false reg.match?('abcdef', 4)
|
---|
534 | assert_false reg.match?(nil, 3)
|
---|
535 | end
|
---|
536 |
|
---|
537 | assert('String#match?') do
|
---|
538 | assert_equal false, 'abc'.onig_regexp_match?(OnigRegexp.new('^[123]+$'))
|
---|
539 | assert_equal true, '321'.onig_regexp_match?(OnigRegexp.new('^[123]+$'))
|
---|
540 | assert_true 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'.onig_regexp_match?(OnigRegexp.new('webp'))
|
---|
541 | end
|
---|
542 |
|
---|
543 | assert('String#[]=') do
|
---|
544 | string = 'abc'
|
---|
545 | string[OnigRegexp.new('.')] = 'A'
|
---|
546 | assert_equal 'Abc', string
|
---|
547 | string[OnigRegexp.new('.(.)'), 1] = 'B'
|
---|
548 | assert_equal 'ABc', string
|
---|
549 | string[OnigRegexp.new('(?<a>.)(?<b>.)(?<c>.)'), 'c'] = 'C'
|
---|
550 | assert_equal 'ABC', string
|
---|
551 |
|
---|
552 | assert_raise(ArgumentError) do
|
---|
553 | string[OnigRegexp.new('.'), 0, :extra] = 'x'
|
---|
554 | end
|
---|
555 | end
|
---|
556 |
|
---|
557 | assert('String#slice!') do
|
---|
558 | string = 'abc'
|
---|
559 | assert_equal 'a', string.slice!(OnigRegexp.new('.'))
|
---|
560 | assert_equal 'bc', string
|
---|
561 | end
|
---|
562 |
|
---|
563 | assert 'raises RegexpError' do
|
---|
564 | assert_raise(RegexpError) { OnigRegexp.new('*') }
|
---|
565 | end
|
---|
566 |
|
---|
567 | Regexp = Object
|
---|
568 |
|
---|
569 | assert('OnigRegexp not default') do
|
---|
570 | $~ = nil
|
---|
571 | onig_match_data_example
|
---|
572 | assert_nil $~
|
---|
573 | end
|
---|
574 |
|
---|
575 | Regexp = prev_regexp
|
---|