- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mrbgems/mruby-onig-regexp/test/mruby_onig_regexp.rb
r321 r439 2 2 # Constant 3 3 assert("OnigRegexp::CONSTANT") do 4 OnigRegexp::IGNORECASE == 1 and OnigRegexp::EXTENDED == 2 and OnigRegexp::MULTILINE == 4 4 assert_equal 1, OnigRegexp::IGNORECASE 5 assert_equal 2, OnigRegexp::EXTENDED 6 assert_equal 4, OnigRegexp::MULTILINE 5 7 end 6 8 … … 20 22 OnigRegexp.new('.*') =~ 'ginka' 21 23 assert_equal 'ginka', OnigRegexp.last_match[0] 24 25 OnigRegexp.new('zzz') =~ 'ginka' 26 assert_nil OnigRegexp.last_match 22 27 end 23 28 … … 28 33 # Instance method 29 34 assert('OnigRegexp#initialize', '15.2.15.7.1') do 30 OnigRegexp.new(".*") and OnigRegexp.new(".*", OnigRegexp::MULTILINE) 35 assert_equal OnigRegexp, OnigRegexp.new(".*").class 36 assert_equal OnigRegexp, OnigRegexp.new(".*", OnigRegexp::MULTILINE).class 31 37 end 32 38 … … 52 58 assert_true reg === "http://example.com" 53 59 assert_false reg === "htt://example.com" 60 61 assert_false /a/ === Object.new 54 62 end 55 63 … … 71 79 assert_false reg.match("http://masamitsu-murase.12345/hoge.html").nil? 72 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) 73 87 end 74 88 … … 77 91 reg = OnigRegexp.new(str) 78 92 79 reg.source == str93 assert_equal str, reg.source 80 94 end 81 95 … … 96 110 assert_equal '/abc\nd\te/mi', OnigRegexp.new("abc\nd\te", OnigRegexp::MULTILINE | OnigRegexp::IGNORECASE).inspect 97 111 assert_equal '/abc/min', OnigRegexp.new("abc", OnigRegexp::MULTILINE | OnigRegexp::IGNORECASE, "none").inspect 112 assert_equal "/\\\\\\+\\//", /\\\+\//.inspect 98 113 end 99 114 … … 104 119 assert_equal '(?mi-x:ab+c)', /ab+c/im.to_s 105 120 assert_equal '(?mi-x:ab+c)', /ab+c/imn.to_s 121 assert_equal "(?-mix:\\\\\\+)", /\\\+/.to_s 106 122 end 107 123 … … 114 130 re4 = OnigRegexp.new("xy#{re3}z", OnigRegexp::IGNORECASE) 115 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 116 136 end 117 137 … … 122 142 [ "^a.", "abcd\naefg", "ab" ], 123 143 [ "^a.", "bacd\naefg", "ae" ], 124 [ ".$", "bacd\naefg", "d" ] 144 [ ".$", "bacd\naefg", "d" ], 145 [ "bc", :abc, "bc"], 146 [ "bj", "obj", "bj"], 125 147 ].each do |reg, str, result| 126 148 m = OnigRegexp.new(reg).match(str) 127 149 assert_equal result, m[0] if assert_false m.nil? 128 150 end 151 152 assert_raise(TypeError) { /a/.match(Object.new) } 129 153 end 130 154 … … 134 158 ] 135 159 136 patterns. all?{ |reg, str, result| reg.match(str)[0] == result}160 patterns.each{ |reg, str, result| assert_equal result, reg.match(str)[0] } 137 161 end 138 162 … … 152 176 end 153 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 154 186 assert('OnigRegexp.version') do 155 OnigRegexp.version.kind_of? String187 assert_kind_of String, OnigRegexp.version 156 188 end 157 189 158 190 def onig_match_data_example 159 191 OnigRegexp.new('(\w+)(\w)').match('+aaabb-') 192 end 193 194 def onig_mismatch_data_example 195 OnigRegexp.new('abc').match('z') 160 196 end 161 197 … … 248 284 249 285 assert('Invalid regexp') do 250 assert_raise(ArgumentError) { OnigRegexp.new '[aio' } 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") } 251 293 end 252 294 … … 257 299 assert_equal 'h e l l o m r u b y ', test_str.onig_regexp_gsub(OnigRegexp.new('\w')) { |v| v + ' ' } 258 300 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>') } 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")) 260 314 end 261 315 … … 274 328 assert_equal test_str, test_str.onig_regexp_scan(OnigRegexp.new('(.)(.)')) { |x, y| result += y; result += x } 275 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('')) 276 333 end 277 334 … … 282 339 assert_equal 'h ello mruby', test_str.onig_regexp_sub(OnigRegexp.new('\w')) { |v| v + ' ' } 283 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")) 284 352 end 285 353 … … 292 360 $; = OnigRegexp.new ' \w' 293 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) 294 364 $; = 't' 295 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 } 296 374 $; = prev_splitter 297 375 298 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 299 393 assert_equal ['h', 'e', 'llo'], 'hello'.onig_regexp_split(OnigRegexp.new(''), 3) 300 394 assert_equal ['h', 'i', 'd', 'a', 'd'], 'hi dad'.onig_regexp_split(OnigRegexp.new('\s*')) … … 309 403 310 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 311 419 end 312 420 … … 332 440 m = onig_match_data_example 333 441 assert_equal m[0], $~[0] 442 443 onig_mismatch_data_example 444 assert_nil $~ 334 445 end 335 446 … … 337 448 m = onig_match_data_example 338 449 assert_equal m[0], $& 450 451 onig_mismatch_data_example 452 assert_nil $& 339 453 end 340 454 … … 342 456 m = onig_match_data_example 343 457 assert_equal m.pre_match, $` 458 459 onig_mismatch_data_example 460 assert_nil $` 344 461 end 345 462 … … 347 464 m = onig_match_data_example 348 465 assert_equal m.post_match, $' 466 467 onig_mismatch_data_example 468 assert_nil $' 349 469 end 350 470 … … 352 472 m = onig_match_data_example 353 473 assert_equal m[-1], $+ 474 475 onig_mismatch_data_example 476 assert_nil $+ 354 477 end 355 478 … … 365 488 assert_nil $8 366 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 367 501 end 368 502 … … 387 521 end 388 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 389 567 Regexp = Object 390 568 391 569 assert('OnigRegexp not default') do 570 $~ = nil 392 571 onig_match_data_example 393 572 assert_nil $~
Note:
See TracChangeset
for help on using the changeset viewer.