Skip to content

Commit 29f358e

Browse files
committed
1 parent 3ba7ec4 commit 29f358e

File tree

18 files changed

+241
-39
lines changed

18 files changed

+241
-39
lines changed

spec/ruby/command_line/dash_v_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
describe "when used alone" do
88
it "prints version and ends" do
9-
ruby_exe(nil, args: '-v').sub("+PRISM ", "").should include(RUBY_DESCRIPTION)
9+
ruby_exe(nil, args: '-v').sub("+PRISM ", "").should include(RUBY_DESCRIPTION.sub("+PRISM ", ""))
1010
end unless (defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?) ||
1111
(defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?) ||
1212
(ENV['RUBY_MN_THREADS'] == '1')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
frozen = "test".frozen?
3+
interned = "test".equal?("test")
4+
puts "frozen:#{frozen} interned:#{interned}"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: false
2+
frozen = "test".frozen?
3+
interned = "test".equal?("test")
4+
puts "frozen:#{frozen} interned:#{interned}"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
frozen = "test".frozen?
2+
interned = "test".equal?("test")
3+
puts "frozen:#{frozen} interned:#{interned}"

spec/ruby/command_line/frozen_strings_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,50 @@
1919
end
2020
end
2121

22+
describe "The --disable-frozen-string-literal flag causes string literals to" do
23+
24+
it "produce a different object each time" do
25+
ruby_exe(fixture(__FILE__, "freeze_flag_one_literal.rb"), options: "--disable-frozen-string-literal").chomp.should == "false"
26+
end
27+
28+
end
29+
30+
describe "With neither --enable-frozen-string-literal nor --disable-frozen-string-literal flag set" do
31+
before do
32+
# disable --enable-frozen-string-literal and --disable-frozen-string-literal passed in $RUBYOPT
33+
@rubyopt = ENV["RUBYOPT"]
34+
ENV["RUBYOPT"] = ""
35+
end
36+
37+
after do
38+
ENV["RUBYOPT"] = @rubyopt
39+
end
40+
41+
it "produce a different object each time" do
42+
ruby_exe(fixture(__FILE__, "freeze_flag_one_literal.rb")).chomp.should == "false"
43+
end
44+
45+
ruby_version_is "3.4" do
46+
it "if file has no frozen_string_literal comment produce different frozen strings each time" do
47+
ruby_exe(fixture(__FILE__, "string_literal_raw.rb")).chomp.should == "frozen:true interned:false"
48+
end
49+
end
50+
51+
ruby_version_is ""..."3.4" do
52+
it "if file has no frozen_string_literal comment produce different mutable strings each time" do
53+
ruby_exe(fixture(__FILE__, "string_literal_raw.rb")).chomp.should == "frozen:false interned:false"
54+
end
55+
end
56+
57+
it "if file has frozen_string_literal:true comment produce same frozen strings each time" do
58+
ruby_exe(fixture(__FILE__, "string_literal_frozen_comment.rb")).chomp.should == "frozen:true interned:true"
59+
end
60+
61+
it "if file has frozen_string_literal:false comment produce different mutable strings each time" do
62+
ruby_exe(fixture(__FILE__, "string_literal_mutable_comment.rb")).chomp.should == "frozen:false interned:false"
63+
end
64+
end
65+
2266
describe "The --debug flag produces" do
2367
it "debugging info on attempted frozen string modification" do
2468
error_str = ruby_exe(fixture(__FILE__, 'debug_info.rb'), options: '--debug', args: "2>&1")

spec/ruby/command_line/rubyopt_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
guard -> { not CROSS_COMPILING } do
2626
it "prints the version number for '-v'" do
2727
ENV["RUBYOPT"] = '-v'
28-
ruby_exe("")[/\A.*/].should == RUBY_DESCRIPTION
28+
ruby_exe("")[/\A.*/].should == RUBY_DESCRIPTION.sub("+PRISM ", "")
2929
end
3030

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

spec/ruby/core/class/subclasses_spec.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
assert_subclasses(ModuleSpecs::Parent, [ModuleSpecs::Child, ModuleSpecs::Child2])
88
end
99

10-
it "does not return included modules" do
10+
it "does not return included modules from the parent" do
1111
parent = Class.new
1212
child = Class.new(parent)
1313
mod = Module.new
@@ -16,6 +16,33 @@
1616
assert_subclasses(parent, [child])
1717
end
1818

19+
it "does not return included modules from the child" do
20+
parent = Class.new
21+
child = Class.new(parent)
22+
mod = Module.new
23+
parent.include(mod)
24+
25+
assert_subclasses(parent, [child])
26+
end
27+
28+
it "does not return prepended modules from the parent" do
29+
parent = Class.new
30+
child = Class.new(parent)
31+
mod = Module.new
32+
parent.prepend(mod)
33+
34+
assert_subclasses(parent, [child])
35+
end
36+
37+
it "does not return prepended modules from the child" do
38+
parent = Class.new
39+
child = Class.new(parent)
40+
mod = Module.new
41+
child.prepend(mod)
42+
43+
assert_subclasses(parent, [child])
44+
end
45+
1946
it "does not return singleton classes" do
2047
a = Class.new
2148

spec/ruby/core/exception/top_level_spec.rb

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,32 @@
88
it "the Exception#cause is printed to STDERR with backtraces" do
99
code = <<-RUBY
1010
def raise_cause
11-
raise "the cause"
11+
raise "the cause" # 2
1212
end
1313
def raise_wrapped
14-
raise "wrapped"
14+
raise "wrapped" # 5
1515
end
1616
begin
17-
raise_cause
17+
raise_cause # 8
1818
rescue
19-
raise_wrapped
19+
raise_wrapped # 10
2020
end
2121
RUBY
2222
lines = ruby_exe(code, args: "2>&1", exit_status: 1).lines
23-
lines.map! { |l| l.chomp[/:(in.+)/, 1] }
24-
expected = [
25-
/\Ain [`'](?:Object#)?raise_wrapped': wrapped \(RuntimeError\)\z/,
26-
# https://bugs.ruby-lang.org/issues/20275
27-
*(/\Ain [`'](?:rescue in )?<main>'\z/ if RUBY_ENGINE == 'ruby'),
28-
/\Ain [`']<main>'\z/,
29-
/\Ain [`'](?:Object#)?raise_cause': the cause \(RuntimeError\)\z/,
30-
/\Ain [`']<main>'\z/,
31-
]
32-
lines.size.should == expected.size
33-
lines.zip(expected) { |l,e| l.should =~ e }
23+
24+
lines.map! { |l| l.chomp[/:(\d+:in.+)/, 1] }
25+
lines[0].should =~ /\A5:in [`'](?:Object#)?raise_wrapped': wrapped \(RuntimeError\)\z/
26+
if lines[1].include? 'rescue in'
27+
# CRuby < 3.4 has an extra 'rescue in' backtrace entry
28+
lines[1].should =~ /\A10:in [`']rescue in <main>'\z/
29+
lines.delete_at 1
30+
lines[1].should =~ /\A7:in [`']<main>'\z/
31+
else
32+
lines[1].should =~ /\A10:in [`']<main>'\z/
33+
end
34+
lines[2].should =~ /\A2:in [`'](?:Object#)?raise_cause': the cause \(RuntimeError\)\z/
35+
lines[3].should =~ /\A8:in [`']<main>'\z/
36+
lines.size.should == 4
3437
end
3538

3639
describe "with a custom backtrace" do

spec/ruby/core/kernel/eval_spec.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,11 @@ class EvalSpecs
350350
end
351351

352352
it "allows a magic encoding comment and a subsequent frozen_string_literal magic comment" do
353+
frozen_string_default = "test".frozen?
354+
353355
code = <<CODE.b
354356
# encoding: UTF-8
355-
# frozen_string_literal: true
357+
# frozen_string_literal: #{!frozen_string_default}
356358
class EvalSpecs
357359
Vπstring = "frozen"
358360
end
@@ -362,7 +364,7 @@ class EvalSpecs
362364
EvalSpecs.constants(false).should include(:"Vπstring")
363365
EvalSpecs::Vπstring.should == "frozen"
364366
EvalSpecs::Vπstring.encoding.should == Encoding::UTF_8
365-
EvalSpecs::Vπstring.frozen?.should be_true
367+
EvalSpecs::Vπstring.frozen?.should == !frozen_string_default
366368
end
367369

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

383385
it "ignores the magic encoding comment if it is after a frozen_string_literal magic comment" do
386+
frozen_string_default = "test".frozen?
384387
code = <<CODE.b
385-
# frozen_string_literal: true
388+
# frozen_string_literal: #{!frozen_string_default}
386389
# encoding: UTF-8
387390
class EvalSpecs
388391
Vπfrozen_first = "frozen"
@@ -396,24 +399,24 @@ class EvalSpecs
396399
value = EvalSpecs.const_get(binary_constant)
397400
value.should == "frozen"
398401
value.encoding.should == Encoding::BINARY
399-
value.frozen?.should be_true
402+
value.frozen?.should == !frozen_string_default
400403
end
401404

402405
it "ignores the frozen_string_literal magic comment if it appears after a token and warns if $VERBOSE is true" do
403-
default_frozen_string_literal = "test".frozen?
406+
frozen_string_default = "test".frozen?
404407
code = <<CODE
405408
some_token_before_magic_comment = :anything
406-
# frozen_string_literal: true
409+
# frozen_string_literal: #{!frozen_string_default}
407410
class EvalSpecs
408411
Vπstring_not_frozen = "not frozen"
409412
end
410413
CODE
411414
-> { eval(code) }.should complain(/warning: [`']frozen_string_literal' is ignored after any tokens/, verbose: true)
412-
EvalSpecs::Vπstring_not_frozen.frozen?.should == default_frozen_string_literal
415+
EvalSpecs::Vπstring_not_frozen.frozen?.should == frozen_string_default
413416
EvalSpecs.send :remove_const, :Vπstring_not_frozen
414417

415418
-> { eval(code) }.should_not complain(verbose: false)
416-
EvalSpecs::Vπstring_not_frozen.frozen?.should == default_frozen_string_literal
419+
EvalSpecs::Vπstring_not_frozen.frozen?.should == frozen_string_default
417420
EvalSpecs.send :remove_const, :Vπstring_not_frozen
418421
end
419422
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require_relative '../../spec_helper'
2+
3+
describe "chilled String" do
4+
guard -> { ruby_version_is "3.4" and !"test".equal?("test") } do
5+
describe "#frozen?" do
6+
it "returns true" do
7+
"chilled".frozen?.should == true
8+
end
9+
end
10+
11+
describe "#-@" do
12+
it "returns a different instance" do
13+
input = "chilled"
14+
interned = (-input)
15+
interned.frozen?.should == true
16+
interned.object_id.should_not == input.object_id
17+
end
18+
end
19+
20+
describe "#+@" do
21+
it "returns a different instance" do
22+
input = "chilled"
23+
duped = (+input)
24+
duped.frozen?.should == false
25+
duped.object_id.should_not == input.object_id
26+
end
27+
end
28+
29+
describe "#clone" do
30+
it "preserves chilled status" do
31+
input = "chilled".clone
32+
-> {
33+
input << "-mutated"
34+
}.should complain(/literal string will be frozen in the future/)
35+
input.should == "chilled-mutated"
36+
end
37+
end
38+
39+
describe "mutation" do
40+
it "emits a warning" do
41+
input = "chilled"
42+
-> {
43+
input << "-mutated"
44+
}.should complain(/literal string will be frozen in the future/)
45+
input.should == "chilled-mutated"
46+
end
47+
48+
it "emits a warning on singleton_class creaation" do
49+
-> {
50+
"chilled".singleton_class
51+
}.should complain(/literal string will be frozen in the future/)
52+
end
53+
54+
it "emits a warning on instance variable assignment" do
55+
-> {
56+
"chilled".instance_variable_set(:@ivar, 42)
57+
}.should complain(/literal string will be frozen in the future/)
58+
end
59+
60+
it "raises FrozenError after the string was explictly frozen" do
61+
input = "chilled"
62+
input.freeze
63+
-> {
64+
input << "mutated"
65+
}.should raise_error(FrozenError)
66+
end
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)