Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

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
    12##
    23# String(Ext) Test
    34
    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))
     5UTF8STRING = __ENCODING__ == "UTF-8"
     6
     7def assert_upto(exp, receiver, *args)
     8  act = []
     9  receiver.upto(*args) { |v| act << v }
     10  assert_equal exp, act
    5111end
    5212
    5313assert('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
    5718end
    5819
    5920assert('String#strip') do
    6021  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)
    6830end
    6931
    7032assert('String#lstrip') do
    7133  s = "  abc  "
    72   s.lstrip
    73   "".lstrip == "" and " \t\r\n\f\v".lstrip == "" and
    74   "\0a\0".lstrip == "\0a\0" and
    75   "abc".lstrip     == "abc"   and
    76   "  abc".lstrip   == "abc"   and
    77   "abc  ".lstrip   == "abc  " and
    78   "  abc  ".lstrip == "abc  " and
    79   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)
    8042end
    8143
    8244assert('String#rstrip') do
    8345  s = "  abc  "
    84   s.rstrip
    85   "".rstrip == "" and " \t\r\n\f\v".rstrip == "" and
    86   "\0a\0".rstrip == "\0a" and
    87   "abc".rstrip     == "abc"   and
    88   "  abc".rstrip   == "  abc" and
    89   "abc  ".rstrip   == "abc"   and
    90   "  abc  ".rstrip == "  abc" and
    91   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)
    9254end
    9355
     
    9557  s = "  abc  "
    9658  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)
    9863end
    9964
     
    10166  s = "  abc  "
    10267  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)
    10472end
    10573
     
    10775  s = "  abc  "
    10876  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)
    11081end
    11182
     
    12596  assert_equal "Hello World!", "Hello " << "World" << 33
    12697  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 
    13498  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
    135107end
    136108
     
    140112  assert_equal(-1, "abcdef".casecmp("abcdefg"))
    141113  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)
     114end
     115
     116assert('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")
     124end
     125
     126assert('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', '')
     132end
     133
     134assert('String#tr!') do
     135  s = "abcdefghijklmnopqR"
     136  assert_equal "ab12222hijklmnopqR", s.tr!("cdefg", "12")
     137  assert_equal "ab12222hijklmnopqR", s
     138end
     139
     140assert('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')
     144end
     145
     146assert('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')
     151end
     152
     153assert('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")
     157end
     158
     159assert('String#squeeze!') do
     160  s = "  now   is  the"
     161  assert_equal " now is the", s.squeeze!(" ")
     162  assert_equal " now is the", s
     163end
     164
     165assert('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")
     170end
     171
     172assert('String#delete!') do
     173  s = "hello"
     174  assert_equal "he", s.delete!("lo")
     175  assert_equal "he", s
     176  assert_nil s.delete!("lz")
    147177end
    148178
     
    202232  assert_equal 8, "010".oct
    203233  assert_equal (-8), "-10".oct
    204 end
    205 
    206 assert('String#chr') do
    207   assert_equal "a", "abcde".chr
    208   # test Fixnum#chr as well
    209   assert_equal "a", 97.chr
    210234end
    211235
     
    496520
    497521assert('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
    508531
    509532  a     = "aa"
     
    587610
    588611assert('String#chr') do
     612  assert_equal "a", "abcde".chr
    589613  assert_equal "h", "hello!".chr
    590 end
     614  assert_equal "", "".chr
     615end
     616
    591617assert('String#chr(UTF-8)') do
    592618  assert_equal "こ", "こんにちは世界!".chr
     
    614640
    615641assert('String#each_char') do
    616   s = ""
     642  chars = []
    617643  "hello!".each_char do |x|
    618     s += x
    619   end
    620   assert_equal "hello!", s
     644    chars << x
     645  end
     646  assert_equal ["h", "e", "l", "l", "o", "!"], chars
    621647end
    622648
    623649assert('String#each_char(UTF-8)') do
    624   s = ""
     650  chars = []
    625651  "こんにちは世界!".each_char do |x|
    626     s += x
    627   end
    628   assert_equal "こんにちは世界!", s
     652    chars << x
     653  end
     654  assert_equal ["こ", "ん", "に", "ち", "は", "世", "界", "!"], chars
    629655end if UTF8STRING
    630656
     
    666692  assert_equal expect, cp
    667693end if UTF8STRING
     694
     695assert('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")
     700end
     701
     702assert('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")
     707end
Note: See TracChangeset for help on using the changeset viewer.