Thread::Queue.new should accept an Enumerable [Feature #17327]
Enumerable implements #to_a but not #to_array.
This commit is contained in:
parent
e8b210542b
commit
1f0e0dfb22
@ -18,9 +18,9 @@ describe "Queue#initialize" do
|
|||||||
q.should.empty?
|
q.should.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
it "uses #to_ary on the provided Enumerable" do
|
it "uses #to_a on the provided Enumerable" do
|
||||||
enumerable = MockObject.new('mock-enumerable')
|
enumerable = MockObject.new('mock-enumerable')
|
||||||
enumerable.should_receive(:to_ary).and_return([1, 2, 3])
|
enumerable.should_receive(:to_a).and_return([1, 2, 3])
|
||||||
q = Queue.new(enumerable)
|
q = Queue.new(enumerable)
|
||||||
q.size.should == 3
|
q.size.should == 3
|
||||||
q.should_not.empty?
|
q.should_not.empty?
|
||||||
@ -30,15 +30,9 @@ describe "Queue#initialize" do
|
|||||||
q.should.empty?
|
q.should.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises if the provided Enumerable does not respond to #to_ary" do
|
it "raises if the provided Enumerable does not respond to #to_a" do
|
||||||
enumerable = MockObject.new('mock-enumerable')
|
enumerable = MockObject.new('mock-enumerable')
|
||||||
-> { Queue.new(enumerable) }.should raise_error(TypeError, "no implicit conversion of MockObject into Array")
|
-> { Queue.new(enumerable) }.should raise_error(TypeError, "can't convert MockObject into Array")
|
||||||
end
|
|
||||||
|
|
||||||
it "raises if the provided Enumerable #to_ary does not return an Array" do
|
|
||||||
enumerable = MockObject.new('mock-enumerable')
|
|
||||||
enumerable.should_receive(:to_ary).and_return(14)
|
|
||||||
-> { Queue.new(enumerable) }.should raise_error(TypeError, "can't convert MockObject to Array (MockObject#to_ary gives Integer)")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -54,6 +54,28 @@ class TestThreadQueue < Test::Unit::TestCase
|
|||||||
assert_equal 0, to_workers.size
|
assert_equal 0, to_workers.size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_queue_initialize
|
||||||
|
e = Class.new do
|
||||||
|
include Enumerable
|
||||||
|
def initialize(list) @list = list end
|
||||||
|
def each(&block) @list.each(&block) end
|
||||||
|
end
|
||||||
|
|
||||||
|
all_assertions_foreach(nil,
|
||||||
|
[Array, "Array"],
|
||||||
|
[e, "Enumerable"],
|
||||||
|
[Struct.new(:to_a), "Array-like"],
|
||||||
|
) do |a, type|
|
||||||
|
q = Queue.new(a.new([1,2,3]))
|
||||||
|
assert_equal(3, q.size, type)
|
||||||
|
assert_not_predicate(q, :empty?, type)
|
||||||
|
assert_equal(1, q.pop, type)
|
||||||
|
assert_equal(2, q.pop, type)
|
||||||
|
assert_equal(3, q.pop, type)
|
||||||
|
assert_predicate(q, :empty?, type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_sized_queue_initialize
|
def test_sized_queue_initialize
|
||||||
q = SizedQueue.new(1)
|
q = SizedQueue.new(1)
|
||||||
assert_equal 1, q.max
|
assert_equal 1, q.max
|
||||||
|
@ -858,7 +858,7 @@ rb_queue_initialize(int argc, VALUE *argv, VALUE self)
|
|||||||
list_head_init(queue_waitq(q));
|
list_head_init(queue_waitq(q));
|
||||||
rb_scan_args(argc, argv, "01", &initial);
|
rb_scan_args(argc, argv, "01", &initial);
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
rb_ary_concat(q->que, rb_convert_type(initial, T_ARRAY, "Array", "to_ary"));
|
rb_ary_concat(q->que, rb_to_array(initial));
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user