diff options
Diffstat (limited to 'spec/ruby/core')
26 files changed, 308 insertions, 25 deletions
diff --git a/spec/ruby/core/array/pack/x_spec.rb b/spec/ruby/core/array/pack/x_spec.rb index a28dd0bf21..86c3ad1aa4 100644 --- a/spec/ruby/core/array/pack/x_spec.rb +++ b/spec/ruby/core/array/pack/x_spec.rb @@ -30,6 +30,7 @@ describe "Array#pack with format 'x'" do it "does not add a NULL byte when passed the '*' modifier" do [].pack("x*").should == "" + [1, 2].pack("Cx*C").should == "\x01\x02" end end diff --git a/spec/ruby/core/array/sample_spec.rb b/spec/ruby/core/array/sample_spec.rb index 565d7ff7f9..755b46f126 100644 --- a/spec/ruby/core/array/sample_spec.rb +++ b/spec/ruby/core/array/sample_spec.rb @@ -19,10 +19,18 @@ describe "Array#sample" do [].sample.should be_nil end + it "returns nil for an empty array when called without n and a Random is given" do + [].sample(random: Random.new(42)).should be_nil + end + it "returns a single value when not passed a count" do [4].sample.should equal(4) end + it "returns a single value when not passed a count and a Random is given" do + [4].sample(random: Random.new(42)).should equal(4) + end + it "returns an empty Array when passed zero" do [4].sample(0).should == [] end diff --git a/spec/ruby/core/dir/foreach_spec.rb b/spec/ruby/core/dir/foreach_spec.rb index c3ddb27a84..85e8c61fd6 100644 --- a/spec/ruby/core/dir/foreach_spec.rb +++ b/spec/ruby/core/dir/foreach_spec.rb @@ -42,6 +42,13 @@ describe "Dir.foreach" do it "accepts an encoding keyword for the encoding of the entries" do dirs = Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").to_a.sort dirs.each {|dir| dir.encoding.should == Encoding::UTF_8} + + dirs = Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::UTF_16LE).to_a.sort + dirs.each {|dir| dir.encoding.should == Encoding::UTF_16LE} + + Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::UTF_16LE) do |f| + f.encoding.should == Encoding::UTF_16LE + end end ruby_version_is ""..."2.7" do diff --git a/spec/ruby/core/enumerable/shared/inject.rb b/spec/ruby/core/enumerable/shared/inject.rb index 12e0665dda..e59b82b02e 100644 --- a/spec/ruby/core/enumerable/shared/inject.rb +++ b/spec/ruby/core/enumerable/shared/inject.rb @@ -66,4 +66,11 @@ describe :enumerable_inject, shared: true do it "returns nil when fails(legacy rubycon)" do EnumerableSpecs::EachDefiner.new().send(@method) {|acc,x| 999 }.should == nil end + + ruby_bug '#18635', ''...'3.2' do + it "raises an ArgumentError when no parameters or block is given" do + -> { [1,2].send(@method) }.should raise_error(ArgumentError) + -> { {one: 1, two: 2}.send(@method) }.should raise_error(ArgumentError) + end + end end diff --git a/spec/ruby/core/enumerator/lazy/with_index_spec.rb b/spec/ruby/core/enumerator/lazy/with_index_spec.rb index 3a59ba4116..b6dbe554d9 100644 --- a/spec/ruby/core/enumerator/lazy/with_index_spec.rb +++ b/spec/ruby/core/enumerator/lazy/with_index_spec.rb @@ -26,5 +26,13 @@ ruby_version_is "2.7" do (0..Float::INFINITY).lazy.with_index { |i, idx| result << [i * 2, idx] }.first(3) result.should == [[0,0],[2,1],[4,2]] end + + it "resets after a new call to each" do + enum = (0..2).lazy.with_index.map { |i, idx| [i, idx] } + result = [] + enum.each { |i, idx| result << [i, idx] } + enum.each { |i, idx| result << [i, idx] } + result.should == [[0,0], [1,1], [2,2], [0,0], [1,1], [2,2]] + end end end diff --git a/spec/ruby/core/exception/full_message_spec.rb b/spec/ruby/core/exception/full_message_spec.rb index 4cece9ebf9..9757a2f407 100644 --- a/spec/ruby/core/exception/full_message_spec.rb +++ b/spec/ruby/core/exception/full_message_spec.rb @@ -15,13 +15,19 @@ describe "Exception#full_message" do it "supports :highlight option and adds escape sequences to highlight some strings" do e = RuntimeError.new("Some runtime error") - full_message = e.full_message(highlight: true, order: :bottom) - full_message.should include "\e[1mTraceback\e[m (most recent call last)" - full_message.should include "\e[1mSome runtime error (\e[1;4mRuntimeError\e[m\e[1m)" + full_message = e.full_message(highlight: true, order: :top).lines + full_message[0].should.end_with? "\e[1mSome runtime error (\e[1;4mRuntimeError\e[m\e[1m)\e[m\n" - full_message = e.full_message(highlight: false, order: :bottom) - full_message.should include "Traceback (most recent call last)" - full_message.should include "Some runtime error (RuntimeError)" + full_message = e.full_message(highlight: true, order: :bottom).lines + full_message[0].should == "\e[1mTraceback\e[m (most recent call last):\n" + full_message[-1].should.end_with? "\e[1mSome runtime error (\e[1;4mRuntimeError\e[m\e[1m)\e[m\n" + + full_message = e.full_message(highlight: false, order: :top).lines + full_message[0].should.end_with? "Some runtime error (RuntimeError)\n" + + full_message = e.full_message(highlight: false, order: :bottom).lines + full_message[0].should == "Traceback (most recent call last):\n" + full_message[-1].should.end_with? "Some runtime error (RuntimeError)\n" end it "supports :order option and places the error message and the backtrace at the top or the bottom" do @@ -35,9 +41,9 @@ describe "Exception#full_message" do 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") + full_message = e.full_message(highlight: false, order: :top).lines + full_message[0].should.start_with?("#{__FILE__}:#{__LINE__-1}:in `") + full_message[0].should.end_with?("': 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 @@ -45,12 +51,24 @@ describe "Exception#full_message" do 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[0].should.start_with?("#{__FILE__}:#{line}:in `") + full_message[0].should.end_with?(": first line (RuntimeError)\n") full_message[1].should == "second line\n" end end + it "highlights the entire message when the message contains multiple lines" do + begin + line = __LINE__; raise "first line\nsecond line\nthird line" + rescue => e + full_message = e.full_message(highlight: true, order: :top).lines + full_message[0].should.start_with?("#{__FILE__}:#{line}:in `") + full_message[0].should.end_with?(": \e[1mfirst line (\e[1;4mRuntimeError\e[m\e[1m)\e[m\n") + full_message[1].should == "\e[1msecond line\e[m\n" + full_message[2].should == "\e[1mthird line\e[m\n" + end + end + it "contains cause of exception" do begin begin diff --git a/spec/ruby/core/exception/system_exit_spec.rb b/spec/ruby/core/exception/system_exit_spec.rb index eef9faf271..d899844c4e 100644 --- a/spec/ruby/core/exception/system_exit_spec.rb +++ b/spec/ruby/core/exception/system_exit_spec.rb @@ -1,6 +1,48 @@ require_relative '../../spec_helper' describe "SystemExit" do + describe "#initialize" do + it "accepts a status and message" do + exc = SystemExit.new(42, "message") + exc.status.should == 42 + exc.message.should == "message" + + exc = SystemExit.new(true, "message") + exc.status.should == 0 + exc.message.should == "message" + + exc = SystemExit.new(false, "message") + exc.status.should == 1 + exc.message.should == "message" + end + + it "accepts a status only" do + exc = SystemExit.new(42) + exc.status.should == 42 + exc.message.should == "SystemExit" + + exc = SystemExit.new(true) + exc.status.should == 0 + exc.message.should == "SystemExit" + + exc = SystemExit.new(false) + exc.status.should == 1 + exc.message.should == "SystemExit" + end + + it "accepts a message only" do + exc = SystemExit.new("message") + exc.status.should == 0 + exc.message.should == "message" + end + + it "accepts no arguments" do + exc = SystemExit.new + exc.status.should == 0 + exc.message.should == "SystemExit" + end + end + it "sets the exit status and exits silently when raised" do code = 'raise SystemExit.new(7)' result = ruby_exe(code, args: "2>&1", exit_status: 7) diff --git a/spec/ruby/core/file/utime_spec.rb b/spec/ruby/core/file/utime_spec.rb index 59eef20c66..a191e29240 100644 --- a/spec/ruby/core/file/utime_spec.rb +++ b/spec/ruby/core/file/utime_spec.rb @@ -77,6 +77,10 @@ describe "File.utime" do File.mtime(@file1).nsec.should.between?(0, 123500000) end + it "returns the number of filenames in the arguments" do + File.utime(@atime.to_f, @mtime.to_f, @file1, @file2).should == 2 + end + platform_is :linux do platform_is wordsize: 64 do it "allows Time instances in the far future to set mtime and atime (but some filesystems limit it up to 2446-05-10 or 2038-01-19)" do diff --git a/spec/ruby/core/hash/ruby2_keywords_hash_spec.rb b/spec/ruby/core/hash/ruby2_keywords_hash_spec.rb index 005886a482..13e8ffe4af 100644 --- a/spec/ruby/core/hash/ruby2_keywords_hash_spec.rb +++ b/spec/ruby/core/hash/ruby2_keywords_hash_spec.rb @@ -40,6 +40,20 @@ ruby_version_is "2.7" do kw.should == h end + it "copies instance variables" do + h = {a: 1} + h.instance_variable_set(:@foo, 42) + kw = Hash.ruby2_keywords_hash(h) + kw.instance_variable_get(:@foo).should == 42 + end + + it "copies the hash internals" do + h = {a: 1} + kw = Hash.ruby2_keywords_hash(h) + h[:a] = 2 + kw[:a].should == 1 + end + it "raises TypeError for non-Hash" do -> { Hash.ruby2_keywords_hash(nil) }.should raise_error(TypeError) end diff --git a/spec/ruby/core/hash/shared/to_s.rb b/spec/ruby/core/hash/shared/to_s.rb index b0e3705d01..c74537aed4 100644 --- a/spec/ruby/core/hash/shared/to_s.rb +++ b/spec/ruby/core/hash/shared/to_s.rb @@ -2,7 +2,6 @@ require_relative '../../../spec_helper' require_relative '../fixtures/classes' describe :hash_to_s, shared: true do - it "returns a string representation with same order as each()" do h = { a: [1, 2], b: -2, d: -6, nil => nil } @@ -95,4 +94,8 @@ describe :hash_to_s, shared: true do {a: utf_16be}.send(@method).should == '{:a=>"utf_16be \u3042"}' end + + it "works for keys and values whose #inspect return a frozen String" do + { true => false }.to_s.should == "{true=>false}" + end end diff --git a/spec/ruby/core/io/shared/readlines.rb b/spec/ruby/core/io/shared/readlines.rb index 8e4a73bb98..52b20364ef 100644 --- a/spec/ruby/core/io/shared/readlines.rb +++ b/spec/ruby/core/io/shared/readlines.rb @@ -73,6 +73,11 @@ describe :io_readlines_options_19, shared: true do result = IO.send(@method, @name, 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_limit end + + it "ignores the object as a limit if it is negative" do + result = IO.send(@method, @name, -2, &@object) + (result ? result : ScratchPad.recorded).should == IOSpecs.lines + end end describe "when the object is a String" do diff --git a/spec/ruby/core/kernel/define_singleton_method_spec.rb b/spec/ruby/core/kernel/define_singleton_method_spec.rb index dc77c3e6f8..2d8b1bf413 100644 --- a/spec/ruby/core/kernel/define_singleton_method_spec.rb +++ b/spec/ruby/core/kernel/define_singleton_method_spec.rb @@ -96,4 +96,19 @@ describe "Kernel#define_singleton_method" do o.define(:foo) { raise "not used" } }.should raise_error(ArgumentError) end + + it "always defines the method with public visibility" do + cls = Class.new + def cls.define(name, &block) + private + define_singleton_method(name, &block) + end + + -> { + suppress_warning do + cls.define(:foo) { :ok } + end + cls.foo.should == :ok + }.should_not raise_error(NoMethodError) + end end diff --git a/spec/ruby/core/math/sqrt_spec.rb b/spec/ruby/core/math/sqrt_spec.rb index 779aea2a0a..918e7c3a17 100644 --- a/spec/ruby/core/math/sqrt_spec.rb +++ b/spec/ruby/core/math/sqrt_spec.rb @@ -27,6 +27,10 @@ describe "Math.sqrt" do it "accepts any argument that can be coerced with Float()" do Math.sqrt(MathSpecs::Float.new).should be_close(1.0, TOLERANCE) end + + it "raises a Math::DomainError when given a negative number" do + -> { Math.sqrt(-1) }.should raise_error(Math::DomainError) + end end describe "Math#sqrt" do diff --git a/spec/ruby/core/module/const_get_spec.rb b/spec/ruby/core/module/const_get_spec.rb index 9f9fafe5bb..69f181cf51 100644 --- a/spec/ruby/core/module/const_get_spec.rb +++ b/spec/ruby/core/module/const_get_spec.rb @@ -100,6 +100,16 @@ describe "Module#const_get" do ConstantSpecs.const_get("::CS_CONST1").should == :const1 end + it "accepts a toplevel scope qualifier when inherit is false" do + ConstantSpecs.const_get("::CS_CONST1", false).should == :const1 + -> { ConstantSpecs.const_get("CS_CONST1", false) }.should raise_error(NameError) + end + + it "returns a constant whose module is defined the the toplevel" do + ConstantSpecs.const_get("ConstantSpecsTwo::Foo").should == :cs_two_foo + ConstantSpecsThree.const_get("ConstantSpecsTwo::Foo").should == :cs_three_foo + end + it "accepts a scoped constant name" do ConstantSpecs.const_get("ClassA::CS_CONST10").should == :const10_10 end @@ -140,6 +150,10 @@ describe "Module#const_get" do Object.const_get('CSAutoloadD::InnerModule').name.should == 'CSAutoloadD::InnerModule' end + it "raises a NameError when the nested constant does not exist on the module but exists in Object" do + -> { Object.const_get('ConstantSpecs::CS_CONST1') }.should raise_error(NameError) + end + describe "with statically assigned constants" do it "searches the immediate class or module first" do ConstantSpecs::ClassA.const_get(:CS_CONST10).should == :const10_10 diff --git a/spec/ruby/core/module/ruby2_keywords_spec.rb b/spec/ruby/core/module/ruby2_keywords_spec.rb index 996774891c..6de3fdec80 100644 --- a/spec/ruby/core/module/ruby2_keywords_spec.rb +++ b/spec/ruby/core/module/ruby2_keywords_spec.rb @@ -15,6 +15,83 @@ ruby_version_is "2.7" do Hash.ruby2_keywords_hash?(last).should == true end + it "makes a copy of the hash and only marks the copy as keyword hash" do + obj = Object.new + obj.singleton_class.class_exec do + def regular(*args) + args.last + end + + ruby2_keywords def foo(*args) + args.last + end + end + + h = {a: 1} + ruby_version_is "3.0" do + obj.regular(**h).should.equal?(h) + end + + last = obj.foo(**h) + Hash.ruby2_keywords_hash?(last).should == true + Hash.ruby2_keywords_hash?(h).should == false + + last2 = obj.foo(**last) # last is already marked + Hash.ruby2_keywords_hash?(last2).should == true + Hash.ruby2_keywords_hash?(last).should == true + last2.should_not.equal?(last) + Hash.ruby2_keywords_hash?(h).should == false + end + + it "makes a copy and unmark at the call site when calling with marked *args" do + obj = Object.new + obj.singleton_class.class_exec do + ruby2_keywords def foo(*args) + args + end + + def single(arg) + arg + end + + def splat(*args) + args.last + end + + def kwargs(**kw) + kw + end + end + + h = { a: 1 } + args = obj.foo(**h) + marked = args.last + Hash.ruby2_keywords_hash?(marked).should == true + + after_usage = obj.single(*args) + after_usage.should == h + after_usage.should_not.equal?(h) + after_usage.should_not.equal?(marked) + Hash.ruby2_keywords_hash?(after_usage).should == false + Hash.ruby2_keywords_hash?(marked).should == true + + after_usage = obj.splat(*args) + after_usage.should == h + after_usage.should_not.equal?(h) + after_usage.should_not.equal?(marked) + ruby_bug "#18625", ""..."3.3" do # might be fixed in 3.2 + Hash.ruby2_keywords_hash?(after_usage).should == false + end + Hash.ruby2_keywords_hash?(marked).should == true + + after_usage = obj.kwargs(*args) + after_usage.should == h + after_usage.should_not.equal?(h) + after_usage.should_not.equal?(marked) + Hash.ruby2_keywords_hash?(after_usage).should == false + Hash.ruby2_keywords_hash?(marked).should == true + end + it "applies to the underlying method and applies across aliasing" do obj = Object.new @@ -80,7 +157,7 @@ ruby_version_is "2.7" do }.should raise_error(NameError, /undefined method `not_existing'/) end - it "acceps String as well" do + it "accepts String as well" do obj = Object.new obj.singleton_class.class_exec do diff --git a/spec/ruby/core/proc/parameters_spec.rb b/spec/ruby/core/proc/parameters_spec.rb index 3ced7b22ab..3a56b613cd 100644 --- a/spec/ruby/core/proc/parameters_spec.rb +++ b/spec/ruby/core/proc/parameters_spec.rb @@ -31,7 +31,7 @@ describe "Proc#parameters" do end it "regards named parameters in lambda as optional if lambda: false keyword used" do - lambda {|x| }.parameters(lambda: false).first.first.should == :opt + -> x { }.parameters(lambda: false).first.first.should == :opt end end diff --git a/spec/ruby/core/random/rand_spec.rb b/spec/ruby/core/random/rand_spec.rb index 6ea7eece5f..9244177ab5 100644 --- a/spec/ruby/core/random/rand_spec.rb +++ b/spec/ruby/core/random/rand_spec.rb @@ -205,6 +205,11 @@ describe "Random#rand with Range" do Random.new(42).rand(0..1.0).should be_kind_of(Float) end + it "returns a float within a given float range" do + Random.new(42).rand(0.0...100.0).should == 37.454011884736246 + Random.new(42).rand(-100.0...0.0).should == -62.545988115263754 + end + it "raises an ArgumentError when the startpoint lacks #+ and #- methods" do -> do Random.new.rand(Object.new..67) diff --git a/spec/ruby/core/string/append_spec.rb b/spec/ruby/core/string/append_spec.rb index 1e1667f617..e001257621 100644 --- a/spec/ruby/core/string/append_spec.rb +++ b/spec/ruby/core/string/append_spec.rb @@ -5,4 +5,9 @@ require_relative 'shared/concat' describe "String#<<" do it_behaves_like :string_concat, :<< it_behaves_like :string_concat_encoding, :<< + + it "raises an ArgumentError when given the incorrect number of arguments" do + -> { "hello".send(:<<) }.should raise_error(ArgumentError) + -> { "hello".send(:<<, "one", "two") }.should raise_error(ArgumentError) + end end diff --git a/spec/ruby/core/string/comparison_spec.rb b/spec/ruby/core/string/comparison_spec.rb index 01199274b6..91cfdca25a 100644 --- a/spec/ruby/core/string/comparison_spec.rb +++ b/spec/ruby/core/string/comparison_spec.rb @@ -75,6 +75,10 @@ describe "String#<=> with String" do (xff_1 <=> xff_2).should == -1 (xff_2 <=> xff_1).should == 1 end + + it "returns 0 when comparing 2 empty strings but one is not ASCII-compatible" do + ("" <=> "".force_encoding('iso-2022-jp')).should == 0 + end end # Note: This is inconsistent with Array#<=> which calls #to_ary instead of diff --git a/spec/ruby/core/string/lstrip_spec.rb b/spec/ruby/core/string/lstrip_spec.rb index 20e4cdeabd..8b5dd1b467 100644 --- a/spec/ruby/core/string/lstrip_spec.rb +++ b/spec/ruby/core/string/lstrip_spec.rb @@ -12,7 +12,7 @@ describe "String#lstrip" do "hello".lstrip.should == "hello" end - ruby_version_is '3.1' do + ruby_version_is '3.0' do it "strips leading \\0" do "\x00hello".lstrip.should == "hello" "\000 \000hello\000 \000".lstrip.should == "hello\000 \000" @@ -35,7 +35,7 @@ describe "String#lstrip!" do a.should == "hello " end - ruby_version_is '3.1' do + ruby_version_is '3.0' do it "strips leading \\0" do a = "\000 \000hello\000 \000" a.lstrip! diff --git a/spec/ruby/core/string/rindex_spec.rb b/spec/ruby/core/string/rindex_spec.rb index 7a6af0c9d0..a3b437a1e4 100644 --- a/spec/ruby/core/string/rindex_spec.rb +++ b/spec/ruby/core/string/rindex_spec.rb @@ -4,13 +4,17 @@ require_relative 'fixtures/classes' require_relative 'fixtures/utf-8-encoding' describe "String#rindex with object" do - it "raises a TypeError if obj isn't a String, Integer or Regexp" do + it "raises a TypeError if obj isn't a String or Regexp" do not_supported_on :opal do -> { "hello".rindex(:sym) }.should raise_error(TypeError) end -> { "hello".rindex(mock('x')) }.should raise_error(TypeError) end + it "raises a TypeError if obj is an Integer" do + -> { "hello".rindex(42) }.should raise_error(TypeError) + end + it "doesn't try to convert obj to an integer via to_int" do obj = mock('x') obj.should_not_receive(:to_int) diff --git a/spec/ruby/core/string/shared/eql.rb b/spec/ruby/core/string/shared/eql.rb index b57d6895ff..6f268c929c 100644 --- a/spec/ruby/core/string/shared/eql.rb +++ b/spec/ruby/core/string/shared/eql.rb @@ -31,4 +31,8 @@ describe :string_eql_value, shared: true do a.send(@method, b).should be_true b.send(@method, a).should be_true end + + it "returns true when comparing 2 empty strings but one is not ASCII-compatible" do + "".send(@method, "".force_encoding('iso-2022-jp')).should == true + end end diff --git a/spec/ruby/core/string/split_spec.rb b/spec/ruby/core/string/split_spec.rb index 2e03955a26..2da71948b3 100644 --- a/spec/ruby/core/string/split_spec.rb +++ b/spec/ruby/core/string/split_spec.rb @@ -589,4 +589,11 @@ describe "String#split with Regexp" do end end end + + it "raises a TypeError when not called with nil, String, or Regexp" do + -> { "hello".split(42) }.should raise_error(TypeError) + -> { "hello".split(:ll) }.should raise_error(TypeError) + -> { "hello".split(false) }.should raise_error(TypeError) + -> { "hello".split(Object.new) }.should raise_error(TypeError) + end end diff --git a/spec/ruby/core/string/strip_spec.rb b/spec/ruby/core/string/strip_spec.rb index 8517bf2d2a..da06862d06 100644 --- a/spec/ruby/core/string/strip_spec.rb +++ b/spec/ruby/core/string/strip_spec.rb @@ -11,7 +11,7 @@ describe "String#strip" do "\tgoodbye\r\v\n".strip.should == "goodbye" end - ruby_version_is '3.1' do + ruby_version_is '3.0' do it "returns a copy of self without leading and trailing NULL bytes and whitespace" do " \x00 goodbye \x00 ".strip.should == "goodbye" end @@ -43,7 +43,7 @@ describe "String#strip!" do a.should == "hello" end - ruby_version_is '3.1' do + ruby_version_is '3.0' do it "removes leading and trailing NULL bytes and whitespace" do a = "\000 goodbye \000" a.strip! diff --git a/spec/ruby/core/struct/new_spec.rb b/spec/ruby/core/struct/new_spec.rb index fdbf8c2c91..7b4a4f7980 100644 --- a/spec/ruby/core/struct/new_spec.rb +++ b/spec/ruby/core/struct/new_spec.rb @@ -60,14 +60,21 @@ describe "Struct.new" do -> { Struct.new(:animal, nil) }.should raise_error(TypeError) -> { Struct.new(:animal, true) }.should raise_error(TypeError) -> { Struct.new(:animal, ['chris', 'evan']) }.should raise_error(TypeError) - ruby_version_is "3.2" do - -> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(TypeError) - end end ruby_version_is ""..."3.2" do - it "raises a ArgumentError if passed a Hash with an unknown key" do - -> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(ArgumentError) + it "raises a TypeError or ArgumentError if passed a Hash with an unknown key" do + # CRuby < 3.2 raises ArgumentError: unknown keyword: :name, but that seems a bug: + # https://bugs.ruby-lang.org/issues/18632 + -> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(StandardError) { |e| + [ArgumentError, TypeError].should.include?(e.class) + } + end + end + + ruby_version_is "3.2" do + it "raises a TypeError if passed a Hash with an unknown key" do + -> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(TypeError) end end diff --git a/spec/ruby/core/thread/shared/exit.rb b/spec/ruby/core/thread/shared/exit.rb index 40dc478947..3663827579 100644 --- a/spec/ruby/core/thread/shared/exit.rb +++ b/spec/ruby/core/thread/shared/exit.rb @@ -66,6 +66,26 @@ describe :thread_exit, shared: true do ScratchPad.recorded.should == nil end + it "does not reset $!" do + ScratchPad.record [] + + exc = RuntimeError.new("foo") + thread = Thread.new do + begin + raise exc + ensure + ScratchPad << $! + begin + Thread.current.send(@method) + ensure + ScratchPad << $! + end + end + end + thread.join + ScratchPad.recorded.should == [exc, exc] + end + it "cannot be rescued" do thread = Thread.new do begin @@ -73,7 +93,7 @@ describe :thread_exit, shared: true do rescue Exception ScratchPad.record :in_rescue end - ScratchPad.record :end_of_thread_block + ScratchPad.record :end_of_thread_block end thread.join |