Skip to content

Commit

Permalink
[GR-14806] Update specs
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4267
  • Loading branch information
andrykonchin committed May 14, 2024
2 parents 2124e4a + ca7823d commit 68cd4ea
Show file tree
Hide file tree
Showing 22 changed files with 315 additions and 31 deletions.
17 changes: 17 additions & 0 deletions spec/ruby/core/binding/dup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,21 @@
bind.frozen?.should == true
bind.dup.frozen?.should == false
end

it "retains original binding variables but the list is distinct" do
bind1 = binding
eval "a = 1", bind1

bind2 = bind1.dup
eval("a = 2", bind2)
eval("a", bind1).should == 2
eval("a", bind2).should == 2

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
end
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
8 changes: 7 additions & 1 deletion spec/ruby/core/enumerator/next_values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def o.each
yield :e1, :e2, :e3
yield nil
yield
yield [:f1, :f2]
end

@e = o.to_enum
Expand Down Expand Up @@ -48,8 +49,13 @@ def o.each
@e.next_values.should == []
end

it "raises StopIteration if called on a finished enumerator" do
it "returns an array of array if yield is called with an array" do
7.times { @e.next }
@e.next_values.should == [[:f1, :f2]]
end

it "raises StopIteration if called on a finished enumerator" do
8.times { @e.next }
-> { @e.next_values }.should raise_error(StopIteration)
end
end
8 changes: 7 additions & 1 deletion spec/ruby/core/enumerator/peek_values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def o.each
yield :e1, :e2, :e3
yield nil
yield
yield [:f1, :f2]
end

@e = o.to_enum
Expand Down Expand Up @@ -50,8 +51,13 @@ def o.each
@e.peek_values.should == []
end

it "raises StopIteration if called on a finished enumerator" do
it "returns an array of array if yield is called with an array" do
7.times { @e.next }
@e.peek_values.should == [[:f1, :f2]]
end

it "raises StopIteration if called on a finished enumerator" do
8.times { @e.next }
-> { @e.peek_values }.should raise_error(StopIteration)
end
end
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
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
4 changes: 4 additions & 0 deletions spec/ruby/core/warning/element_reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe "Warning.[]" do
it "returns default values for categories :deprecated and :experimental" do
# If any warning options were set on the Ruby that will be executed, then
# it's possible this test will fail. In this case we will skip this test.
skip if ruby_exe.any? { |opt| opt.start_with?("-W") }

ruby_exe('p [Warning[:deprecated], Warning[:experimental]]').chomp.should == "[false, true]"
ruby_exe('p [Warning[:deprecated], Warning[:experimental]]', options: "-w").chomp.should == "[true, true]"
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/core/warning/performance_warning_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def +(...)
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/ruby/language/break_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def three
end.should_not raise_error
end

it "raises LocalJumpError when converted into a proc during a a super call" do
it "raises LocalJumpError when converted into a proc during a super call" do
cls1 = Class.new { def foo(&b); b; end }
cls2 = Class.new(cls1) { def foo; super { break 1 }.call; end }

Expand Down
78 changes: 78 additions & 0 deletions spec/ruby/language/execution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,89 @@
ip = 'world'
`echo disc #{ip}`.should == "disc world\n"
end

it "can be redefined and receive a frozen string as argument" do
called = false
runner = Object.new

runner.singleton_class.define_method(:`) do |str|
called = true

str.should == "test command"
str.frozen?.should == true
end

runner.instance_exec do
`test command`
end

called.should == true
end

it "the argument isn't frozen if it contains interpolation" do
called = false
runner = Object.new

runner.singleton_class.define_method(:`) do |str|
called = true

str.should == "test command"
str.frozen?.should == false
str << "mutated"
end

2.times do
runner.instance_exec do
`test #{:command}` # rubocop:disable Lint/LiteralInInterpolation
end
end

called.should == true
end
end

describe "%x" do
it "is the same as ``" do
ip = 'world'
%x(echo disc #{ip}).should == "disc world\n"
end

it "can be redefined and receive a frozen string as argument" do
called = false
runner = Object.new

runner.singleton_class.define_method(:`) do |str|
called = true

str.should == "test command"
str.frozen?.should == true
end

runner.instance_exec do
%x{test command}
end

called.should == true
end

it "the argument isn't frozen if it contains interpolation" do
called = false
runner = Object.new

runner.singleton_class.define_method(:`) do |str|
called = true

str.should == "test command"
str.frozen?.should == false
str << "mutated"
end

2.times do
runner.instance_exec do
%x{test #{:command}} # rubocop:disable Lint/LiteralInInterpolation
end
end

called.should == true
end
end

0 comments on commit 68cd4ea

Please sign in to comment.