- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-string-ext/test/string.rb
r331 r439 1 # coding: utf-8 1 2 ## 2 3 # String(Ext) Test 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 12 13 assert('String#getbyte') do 14 str1 = "hello" 15 bytes1 = [104, 101, 108, 108, 111] 16 assert_equal bytes1[0], str1.getbyte(0) 17 assert_equal bytes1[-1], str1.getbyte(-1) 18 assert_equal bytes1[6], str1.getbyte(6) 19 20 str2 = "\xFF" 21 bytes2 = [0xFF] 22 assert_equal bytes2[0], str2.getbyte(0) 23 end 24 25 assert('String#setbyte') do 26 str1 = "hello" 27 h = "H".getbyte(0) 28 str1.setbyte(0, h) 29 assert_equal(h, str1.getbyte(0)) 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) } 43 end 44 45 assert('String#byteslice') do 46 str1 = "hello" 47 assert_equal("e", str1.byteslice(1)) 48 assert_equal("o", str1.byteslice(-1)) 49 assert_equal("ell", str1.byteslice(1..3)) 50 assert_equal("el", str1.byteslice(1...3)) 5 UTF8STRING = __ENCODING__ == "UTF-8" 6 7 def assert_upto(exp, receiver, *args) 8 act = [] 9 receiver.upto(*args) { |v| act << v } 10 assert_equal exp, act 51 11 end 52 12 53 13 assert('String#dump') do 54 ("\1" * 100).dump # should not raise an exception - regress #1210 55 "\0".inspect == "\"\\000\"" and 56 "foo".dump == "\"foo\"" 14 assert_equal("\"\\x00\"", "\0".dump) 15 assert_equal("\"foo\"", "foo".dump) 16 assert_equal('"\xe3\x82\x8b"', "る".dump) 17 assert_nothing_raised { ("\1" * 100).dump } # regress #1210 57 18 end 58 19 59 20 assert('String#strip') do 60 21 s = " abc " 61 "".strip == "" and " \t\r\n\f\v".strip == "" and 62 "\0a\0".strip == "\0a" and 63 "abc".strip == "abc" and 64 " abc".strip == "abc" and 65 "abc ".strip == "abc" and 66 " abc ".strip == "abc" and 67 s == " abc " 22 assert_equal("abc", s.strip) 23 assert_equal(" abc ", s) 24 assert_equal("", "".strip) 25 assert_equal("", " \t\r\n\f\v".strip) 26 assert_equal("\0a", "\0a\0".strip) 27 assert_equal("abc", "abc".strip) 28 assert_equal("abc", " abc".strip) 29 assert_equal("abc", "abc ".strip) 68 30 end 69 31 70 32 assert('String#lstrip') do 71 33 s = " abc " 72 s.lstrip73 "".lstrip == "" and " \t\r\n\f\v".lstrip == "" and74 "\0a\0".lstrip == "\0a\0" and75 "abc".lstrip == "abc" and76 " abc".lstrip == "abc" and77 "abc ".lstrip == "abc " and78 " abc ".lstrip == "abc " and79 s == " abc "34 assert_equal("abc ", s.lstrip) 35 assert_equal(" abc ", s) 36 assert_equal("", "".lstrip) 37 assert_equal("", " \t\r\n\f\v".lstrip) 38 assert_equal("\0a\0", "\0a\0".lstrip) 39 assert_equal("abc", "abc".lstrip) 40 assert_equal("abc", " abc".lstrip) 41 assert_equal("abc ", "abc ".lstrip) 80 42 end 81 43 82 44 assert('String#rstrip') do 83 45 s = " abc " 84 s.rstrip85 "".rstrip == "" and " \t\r\n\f\v".rstrip == "" and86 "\0a\0".rstrip == "\0a" and87 "abc".rstrip == "abc" and88 " abc".rstrip == " abc" and89 "abc ".rstrip == "abc" and90 " abc ".rstrip == " abc" and91 s == " abc "46 assert_equal(" abc", s.rstrip) 47 assert_equal(" abc ", s) 48 assert_equal("", "".rstrip) 49 assert_equal("", " \t\r\n\f\v".rstrip) 50 assert_equal("\0a", "\0a\0".rstrip) 51 assert_equal("abc", "abc".rstrip) 52 assert_equal(" abc", " abc".rstrip) 53 assert_equal("abc", "abc ".rstrip) 92 54 end 93 55 … … 95 57 s = " abc " 96 58 t = "abc" 97 s.strip! == "abc" and s == "abc" and t.strip! == nil 59 assert_equal("abc", s.strip!) 60 assert_equal("abc", s) 61 assert_nil(t.strip!) 62 assert_equal("abc", t) 98 63 end 99 64 … … 101 66 s = " abc " 102 67 t = "abc " 103 s.lstrip! == "abc " and s == "abc " and t.lstrip! == nil 68 assert_equal("abc ", s.lstrip!) 69 assert_equal("abc ", s) 70 assert_nil(t.lstrip!) 71 assert_equal("abc ", t) 104 72 end 105 73 … … 107 75 s = " abc " 108 76 t = " abc" 109 s.rstrip! == " abc" and s == " abc" and t.rstrip! == nil 77 assert_equal(" abc", s.rstrip!) 78 assert_equal(" abc", s) 79 assert_nil(t.rstrip!) 80 assert_equal(" abc", t) 110 81 end 111 82 … … 125 96 assert_equal "Hello World!", "Hello " << "World" << 33 126 97 assert_equal "Hello World!", "Hello ".concat("World").concat(33) 127 128 o = Object.new129 def o.to_str130 "to_str"131 end132 assert_equal "hi to_str", "hi " << o133 134 98 assert_raise(TypeError) { "".concat(Object.new) } 99 100 if UTF8STRING 101 assert_equal "H«", "H" << 0xab 102 assert_equal "Hは", "H" << 12399 103 else 104 assert_equal "H\xab", "H" << 0xab 105 assert_raise(RangeError) { "H" << 12399 } 106 end 135 107 end 136 108 … … 140 112 assert_equal(-1, "abcdef".casecmp("abcdefg")) 141 113 assert_equal 0, "abcdef".casecmp("ABCDEF") 142 o = Object.new 143 def o.to_str 144 "ABCDEF" 145 end 146 assert_equal 0, "abcdef".casecmp(o) 114 end 115 116 assert('String#count') do 117 s = "abccdeff123" 118 assert_equal 0, s.count("") 119 assert_equal 1, s.count("a") 120 assert_equal 2, s.count("ab") 121 assert_equal 9, s.count("^c") 122 assert_equal 8, s.count("a-z") 123 assert_equal 4, s.count("a0-9") 124 end 125 126 assert('String#tr') do 127 assert_equal "ABC", "abc".tr('a-z', 'A-Z') 128 assert_equal "hippo", "hello".tr('el', 'ip') 129 assert_equal "Ruby", "Lisp".tr("Lisp", "Ruby") 130 assert_equal "*e**o", "hello".tr('^aeiou', '*') 131 assert_equal "heo", "hello".tr('l', '') 132 end 133 134 assert('String#tr!') do 135 s = "abcdefghijklmnopqR" 136 assert_equal "ab12222hijklmnopqR", s.tr!("cdefg", "12") 137 assert_equal "ab12222hijklmnopqR", s 138 end 139 140 assert('String#tr_s') do 141 assert_equal "hero", "hello".tr_s('l', 'r') 142 assert_equal "h*o", "hello".tr_s('el', '*') 143 assert_equal "hhxo", "hello".tr_s('el', 'hx') 144 end 145 146 assert('String#tr_s!') do 147 s = "hello" 148 assert_equal "hero", s.tr_s!('l', 'r') 149 assert_equal "hero", s 150 assert_nil s.tr_s!('l', 'r') 151 end 152 153 assert('String#squeeze') do 154 assert_equal "yelow mon", "yellow moon".squeeze 155 assert_equal " now is the", " now is the".squeeze(" ") 156 assert_equal "puters shot balls", "putters shoot balls".squeeze("m-z") 157 end 158 159 assert('String#squeeze!') do 160 s = " now is the" 161 assert_equal " now is the", s.squeeze!(" ") 162 assert_equal " now is the", s 163 end 164 165 assert('String#delete') do 166 assert_equal "he", "hello".delete("lo") 167 assert_equal "hll", "hello".delete("aeiou") 168 assert_equal "ll", "hello".delete("^l") 169 assert_equal "ho", "hello".delete("ej-m") 170 end 171 172 assert('String#delete!') do 173 s = "hello" 174 assert_equal "he", s.delete!("lo") 175 assert_equal "he", s 176 assert_nil s.delete!("lz") 147 177 end 148 178 … … 202 232 assert_equal 8, "010".oct 203 233 assert_equal (-8), "-10".oct 204 end205 206 assert('String#chr') do207 assert_equal "a", "abcde".chr208 # test Fixnum#chr as well209 assert_equal "a", 97.chr210 234 end 211 235 … … 496 520 497 521 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 522 assert_upto %w(a8 a9 b0 b1 b2 b3 b4 b5 b6), "a8", "b6" 523 assert_upto ["9", "10", "11"], "9", "11" 524 assert_upto [], "25", "5" 525 assert_upto ["07", "08", "09", "10", "11"], "07", "11" 526 assert_upto ["9", ":", ";", "<", "=", ">", "?", "@", "A"], "9", "A" 527 528 if UTF8STRING 529 assert_upto %w(あ ぃ い ぅ う ぇ え ぉ お), "あ", "お" 530 end 508 531 509 532 a = "aa" … … 587 610 588 611 assert('String#chr') do 612 assert_equal "a", "abcde".chr 589 613 assert_equal "h", "hello!".chr 590 end 614 assert_equal "", "".chr 615 end 616 591 617 assert('String#chr(UTF-8)') do 592 618 assert_equal "こ", "こんにちは世界!".chr … … 614 640 615 641 assert('String#each_char') do 616 s = ""642 chars = [] 617 643 "hello!".each_char do |x| 618 s +=x619 end 620 assert_equal "hello!",s644 chars << x 645 end 646 assert_equal ["h", "e", "l", "l", "o", "!"], chars 621 647 end 622 648 623 649 assert('String#each_char(UTF-8)') do 624 s = ""650 chars = [] 625 651 "こんにちは世界!".each_char do |x| 626 s +=x627 end 628 assert_equal "こんにちは世界!",s652 chars << x 653 end 654 assert_equal ["こ", "ん", "に", "ち", "は", "世", "界", "!"], chars 629 655 end if UTF8STRING 630 656 … … 666 692 assert_equal expect, cp 667 693 end if UTF8STRING 694 695 assert('String#delete_prefix') do 696 assert_equal "llo", "hello".delete_prefix("he") 697 assert_equal "hello", "hello".delete_prefix("llo") 698 assert_equal "llo", "hello".delete_prefix!("he") 699 assert_nil "hello".delete_prefix!("llo") 700 end 701 702 assert('String#delete_suffix') do 703 assert_equal "he", "hello".delete_suffix("llo") 704 assert_equal "hello", "hello".delete_suffix("he") 705 assert_equal "he", "hello".delete_suffix!("llo") 706 assert_nil "hello".delete_suffix!("he") 707 end
Note:
See TracChangeset
for help on using the changeset viewer.