diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-02-21 15:38:59 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-02-21 15:38:59 +0000 |
commit | da7976235fbc2986925969646071bebe3702e49f (patch) | |
tree | 83bbf6a8e03cf990369feabe6c1c9d45c69f4c85 /spec/ruby/core | |
parent | b8e389a0f3226c90e96e02e6396686a3bef6a456 (diff) | |
download | ruby-da7976235fbc2986925969646071bebe3702e49f.tar.gz |
Update to ruby/spec@7a16e01
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67112 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core')
51 files changed, 482 insertions, 125 deletions
diff --git a/spec/ruby/core/array/each_spec.rb b/spec/ruby/core/array/each_spec.rb index ad8a5ad3d5..256647d61e 100644 --- a/spec/ruby/core/array/each_spec.rb +++ b/spec/ruby/core/array/each_spec.rb @@ -27,6 +27,22 @@ describe "Array#each" do b.should == [2, nil, 4] end + it "yields elements added to the end of the array by the block" do + a = [2] + iterated = [] + a.each { |x| iterated << x; x.times { a << 0 } } + + iterated.should == [2, 0, 0] + end + + it "does not yield elements deleted from the end of the array" do + a = [2, 3, 1] + iterated = [] + a.each { |x| iterated << x; a.delete_at(2) if x == 3 } + + iterated.should == [2, 3] + end + it_behaves_like :enumeratorize, :each it_behaves_like :enumeratorized_with_origin_size, :each, [1,2,3] end diff --git a/spec/ruby/core/enumerable/chain_spec.rb b/spec/ruby/core/enumerable/chain_spec.rb index 0e86a02905..85c0c03603 100644 --- a/spec/ruby/core/enumerable/chain_spec.rb +++ b/spec/ruby/core/enumerable/chain_spec.rb @@ -9,14 +9,17 @@ ruby_version_is "2.6" do it "returns a chain of self and provided enumerables" do one = EnumerableSpecs::Numerous.new(1) - two = EnumerableSpecs::Numerous.new(2) - three = EnumerableSpecs::Numerous.new(3) + two = EnumerableSpecs::Numerous.new(2, 3) + three = EnumerableSpecs::Numerous.new(4, 5, 6) chain = one.chain(two, three) - chain.should be_an_instance_of(Enumerator::Chain) chain.each { |item| ScratchPad << item } - ScratchPad.recorded.should == [1, 2, 3] + ScratchPad.recorded.should == [1, 2, 3, 4, 5, 6] + end + + it "returns an Enumerator::Chain if given a block" do + EnumerableSpecs::Numerous.new.chain.should be_an_instance_of(Enumerator::Chain) end end end diff --git a/spec/ruby/core/enumerator/lazy/chunk_spec.rb b/spec/ruby/core/enumerator/lazy/chunk_spec.rb index 246dd72712..3f60c6ea3f 100644 --- a/spec/ruby/core/enumerator/lazy/chunk_spec.rb +++ b/spec/ruby/core/enumerator/lazy/chunk_spec.rb @@ -68,4 +68,10 @@ describe "Enumerator::Lazy#chunk" do end end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.chunk { |n| n.even? }.first(100).should == + s.first(100).chunk { |n| n.even? }.to_a + end end diff --git a/spec/ruby/core/enumerator/lazy/chunk_while_spec.rb b/spec/ruby/core/enumerator/lazy/chunk_while_spec.rb new file mode 100644 index 0000000000..d555089872 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/chunk_while_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' + +describe "Enumerator::Lazy#chunk_while" do + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.chunk_while { |a, b| false }.first(100).should == + s.first(100).chunk_while { |a, b| false }.to_a + end +end diff --git a/spec/ruby/core/enumerator/lazy/drop_spec.rb b/spec/ruby/core/enumerator/lazy/drop_spec.rb index 8c15dea11d..822b8034fb 100644 --- a/spec/ruby/core/enumerator/lazy/drop_spec.rb +++ b/spec/ruby/core/enumerator/lazy/drop_spec.rb @@ -49,4 +49,10 @@ describe "Enumerator::Lazy#drop" do end end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.drop(100).first(100).should == + s.first(200).drop(100) + end end diff --git a/spec/ruby/core/enumerator/lazy/drop_while_spec.rb b/spec/ruby/core/enumerator/lazy/drop_while_spec.rb index e29026be09..6f6472e393 100644 --- a/spec/ruby/core/enumerator/lazy/drop_while_spec.rb +++ b/spec/ruby/core/enumerator/lazy/drop_while_spec.rb @@ -57,4 +57,10 @@ describe "Enumerator::Lazy#drop_while" do end end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.drop_while { |n| n < 100 }.first(100).should == + s.first(200).drop_while { |n| n < 100 } + end end diff --git a/spec/ruby/core/enumerator/lazy/force_spec.rb b/spec/ruby/core/enumerator/lazy/force_spec.rb index 15701d1fd7..a7fa029135 100644 --- a/spec/ruby/core/enumerator/lazy/force_spec.rb +++ b/spec/ruby/core/enumerator/lazy/force_spec.rb @@ -27,4 +27,10 @@ describe "Enumerator::Lazy#force" do ScratchPad.recorded.should == [:before_yield] end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.take(100).force.should == + s.take(100) + end end diff --git a/spec/ruby/core/enumerator/lazy/grep_spec.rb b/spec/ruby/core/enumerator/lazy/grep_spec.rb index c46c760402..e759cc4a2c 100644 --- a/spec/ruby/core/enumerator/lazy/grep_spec.rb +++ b/spec/ruby/core/enumerator/lazy/grep_spec.rb @@ -79,4 +79,10 @@ describe "Enumerator::Lazy#grep" do end end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.grep(Numeric).first(100).should == + s.first(100).grep(Numeric) + end end diff --git a/spec/ruby/core/enumerator/lazy/grep_v_spec.rb b/spec/ruby/core/enumerator/lazy/grep_v_spec.rb index a0ec819505..13d474a8c4 100644 --- a/spec/ruby/core/enumerator/lazy/grep_v_spec.rb +++ b/spec/ruby/core/enumerator/lazy/grep_v_spec.rb @@ -81,4 +81,10 @@ describe "Enumerator::Lazy#grep_v" do end end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.grep_v(String).first(100).should == + s.first(100).grep_v(String) + end end diff --git a/spec/ruby/core/enumerator/lazy/lazy_spec.rb b/spec/ruby/core/enumerator/lazy/lazy_spec.rb index 5f386e2184..21fbfc27ab 100644 --- a/spec/ruby/core/enumerator/lazy/lazy_spec.rb +++ b/spec/ruby/core/enumerator/lazy/lazy_spec.rb @@ -6,6 +6,20 @@ describe "Enumerator::Lazy" do it "is a subclass of Enumerator" do Enumerator::Lazy.superclass.should equal(Enumerator) end + + it "defines lazy versions of a whitelist of Enumerator methods" do + lazy_methods = [ + :chunk, :collect, :collect_concat, :drop, :drop_while, :enum_for, + :find_all, :flat_map, :force, :grep, :grep_v, :lazy, :map, :reject, + :select, :slice_after, :slice_before, :slice_when, :take, :take_while, + :to_enum, :zip + ] + ruby_version_is "2.4" do + lazy_methods += [:chunk_while, :uniq] + end + + Enumerator::Lazy.instance_methods(false).should include(*lazy_methods) + end end describe "Enumerator::Lazy#lazy" do diff --git a/spec/ruby/core/enumerator/lazy/reject_spec.rb b/spec/ruby/core/enumerator/lazy/reject_spec.rb index 2661907b39..03444b471f 100644 --- a/spec/ruby/core/enumerator/lazy/reject_spec.rb +++ b/spec/ruby/core/enumerator/lazy/reject_spec.rb @@ -33,6 +33,18 @@ describe "Enumerator::Lazy#reject" do end end + it "lets exceptions raised in the block go through" do + lazy = 10.times.lazy.map do |i| + raise "foo" + end + + lazy = lazy.reject(&:nil?) + + -> { + lazy.first + }.should raise_error(RuntimeError, "foo") + end + it "calls the block with a gathered array when yield with multiple arguments" do yields = [] @yieldsmixed.reject { |v| yields << v }.force @@ -57,4 +69,10 @@ describe "Enumerator::Lazy#reject" do end end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.reject { |n| false }.first(100).should == + s.first(100).reject { |n| false } + end end diff --git a/spec/ruby/core/enumerator/lazy/shared/collect.rb b/spec/ruby/core/enumerator/lazy/shared/collect.rb index a686f30fd8..5690255a0c 100644 --- a/spec/ruby/core/enumerator/lazy/shared/collect.rb +++ b/spec/ruby/core/enumerator/lazy/shared/collect.rb @@ -53,4 +53,10 @@ describe :enumerator_lazy_collect, shared: true do end end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.send(@method) { |n| n }.first(100).should == + s.first(100).send(@method) { |n| n }.to_a + end end diff --git a/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb b/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb index 26478f4ce0..e8ee4b6940 100644 --- a/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb +++ b/spec/ruby/core/enumerator/lazy/shared/collect_concat.rb @@ -69,4 +69,10 @@ describe :enumerator_lazy_collect_concat, shared: true do end end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.send(@method) { |n| [-n, +n] }.first(200).should == + s.first(100).send(@method) { |n| [-n, +n] }.to_a + end end diff --git a/spec/ruby/core/enumerator/lazy/shared/select.rb b/spec/ruby/core/enumerator/lazy/shared/select.rb index 9127cf72b4..39074408ee 100644 --- a/spec/ruby/core/enumerator/lazy/shared/select.rb +++ b/spec/ruby/core/enumerator/lazy/shared/select.rb @@ -57,4 +57,10 @@ describe :enumerator_lazy_select, shared: true do end end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.send(@method) { |n| true }.first(100).should == + s.first(100).send(@method) { |n| true } + end end diff --git a/spec/ruby/core/enumerator/lazy/shared/to_enum.rb b/spec/ruby/core/enumerator/lazy/shared/to_enum.rb index a62a8ef90e..d488c9b263 100644 --- a/spec/ruby/core/enumerator/lazy/shared/to_enum.rb +++ b/spec/ruby/core/enumerator/lazy/shared/to_enum.rb @@ -47,4 +47,10 @@ describe :enumerator_lazy_to_enum, shared: true do @infinite.send(method, *args).should be_an_instance_of(Enumerator::Lazy) end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.send(@method, :with_index).first(100).should == + s.first(100).to_enum.send(@method, :with_index).to_a + end end diff --git a/spec/ruby/core/enumerator/lazy/slice_after_spec.rb b/spec/ruby/core/enumerator/lazy/slice_after_spec.rb new file mode 100644 index 0000000000..438df8d550 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/slice_after_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' + +describe "Enumerator::Lazy#slice_after" do + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.slice_after { |n| true }.first(100).should == + s.first(100).slice_after { |n| true }.to_a + end +end diff --git a/spec/ruby/core/enumerator/lazy/slice_before_spec.rb b/spec/ruby/core/enumerator/lazy/slice_before_spec.rb new file mode 100644 index 0000000000..6c8660c1a1 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/slice_before_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' + +describe "Enumerator::Lazy#slice_before" do + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.slice_before { |n| true }.first(100).should == + s.first(100).slice_before { |n| true }.to_a + end +end diff --git a/spec/ruby/core/enumerator/lazy/slice_when_spec.rb b/spec/ruby/core/enumerator/lazy/slice_when_spec.rb new file mode 100644 index 0000000000..e7673def47 --- /dev/null +++ b/spec/ruby/core/enumerator/lazy/slice_when_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../../spec_helper' + +describe "Enumerator::Lazy#slice_when" do + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.slice_when { |a, b| true }.first(100).should == + s.first(100).slice_when { |a, b| true }.to_a + end +end diff --git a/spec/ruby/core/enumerator/lazy/uniq_spec.rb b/spec/ruby/core/enumerator/lazy/uniq_spec.rb index fea2bec637..d337d063d6 100644 --- a/spec/ruby/core/enumerator/lazy/uniq_spec.rb +++ b/spec/ruby/core/enumerator/lazy/uniq_spec.rb @@ -72,5 +72,11 @@ ruby_version_is '2.4' do @lazy.uniq { |_, label| label.downcase }.force.should == [[0, 'foo'], [2, 'bar']] end end + + it "works with an infinite enumerable" do + s = 0..Float::INFINITY + s.lazy.uniq.first(100).should == + s.first(100).uniq + end end end diff --git a/spec/ruby/core/enumerator/lazy/zip_spec.rb b/spec/ruby/core/enumerator/lazy/zip_spec.rb index f3dbb63158..a28a7f5d5e 100644 --- a/spec/ruby/core/enumerator/lazy/zip_spec.rb +++ b/spec/ruby/core/enumerator/lazy/zip_spec.rb @@ -71,4 +71,16 @@ describe "Enumerator::Lazy#zip" do end end end + + it "works with an infinite enumerable and an array" do + s = 0..Float::INFINITY + s.lazy.zip(0..1000).first(100).should == + s.first(100).zip(0..100) + end + + it "works with two infinite enumerables" do + s = 0..Float::INFINITY + s.lazy.zip(s).first(100).should == + s.first(100).zip(s) + end end diff --git a/spec/ruby/core/exception/cause_spec.rb b/spec/ruby/core/exception/cause_spec.rb index 736ff1a046..007df41366 100644 --- a/spec/ruby/core/exception/cause_spec.rb +++ b/spec/ruby/core/exception/cause_spec.rb @@ -16,4 +16,29 @@ describe "Exception#cause" do end end end + + it "is set for user errors caused by internal errors" do + -> { + begin + 1 / 0 + rescue + raise "foo" + end + }.should raise_error(RuntimeError) { |e| + e.cause.should be_kind_of(ZeroDivisionError) + } + end + + it "is set for internal errors caused by user errors" do + cause = RuntimeError.new "cause" + -> { + begin + raise cause + rescue + 1 / 0 + end + }.should raise_error(ZeroDivisionError) { |e| + e.cause.should equal(cause) + } + end end diff --git a/spec/ruby/core/exception/dup_spec.rb b/spec/ruby/core/exception/dup_spec.rb index 3ad1fc063b..c7fcd280dd 100644 --- a/spec/ruby/core/exception/dup_spec.rb +++ b/spec/ruby/core/exception/dup_spec.rb @@ -58,4 +58,17 @@ describe "Exception#dup" do @obj.dup.backtrace.should == @obj.backtrace end + + it "does copy the cause" do + begin + raise StandardError, "the cause" + rescue StandardError => cause + begin + raise RuntimeError, "the consequence" + rescue RuntimeError => e + e.cause.should equal(cause) + e.dup.cause.should equal(cause) + end + end + end end diff --git a/spec/ruby/core/exception/full_message_spec.rb b/spec/ruby/core/exception/full_message_spec.rb index bc611c165b..3df2d47f61 100644 --- a/spec/ruby/core/exception/full_message_spec.rb +++ b/spec/ruby/core/exception/full_message_spec.rb @@ -33,6 +33,25 @@ ruby_version_is "2.5" do e.full_message(order: :top, highlight: false).should =~ /a.rb:1.*b.rb:2/m e.full_message(order: :bottom, highlight: false).should =~ /b.rb:2.*a.rb:1/m end + + it "shows the caller if the exception has no backtrace" do + e = RuntimeError.new("Some runtime error") + e.backtrace.should == nil + full_message = e.full_message(highlight: false, order: :top) + full_message.should include("#{__FILE__}:#{__LINE__-1}:in `") + full_message.should include("': Some runtime error (RuntimeError)\n") + end + + it "shows the exception class at the end of the first line of the message when the message contains multiple lines" do + begin + line = __LINE__; raise "first line\nsecond line" + rescue => e + full_message = e.full_message(highlight: false, order: :top).lines + full_message[0].should include("#{__FILE__}:#{line}:in `") + full_message[0].should include(": first line (RuntimeError)\n") + full_message[1].should == "second line\n" + end + end end ruby_version_is "2.6" do diff --git a/spec/ruby/core/file/split_spec.rb b/spec/ruby/core/file/split_spec.rb index 7a345e8891..2b22b0a08f 100644 --- a/spec/ruby/core/file/split_spec.rb +++ b/spec/ruby/core/file/split_spec.rb @@ -53,8 +53,9 @@ describe "File.split" do end it "coerces the argument with to_str if it is not a String type" do - class C; def to_str; "/rubinius/better/than/ruby"; end; end - File.split(C.new).should == ["/rubinius/better/than", "ruby"] + obj = mock("str") + obj.should_receive(:to_str).and_return("/one/two/three") + File.split(obj).should == ["/one/two", "three"] end it "accepts an object that has a #to_path method" do diff --git a/spec/ruby/core/integer/div_spec.rb b/spec/ruby/core/integer/div_spec.rb index 9329f5062e..ca69ff4681 100644 --- a/spec/ruby/core/integer/div_spec.rb +++ b/spec/ruby/core/integer/div_spec.rb @@ -81,6 +81,14 @@ describe "Integer#div" do (10**50).div(-(10**40 + 1)).should == -10000000000 end + it "handles fixnum_min / -1" do + (fixnum_min / -1).should == -fixnum_min + (fixnum_min / -1).should > 0 + + int_min = -2147483648 + (int_min / -1).should == 2147483648 + end + it "calls #coerce and #div if argument responds to #coerce" do x = mock("x") y = mock("y") diff --git a/spec/ruby/core/integer/uminus_spec.rb b/spec/ruby/core/integer/uminus_spec.rb index 56c5f7a085..b6b110dec4 100644 --- a/spec/ruby/core/integer/uminus_spec.rb +++ b/spec/ruby/core/integer/uminus_spec.rb @@ -11,8 +11,10 @@ describe "Integer#-@" do end it "negates self at Fixnum/Bignum boundaries" do - fixnum_max.send(:-@).should == (0 - fixnum_max) - fixnum_min.send(:-@).should == (0 - fixnum_min) + (-fixnum_max).should == (0 - fixnum_max) + (-fixnum_max).should < 0 + (-fixnum_min).should == (0 - fixnum_min) + (-fixnum_min).should > 0 end end diff --git a/spec/ruby/core/io/ioctl_spec.rb b/spec/ruby/core/io/ioctl_spec.rb index 726f0c9c51..0f2b67ac44 100644 --- a/spec/ruby/core/io/ioctl_spec.rb +++ b/spec/ruby/core/io/ioctl_spec.rb @@ -9,7 +9,7 @@ describe "IO#ioctl" do end platform_is :linux do - platform_is "86" do # x86 / x86_64 + guard -> { RUBY_PLATFORM.include?("86") } do # x86 / x86_64 it "resizes an empty String to match the output size" do File.open(__FILE__, 'r') do |f| buffer = '' diff --git a/spec/ruby/core/kernel/extend_spec.rb b/spec/ruby/core/kernel/extend_spec.rb index 2dbc224177..f4f9dde098 100644 --- a/spec/ruby/core/kernel/extend_spec.rb +++ b/spec/ruby/core/kernel/extend_spec.rb @@ -46,10 +46,10 @@ describe "Kernel#extend" do end it "makes the class a kind_of? the argument" do - class C + c = Class.new do extend KernelSpecs::M end - (C.kind_of? KernelSpecs::M).should == true + (c.kind_of? KernelSpecs::M).should == true end it "raises an ArgumentError when no arguments given" do diff --git a/spec/ruby/core/kernel/fail_spec.rb b/spec/ruby/core/kernel/fail_spec.rb index 01dffa4a69..a5948cefae 100644 --- a/spec/ruby/core/kernel/fail_spec.rb +++ b/spec/ruby/core/kernel/fail_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -describe "Kernel.fail" do +describe "Kernel#fail" do it "is a private method" do Kernel.should have_private_instance_method(:fail) end @@ -11,17 +11,16 @@ describe "Kernel.fail" do end it "accepts an Object with an exception method returning an Exception" do - class Boring - def self.exception(msg) - StandardError.new msg - end + obj = Object.new + def obj.exception(msg) + StandardError.new msg end - lambda { fail Boring, "..." }.should raise_error(StandardError) + lambda { fail obj, "..." }.should raise_error(StandardError, "...") end it "instantiates the specified exception class" do - class LittleBunnyFooFoo < RuntimeError; end - lambda { fail LittleBunnyFooFoo }.should raise_error(LittleBunnyFooFoo) + error_class = Class.new(RuntimeError) + lambda { fail error_class }.should raise_error(error_class) end it "uses the specified message" do @@ -38,6 +37,6 @@ describe "Kernel.fail" do end end -describe "Kernel#fail" do +describe "Kernel.fail" do it "needs to be reviewed for spec completeness" end diff --git a/spec/ruby/core/kernel/instance_variable_set_spec.rb b/spec/ruby/core/kernel/instance_variable_set_spec.rb index ce74b6fc29..6d84015f50 100644 --- a/spec/ruby/core/kernel/instance_variable_set_spec.rb +++ b/spec/ruby/core/kernel/instance_variable_set_spec.rb @@ -3,32 +3,32 @@ require_relative 'fixtures/classes' describe "Kernel#instance_variable_set" do it "sets the value of the specified instance variable" do - class Dog + dog = Class.new do def initialize(p1, p2) @a, @b = p1, p2 end end - Dog.new('cat', 99).instance_variable_set(:@a, 'dog').should == "dog" + dog.new('cat', 99).instance_variable_set(:@a, 'dog').should == "dog" end it "sets the value of the instance variable when no instance variables exist yet" do - class NoVariables; end - NoVariables.new.instance_variable_set(:@a, "new").should == "new" + no_variables = Class.new + no_variables.new.instance_variable_set(:@a, "new").should == "new" end it "raises a NameError exception if the argument is not of form '@x'" do - class NoDog; end - lambda { NoDog.new.instance_variable_set(:c, "cat") }.should raise_error(NameError) + no_dog = Class.new + lambda { no_dog.new.instance_variable_set(:c, "cat") }.should raise_error(NameError) end it "raises a NameError exception if the argument is an invalid instance variable name" do - class DigitDog; end - lambda { DigitDog.new.instance_variable_set(:"@0", "cat") }.should raise_error(NameError) + digit_dog = Class.new + lambda { digit_dog.new.instance_variable_set(:"@0", "cat") }.should raise_error(NameError) end it "raises a NameError when the argument is '@'" do - class DogAt; end - lambda { DogAt.new.instance_variable_set(:"@", "cat") }.should raise_error(NameError) + dog_at = Class.new + lambda { dog_at.new.instance_variable_set(:"@", "cat") }.should raise_error(NameError) end it "raises a TypeError if the instance variable name is a Fixnum" do diff --git a/spec/ruby/core/kernel/warn_spec.rb b/spec/ruby/core/kernel/warn_spec.rb index 7a0c431ff0..0b461ec25a 100644 --- a/spec/ruby/core/kernel/warn_spec.rb +++ b/spec/ruby/core/kernel/warn_spec.rb @@ -77,6 +77,15 @@ describe "Kernel#warn" do }.should output(nil, /\n/) end + it "writes to_s representation if passed a non-string" do + obj = mock("obj") + obj.should_receive(:to_s).and_return("to_s called") + lambda { + $VERBOSE = true + warn(obj) + }.should output(nil, "to_s called\n") + end + ruby_version_is "2.5" do describe ":uplevel keyword argument" do before :each do diff --git a/spec/ruby/core/marshal/dump_spec.rb b/spec/ruby/core/marshal/dump_spec.rb index c53840962c..b2120ab2e6 100644 --- a/spec/ruby/core/marshal/dump_spec.rb +++ b/spec/ruby/core/marshal/dump_spec.rb @@ -475,6 +475,24 @@ describe "Marshal.dump" do obj.set_backtrace(["foo/bar.rb:10"]) Marshal.dump(obj).should == "\x04\bo:\x0EException\a:\tmesg\"\bfoo:\abt[\x06\"\x12foo/bar.rb:10" end + + it "dumps the cause for the exception" do + exc = nil + begin + raise StandardError, "the cause" + rescue StandardError => cause + begin + raise RuntimeError, "the consequence" + rescue RuntimeError => e + e.cause.should equal(cause) + exc = e + end + end + + reloaded = Marshal.load(Marshal.dump(exc)) + reloaded.cause.should be_an_instance_of(StandardError) + reloaded.cause.message.should == "the cause" + end end it "dumps subsequent appearances of a symbol as a link" do diff --git a/spec/ruby/core/module/autoload_spec.rb b/spec/ruby/core/module/autoload_spec.rb index 05e9a61675..224d5fd57b 100644 --- a/spec/ruby/core/module/autoload_spec.rb +++ b/spec/ruby/core/module/autoload_spec.rb @@ -423,12 +423,12 @@ describe "Module#autoload" do ModuleSpecs::Autoload::U::V::X.should == :autoload_uvx end - it "loads the file that defines subclass XX::YY < YY and YY is a top level constant" do + it "loads the file that defines subclass XX::CS_CONST_AUTOLOAD < CS_CONST_AUTOLOAD and CS_CONST_AUTOLOAD is a top level constant" do module ModuleSpecs::Autoload::XX - autoload :YY, fixture(__FILE__, "autoload_subclass.rb") + autoload :CS_CONST_AUTOLOAD, fixture(__FILE__, "autoload_subclass.rb") end - ModuleSpecs::Autoload::XX::YY.superclass.should == YY + ModuleSpecs::Autoload::XX::CS_CONST_AUTOLOAD.superclass.should == CS_CONST_AUTOLOAD end describe "after autoloading searches for the constant like the original lookup" do diff --git a/spec/ruby/core/module/fixtures/autoload_subclass.rb b/spec/ruby/core/module/fixtures/autoload_subclass.rb index 569972118c..8027fa3fcd 100644 --- a/spec/ruby/core/module/fixtures/autoload_subclass.rb +++ b/spec/ruby/core/module/fixtures/autoload_subclass.rb @@ -1,10 +1,10 @@ -class YY +class CS_CONST_AUTOLOAD end module ModuleSpecs module Autoload module XX - class YY < YY + class CS_CONST_AUTOLOAD < CS_CONST_AUTOLOAD end end end diff --git a/spec/ruby/core/module/remove_method_spec.rb b/spec/ruby/core/module/remove_method_spec.rb index f83928f715..70048a83fb 100644 --- a/spec/ruby/core/module/remove_method_spec.rb +++ b/spec/ruby/core/module/remove_method_spec.rb @@ -77,19 +77,19 @@ describe "Module#remove_method" do end it "raises a NameError when attempting to remove method further up the inheritance tree" do - lambda { - class Third < ModuleSpecs::Second + Class.new(ModuleSpecs::Second) do + -> { remove_method :method_to_remove - end - }.should raise_error(NameError) + }.should raise_error(NameError) + end end it "raises a NameError when attempting to remove a missing method" do - lambda { - class Third < ModuleSpecs::Second + Class.new(ModuleSpecs::Second) do + -> { remove_method :blah - end - }.should raise_error(NameError) + }.should raise_error(NameError) + end end describe "on frozen instance" do diff --git a/spec/ruby/core/numeric/shared/step.rb b/spec/ruby/core/numeric/shared/step.rb index 0fb2336bf5..066f499dc5 100644 --- a/spec/ruby/core/numeric/shared/step.rb +++ b/spec/ruby/core/numeric/shared/step.rb @@ -16,6 +16,14 @@ describe :numeric_step, :shared => true do ScratchPad.recorded.should eql [1, 2, 3, 4, 5] end + it "defaults to an infinite limit with a step size of 1 for Integers" do + 1.step.first(5).should == [1, 2, 3, 4, 5] + end + + it "defaults to an infinite limit with a step size of 1.0 for Floats" do + 1.0.step.first(5).should == [1.0, 2.0, 3.0, 4.0, 5.0] + end + describe "when self, stop and step are Fixnums" do it "yields only Fixnums" do 1.send(@method, *@step_args.call(5, 1)) { |x| x.should be_an_instance_of(Fixnum) } diff --git a/spec/ruby/core/numeric/step_spec.rb b/spec/ruby/core/numeric/step_spec.rb index a0a4c7c9f8..12369a47a2 100644 --- a/spec/ruby/core/numeric/step_spec.rb +++ b/spec/ruby/core/numeric/step_spec.rb @@ -60,6 +60,11 @@ describe "Numeric#step" do enum.size.should == Float::INFINITY end end + + it "defaults to an infinite size" do + enum = 1.step + enum.size.should == Float::INFINITY + end end describe "type" do diff --git a/spec/ruby/core/range/step_spec.rb b/spec/ruby/core/range/step_spec.rb index 818207974a..eb136a8902 100644 --- a/spec/ruby/core/range/step_spec.rb +++ b/spec/ruby/core/range/step_spec.rb @@ -102,6 +102,15 @@ describe "Range#step" do ScratchPad.recorded.should eql([1.0, 2.8, 4.6, 6.4, 1.0, 2.3, 3.6, 4.9, 6.2, 7.5, 8.8, 10.1, 11.4, 12.7]) end + + it "handles infinite values at either end" do + (-Float::INFINITY..0.0).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 } + ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY]) + + ScratchPad.record [] + (0.0..Float::INFINITY).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 } + ScratchPad.recorded.should eql([0.0, 2.0, 4.0]) + end end describe "and Integer, Float values" do @@ -203,6 +212,15 @@ describe "Range#step" do (1.0...55.6).step(18.2) { |x| ScratchPad << x } ScratchPad.recorded.should eql([1.0, 2.8, 4.6, 1.0, 19.2, 37.4]) end + + it "handles infinite values at either end" do + (-Float::INFINITY...0.0).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 } + ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY]) + + ScratchPad.record [] + (0.0...Float::INFINITY).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 } + ScratchPad.recorded.should eql([0.0, 2.0, 4.0]) + end end describe "and Integer, Float values" do diff --git a/spec/ruby/core/string/shared/codepoints.rb b/spec/ruby/core/string/shared/codepoints.rb index 589d2ee1d0..84a0e3ae39 100644 --- a/spec/ruby/core/string/shared/codepoints.rb +++ b/spec/ruby/core/string/shared/codepoints.rb @@ -1,5 +1,11 @@ # -*- encoding: binary -*- describe :string_codepoints, shared: true do + it "returns self" do + s = "foo" + result = s.send(@method) {} + result.should equal s + end + it "raises an ArgumentError when self has an invalid encoding and a method is called on the returned Enumerator" do s = "\xDF".force_encoding(Encoding::UTF_8) s.valid_encoding?.should be_false @@ -20,13 +26,13 @@ describe :string_codepoints, shared: true do lambda { s.send(@method) { } }.should raise_error(ArgumentError) end - it "returns codepoints as Fixnums" do + it "yields codepoints as Fixnums" do "glark\u{20}".send(@method).to_a.each do |codepoint| codepoint.should be_an_instance_of(Fixnum) end end - it "returns one codepoint for each character" do + it "yields one codepoint for each character" do s = "\u{9876}\u{28}\u{1987}" s.send(@method).to_a.size.should == s.chars.to_a.size end @@ -37,7 +43,7 @@ describe :string_codepoints, shared: true do s.send(@method).to_a.should == [38937] end - it "returns the codepoint corresponding to the character's position in the String's encoding" do + it "yields the codepoints corresponding to the character's position in the String's encoding" do "\u{787}".send(@method).to_a.should == [1927] end diff --git a/spec/ruby/core/string/split_spec.rb b/spec/ruby/core/string/split_spec.rb index b451921c66..0c82ef8c58 100644 --- a/spec/ruby/core/string/split_spec.rb +++ b/spec/ruby/core/string/split_spec.rb @@ -413,15 +413,17 @@ describe "String#split with Regexp" do end describe "for a String subclass" do - a = [] - StringSpecs::MyString.new("a|b").split("|") { |str| a << str } - first, last = a + it "yields instances of the same subclass" do + a = [] + StringSpecs::MyString.new("a|b").split("|") { |str| a << str } + first, last = a - first.should be_an_instance_of(StringSpecs::MyString) - first.should == "a" + first.should be_an_instance_of(StringSpecs::MyString) + first.should == "a" - last.should be_an_instance_of(StringSpecs::MyString) - last.should == "b" + last.should be_an_instance_of(StringSpecs::MyString) + last.should == "b" + end end end end diff --git a/spec/ruby/core/struct/inspect_spec.rb b/spec/ruby/core/struct/inspect_spec.rb index 3df97c8604..83e13597ba 100644 --- a/spec/ruby/core/struct/inspect_spec.rb +++ b/spec/ruby/core/struct/inspect_spec.rb @@ -3,12 +3,9 @@ require_relative 'fixtures/classes' require_relative 'shared/inspect' describe "Struct#inspect" do - it "returns a string representation of some kind" do + it "returns a string representation showing members and values" do car = StructClasses::Car.new('Ford', 'Ranger') car.inspect.should == '#<struct StructClasses::Car make="Ford", model="Ranger", year=nil>' - - Whiskey = Struct.new(:name, :ounces) - Whiskey.new('Jack', 100).inspect.should == '#<struct Whiskey name="Jack", ounces=100>' end it_behaves_like :struct_inspect, :inspect diff --git a/spec/ruby/core/thread/new_spec.rb b/spec/ruby/core/thread/new_spec.rb index c1e0e2a5ad..80929035c7 100644 --- a/spec/ruby/core/thread/new_spec.rb +++ b/spec/ruby/core/thread/new_spec.rb @@ -53,4 +53,31 @@ describe "Thread.new" do ScratchPad.recorded.should == [:good, :in_thread] end + it "releases Mutexes held by the Thread when the Thread finishes" do + m1 = Mutex.new + m2 = Mutex.new + t = Thread.new { + m1.lock + m1.locked?.should == true + m2.lock + m2.locked?.should == true + } + t.join + m1.locked?.should == false + m2.locked?.should == false + end + + it "releases Mutexes held by the Thread when the Thread finishes, also with Mutex#synchronize" do + m = Mutex.new + t = Thread.new { + m.synchronize { + m.unlock + m.lock + } + m.lock + m.locked?.should == true + } + t.join + m.locked?.should == false + end end diff --git a/spec/ruby/core/time/shared/gmt_offset.rb b/spec/ruby/core/time/shared/gmt_offset.rb index cb842be2f3..839566c249 100644 --- a/spec/ruby/core/time/shared/gmt_offset.rb +++ b/spec/ruby/core/time/shared/gmt_offset.rb @@ -5,6 +5,12 @@ describe :time_gmt_offset, shared: true do end end + it "returns 0 when the date is UTC" do + with_timezone("AST", 3) do + Time.new.utc.send(@method).should == 0 + end + end + platform_is_not :windows do it "returns the correct offset for US Eastern time zone around daylight savings time change" do # "2010-03-14 01:59:59 -0500" + 1 ==> "2010-03-14 03:00:00 -0400" diff --git a/spec/ruby/core/tracepoint/disable_spec.rb b/spec/ruby/core/tracepoint/disable_spec.rb index e936d3498d..25d54502ab 100644 --- a/spec/ruby/core/tracepoint/disable_spec.rb +++ b/spec/ruby/core/tracepoint/disable_spec.rb @@ -1,34 +1,37 @@ require_relative '../../spec_helper' describe 'TracePoint#disable' do - def test; end it 'returns true if trace was enabled' do called = false - trace = TracePoint.new(:call) do |tp| + trace = TracePoint.new(:line) do |tp| called = true end trace.enable - trace.disable.should be_true + begin + line_event = true + ensure + ret = trace.disable + ret.should == true + end + called.should == true # Check the TracePoint is disabled called = false - test + line_event = true called.should == false end it 'returns false if trace was disabled' do - event_name, method_name = nil - trace = TracePoint.new(:call) do |tp| - event_name = tp.event - method_name = tp.method_id + called = false + trace = TracePoint.new(:line) do |tp| + called = true end - trace.disable.should be_false - event_name, method_name = nil - test - method_name.equal?(:test).should be_false - event_name.should equal(nil) + line_event = true + trace.disable.should == false + line_event = true + called.should == false end it 'is disabled within a block & is enabled outside the block' do @@ -37,19 +40,19 @@ describe 'TracePoint#disable' do trace.enable begin trace.disable { enabled = trace.enabled? } - enabled.should be_false - trace.enabled?.should be_true + enabled.should == false + trace.enabled?.should == true ensure trace.disable end end - it 'is disabled within a block & also returns false when its called with a block' do + it 'returns the return value of the block' do trace = TracePoint.new(:line) {} trace.enable begin - trace.disable { trace.enabled? }.should == false - trace.enabled?.should equal(true) + trace.disable { 42 }.should == 42 + trace.enabled?.should == true ensure trace.disable end @@ -57,14 +60,13 @@ describe 'TracePoint#disable' do ruby_bug "#14057", ""..."2.5" do it 'can accept param within a block but it should not yield arguments' do - event_name = nil trace = TracePoint.new(:line) {} trace.enable begin trace.disable do |*args| args.should == [] end - trace.enabled?.should be_true + trace.enabled?.should == true ensure trace.disable end diff --git a/spec/ruby/core/tracepoint/enable_spec.rb b/spec/ruby/core/tracepoint/enable_spec.rb index 3b43d3d6b4..720adfcd4b 100644 --- a/spec/ruby/core/tracepoint/enable_spec.rb +++ b/spec/ruby/core/tracepoint/enable_spec.rb @@ -1,53 +1,49 @@ require_relative '../../spec_helper' describe 'TracePoint#enable' do - def test; end + # def test; end describe 'without a block' do it 'returns true if trace was enabled' do - event_name = nil - trace = TracePoint.new(:call) do |tp| - event_name = tp.event + called = false + trace = TracePoint.new(:line) do |tp| + called = true end - test - event_name.should == nil + line_event = true + called.should == false trace.enable begin - test - event_name.should equal(:call) + line_event = true + called.should == true ensure trace.disable end end it 'returns false if trace was disabled' do - event_name, method_name = nil, nil - trace = TracePoint.new(:call) do |tp| - event_name = tp.event - method_name = tp.method_id + called = false + trace = TracePoint.new(:line) do |tp| + called = true end - trace.enable.should be_false + trace.enable.should == false begin - event_name.should equal(:call) - test - method_name.equal?(:test).should be_true + line_event = true + called.should == true ensure trace.disable end - event_name, method_name = nil - test - method_name.equal?(:test).should be_false - event_name.should equal(nil) + called = false + line_event = true + called.should == false - trace.enable.should be_false + trace.enable.should == false begin - event_name.should equal(:call) - test - method_name.equal?(:test).should be_true + line_event = true + called.should == true ensure trace.disable end @@ -70,7 +66,7 @@ describe 'TracePoint#enable' do event_name.should equal(:line) args.should == [] end - trace.enabled?.should be_false + trace.enabled?.should == false end end @@ -86,17 +82,19 @@ describe 'TracePoint#enable' do end end - it 'returns value returned by the block' do + it 'returns the return value of the block' do trace = TracePoint.new(:line) {} - trace.enable { true; 'test' }.should == 'test' + trace.enable { 42 }.should == 42 end it 'disables the trace object outside the block' do - event_name = nil - trace = TracePoint.new(:line) { |tp|event_name = tp.event } - trace.enable { '2 + 2' } - event_name.should equal(:line) - trace.enabled?.should be_false + called = false + trace = TracePoint.new(:line) { called = true } + trace.enable { + line_event = true + } + called.should == true + trace.enabled?.should == false end end diff --git a/spec/ruby/core/tracepoint/enabled_spec.rb b/spec/ruby/core/tracepoint/enabled_spec.rb index 1c5d3d4bdd..0167d32fb0 100644 --- a/spec/ruby/core/tracepoint/enabled_spec.rb +++ b/spec/ruby/core/tracepoint/enabled_spec.rb @@ -2,13 +2,13 @@ require_relative '../../spec_helper' describe 'TracePoint#enabled?' do it 'returns true when current status of the trace is enable' do - trace = TracePoint.new(:call) {} + trace = TracePoint.new(:line) {} trace.enable do - trace.enabled?.should be_true + trace.enabled?.should == true end end it 'returns false when current status of the trace is disabled' do - TracePoint.new(:call) {}.enabled?.should be_false + TracePoint.new(:line) {}.enabled?.should == false end end diff --git a/spec/ruby/core/tracepoint/inspect_spec.rb b/spec/ruby/core/tracepoint/inspect_spec.rb index 19e345c7cf..9ff1653ae8 100644 --- a/spec/ruby/core/tracepoint/inspect_spec.rb +++ b/spec/ruby/core/tracepoint/inspect_spec.rb @@ -1,8 +1,28 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe 'TracePoint#inspect' do it 'returns a string containing a human-readable TracePoint status' do - TracePoint.new(:call) {}.inspect.should == + TracePoint.new(:line) {}.inspect.should == '#<TracePoint:disabled>' end + + it 'returns a String showing the event, path and line' do + inspect = nil + line = __LINE__ + TracePoint.new(:line) { |tp| inspect = tp.inspect }.enable do + inspect.should == "#<TracePoint:line@#{__FILE__}:#{line+2}>" + end + end + + it 'returns a String showing the event, path and line for a :class event' do + inspect = nil + line = __LINE__ + TracePoint.new(:class) { |tp| inspect = tp.inspect }.enable do + class TracePointSpec::C + end + end + + inspect.should == "#<TracePoint:class@#{__FILE__}:#{line+2}>" + end end diff --git a/spec/ruby/core/tracepoint/new_spec.rb b/spec/ruby/core/tracepoint/new_spec.rb index 77675561f6..d333fd069a 100644 --- a/spec/ruby/core/tracepoint/new_spec.rb +++ b/spec/ruby/core/tracepoint/new_spec.rb @@ -3,7 +3,7 @@ require_relative 'fixtures/classes' describe 'TracePoint.new' do it 'returns a new TracePoint object, not enabled by default' do - TracePoint.new(:call) {}.enabled?.should be_false + TracePoint.new(:line) {}.enabled?.should be_false end it 'includes :line event when event is not specified' do @@ -23,12 +23,11 @@ describe 'TracePoint.new' do it 'converts given event name as string into symbol using to_sym' do event_name = nil - (o = mock('return')).should_receive(:to_sym).and_return(:return) + (o = mock('line')).should_receive(:to_sym).and_return(:line) - TracePoint.new(o) { |tp| event_name = tp.event}.enable do - event_name.should equal(nil) - TracePointSpec.test - event_name.should equal(:return) + TracePoint.new(o) { |tp| event_name = tp.event }.enable do + line_event = true + event_name.should == :line end end @@ -58,11 +57,11 @@ describe 'TracePoint.new' do ruby_bug "#140740", ""..."2.5" do it 'expects to be called with a block' do - -> { TracePoint.new(:line) }.should raise_error(ArgumentError) + -> { TracePoint.new(:line) }.should raise_error(ArgumentError, "must be called with a block") end end - it "raises a Argument error when the give argument doesn't match an event name" do - -> { TracePoint.new(:test) }.should raise_error(ArgumentError) + it "raises a Argument error when the given argument doesn't match an event name" do + -> { TracePoint.new(:test) }.should raise_error(ArgumentError, "unknown event: test") end end diff --git a/spec/ruby/core/tracepoint/path_spec.rb b/spec/ruby/core/tracepoint/path_spec.rb index 99751b0025..1e31c1bb68 100644 --- a/spec/ruby/core/tracepoint/path_spec.rb +++ b/spec/ruby/core/tracepoint/path_spec.rb @@ -11,7 +11,7 @@ describe 'TracePoint#path' do it 'equals (eval) inside an eval for :end event' do path = nil TracePoint.new(:end) { |tp| path = tp.path }.enable do - eval("class A; end") + eval("module TracePointSpec; end") path.should == '(eval)' end end diff --git a/spec/ruby/core/tracepoint/self_spec.rb b/spec/ruby/core/tracepoint/self_spec.rb index c76464f8d1..8bfd09301e 100644 --- a/spec/ruby/core/tracepoint/self_spec.rb +++ b/spec/ruby/core/tracepoint/self_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe 'TracePoint#self' do it 'return the trace object from event' do @@ -7,4 +8,13 @@ describe 'TracePoint#self' do trace.equal?(self).should be_true end end + + it 'return the class object from a class event' do + trace = nil + TracePoint.new(:class) { |tp| trace = tp.self }.enable do + class TracePointSpec::C + end + end + trace.should equal TracePointSpec::C + end end diff --git a/spec/ruby/core/tracepoint/trace_spec.rb b/spec/ruby/core/tracepoint/trace_spec.rb index e5798df9fb..ea6c85bcc5 100644 --- a/spec/ruby/core/tracepoint/trace_spec.rb +++ b/spec/ruby/core/tracepoint/trace_spec.rb @@ -2,8 +2,8 @@ require_relative '../../spec_helper' describe 'TracePoint.trace' do it 'activates the trace automatically' do - trace = TracePoint.trace(:call) {} - trace.enabled?.should be_true + trace = TracePoint.trace(:line) {} + trace.enabled?.should == true trace.disable end end |