- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-1.3.0
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-string-ext/test/string.rb
r321 r331 3 3 4 4 UTF8STRING = ("\343\201\202".size == 1) 5 6 assert('String.try_convert') do 7 assert_nil String.try_convert(nil) 8 assert_nil String.try_convert(:foo) 9 assert_equal "", String.try_convert("") 10 assert_equal "1,2,3", String.try_convert("1,2,3") 11 end 5 12 6 13 assert('String#getbyte') do … … 22 29 assert_equal(h, str1.getbyte(0)) 23 30 assert_equal("Hello", str1) 31 end 32 33 assert("String#setbyte raises IndexError if arg conversion resizes String") do 34 $s = "01234\n" 35 class Tmp 36 def to_i 37 $s.chomp! '' 38 95 39 end 40 end 41 tmp = Tmp.new 42 assert_raise(IndexError) { $s.setbyte(5, tmp) } 24 43 end 25 44 … … 104 123 105 124 assert('String#concat') do 106 s = "Hello " 107 s.concat "World!" 108 t = "Hello " 109 t << "World!" 110 assert_equal "Hello World!", t 111 assert_equal "Hello World!", s 125 assert_equal "Hello World!", "Hello " << "World" << 33 126 assert_equal "Hello World!", "Hello ".concat("World").concat(33) 127 128 o = Object.new 129 def o.to_str 130 "to_str" 131 end 132 assert_equal "hi to_str", "hi " << o 133 134 assert_raise(TypeError) { "".concat(Object.new) } 112 135 end 113 136 … … 400 423 assert_raise(IndexError) { "abcd".insert(5, 'X') } 401 424 assert_raise(IndexError) { "abcd".insert(-6, 'X') } 425 426 a = "abcd" 427 a.insert(0, 'X') 428 assert_equal "Xabcd", a 402 429 end 403 430 … … 411 438 assert_equal "hello", "hello".ljust(4) 412 439 assert_equal "hello ", "hello".ljust(20) 440 assert_equal 20, "hello".ljust(20).length 413 441 assert_equal "hello123412341234123", "hello".ljust(20, '1234') 414 442 assert_equal "hello", "hello".ljust(-3) 415 443 end 416 444 445 assert('String#rjust') do 446 assert_equal "hello", "hello".rjust(4) 447 assert_equal " hello", "hello".rjust(20) 448 assert_equal 20, "hello".rjust(20).length 449 assert_equal "123412341234123hello", "hello".rjust(20, '1234') 450 assert_equal "hello", "hello".rjust(-3) 451 end 452 453 if UTF8STRING 454 assert('String#ljust with UTF8') do 455 assert_equal "helloん ", "helloん".ljust(20) 456 assert_equal "helloó ", "helloó".ljust(34) 457 assert_equal 34, "helloó".ljust(34).length 458 assert_equal "helloんんんんんんんんんんんんんん", "hello".ljust(19, 'ん') 459 assert_equal "helloんんんんんんんんんんんんんんん", "hello".ljust(20, 'ん') 460 end 461 462 assert('String#rjust with UTF8') do 463 assert_equal " helloん", "helloん".rjust(20) 464 assert_equal " helloó", "helloó".rjust(34) 465 # assert_equal 34, "helloó".rjust(34).length 466 assert_equal "んんんんんんんんんんんんんんhello", "hello".rjust(19, 'ん') 467 assert_equal "んんんんんんんんんんんんんんんhello", "hello".rjust(20, 'ん') 468 end 469 470 assert('UTF8 byte counting') do 471 ret = ' ' 472 ret[-6..-1] = "helloó" 473 assert_equal 34, ret.length 474 end 475 end 476 477 assert('String#ljust should not change string') do 478 a = "hello" 479 a.ljust(20) 480 assert_equal "hello", a 481 end 482 483 assert('String#rjust should not change string') do 484 a = "hello" 485 a.rjust(20) 486 assert_equal "hello", a 487 end 488 489 assert('String#ljust should raise on zero width padding') do 490 assert_raise(ArgumentError) { "foo".ljust(10, '') } 491 end 492 493 assert('String#rjust should raise on zero width padding') do 494 assert_raise(ArgumentError) { "foo".rjust(10, '') } 495 end 496 417 497 assert('String#upto') do 498 assert_equal %w(a8 a9 b0 b1 b2 b3 b4 b5 b6), "a8".upto("b6").to_a 499 assert_equal ["9", "10", "11"], "9".upto("11").to_a 500 assert_equal [], "25".upto("5").to_a 501 assert_equal ["07", "08", "09", "10", "11"], "07".upto("11").to_a 502 503 if UTF8STRING 504 assert_equal ["あ", "ぃ", "い", "ぅ", "う", "ぇ", "え", "ぉ", "お"], "あ".upto("お").to_a 505 end 506 507 assert_equal ["9", ":", ";", "<", "=", ">", "?", "@", "A"], "9".upto("A").to_a 508 418 509 a = "aa" 419 510 start = "aa" … … 475 566 }) 476 567 assert_equal(2, count) 568 569 assert_raise(TypeError) { "a".upto(:c) {} } 477 570 end 478 571 … … 480 573 got = "hello!".split('').map {|x| x.ord} 481 574 expect = [104, 101, 108, 108, 111, 33] 575 unless UTF8STRING 576 got << "\xff".ord 577 expect << 0xff 578 end 482 579 assert_equal expect, got 483 580 end
Note:
See TracChangeset
for help on using the changeset viewer.