Skip to content

Commit

Permalink
Update to ruby/spec@573cf97
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Apr 1, 2024
1 parent 3ba7ec4 commit 29f358e
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 39 deletions.
2 changes: 1 addition & 1 deletion spec/ruby/command_line/dash_v_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

describe "when used alone" do
it "prints version and ends" do
ruby_exe(nil, args: '-v').sub("+PRISM ", "").should include(RUBY_DESCRIPTION)
ruby_exe(nil, args: '-v').sub("+PRISM ", "").should include(RUBY_DESCRIPTION.sub("+PRISM ", ""))
end unless (defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?) ||
(defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?) ||
(ENV['RUBY_MN_THREADS'] == '1')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true
frozen = "test".frozen?
interned = "test".equal?("test")
puts "frozen:#{frozen} interned:#{interned}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: false
frozen = "test".frozen?
interned = "test".equal?("test")
puts "frozen:#{frozen} interned:#{interned}"
3 changes: 3 additions & 0 deletions spec/ruby/command_line/fixtures/string_literal_raw.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
frozen = "test".frozen?
interned = "test".equal?("test")
puts "frozen:#{frozen} interned:#{interned}"
44 changes: 44 additions & 0 deletions spec/ruby/command_line/frozen_strings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,50 @@
end
end

describe "The --disable-frozen-string-literal flag causes string literals to" do

it "produce a different object each time" do
ruby_exe(fixture(__FILE__, "freeze_flag_one_literal.rb"), options: "--disable-frozen-string-literal").chomp.should == "false"
end

end

describe "With neither --enable-frozen-string-literal nor --disable-frozen-string-literal flag set" do
before do
# disable --enable-frozen-string-literal and --disable-frozen-string-literal passed in $RUBYOPT
@rubyopt = ENV["RUBYOPT"]
ENV["RUBYOPT"] = ""
end

after do
ENV["RUBYOPT"] = @rubyopt
end

it "produce a different object each time" do
ruby_exe(fixture(__FILE__, "freeze_flag_one_literal.rb")).chomp.should == "false"
end

ruby_version_is "3.4" do
it "if file has no frozen_string_literal comment produce different frozen strings each time" do
ruby_exe(fixture(__FILE__, "string_literal_raw.rb")).chomp.should == "frozen:true interned:false"
end
end

ruby_version_is ""..."3.4" do
it "if file has no frozen_string_literal comment produce different mutable strings each time" do
ruby_exe(fixture(__FILE__, "string_literal_raw.rb")).chomp.should == "frozen:false interned:false"
end
end

it "if file has frozen_string_literal:true comment produce same frozen strings each time" do
ruby_exe(fixture(__FILE__, "string_literal_frozen_comment.rb")).chomp.should == "frozen:true interned:true"
end

it "if file has frozen_string_literal:false comment produce different mutable strings each time" do
ruby_exe(fixture(__FILE__, "string_literal_mutable_comment.rb")).chomp.should == "frozen:false interned:false"
end
end

describe "The --debug flag produces" do
it "debugging info on attempted frozen string modification" do
error_str = ruby_exe(fixture(__FILE__, 'debug_info.rb'), options: '--debug', args: "2>&1")
Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/command_line/rubyopt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
guard -> { not CROSS_COMPILING } do
it "prints the version number for '-v'" do
ENV["RUBYOPT"] = '-v'
ruby_exe("")[/\A.*/].should == RUBY_DESCRIPTION
ruby_exe("")[/\A.*/].should == RUBY_DESCRIPTION.sub("+PRISM ", "")
end

it "ignores whitespace around the option" do
ENV["RUBYOPT"] = ' -v '
ruby_exe("")[/\A.*/].should == RUBY_DESCRIPTION
ruby_exe("")[/\A.*/].should == RUBY_DESCRIPTION.sub("+PRISM ", "")
end
end

Expand Down
29 changes: 28 additions & 1 deletion spec/ruby/core/class/subclasses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
assert_subclasses(ModuleSpecs::Parent, [ModuleSpecs::Child, ModuleSpecs::Child2])
end

it "does not return included modules" do
it "does not return included modules from the parent" do
parent = Class.new
child = Class.new(parent)
mod = Module.new
Expand All @@ -16,6 +16,33 @@
assert_subclasses(parent, [child])
end

it "does not return included modules from the child" do
parent = Class.new
child = Class.new(parent)
mod = Module.new
parent.include(mod)

assert_subclasses(parent, [child])
end

it "does not return prepended modules from the parent" do
parent = Class.new
child = Class.new(parent)
mod = Module.new
parent.prepend(mod)

assert_subclasses(parent, [child])
end

it "does not return prepended modules from the child" do
parent = Class.new
child = Class.new(parent)
mod = Module.new
child.prepend(mod)

assert_subclasses(parent, [child])
end

it "does not return singleton classes" do
a = Class.new

Expand Down
33 changes: 18 additions & 15 deletions spec/ruby/core/exception/top_level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,32 @@
it "the Exception#cause is printed to STDERR with backtraces" do
code = <<-RUBY
def raise_cause
raise "the cause"
raise "the cause" # 2
end
def raise_wrapped
raise "wrapped"
raise "wrapped" # 5
end
begin
raise_cause
raise_cause # 8
rescue
raise_wrapped
raise_wrapped # 10
end
RUBY
lines = ruby_exe(code, args: "2>&1", exit_status: 1).lines
lines.map! { |l| l.chomp[/:(in.+)/, 1] }
expected = [
/\Ain [`'](?:Object#)?raise_wrapped': wrapped \(RuntimeError\)\z/,
# https://bugs.ruby-lang.org/issues/20275
*(/\Ain [`'](?:rescue in )?<main>'\z/ if RUBY_ENGINE == 'ruby'),
/\Ain [`']<main>'\z/,
/\Ain [`'](?:Object#)?raise_cause': the cause \(RuntimeError\)\z/,
/\Ain [`']<main>'\z/,
]
lines.size.should == expected.size
lines.zip(expected) { |l,e| l.should =~ e }

lines.map! { |l| l.chomp[/:(\d+:in.+)/, 1] }
lines[0].should =~ /\A5:in [`'](?:Object#)?raise_wrapped': wrapped \(RuntimeError\)\z/
if lines[1].include? 'rescue in'
# CRuby < 3.4 has an extra 'rescue in' backtrace entry
lines[1].should =~ /\A10:in [`']rescue in <main>'\z/
lines.delete_at 1
lines[1].should =~ /\A7:in [`']<main>'\z/
else
lines[1].should =~ /\A10:in [`']<main>'\z/
end
lines[2].should =~ /\A2:in [`'](?:Object#)?raise_cause': the cause \(RuntimeError\)\z/
lines[3].should =~ /\A8:in [`']<main>'\z/
lines.size.should == 4
end

describe "with a custom backtrace" do
Expand Down
19 changes: 11 additions & 8 deletions spec/ruby/core/kernel/eval_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,11 @@ class EvalSpecs
end

it "allows a magic encoding comment and a subsequent frozen_string_literal magic comment" do
frozen_string_default = "test".frozen?

code = <<CODE.b
# encoding: UTF-8
# frozen_string_literal: true
# frozen_string_literal: #{!frozen_string_default}
class EvalSpecs
Vπstring = "frozen"
end
Expand All @@ -362,7 +364,7 @@ class EvalSpecs
EvalSpecs.constants(false).should include(:"Vπstring")
EvalSpecs::Vπstring.should == "frozen"
EvalSpecs::Vπstring.encoding.should == Encoding::UTF_8
EvalSpecs::Vπstring.frozen?.should be_true
EvalSpecs::Vπstring.frozen?.should == !frozen_string_default
end

it "allows a magic encoding comment and a frozen_string_literal magic comment on the same line in emacs style" do
Expand All @@ -381,8 +383,9 @@ class EvalSpecs
end

it "ignores the magic encoding comment if it is after a frozen_string_literal magic comment" do
frozen_string_default = "test".frozen?
code = <<CODE.b
# frozen_string_literal: true
# frozen_string_literal: #{!frozen_string_default}
# encoding: UTF-8
class EvalSpecs
Vπfrozen_first = "frozen"
Expand All @@ -396,24 +399,24 @@ class EvalSpecs
value = EvalSpecs.const_get(binary_constant)
value.should == "frozen"
value.encoding.should == Encoding::BINARY
value.frozen?.should be_true
value.frozen?.should == !frozen_string_default
end

it "ignores the frozen_string_literal magic comment if it appears after a token and warns if $VERBOSE is true" do
default_frozen_string_literal = "test".frozen?
frozen_string_default = "test".frozen?
code = <<CODE
some_token_before_magic_comment = :anything
# frozen_string_literal: true
# frozen_string_literal: #{!frozen_string_default}
class EvalSpecs
Vπstring_not_frozen = "not frozen"
end
CODE
-> { eval(code) }.should complain(/warning: [`']frozen_string_literal' is ignored after any tokens/, verbose: true)
EvalSpecs::Vπstring_not_frozen.frozen?.should == default_frozen_string_literal
EvalSpecs::Vπstring_not_frozen.frozen?.should == frozen_string_default
EvalSpecs.send :remove_const, :Vπstring_not_frozen

-> { eval(code) }.should_not complain(verbose: false)
EvalSpecs::Vπstring_not_frozen.frozen?.should == default_frozen_string_literal
EvalSpecs::Vπstring_not_frozen.frozen?.should == frozen_string_default
EvalSpecs.send :remove_const, :Vπstring_not_frozen
end
end
Expand Down
69 changes: 69 additions & 0 deletions spec/ruby/core/string/chilled_string_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require_relative '../../spec_helper'

describe "chilled String" do
guard -> { ruby_version_is "3.4" and !"test".equal?("test") } do
describe "#frozen?" do
it "returns true" do
"chilled".frozen?.should == true
end
end

describe "#-@" do
it "returns a different instance" do
input = "chilled"
interned = (-input)
interned.frozen?.should == true
interned.object_id.should_not == input.object_id
end
end

describe "#+@" do
it "returns a different instance" do
input = "chilled"
duped = (+input)
duped.frozen?.should == false
duped.object_id.should_not == input.object_id
end
end

describe "#clone" do
it "preserves chilled status" do
input = "chilled".clone
-> {
input << "-mutated"
}.should complain(/literal string will be frozen in the future/)
input.should == "chilled-mutated"
end
end

describe "mutation" do
it "emits a warning" do
input = "chilled"
-> {
input << "-mutated"
}.should complain(/literal string will be frozen in the future/)
input.should == "chilled-mutated"
end

it "emits a warning on singleton_class creaation" do
-> {
"chilled".singleton_class
}.should complain(/literal string will be frozen in the future/)
end

it "emits a warning on instance variable assignment" do
-> {
"chilled".instance_variable_set(:@ivar, 42)
}.should complain(/literal string will be frozen in the future/)
end

it "raises FrozenError after the string was explictly frozen" do
input = "chilled"
input.freeze
-> {
input << "mutated"
}.should raise_error(FrozenError)
end
end
end
end
17 changes: 17 additions & 0 deletions spec/ruby/language/ensure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,21 @@ class EnsureInClassExample

result.should == :begin
end

ruby_version_is "3.4" do
it "does not introduce extra backtrace entries" do
def foo
begin
raise "oops"
ensure
return caller(0, 2) # rubocop:disable Lint/EnsureReturn
end
end
line = __LINE__
foo.should == [
"#{__FILE__}:#{line-3}:in 'foo'",
"#{__FILE__}:#{line+1}:in 'block (3 levels) in <top (required)>'"
]
end
end
end
17 changes: 17 additions & 0 deletions spec/ruby/language/rescue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,23 @@ class RescueInClassExample
eval('1.+((1 rescue 1))').should == 2
end

ruby_version_is "3.4" do
it "does not introduce extra backtrace entries" do
def foo
begin
raise "oops"
rescue
return caller(0, 2)
end
end
line = __LINE__
foo.should == [
"#{__FILE__}:#{line-3}:in 'foo'",
"#{__FILE__}:#{line+1}:in 'block (3 levels) in <top (required)>'"
]
end
end

describe "inline form" do
it "can be inlined" do
a = 1/0 rescue 1
Expand Down
6 changes: 2 additions & 4 deletions spec/ruby/language/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,14 @@ def long_string_literals
ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files.rb")).chomp.should == "true"
end

guard -> { !eval("'test'").frozen? } do
guard -> { !(eval("'test'").frozen? && "test".equal?("test")) } do
it "produces different objects for literals with the same content in different files if the other file doesn't have the comment and String literals aren't frozen by default" do
frozen_literals_by_default = eval("'test'").frozen?
ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files_no_comment.rb")).chomp.should == "true"
end
end

guard -> { eval("'test'").frozen? } do
guard -> { eval("'test'").frozen? && "test".equal?("test") } do
it "produces the same objects for literals with the same content in different files if the other file doesn't have the comment and String literals are frozen by default" do
frozen_literals_by_default = eval("'test'").frozen?
ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files_no_comment.rb")).chomp.should == "false"
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/library/net-ftp/shared/puttextfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
it "sends the contents of the passed local_file, using \\r\\n as the newline separator" do
@ftp.send(@method, @local_fixture_file, "text")

remote_lines = open(@remote_tmp_file, "rb") {|f| f.read }
local_lines = open(@local_fixture_file, "rb") {|f| f.read }
remote_lines = File.binread(@remote_tmp_file)
local_lines = File.binread(@local_fixture_file)

remote_lines.should_not == local_lines
remote_lines.should == local_lines.gsub("\n", "\r\n")
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/library/stringio/binmode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

describe "StringIO#binmode" do
it "returns self" do
io = StringIO.new("example")
io = StringIO.new(+"example")
io.binmode.should equal(io)
end

Expand Down
Loading

0 comments on commit 29f358e

Please sign in to comment.