Skip to content

Commit

Permalink
Merge pull request #8231 from andrykonchin/update-specs
Browse files Browse the repository at this point in the history
Update ruby/specs
  • Loading branch information
enebo committed May 10, 2024
2 parents d7358ea + dafcb99 commit 545313d
Show file tree
Hide file tree
Showing 46 changed files with 583 additions and 44 deletions.
2 changes: 1 addition & 1 deletion spec/ruby/core/binding/dup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
eval("b = 2", bind2)
-> { eval("b", bind1) }.should raise_error(NameError)
eval("b", bind2).should == 2

bind1.local_variables.sort.should == [:a, :bind1, :bind2]
bind2.local_variables.sort.should == [:a, :b, :bind1, :bind2]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/core/binding/irb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
IO.popen([envs, *ruby_exe, irb_fixture, chdir: dir], "r+") do |pipe|
pipe.puts "a ** 2"
pipe.puts "exit"
pipe.readlines.map(&:chomp)
pipe.readlines.map(&:chomp).reject(&:empty?)
end
end

Expand Down
20 changes: 17 additions & 3 deletions spec/ruby/core/encoding/inspect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@
Encoding::UTF_8.inspect.should be_an_instance_of(String)
end

it "returns #<Encoding:name> for a non-dummy encoding named 'name'" do
Encoding.list.to_a.reject {|e| e.dummy? }.each do |enc|
enc.inspect.should =~ /#<Encoding:#{enc.name}>/
ruby_version_is ""..."3.4" do
it "returns #<Encoding:name> for a non-dummy encoding named 'name'" do
Encoding.list.to_a.reject {|e| e.dummy? }.each do |enc|
enc.inspect.should =~ /#<Encoding:#{enc.name}>/
end
end
end

ruby_version_is "3.4" do
it "returns #<Encoding:name> for a non-dummy encoding named 'name'" do
Encoding.list.to_a.reject {|e| e.dummy? }.each do |enc|
if enc.name == "ASCII-8BIT"
enc.inspect.should == "#<Encoding:BINARY (ASCII-8BIT)>"
else
enc.inspect.should =~ /#<Encoding:#{enc.name}>/
end
end
end
end

Expand Down
34 changes: 34 additions & 0 deletions spec/ruby/core/fiber/raise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,40 @@

fiber_two.resume.should == [:yield_one, :rescued]
end

ruby_version_is "3.4" do
it "raises on the resumed fiber" do
root_fiber = Fiber.current
f1 = Fiber.new { root_fiber.transfer }
f2 = Fiber.new { f1.resume }
f2.transfer

-> do
f2.raise(RuntimeError, "Expected error")
end.should raise_error(RuntimeError, "Expected error")
end

it "raises on itself" do
-> do
Fiber.current.raise(RuntimeError, "Expected error")
end.should raise_error(RuntimeError, "Expected error")
end

it "should raise on parent fiber" do
f2 = nil
f1 = Fiber.new do
# This is equivalent to Kernel#raise:
f2.raise(RuntimeError, "Expected error")
end
f2 = Fiber.new do
f1.resume
end

-> do
f2.resume
end.should raise_error(RuntimeError, "Expected error")
end
end
end


Expand Down
9 changes: 8 additions & 1 deletion spec/ruby/core/io/pread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

it "accepts a length, an offset, and an output buffer" do
buffer = +"foo"
@file.pread(3, 4, buffer)
@file.pread(3, 4, buffer).should.equal?(buffer)
buffer.should == "567"
end

Expand All @@ -38,6 +38,13 @@
buffer.should == "12345"
end

it "preserves the encoding of the given buffer" do
buffer = ''.encode(Encoding::ISO_8859_1)
@file.pread(10, 0, buffer)

buffer.encoding.should == Encoding::ISO_8859_1
end

it "does not advance the file pointer" do
@file.pread(4, 0).should == "1234"
@file.read.should == "1234567890"
Expand Down
15 changes: 15 additions & 0 deletions spec/ruby/core/io/read_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,21 @@
buf.should == @contents[0..4]
end

it "preserves the encoding of the given buffer" do
buffer = ''.encode(Encoding::ISO_8859_1)
@io.read(10, buffer)

buffer.encoding.should == Encoding::ISO_8859_1
end

# https://bugs.ruby-lang.org/issues/20416
it "does not preserve the encoding of the given buffer when max length is not provided" do
buffer = ''.encode(Encoding::ISO_8859_1)
@io.read(nil, buffer)

buffer.encoding.should_not == Encoding::ISO_8859_1
end

it "returns the given buffer" do
buf = +""

Expand Down
3 changes: 2 additions & 1 deletion spec/ruby/core/io/readpartial_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
buffer = +"existing content"
@wr.write("hello world")
@wr.close
@rd.readpartial(11, buffer)
@rd.readpartial(11, buffer).should.equal?(buffer)
buffer.should == "hello world"
end

Expand Down Expand Up @@ -106,6 +106,7 @@
@wr.write("abc")
@wr.close
@rd.readpartial(10, buffer)

buffer.encoding.should == Encoding::ISO_8859_1
end
end
9 changes: 8 additions & 1 deletion spec/ruby/core/io/sysread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@

it "discards the existing buffer content upon successful read" do
buffer = +"existing content"
@file.sysread(11, buffer)
@file.sysread(11, buffer).should.equal?(buffer)
buffer.should == "01234567890"
end

Expand All @@ -107,6 +107,13 @@
-> { @file.sysread(1, buffer) }.should raise_error(EOFError)
buffer.should be_empty
end

it "preserves the encoding of the given buffer" do
buffer = ''.encode(Encoding::ISO_8859_1)
string = @file.sysread(10, buffer)

buffer.encoding.should == Encoding::ISO_8859_1
end
end

describe "IO#sysread" do
Expand Down
28 changes: 28 additions & 0 deletions spec/ruby/core/module/include_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ def self.append_features(mod)
-> { ModuleSpecs::SubclassSpec.include(ModuleSpecs::Subclass.new) }.should_not raise_error(TypeError)
end

ruby_version_is ""..."3.2" do
it "raises ArgumentError when the argument is a refinement" do
refinement = nil

Module.new do
refine String do
refinement = self
end
end

-> { ModuleSpecs::Basic.include(refinement) }.should raise_error(ArgumentError, "refinement module is not allowed")
end
end

ruby_version_is "3.2" do
it "raises a TypeError when the argument is a refinement" do
refinement = nil

Module.new do
refine String do
refinement = self
end
end

-> { ModuleSpecs::Basic.include(refinement) }.should raise_error(TypeError, "Cannot include refinement")
end
end

it "imports constants to modules and classes" do
ModuleSpecs::A.constants.should include(:CONSTANT_A)
ModuleSpecs::B.constants.should include(:CONSTANT_A, :CONSTANT_B)
Expand Down
48 changes: 48 additions & 0 deletions spec/ruby/core/module/prepend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ def foo
foo.call.should == 'm'
end

it "updates the optimized method when a prepended module is updated" do
out = ruby_exe(<<~RUBY)
module M; end
class Integer
prepend M
end
l = -> { 1 + 2 }
p l.call
M.module_eval do
def +(o)
$called = true
super(o)
end
end
p l.call
p $called
RUBY
out.should == "3\n3\ntrue\n"
end

it "updates the method when there is a base included method and the prepended module overrides it" do
base_module = Module.new do
def foo
Expand Down Expand Up @@ -415,6 +435,34 @@ module M
-> { ModuleSpecs::SubclassSpec.prepend(ModuleSpecs::Subclass.new) }.should_not raise_error(TypeError)
end

ruby_version_is ""..."3.2" do
it "raises ArgumentError when the argument is a refinement" do
refinement = nil

Module.new do
refine String do
refinement = self
end
end

-> { ModuleSpecs::Basic.prepend(refinement) }.should raise_error(ArgumentError, "refinement module is not allowed")
end
end

ruby_version_is "3.2" do
it "raises a TypeError when the argument is a refinement" do
refinement = nil

Module.new do
refine String do
refinement = self
end
end

-> { ModuleSpecs::Basic.prepend(refinement) }.should raise_error(TypeError, "Cannot prepend refinement")
end
end

it "imports constants" do
m1 = Module.new
m1::MY_CONSTANT = 1
Expand Down
62 changes: 49 additions & 13 deletions spec/ruby/core/range/size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,22 @@
it "returns the number of elements in the range" do
(1..16).size.should == 16
(1...16).size.should == 15

(1.0..16.0).size.should == 16
(1.0...16.0).size.should == 15
(1.0..15.9).size.should == 15
(1.1..16.0).size.should == 15
(1.1..15.9).size.should == 15
end

it "returns 0 if last is less than first" do
(16..0).size.should == 0
(16.0..0.0).size.should == 0
(Float::INFINITY..0).size.should == 0
end

it 'returns Float::INFINITY for increasing, infinite ranges' do
(0..Float::INFINITY).size.should == Float::INFINITY
(-Float::INFINITY..0).size.should == Float::INFINITY
(-Float::INFINITY..Float::INFINITY).size.should == Float::INFINITY
end

it 'returns Float::INFINITY for endless ranges if the start is numeric' do
eval("(1..)").size.should == Float::INFINITY
eval("(0.5...)").size.should == Float::INFINITY
end

it 'returns nil for endless ranges if the start is not numeric' do
eval("('z'..)").size.should == nil
eval("([]...)").size.should == nil
end

ruby_version_is ""..."3.2" do
Expand All @@ -43,7 +31,7 @@
end
end

ruby_version_is "3.2" do
ruby_version_is "3.2"..."3.4" do
it 'returns Float::INFINITY for all beginless ranges if the end is numeric' do
(..1).size.should == Float::INFINITY
(...0.5).size.should == Float::INFINITY
Expand All @@ -58,6 +46,54 @@
end
end

ruby_version_is ""..."3.4" do
it "returns the number of elements in the range" do
(1.0..16.0).size.should == 16
(1.0...16.0).size.should == 15
(1.0..15.9).size.should == 15
(1.1..16.0).size.should == 15
(1.1..15.9).size.should == 15
end

it "returns 0 if last is less than first" do
(16.0..0.0).size.should == 0
(Float::INFINITY..0).size.should == 0
end

it 'returns Float::INFINITY for increasing, infinite ranges' do
(-Float::INFINITY..0).size.should == Float::INFINITY
(-Float::INFINITY..Float::INFINITY).size.should == Float::INFINITY
end

it 'returns Float::INFINITY for endless ranges if the start is numeric' do
eval("(0.5...)").size.should == Float::INFINITY
end

it 'returns nil for endless ranges if the start is not numeric' do
eval("([]...)").size.should == nil
end
end

ruby_version_is "3.4" do
it 'raises TypeError if a range is not iterable' do
-> { (1.0..16.0).size }.should raise_error(TypeError, /can't iterate from/)
-> { (1.0...16.0).size }.should raise_error(TypeError, /can't iterate from/)
-> { (1.0..15.9).size }.should raise_error(TypeError, /can't iterate from/)
-> { (1.1..16.0).size }.should raise_error(TypeError, /can't iterate from/)
-> { (1.1..15.9).size }.should raise_error(TypeError, /can't iterate from/)
-> { (16.0..0.0).size }.should raise_error(TypeError, /can't iterate from/)
-> { (Float::INFINITY..0).size }.should raise_error(TypeError, /can't iterate from/)
-> { (-Float::INFINITY..0).size }.should raise_error(TypeError, /can't iterate from/)
-> { (-Float::INFINITY..Float::INFINITY).size }.should raise_error(TypeError, /can't iterate from/)
-> { (..1).size }.should raise_error(TypeError, /can't iterate from/)
-> { (...0.5).size }.should raise_error(TypeError, /can't iterate from/)
-> { (..nil).size }.should raise_error(TypeError, /can't iterate from/)
-> { (...'o').size }.should raise_error(TypeError, /can't iterate from/)
-> { eval("(0.5...)").size }.should raise_error(TypeError, /can't iterate from/)
-> { eval("([]...)").size }.should raise_error(TypeError, /can't iterate from/)
end
end

it "returns nil if first and last are not Numeric" do
(:a..:z).size.should be_nil
('a'..'z').size.should be_nil
Expand Down
11 changes: 11 additions & 0 deletions spec/ruby/core/string/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@
$~.should == nil
end

ruby_bug "#20421", ""..."3.3" do
it "always clear $~" do
"a".index(/a/)
$~.should_not == nil

string = "blablabla"
string.index(/bla/, string.length + 1)
$~.should == nil
end
end

it "starts the search at the given offset" do
"blablabla".index(/.{0}/, 5).should == 5
"blablabla".index(/.{1}/, 5).should == 5
Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/core/thread/each_caller_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
}.should raise_error(LocalJumpError, "no block given")
end

it "doesn't accept positional and keyword arguments" do
it "doesn't accept keyword arguments" do
-> {
Thread.each_caller_location(12, foo: 10) {}
}.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 0)")
}.should raise_error(ArgumentError);
end
end
end

0 comments on commit 545313d

Please sign in to comment.