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-enumerator/test/enumerator.rb

    r331 r439  
    77end
    88
    9 assert 'Enumerator' do
     9def assert_take(exp, enumerator)
     10  result = []
     11  n = exp.size
     12  enumerator.each do |v|
     13    result << v
     14    n -= 1
     15    break if n == 0
     16  end if n > 0
     17  assert_equal exp, result
     18end
     19
     20assert 'Enumerator.class' do
    1021  assert_equal Class, Enumerator.class
    1122end
    1223
    13 assert 'Enumerator' do
     24assert 'Enumerator.superclass' do
    1425  assert_equal Object, Enumerator.superclass
    1526end
     
    2031  assert_equal [[:x,1],[:y,2]], {x:1, y:2}.each.map{|i| i}.sort
    2132  assert_equal [1,2,3], @obj.to_enum(:foo, 1,2,3).to_a
    22   assert_equal [1,2,3], Enumerator.new(@obj, :foo, 1,2,3).to_a
    23   assert_equal [1,2,3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.take(3)
     33  assert_take [1,2,3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }
    2434  assert_raise(ArgumentError) { Enumerator.new }
    25   enum = @obj.to_enum
    26   assert_raise(NoMethodError) { enum.each {} }
    2735
    2836  # examples
     
    3442    end
    3543  end
    36   assert_equal fib.take(10), [1,1,2,3,5,8,13,21,34,55]
     44  assert_take [1,1,2,3,5,8,13,21,34,55], fib
    3745end
    3846
     
    5462  @obj.to_enum(:foo, 1, 2, 3).with_index(10).with_index(20) { |*i| a << i }
    5563  assert_equal [[[1, 10], 20], [[2, 11], 21], [[3, 12], 22]], a
    56 end
    57 
    58 assert 'Enumerator#with_index nonnum offset' do
    59   s = Object.new
    60   def s.to_int; 1 end
    61   assert_equal([[1,1],[2,2],[3,3]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a)
    6264end
    6365
     
    100102assert 'Enumerator#inspect' do
    101103  e = (0..10).each
    102   assert_equal("#<Enumerator: 0..10:each>", e.inspect)
    103   e = Enumerator.new("FooObject", :foo, 1)
    104   assert_equal("#<Enumerator: FooObject:foo(1)>", e.inspect)
    105   e = Enumerator.new("FooObject", :foo, 1, 2, 3)
    106   assert_equal("#<Enumerator: FooObject:foo(1, 2, 3)>", e.inspect)
     104  assert_equal('#<Enumerator: 0..10:each>', e.inspect)
     105  e = 'FooObject'.enum_for(:foo, 1)
     106  assert_equal('#<Enumerator: "FooObject":foo(1)>', e.inspect)
     107  e = 'FooObject'.enum_for(:foo, 1, 2, 3)
     108  assert_equal('#<Enumerator: "FooObject":foo(1, 2, 3)>', e.inspect)
     109  e = nil.enum_for(:to_s)
     110  assert_equal('#<Enumerator: nil:to_s>', e.inspect)
    107111end
    108112
     
    426430
    427431assert 'Kernel#to_enum' do
     432  e = nil
    428433  assert_equal Enumerator, [].to_enum.class
    429   assert_raise(ArgumentError){ nil.to_enum }
     434  assert_nothing_raised { e = [].to_enum(:_not_implemented_) }
     435  assert_raise(NoMethodError) { e.first }
    430436end
    431437
     
    510516assert 'Hash#select' do
    511517  h = {1=>2,3=>4,5=>6}
    512   hret = h.select.with_index {|a,b| a[1] == 4}
     518  hret = h.select.with_index {|a,_b| a[1] == 4}
    513519  assert_equal({3=>4}, hret)
    514520  assert_equal({1=>2,3=>4,5=>6}, h)
     
    517523assert 'Hash#select!' do
    518524  h = {1=>2,3=>4,5=>6}
    519   hret = h.select!.with_index {|a,b| a[1] == 4}
     525  hret = h.select!.with_index {|a,_b| a[1] == 4}
    520526  assert_equal h, hret
    521527  assert_equal({3=>4}, h)
     
    524530assert 'Hash#reject' do
    525531  h = {1=>2,3=>4,5=>6}
    526   hret = h.reject.with_index {|a,b| a[1] == 4}
     532  hret = h.reject.with_index {|a,_b| a[1] == 4}
    527533  assert_equal({1=>2,5=>6}, hret)
    528534  assert_equal({1=>2,3=>4,5=>6}, h)
     
    531537assert 'Hash#reject!' do
    532538  h = {1=>2,3=>4,5=>6}
    533   hret = h.reject!.with_index {|a,b| a[1] == 4}
     539  hret = h.reject!.with_index {|a,_b| a[1] == 4}
    534540  assert_equal h, hret
    535541  assert_equal({1=>2,5=>6}, h)
     
    545551  assert_equal [1,2,3,4,5], c
    546552end
     553
     554assert 'Enumerable#zip' do
     555  assert_equal [[1, 10], [2, 11], [3, 12]], [1,2,3].zip(10..Float::INFINITY)
     556
     557  ret = []
     558  assert_equal nil, [1,2,3].zip(10..Float::INFINITY) { |i| ret << i }
     559  assert_equal [[1, 10], [2, 11], [3, 12]], ret
     560
     561  assert_raise(TypeError) { [1].zip(1) }
     562end
     563
     564assert 'Enumerator.produce' do
     565  assert_raise(ArgumentError) { Enumerator.produce }
     566
     567  # Without initial object
     568  passed_args = []
     569  enum = Enumerator.produce {|obj| passed_args << obj; (obj || 0).succ }
     570  assert_equal Enumerator, enum.class
     571  assert_take [1, 2, 3], enum
     572  assert_equal [nil, 1, 2], passed_args
     573
     574  # With initial object
     575  passed_args = []
     576  enum = Enumerator.produce(1) {|obj| passed_args << obj; obj.succ }
     577  assert_take [1, 2, 3], enum
     578  assert_equal [1, 2], passed_args
     579
     580  # Raising StopIteration
     581  words = %w[The quick brown fox jumps over the lazy dog]
     582  enum = Enumerator.produce { words.shift or raise StopIteration }
     583  assert_equal %w[The quick brown fox jumps over the lazy dog], enum.to_a
     584
     585  # Raising StopIteration
     586  object = [[[["abc", "def"], "ghi", "jkl"], "mno", "pqr"], "stuv", "wxyz"]
     587  enum = Enumerator.produce(object) {|obj|
     588    obj.respond_to?(:first) or raise StopIteration
     589    obj.first
     590  }
     591  assert_nothing_raised {
     592    assert_equal [
     593      [[[["abc", "def"], "ghi", "jkl"], "mno", "pqr"], "stuv", "wxyz"],
     594      [[["abc", "def"], "ghi", "jkl"], "mno", "pqr"],
     595      [["abc", "def"], "ghi", "jkl"],
     596      ["abc", "def"],
     597      "abc",
     598    ], enum.to_a
     599  }
     600end
Note: See TracChangeset for help on using the changeset viewer.