summaryrefslogtreecommitdiff
path: root/spec/ruby/language
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/language')
-rw-r--r--spec/ruby/language/alias_spec.rb2
-rw-r--r--spec/ruby/language/assignments_spec.rb590
-rw-r--r--spec/ruby/language/block_spec.rb306
-rw-r--r--spec/ruby/language/break_spec.rb21
-rw-r--r--spec/ruby/language/case_spec.rb141
-rw-r--r--spec/ruby/language/class_spec.rb44
-rw-r--r--spec/ruby/language/constants_spec.rb113
-rw-r--r--spec/ruby/language/def_spec.rb57
-rw-r--r--spec/ruby/language/defined_spec.rb125
-rw-r--r--spec/ruby/language/delegation_spec.rb139
-rw-r--r--spec/ruby/language/encoding_spec.rb8
-rw-r--r--spec/ruby/language/ensure_spec.rb15
-rw-r--r--spec/ruby/language/execution_spec.rb78
-rw-r--r--spec/ruby/language/fixtures/class_with_class_variable.rb9
-rw-r--r--spec/ruby/language/fixtures/defined.rb6
-rw-r--r--spec/ruby/language/fixtures/delegation.rb4
-rw-r--r--spec/ruby/language/fixtures/module.rb9
-rw-r--r--spec/ruby/language/fixtures/private.rb26
-rw-r--r--spec/ruby/language/fixtures/rescue/top_level.rb7
-rw-r--r--spec/ruby/language/fixtures/send.rb16
-rw-r--r--spec/ruby/language/fixtures/super.rb48
-rw-r--r--spec/ruby/language/for_spec.rb103
-rw-r--r--spec/ruby/language/hash_spec.rb184
-rw-r--r--spec/ruby/language/heredoc_spec.rb10
-rw-r--r--spec/ruby/language/if_spec.rb10
-rw-r--r--spec/ruby/language/it_parameter_spec.rb66
-rw-r--r--spec/ruby/language/keyword_arguments_spec.rb111
-rw-r--r--spec/ruby/language/lambda_spec.rb23
-rw-r--r--spec/ruby/language/magic_comment_spec.rb3
-rw-r--r--spec/ruby/language/method_spec.rb290
-rw-r--r--spec/ruby/language/module_spec.rb51
-rw-r--r--spec/ruby/language/numbered_parameters_spec.rb24
-rw-r--r--spec/ruby/language/optional_assignments_spec.rb296
-rw-r--r--spec/ruby/language/pattern_matching_spec.rb1485
-rw-r--r--spec/ruby/language/precedence_spec.rb16
-rw-r--r--spec/ruby/language/predefined_spec.rb327
-rw-r--r--spec/ruby/language/private_spec.rb2
-rw-r--r--spec/ruby/language/proc_spec.rb2
-rw-r--r--spec/ruby/language/regexp/anchors_spec.rb8
-rw-r--r--spec/ruby/language/regexp/back-references_spec.rb9
-rw-r--r--spec/ruby/language/regexp/character_classes_spec.rb13
-rw-r--r--spec/ruby/language/regexp/encoding_spec.rb48
-rw-r--r--spec/ruby/language/regexp/escapes_spec.rb2
-rw-r--r--spec/ruby/language/regexp/grouping_spec.rb2
-rw-r--r--spec/ruby/language/regexp_spec.rb4
-rw-r--r--spec/ruby/language/rescue_spec.rb103
-rw-r--r--spec/ruby/language/reserved_keywords.rb149
-rw-r--r--spec/ruby/language/retry_spec.rb5
-rw-r--r--spec/ruby/language/safe_navigator_spec.rb80
-rw-r--r--spec/ruby/language/send_spec.rb20
-rw-r--r--spec/ruby/language/singleton_class_spec.rb9
-rw-r--r--spec/ruby/language/string_spec.rb32
-rw-r--r--spec/ruby/language/super_spec.rb9
-rw-r--r--spec/ruby/language/symbol_spec.rb14
-rw-r--r--spec/ruby/language/undef_spec.rb2
-rw-r--r--spec/ruby/language/variables_spec.rb119
-rw-r--r--spec/ruby/language/yield_spec.rb12
57 files changed, 3957 insertions, 1450 deletions
diff --git a/spec/ruby/language/alias_spec.rb b/spec/ruby/language/alias_spec.rb
index ee928cbfaa..61fddb0184 100644
--- a/spec/ruby/language/alias_spec.rb
+++ b/spec/ruby/language/alias_spec.rb
@@ -252,7 +252,7 @@ describe "The alias keyword" do
it "on top level defines the alias on Object" do
# because it defines on the default definee / current module
- ruby_exe("def foo; end; alias bla foo; print method(:bla).owner", escape: true).should == "Object"
+ ruby_exe("def foo; end; alias bla foo; print method(:bla).owner").should == "Object"
end
it "raises a NameError when passed a missing name" do
diff --git a/spec/ruby/language/assignments_spec.rb b/spec/ruby/language/assignments_spec.rb
new file mode 100644
index 0000000000..c4adf73c1c
--- /dev/null
+++ b/spec/ruby/language/assignments_spec.rb
@@ -0,0 +1,590 @@
+require_relative '../spec_helper'
+
+# Should be synchronized with spec/ruby/language/optional_assignments_spec.rb
+# Some specs for assignments are located in language/variables_spec.rb
+describe 'Assignments' do
+ describe 'using =' do
+ describe 'evaluation order' do
+ it 'evaluates expressions left to right when assignment with an accessor' do
+ object = Object.new
+ def object.a=(value) end
+ ScratchPad.record []
+
+ (ScratchPad << :receiver; object).a = (ScratchPad << :rhs; :value)
+ ScratchPad.recorded.should == [:receiver, :rhs]
+ end
+
+ it 'evaluates expressions left to right when assignment with a #[]=' do
+ object = Object.new
+ def object.[]=(_, _) end
+ ScratchPad.record []
+
+ (ScratchPad << :receiver; object)[(ScratchPad << :argument; :a)] = (ScratchPad << :rhs; :value)
+ ScratchPad.recorded.should == [:receiver, :argument, :rhs]
+ end
+
+ it 'evaluates expressions left to right when assignment with compounded constant' do
+ m = Module.new
+ ScratchPad.record []
+
+ (ScratchPad << :module; m)::A = (ScratchPad << :rhs; :value)
+ ScratchPad.recorded.should == [:module, :rhs]
+ end
+
+ it 'raises TypeError after evaluation of right-hand-side when compounded constant module is not a module' do
+ ScratchPad.record []
+
+ -> {
+ (:not_a_module)::A = (ScratchPad << :rhs; :value)
+ }.should raise_error(TypeError)
+
+ ScratchPad.recorded.should == [:rhs]
+ end
+ end
+
+ context "given block argument" do
+ before do
+ @klass = Class.new do
+ def initialize(h) @h = h end
+ def [](k, &block) @h[k]; end
+ def []=(k, v, &block) @h[k] = v; end
+ end
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "accepts block argument" do
+ obj = @klass.new(a: 1)
+ block = proc {}
+
+ eval "obj[:a, &block] = 2"
+ eval("obj[:a, &block]").should == 2
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "raises SyntaxError" do
+ obj = @klass.new(a: 1)
+ block = proc {}
+
+ -> {
+ eval "obj[:a, &block] = 2"
+ }.should raise_error(SyntaxError, /unexpected block arg given in index assignment|block arg given in index assignment/)
+ end
+ end
+ end
+
+ context "given keyword arguments" do
+ before do
+ @klass = Class.new do
+ attr_reader :x
+
+ def []=(*args, **kw)
+ @x = [args, kw]
+ end
+ end
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "supports keyword arguments in index assignments" do
+ a = @klass.new
+ eval "a[1, 2, 3, b: 4] = 5"
+ a.x.should == [[1, 2, 3, {b: 4}, 5], {}]
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "raises SyntaxError when given keyword arguments in index assignments" do
+ a = @klass.new
+ -> { eval "a[1, 2, 3, b: 4] = 5" }.should raise_error(SyntaxError,
+ /keywords are not allowed in index assignment expressions|keyword arg given in index assignment/) # prism|parse.y
+ end
+ end
+ end
+ end
+
+ describe 'using +=' do
+ describe 'using an accessor' do
+ before do
+ klass = Class.new { attr_accessor :b }
+ @a = klass.new
+ end
+
+ it 'does evaluate receiver only once when assigns' do
+ ScratchPad.record []
+ @a.b = 1
+
+ (ScratchPad << :evaluated; @a).b += 2
+
+ ScratchPad.recorded.should == [:evaluated]
+ @a.b.should == 3
+ end
+
+ it 'ignores method visibility when receiver is self' do
+ klass_with_private_methods = Class.new do
+ def initialize(n) @a = n end
+ def public_method(n); self.a += n end
+ private
+ def a; @a end
+ def a=(n) @a = n; 42 end
+ end
+
+ a = klass_with_private_methods.new(0)
+ a.public_method(2).should == 2
+ end
+ end
+
+ describe 'using a #[]' do
+ before do
+ klass = Class.new do
+ def [](k)
+ @hash ||= {}
+ @hash[k]
+ end
+
+ def []=(k, v)
+ @hash ||= {}
+ @hash[k] = v
+ 7
+ end
+ end
+ @b = klass.new
+ end
+
+ it 'evaluates receiver only once when assigns' do
+ ScratchPad.record []
+ a = {k: 1}
+
+ (ScratchPad << :evaluated; a)[:k] += 2
+
+ ScratchPad.recorded.should == [:evaluated]
+ a[:k].should == 3
+ end
+
+ it 'ignores method visibility when receiver is self' do
+ klass_with_private_methods = Class.new do
+ def initialize(h) @a = h end
+ def public_method(k, n); self[k] += n end
+ private
+ def [](k) @a[k] end
+ def []=(k, v) @a[k] = v; 42 end
+ end
+
+ a = klass_with_private_methods.new(k: 0)
+ a.public_method(:k, 2).should == 2
+ end
+
+ context "given block argument" do
+ before do
+ @klass = Class.new do
+ def initialize(h) @h = h end
+ def [](k, &block) @h[k]; end
+ def []=(k, v, &block) @h[k] = v; end
+ end
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "accepts block argument" do
+ obj = @klass.new(a: 1)
+ block = proc {}
+
+ eval "obj[:a, &block] += 2"
+ eval("obj[:a, &block]").should == 3
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "raises SyntaxError" do
+ obj = @klass.new(a: 1)
+ block = proc {}
+
+ -> {
+ eval "obj[:a, &block] += 2"
+ }.should raise_error(SyntaxError, /unexpected block arg given in index assignment|block arg given in index assignment/)
+ end
+ end
+ end
+
+ context "given keyword arguments" do
+ before do
+ @klass = Class.new do
+ attr_reader :x
+
+ def [](*args)
+ 100
+ end
+
+ def []=(*args, **kw)
+ @x = [args, kw]
+ end
+ end
+ end
+
+ ruby_version_is ""..."3.3" do
+ it "supports keyword arguments in index assignments" do
+ a = @klass.new
+ eval "a[1, 2, 3, b: 4] += 5"
+ a.x.should == [[1, 2, 3, {b: 4}, 105], {}]
+ end
+ end
+
+ ruby_version_is "3.3"..."3.4" do
+ it "supports keyword arguments in index assignments" do
+ a = @klass.new
+ eval "a[1, 2, 3, b: 4] += 5"
+ a.x.should == [[1, 2, 3, 105], {b: 4}]
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "raises SyntaxError when given keyword arguments in index assignments" do
+ a = @klass.new
+ -> { eval "a[1, 2, 3, b: 4] += 5" }.should raise_error(SyntaxError,
+ /keywords are not allowed in index assignment expressions|keyword arg given in index assignment/) # prism|parse.y
+ end
+ end
+ end
+
+ context 'splatted argument' do
+ it 'correctly handles it' do
+ @b[:m] = 10
+ (@b[*[:m]] += 10).should == 20
+ @b[:m].should == 20
+
+ @b[:n] = 10
+ (@b[*(1; [:n])] += 10).should == 20
+ @b[:n].should == 20
+
+ @b[:k] = 10
+ (@b[*begin 1; [:k] end] += 10).should == 20
+ @b[:k].should == 20
+ end
+
+ it 'calls #to_a only once' do
+ k = Object.new
+ def k.to_a
+ ScratchPad << :to_a
+ [:k]
+ end
+
+ ScratchPad.record []
+ @b[:k] = 10
+ (@b[*k] += 10).should == 20
+ @b[:k].should == 20
+ ScratchPad.recorded.should == [:to_a]
+ end
+
+ it 'correctly handles a nested splatted argument' do
+ @b[:k] = 10
+ (@b[*[*[:k]]] += 10).should == 20
+ @b[:k].should == 20
+ end
+
+ it 'correctly handles multiple nested splatted arguments' do
+ klass_with_multiple_parameters = Class.new do
+ def [](k1, k2, k3)
+ @hash ||= {}
+ @hash[:"#{k1}#{k2}#{k3}"]
+ end
+
+ def []=(k1, k2, k3, v)
+ @hash ||= {}
+ @hash[:"#{k1}#{k2}#{k3}"] = v
+ 7
+ end
+ end
+ a = klass_with_multiple_parameters.new
+
+ a[:a, :b, :c] = 10
+ (a[*[:a], *[:b], *[:c]] += 10).should == 20
+ a[:a, :b, :c].should == 20
+ end
+ end
+ end
+
+ describe 'using compounded constants' do
+ it 'causes side-effects of the module part to be applied only once (when assigns)' do
+ module ConstantSpecs
+ OpAssignTrue = 1
+ end
+
+ suppress_warning do # already initialized constant
+ x = 0
+ (x += 1; ConstantSpecs)::OpAssignTrue += 2
+ x.should == 1
+ ConstantSpecs::OpAssignTrue.should == 3
+ end
+
+ ConstantSpecs.send :remove_const, :OpAssignTrue
+ end
+ end
+ end
+end
+
+# generic cases
+describe 'Multiple assignments' do
+ it 'assigns multiple targets when assignment with an accessor' do
+ object = Object.new
+ class << object
+ attr_accessor :a, :b
+ end
+
+ object.a, object.b = :a, :b
+
+ object.a.should == :a
+ object.b.should == :b
+ end
+
+ it 'assigns multiple targets when assignment with a nested accessor' do
+ object = Object.new
+ class << object
+ attr_accessor :a, :b
+ end
+
+ (object.a, object.b), c = [:a, :b], nil
+
+ object.a.should == :a
+ object.b.should == :b
+ end
+
+ it 'assigns multiple targets when assignment with a #[]=' do
+ object = Object.new
+ class << object
+ def []=(k, v) (@h ||= {})[k] = v; end
+ def [](k) (@h ||= {})[k]; end
+ end
+
+ object[:a], object[:b] = :a, :b
+
+ object[:a].should == :a
+ object[:b].should == :b
+ end
+
+ it 'assigns multiple targets when assignment with a nested #[]=' do
+ object = Object.new
+ class << object
+ def []=(k, v) (@h ||= {})[k] = v; end
+ def [](k) (@h ||= {})[k]; end
+ end
+
+ (object[:a], object[:b]), c = [:v1, :v2], nil
+
+ object[:a].should == :v1
+ object[:b].should == :v2
+ end
+
+ it 'assigns multiple targets when assignment with compounded constant' do
+ m = Module.new
+
+ m::A, m::B = :a, :b
+
+ m::A.should == :a
+ m::B.should == :b
+ end
+
+ it 'assigns multiple targets when assignment with a nested compounded constant' do
+ m = Module.new
+
+ (m::A, m::B), c = [:a, :b], nil
+
+ m::A.should == :a
+ m::B.should == :b
+ end
+end
+
+describe 'Multiple assignments' do
+ describe 'evaluation order' do
+ it 'evaluates expressions left to right when assignment with an accessor' do
+ object = Object.new
+ def object.a=(value) end
+ ScratchPad.record []
+
+ (ScratchPad << :a; object).a, (ScratchPad << :b; object).a = (ScratchPad << :c; :c), (ScratchPad << :d; :d)
+ ScratchPad.recorded.should == [:a, :b, :c, :d]
+ end
+
+ it 'evaluates expressions left to right when assignment with a nested accessor' do
+ object = Object.new
+ def object.a=(value) end
+ ScratchPad.record []
+
+ ((ScratchPad << :a; object).a, foo), bar = [(ScratchPad << :b; :b)]
+ ScratchPad.recorded.should == [:a, :b]
+ end
+
+ it 'evaluates expressions left to right when assignment with a deeply nested accessor' do
+ o = Object.new
+ def o.a=(value) end
+ def o.b=(value) end
+ def o.c=(value) end
+ def o.d=(value) end
+ def o.e=(value) end
+ def o.f=(value) end
+ ScratchPad.record []
+
+ (ScratchPad << :a; o).a,
+ ((ScratchPad << :b; o).b,
+ ((ScratchPad << :c; o).c, (ScratchPad << :d; o).d),
+ (ScratchPad << :e; o).e),
+ (ScratchPad << :f; o).f = (ScratchPad << :value; :value)
+
+ ScratchPad.recorded.should == [:a, :b, :c, :d, :e, :f, :value]
+ end
+
+ it 'evaluates expressions left to right when assignment with a #[]=' do
+ object = Object.new
+ def object.[]=(_, _) end
+ ScratchPad.record []
+
+ (ScratchPad << :a; object)[(ScratchPad << :b; :b)], (ScratchPad << :c; object)[(ScratchPad << :d; :d)] = (ScratchPad << :e; :e), (ScratchPad << :f; :f)
+ ScratchPad.recorded.should == [:a, :b, :c, :d, :e, :f]
+ end
+
+ it 'evaluates expressions left to right when assignment with a nested #[]=' do
+ object = Object.new
+ def object.[]=(_, _) end
+ ScratchPad.record []
+
+ ((ScratchPad << :a; object)[(ScratchPad << :b; :b)], foo), bar = [(ScratchPad << :c; :c)]
+ ScratchPad.recorded.should == [:a, :b, :c]
+ end
+
+ it 'evaluates expressions left to right when assignment with a deeply nested #[]=' do
+ o = Object.new
+ def o.[]=(_, _) end
+ ScratchPad.record []
+
+ (ScratchPad << :ra; o)[(ScratchPad << :aa; :aa)],
+ ((ScratchPad << :rb; o)[(ScratchPad << :ab; :ab)],
+ ((ScratchPad << :rc; o)[(ScratchPad << :ac; :ac)], (ScratchPad << :rd; o)[(ScratchPad << :ad; :ad)]),
+ (ScratchPad << :re; o)[(ScratchPad << :ae; :ae)]),
+ (ScratchPad << :rf; o)[(ScratchPad << :af; :af)] = (ScratchPad << :value; :value)
+
+ ScratchPad.recorded.should == [:ra, :aa, :rb, :ab, :rc, :ac, :rd, :ad, :re, :ae, :rf, :af, :value]
+ end
+
+ it 'evaluates expressions left to right when assignment with compounded constant' do
+ m = Module.new
+ ScratchPad.record []
+
+ (ScratchPad << :a; m)::A, (ScratchPad << :b; m)::B = (ScratchPad << :c; :c), (ScratchPad << :d; :d)
+ ScratchPad.recorded.should == [:a, :b, :c, :d]
+ end
+
+ it 'evaluates expressions left to right when assignment with a nested compounded constant' do
+ m = Module.new
+ ScratchPad.record []
+
+ ((ScratchPad << :a; m)::A, foo), bar = [(ScratchPad << :b; :b)]
+ ScratchPad.recorded.should == [:a, :b]
+ end
+
+ it 'evaluates expressions left to right when assignment with deeply nested compounded constants' do
+ m = Module.new
+ ScratchPad.record []
+
+ (ScratchPad << :a; m)::A,
+ ((ScratchPad << :b; m)::B,
+ ((ScratchPad << :c; m)::C, (ScratchPad << :d; m)::D),
+ (ScratchPad << :e; m)::E),
+ (ScratchPad << :f; m)::F = (ScratchPad << :value; :value)
+
+ ScratchPad.recorded.should == [:a, :b, :c, :d, :e, :f, :value]
+ end
+ end
+
+ context 'when assignment with method call and receiver is self' do
+ it 'assigns values correctly when assignment with accessor' do
+ object = Object.new
+ class << object
+ attr_accessor :a, :b
+
+ def assign(v1, v2)
+ self.a, self.b = v1, v2
+ end
+ end
+
+ object.assign :v1, :v2
+ object.a.should == :v1
+ object.b.should == :v2
+ end
+
+ it 'evaluates expressions right to left when assignment with a nested accessor' do
+ object = Object.new
+ class << object
+ attr_accessor :a, :b
+
+ def assign(v1, v2)
+ (self.a, self.b), c = [v1, v2], nil
+ end
+ end
+
+ object.assign :v1, :v2
+ object.a.should == :v1
+ object.b.should == :v2
+ end
+
+ it 'assigns values correctly when assignment with a #[]=' do
+ object = Object.new
+ class << object
+ def []=(key, v)
+ @h ||= {}
+ @h[key] = v
+ end
+
+ def [](key)
+ (@h || {})[key]
+ end
+
+ def assign(k1, v1, k2, v2)
+ self[k1], self[k2] = v1, v2
+ end
+ end
+
+ object.assign :k1, :v1, :k2, :v2
+ object[:k1].should == :v1
+ object[:k2].should == :v2
+ end
+
+ it 'assigns values correctly when assignment with a nested #[]=' do
+ object = Object.new
+ class << object
+ def []=(key, v)
+ @h ||= {}
+ @h[key] = v
+ end
+
+ def [](key)
+ (@h || {})[key]
+ end
+
+ def assign(k1, v1, k2, v2)
+ (self[k1], self[k2]), c = [v1, v2], nil
+ end
+ end
+
+ object.assign :k1, :v1, :k2, :v2
+ object[:k1].should == :v1
+ object[:k2].should == :v2
+ end
+
+ it 'assigns values correctly when assignment with compounded constant' do
+ m = Module.new
+ m.module_exec do
+ self::A, self::B = :v1, :v2
+ end
+
+ m::A.should == :v1
+ m::B.should == :v2
+ end
+
+ it 'assigns values correctly when assignment with a nested compounded constant' do
+ m = Module.new
+ m.module_exec do
+ (self::A, self::B), c = [:v1, :v2], nil
+ end
+
+ m::A.should == :v1
+ m::B.should == :v2
+ end
+ end
+end
diff --git a/spec/ruby/language/block_spec.rb b/spec/ruby/language/block_spec.rb
index 90329e2f6f..cc003b8946 100644
--- a/spec/ruby/language/block_spec.rb
+++ b/spec/ruby/language/block_spec.rb
@@ -40,17 +40,40 @@ describe "A block yielded a single" do
m([1, 2]) { |a=5, b, c, d| [a, b, c, d] }.should == [5, 1, 2, nil]
end
- ruby_version_is "3.2" do
- it "does not autosplat single argument to required arguments when a keyword rest argument is present" do
- m([1, 2]) { |a, **k| [a, k] }.should == [[1, 2], {}]
- end
+ it "assigns elements to pre arguments" do
+ m([1, 2]) { |a, b, c, d=5| [a, b, c, d] }.should == [1, 2, nil, 5]
end
- ruby_version_is ''..."3.2" do
- # https://bugs.ruby-lang.org/issues/18633
- it "autosplats single argument to required arguments when a keyword rest argument is present" do
- m([1, 2]) { |a, **k| [a, k] }.should == [1, {}]
- end
+ it "assigns elements to pre and post arguments" do
+ m([1 ]) { |a, b=5, c=6, d, e| [a, b, c, d, e] }.should == [1, 5, 6, nil, nil]
+ m([1, 2 ]) { |a, b=5, c=6, d, e| [a, b, c, d, e] }.should == [1, 5, 6, 2, nil]
+ m([1, 2, 3 ]) { |a, b=5, c=6, d, e| [a, b, c, d, e] }.should == [1, 5, 6, 2, 3]
+ m([1, 2, 3, 4 ]) { |a, b=5, c=6, d, e| [a, b, c, d, e] }.should == [1, 2, 6, 3, 4]
+ m([1, 2, 3, 4, 5 ]) { |a, b=5, c=6, d, e| [a, b, c, d, e] }.should == [1, 2, 3, 4, 5]
+ m([1, 2, 3, 4, 5, 6]) { |a, b=5, c=6, d, e| [a, b, c, d, e] }.should == [1, 2, 3, 4, 5]
+ end
+
+ it "assigns elements to pre and post arguments when *rest is present" do
+ m([1 ]) { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.should == [1, 5, 6, [], nil, nil]
+ m([1, 2 ]) { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.should == [1, 5, 6, [], 2, nil]
+ m([1, 2, 3 ]) { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.should == [1, 5, 6, [], 2, 3]
+ m([1, 2, 3, 4 ]) { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.should == [1, 2, 6, [], 3, 4]
+ m([1, 2, 3, 4, 5 ]) { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.should == [1, 2, 3, [], 4, 5]
+ m([1, 2, 3, 4, 5, 6]) { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.should == [1, 2, 3, [4], 5, 6]
+ end
+
+ it "does not autosplat single argument to required arguments when a keyword rest argument is present" do
+ m([1, 2]) { |a, **k| [a, k] }.should == [[1, 2], {}]
+ end
+
+ it "does not autosplat single argument to required arguments when keyword arguments are present" do
+ m([1, 2]) { |a, b: :b, c: :c| [a, b, c] }.should == [[1, 2], :b, :c]
+ end
+
+ it "raises error when required keyword arguments are present" do
+ -> {
+ m([1, 2]) { |a, b:, c:| [a, b, c] }
+ }.should raise_error(ArgumentError, "missing keywords: :b, :c")
end
it "assigns elements to mixed argument types" do
@@ -252,7 +275,7 @@ describe "A block" do
end
it "may include a rescue clause" do
- eval("@y.z do raise ArgumentError; rescue ArgumentError; 7; end").should == 7
+ @y.z do raise ArgumentError; rescue ArgumentError; 7; end.should == 7
end
end
@@ -266,7 +289,7 @@ describe "A block" do
end
it "may include a rescue clause" do
- eval('@y.z do || raise ArgumentError; rescue ArgumentError; 7; end').should == 7
+ @y.z do || raise ArgumentError; rescue ArgumentError; 7; end.should == 7
end
end
@@ -295,7 +318,7 @@ describe "A block" do
end
it "may include a rescue clause" do
- eval('@y.s(1) do |x| raise ArgumentError; rescue ArgumentError; 7; end').should == 7
+ @y.s(1) do |x| raise ArgumentError; rescue ArgumentError; 7; end.should == 7
end
end
@@ -368,7 +391,6 @@ describe "A block" do
-> { @y.s(obj) { |a, b| } }.should raise_error(ZeroDivisionError)
end
-
end
describe "taking |a, *b| arguments" do
@@ -696,9 +718,45 @@ describe "A block" do
end
it "accepts unnamed arguments" do
- eval("lambda { |_,_| }").should be_an_instance_of(Proc)
- eval("->(_,_) {}").should be_an_instance_of(Proc)
- eval("Proc.new { |_,_| }").should be_an_instance_of(Proc)
+ lambda { |_,_| }.should be_an_instance_of(Proc) # rubocop:disable Style/Lambda
+ -> _,_ {}.should be_an_instance_of(Proc)
+ Proc.new { |_,_| }.should be_an_instance_of(Proc)
+ end
+ end
+
+ describe 'pre and post parameters' do
+ it "assigns nil to unassigned required arguments" do
+ proc { |a, *b, c, d| [a, b, c, d] }.call(1, 2).should == [1, [], 2, nil]
+ end
+
+ it "assigns elements to optional arguments" do
+ proc { |a=5, b=4, c=3| [a, b, c] }.call(1, 2).should == [1, 2, 3]
+ end
+
+ it "assigns elements to post arguments" do
+ proc { |a=5, b, c, d| [a, b, c, d] }.call(1, 2).should == [5, 1, 2, nil]
+ end
+
+ it "assigns elements to pre arguments" do
+ proc { |a, b, c, d=5| [a, b, c, d] }.call(1, 2).should == [1, 2, nil, 5]
+ end
+
+ it "assigns elements to pre and post arguments" do
+ proc { |a, b=5, c=6, d, e| [a, b, c, d, e] }.call(1 ).should == [1, 5, 6, nil, nil]
+ proc { |a, b=5, c=6, d, e| [a, b, c, d, e] }.call(1, 2 ).should == [1, 5, 6, 2, nil]
+ proc { |a, b=5, c=6, d, e| [a, b, c, d, e] }.call(1, 2, 3 ).should == [1, 5, 6, 2, 3]
+ proc { |a, b=5, c=6, d, e| [a, b, c, d, e] }.call(1, 2, 3, 4 ).should == [1, 2, 6, 3, 4]
+ proc { |a, b=5, c=6, d, e| [a, b, c, d, e] }.call(1, 2, 3, 4, 5 ).should == [1, 2, 3, 4, 5]
+ proc { |a, b=5, c=6, d, e| [a, b, c, d, e] }.call(1, 2, 3, 4, 5, 6).should == [1, 2, 3, 4, 5]
+ end
+
+ it "assigns elements to pre and post arguments when *rest is present" do
+ proc { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.call(1 ).should == [1, 5, 6, [], nil, nil]
+ proc { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.call(1, 2 ).should == [1, 5, 6, [], 2, nil]
+ proc { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.call(1, 2, 3 ).should == [1, 5, 6, [], 2, 3]
+ proc { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.call(1, 2, 3, 4 ).should == [1, 2, 6, [], 3, 4]
+ proc { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.call(1, 2, 3, 4, 5 ).should == [1, 2, 3, [], 4, 5]
+ proc { |a, b=5, c=6, *d, e, f| [a, b, c, d, e, f] }.call(1, 2, 3, 4, 5, 6).should == [1, 2, 3, [4], 5, 6]
end
end
end
@@ -883,24 +941,27 @@ describe "Post-args" do
end
describe "with a circular argument reference" do
- it "raises a SyntaxError if using an existing local with the same name as the argument" do
- a = 1
- -> {
- @proc = eval "proc { |a=a| a }"
- }.should raise_error(SyntaxError)
+ ruby_version_is ""..."3.4" do
+ it "raises a SyntaxError if using the argument in its default value" do
+ a = 1
+ -> {
+ eval "proc { |a=a| a }"
+ }.should raise_error(SyntaxError)
+ end
end
- it "raises a SyntaxError if there is an existing method with the same name as the argument" do
- def a; 1; end
- -> {
- @proc = eval "proc { |a=a| a }"
- }.should raise_error(SyntaxError)
+ ruby_version_is "3.4" do
+ it "is nil if using the argument in its default value" do
+ -> {
+ eval "proc { |a=a| a }.call"
+ }.call.should == nil
+ end
end
+ end
- it "calls an existing method with the same name as the argument if explicitly using ()" do
- def a; 1; end
- proc { |a=a()| a }.call.should == 1
- end
+ it "calls an existing method with the same name as the argument if explicitly using ()" do
+ def a; 1; end
+ proc { |a=a()| a }.call.should == 1
end
end
@@ -919,76 +980,159 @@ describe "Post-args" do
end
end
+# tested more thoroughly in language/delegation_spec.rb
describe "Anonymous block forwarding" do
- ruby_version_is "3.1" do
- it "forwards blocks to other functions that formally declare anonymous blocks" do
- eval <<-EOF
- def b(&); c(&) end
- def c(&); yield :non_null end
- EOF
+ it "forwards blocks to other method that formally declares anonymous block" do
+ def b(&); c(&) end
+ def c(&); yield :non_null end
+
+ b { |c| c }.should == :non_null
+ end
- b { |c| c }.should == :non_null
+ it "requires the anonymous block parameter to be declared if directly passing a block" do
+ -> { eval "def a; b(&); end; def b; end" }.should raise_error(SyntaxError)
+ end
+
+ it "works when it's the only declared parameter" do
+ def inner; yield end
+ def block_only(&); inner(&) end
+
+ block_only { 1 }.should == 1
+ end
+
+ it "works alongside positional parameters" do
+ def inner; yield end
+ def pos(arg1, &); inner(&) end
+
+ pos(:a) { 1 }.should == 1
+ end
+
+ it "works alongside positional arguments and splatted keyword arguments" do
+ def inner; yield end
+ def pos_kwrest(arg1, **kw, &); inner(&) end
+
+ pos_kwrest(:a, arg: 3) { 1 }.should == 1
+ end
+
+ it "works alongside positional arguments and disallowed keyword arguments" do
+ def inner; yield end
+ def no_kw(arg1, **nil, &); inner(&) end
+
+ no_kw(:a) { 1 }.should == 1
+ end
+
+ it "works alongside explicit keyword arguments" do
+ eval <<-EOF
+ def inner; yield end
+ def rest_kw(*a, kwarg: 1, &); inner(&) end
+ def kw(kwarg: 1, &); inner(&) end
+ def pos_kw_kwrest(arg1, kwarg: 1, **kw, &); inner(&) end
+ def pos_rkw(arg1, kwarg1:, &); inner(&) end
+ def all(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, &); inner(&) end
+ def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, **kw, &); inner(&) end
+ EOF
+
+ rest_kw { 1 }.should == 1
+ kw { 1 }.should == 1
+ pos_kw_kwrest(:a) { 1 }.should == 1
+ pos_rkw(:a, kwarg1: 3) { 1 }.should == 1
+ all(:a, :b, :c, :d, :e, okw1: 'x', okw2: 'y') { 1 }.should == 1
+ all_kwrest(:a, :b, :c, :d, :e, okw1: 'x', okw2: 'y') { 1 }.should == 1
+ end
+end
+
+describe "`it` calls without arguments in a block with no ordinary parameters" do
+ ruby_version_is "3.3"..."3.4" do
+ it "emits a deprecation warning" do
+ -> {
+ eval "proc { it }"
+ }.should complain(/warning: `it` calls without arguments will refer to the first block param in Ruby 3.4; use it\(\) or self.it/)
end
- it "requires the anonymous block parameter to be declared if directly passing a block" do
- -> { eval "def a; b(&); end; def b; end" }.should raise_error(SyntaxError)
+ it "emits a deprecation warning if numbered parameters are used" do
+ -> {
+ eval "proc { it; _1 }"
+ }.should complain(/warning: `it` calls without arguments will refer to the first block param in Ruby 3.4; use it\(\) or self.it/)
end
- it "works when it's the only declared parameter" do
- eval <<-EOF
- def inner; yield end
- def block_only(&); inner(&) end
- EOF
+ it "does not emit a deprecation warning when a block has parameters" do
+ -> { eval "proc { |a, b| it }" }.should_not complain
+ -> { eval "proc { |*rest| it }" }.should_not complain
+ -> { eval "proc { |*| it }" }.should_not complain
+ -> { eval "proc { |a:, b:| it }" }.should_not complain
+ -> { eval "proc { |**kw| it }" }.should_not complain
+ -> { eval "proc { |**| it }" }.should_not complain
+ -> { eval "proc { |&block| it }" }.should_not complain
+ -> { eval "proc { |&| it }" }.should_not complain
+ -> { eval "proc { || it }" }.should_not complain
+ end
- block_only { 1 }.should == 1
+ it "does not emit a deprecation warning when `it` calls with arguments" do
+ -> { eval "proc { it(42) }" }.should_not complain
+ -> { eval "proc { it 42 }" }.should_not complain
end
- it "works alongside positional parameters" do
- eval <<-EOF
- def inner; yield end
- def pos(arg1, &); inner(&) end
- EOF
+ it "does not emit a deprecation warning when `it` calls with a block" do
+ -> { eval "proc { it {} }" }.should_not complain
+ end
- pos(:a) { 1 }.should == 1
+ it "does not emit a deprecation warning when a local variable inside the block named `it` exists" do
+ -> { eval "proc { it = 42; it }" }.should_not complain
end
- it "works alongside positional arguments and splatted keyword arguments" do
- eval <<-EOF
- def inner; yield end
- def pos_kwrest(arg1, **kw, &); inner(&) end
- EOF
+ it "does not emit a deprecation warning when `it` calls with explicit empty arguments list" do
+ -> { eval "proc { it() }" }.should_not complain
+ end
- pos_kwrest(:a, arg: 3) { 1 }.should == 1
+ it "calls the method `it` if defined" do
+ o = Object.new
+ def o.it
+ 21
+ end
+ suppress_warning do
+ o.instance_eval("proc { it * 2 }").call(1).should == 42
+ end
end
+ end
- it "works alongside positional arguments and disallowed keyword arguments" do
- eval <<-EOF
- def inner; yield end
- def no_kw(arg1, **nil, &); inner(&) end
- EOF
+ ruby_version_is "3.4" do
+ it "does not emit a deprecation warning" do
+ -> {
+ eval "proc { it }"
+ }.should_not complain
+ end
- no_kw(:a) { 1 }.should == 1
+ it "acts as the first argument if no local variables exist" do
+ eval("proc { it * 2 }").call(5).should == 10
+ end
+
+ it "can be reassigned to act as a local variable" do
+ eval("proc { tmp = it; it = tmp * 2; it }").call(21).should == 42
+ end
+
+ it "can be used in nested calls" do
+ eval("proc { it.map { it * 2 } }").call([1, 2, 3]).should == [2, 4, 6]
end
- end
- ruby_version_is "3.2" do
- it "works alongside explicit keyword arguments" do
- eval <<-EOF
- def inner; yield end
- def rest_kw(*a, kwarg: 1, &); inner(&) end
- def kw(kwarg: 1, &); inner(&) end
- def pos_kw_kwrest(arg1, kwarg: 1, **kw, &); inner(&) end
- def pos_rkw(arg1, kwarg1:, &); inner(&) end
- def all(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, &); inner(&) end
- def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, **kw, &); inner(&) end
- EOF
+ it "cannot be mixed with numbered parameters" do
+ -> {
+ eval "proc { it + _1 }"
+ }.should raise_error(SyntaxError, /numbered parameters are not allowed when 'it' is already used|'it' is already used in/)
- rest_kw { 1 }.should == 1
- kw { 1 }.should == 1
- pos_kw_kwrest(:a) { 1 }.should == 1
- pos_rkw(:a, kwarg1: 3) { 1 }.should == 1
- all(:a, :b, :c, :d, :e, okw1: 'x', okw2: 'y') { 1 }.should == 1
- all_kwrest(:a, :b, :c, :d, :e, okw1: 'x', okw2: 'y') { 1 }.should == 1
+ -> {
+ eval "proc { _1 + it }"
+ }.should raise_error(SyntaxError, /numbered parameter is already used in|'it' is not allowed when a numbered parameter is already used/)
end
end
end
+
+describe "if `it` is defined as a variable" do
+ it "treats `it` as a captured variable if defined outside of a block" do
+ it = 5
+ proc { it }.call(0).should == 5
+ end
+
+ it "treats `it` as a local variable if defined inside of a block" do
+ proc { it = 5; it }.call(0).should == 5
+ end
+end
diff --git a/spec/ruby/language/break_spec.rb b/spec/ruby/language/break_spec.rb
index 627cb4a071..7e5b6fb328 100644
--- a/spec/ruby/language/break_spec.rb
+++ b/spec/ruby/language/break_spec.rb
@@ -252,6 +252,25 @@ describe "Break inside a while loop" do
end
end
+describe "The break statement in a method" do
+ it "is invalid and raises a SyntaxError" do
+ -> {
+ eval("def m; break; end")
+ }.should raise_error(SyntaxError)
+ end
+end
+
+describe "The break statement in a module literal" do
+ it "is invalid and raises a SyntaxError" do
+ code = <<~RUBY
+ module BreakSpecs:ModuleWithBreak
+ break
+ end
+ RUBY
+
+ -> { eval(code) }.should raise_error(SyntaxError)
+ end
+end
# TODO: Rewrite all the specs from here to the end of the file in the style
# above.
@@ -372,7 +391,7 @@ describe "Executing break from within a block" do
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 }
diff --git a/spec/ruby/language/case_spec.rb b/spec/ruby/language/case_spec.rb
index 58e1aaed96..464d06e46a 100644
--- a/spec/ruby/language/case_spec.rb
+++ b/spec/ruby/language/case_spec.rb
@@ -329,49 +329,6 @@ describe "The 'case'-construct" do
100
end.should == 100
end
-end
-
-describe "The 'case'-construct with no target expression" do
- it "evaluates the body of the first clause when at least one of its condition expressions is true" do
- case
- when true, false; 'foo'
- end.should == 'foo'
- end
-
- it "evaluates the body of the first when clause that is not false/nil" do
- case
- when false; 'foo'
- when 2; 'bar'
- when 1 == 1; 'baz'
- end.should == 'bar'
-
- case
- when false; 'foo'
- when nil; 'foo'
- when 1 == 1; 'bar'
- end.should == 'bar'
- end
-
- it "evaluates the body of the else clause if all when clauses are false/nil" do
- case
- when false; 'foo'
- when nil; 'foo'
- when 1 == 2; 'bar'
- else 'baz'
- end.should == 'baz'
- end
-
- it "evaluates multiple conditional expressions as a boolean disjunction" do
- case
- when true, false; 'foo'
- else 'bar'
- end.should == 'foo'
-
- case
- when false, true; 'foo'
- else 'bar'
- end.should == 'foo'
- end
it "evaluates true as only 'true' when true is the first clause" do
case 1
@@ -443,6 +400,95 @@ describe "The 'case'-construct with no target expression" do
end.should == :called
end
+ it "supports declaring variables in the case target expression" do
+ def test(v)
+ case new_variable_in_expression = v
+ when true
+ # This extra block is a test that `new_variable_in_expression` is declared outside of it and not inside
+ self.then { new_variable_in_expression }
+ else
+ # Same
+ self.then { new_variable_in_expression.casecmp?("foo") }
+ end
+ end
+
+ self.test("bar").should == false
+ self.test(true).should == true
+ end
+
+ ruby_version_is ""..."3.4" do
+ it "warns if there are identical when clauses" do
+ -> {
+ eval <<~RUBY
+ case 1
+ when 2
+ :foo
+ when 2
+ :bar
+ end
+ RUBY
+ }.should complain(/warning: (duplicated .when' clause with line \d+ is ignored|'when' clause on line \d+ duplicates 'when' clause on line \d+ and is ignored)/, verbose: true)
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "warns if there are identical when clauses" do
+ -> {
+ eval <<~RUBY
+ case 1
+ when 2
+ :foo
+ when 2
+ :bar
+ end
+ RUBY
+ }.should complain(/warning: 'when' clause on line \d+ duplicates 'when' clause on line \d+ and is ignored/, verbose: true)
+ end
+ end
+end
+
+describe "The 'case'-construct with no target expression" do
+ it "evaluates the body of the first clause when at least one of its condition expressions is true" do
+ case
+ when true, false; 'foo'
+ end.should == 'foo'
+ end
+
+ it "evaluates the body of the first when clause that is not false/nil" do
+ case
+ when false; 'foo'
+ when 2; 'bar'
+ when 1 == 1; 'baz'
+ end.should == 'bar'
+
+ case
+ when false; 'foo'
+ when nil; 'foo'
+ when 1 == 1; 'bar'
+ end.should == 'bar'
+ end
+
+ it "evaluates the body of the else clause if all when clauses are false/nil" do
+ case
+ when false; 'foo'
+ when nil; 'foo'
+ when 1 == 2; 'bar'
+ else 'baz'
+ end.should == 'baz'
+ end
+
+ it "evaluates multiple conditional expressions as a boolean disjunction" do
+ case
+ when true, false; 'foo'
+ else 'bar'
+ end.should == 'foo'
+
+ case
+ when false, true; 'foo'
+ else 'bar'
+ end.should == 'foo'
+ end
+
# Homogeneous cases are often optimized to avoid === using a jump table, and should be tested separately.
# See https://github.com/jruby/jruby/issues/6440
it "handles homogeneous cases" do
@@ -451,4 +497,13 @@ describe "The 'case'-construct with no target expression" do
when 2; 'bar'
end.should == 'foo'
end
+
+ it "expands arrays to lists of values" do
+ case
+ when *[false]
+ "foo"
+ when *[true]
+ "bar"
+ end.should == "bar"
+ end
end
diff --git a/spec/ruby/language/class_spec.rb b/spec/ruby/language/class_spec.rb
index eab3cd0651..6fb785fd56 100644
--- a/spec/ruby/language/class_spec.rb
+++ b/spec/ruby/language/class_spec.rb
@@ -46,7 +46,14 @@ describe "A class definition" do
-> {
class ClassSpecsNumber
end
- }.should raise_error(TypeError)
+ }.should raise_error(TypeError, /\AClassSpecsNumber is not a class/)
+ end
+
+ it "raises TypeError if constant given as class name exists and is a Module but not a Class" do
+ -> {
+ class ClassSpecs
+ end
+ }.should raise_error(TypeError, /\AClassSpecs is not a class/)
end
# test case known to be detecting bugs (JRuby, MRI)
@@ -271,6 +278,8 @@ describe "A class definition" do
AnonWithConstant.name.should == 'AnonWithConstant'
klass.get_class_name.should == 'AnonWithConstant'
+ ensure
+ Object.send(:remove_const, :AnonWithConstant)
end
end
end
@@ -344,6 +353,39 @@ describe "Reopening a class" do
ClassSpecs::M.m.should == 1
ClassSpecs::L.singleton_class.send(:remove_method, :m)
end
+
+ it "does not reopen a class included in Object" do
+ ruby_exe(<<~RUBY).should == "false"
+ module IncludedInObject
+ class IncludedClass
+ end
+ end
+ class Object
+ include IncludedInObject
+ end
+ class IncludedClass
+ end
+ print IncludedInObject::IncludedClass == Object::IncludedClass
+ RUBY
+ end
+
+ it "does not reopen a class included in non-Object modules" do
+ ruby_exe(<<~RUBY).should == "false/false"
+ module Included
+ module IncludedClass; end
+ end
+ module M
+ include Included
+ module IncludedClass; end
+ end
+ class C
+ include Included
+ module IncludedClass; end
+ end
+ print Included::IncludedClass == M::IncludedClass, "/",
+ Included::IncludedClass == C::IncludedClass
+ RUBY
+ end
end
describe "class provides hooks" do
diff --git a/spec/ruby/language/constants_spec.rb b/spec/ruby/language/constants_spec.rb
index 08c534487e..063c52c422 100644
--- a/spec/ruby/language/constants_spec.rb
+++ b/spec/ruby/language/constants_spec.rb
@@ -72,39 +72,60 @@ describe "Literal (A::X) constant resolution" do
ConstantSpecs::ModuleA::CS_CONST101 = :const101_5
ConstantSpecs::ModuleA::CS_CONST101.should == :const101_5
+ ensure
+ ConstantSpecs::ClassB.send(:remove_const, :CS_CONST101)
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST101)
+ ConstantSpecs::ContainerB.send(:remove_const, :CS_CONST101)
+ ConstantSpecs::ContainerB::ChildB.send(:remove_const, :CS_CONST101)
+ ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST101)
end
it "searches a module included in the immediate class before the superclass" do
ConstantSpecs::ParentB::CS_CONST102 = :const102_1
ConstantSpecs::ModuleF::CS_CONST102 = :const102_2
ConstantSpecs::ContainerB::ChildB::CS_CONST102.should == :const102_2
+ ensure
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST102)
+ ConstantSpecs::ModuleF.send(:remove_const, :CS_CONST102)
end
it "searches the superclass before a module included in the superclass" do
ConstantSpecs::ModuleE::CS_CONST103 = :const103_1
ConstantSpecs::ParentB::CS_CONST103 = :const103_2
ConstantSpecs::ContainerB::ChildB::CS_CONST103.should == :const103_2
+ ensure
+ ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST103)
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST103)
end
it "searches a module included in the superclass" do
ConstantSpecs::ModuleA::CS_CONST104 = :const104_1
ConstantSpecs::ModuleE::CS_CONST104 = :const104_2
ConstantSpecs::ContainerB::ChildB::CS_CONST104.should == :const104_2
+ ensure
+ ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST104)
+ ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST104)
end
it "searches the superclass chain" do
ConstantSpecs::ModuleA::CS_CONST105 = :const105
ConstantSpecs::ContainerB::ChildB::CS_CONST105.should == :const105
+ ensure
+ ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST105)
end
it "searches Object if no class or module qualifier is given" do
CS_CONST106 = :const106
CS_CONST106.should == :const106
+ ensure
+ Object.send(:remove_const, :CS_CONST106)
end
it "searches Object if a toplevel qualifier (::X) is given" do
::CS_CONST107 = :const107
::CS_CONST107.should == :const107
+ ensure
+ Object.send(:remove_const, :CS_CONST107)
end
it "does not search the singleton class of the class or module" do
@@ -123,6 +144,9 @@ describe "Literal (A::X) constant resolution" do
end
-> { ConstantSpecs::CS_CONST108 }.should raise_error(NameError)
+ ensure
+ ConstantSpecs::ContainerB::ChildB.singleton_class.send(:remove_const, :CS_CONST108)
+ ConstantSpecs.singleton_class.send(:remove_const, :CS_CONST108)
end
it "returns the updated value when a constant is reassigned" do
@@ -133,36 +157,20 @@ describe "Literal (A::X) constant resolution" do
ConstantSpecs::ClassB::CS_CONST109 = :const109_2
}.should complain(/already initialized constant/)
ConstantSpecs::ClassB::CS_CONST109.should == :const109_2
+ ensure
+ ConstantSpecs::ClassB.send(:remove_const, :CS_CONST109)
end
- ruby_version_is "3.2" do
- it "evaluates left-to-right" do
- mod = Module.new
+ it "evaluates left-to-right" do
+ mod = Module.new
- mod.module_eval <<-EOC
- order = []
- ConstantSpecsRHS = Module.new
- (order << :lhs; ConstantSpecsRHS)::B = (order << :rhs)
- EOC
+ mod.module_eval <<-EOC
+ order = []
+ ConstantSpecsRHS = Module.new
+ (order << :lhs; ConstantSpecsRHS)::B = (order << :rhs)
+ EOC
- mod::ConstantSpecsRHS::B.should == [:lhs, :rhs]
- end
- end
-
- ruby_version_is ""..."3.2" do
- it "evaluates the right hand side before evaluating a constant path" do
- mod = Module.new
-
- mod.module_eval <<-EOC
- ConstantSpecsRHS::B = begin
- module ConstantSpecsRHS; end
-
- "hello"
- end
- EOC
-
- mod::ConstantSpecsRHS::B.should == 'hello'
- end
+ mod::ConstantSpecsRHS::B.should == [:lhs, :rhs]
end
end
@@ -292,6 +300,12 @@ describe "Constant resolution within methods" do
ConstantSpecs::ClassB.new.const201.should == :const201_2
ConstantSpecs::ParentB.new.const201.should == :const201_3
ConstantSpecs::ContainerB::ChildB.new.const201.should == :const201_5
+ ensure
+ ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST201)
+ ConstantSpecs::ClassB.send(:remove_const, :CS_CONST201)
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST201)
+ ConstantSpecs::ContainerB.send(:remove_const, :CS_CONST201)
+ ConstantSpecs::ContainerB::ChildB.send(:remove_const, :CS_CONST201)
end
it "searches a module included in the immediate class before the superclass" do
@@ -300,6 +314,9 @@ describe "Constant resolution within methods" do
ConstantSpecs::ContainerB::ChildB.const202.should == :const202_1
ConstantSpecs::ContainerB::ChildB.new.const202.should == :const202_1
+ ensure
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST202)
+ ConstantSpecs::ContainerB::ChildB.send(:remove_const, :CS_CONST202)
end
it "searches the superclass before a module included in the superclass" do
@@ -308,6 +325,9 @@ describe "Constant resolution within methods" do
ConstantSpecs::ContainerB::ChildB.const203.should == :const203_1
ConstantSpecs::ContainerB::ChildB.new.const203.should == :const203_1
+ ensure
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST203)
+ ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST203)
end
it "searches a module included in the superclass" do
@@ -316,6 +336,9 @@ describe "Constant resolution within methods" do
ConstantSpecs::ContainerB::ChildB.const204.should == :const204_1
ConstantSpecs::ContainerB::ChildB.new.const204.should == :const204_1
+ ensure
+ ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST204)
+ ConstantSpecs::ModuleE.send(:remove_const, :CS_CONST204)
end
it "searches the superclass chain" do
@@ -323,6 +346,8 @@ describe "Constant resolution within methods" do
ConstantSpecs::ContainerB::ChildB.const205.should == :const205
ConstantSpecs::ContainerB::ChildB.new.const205.should == :const205
+ ensure
+ ConstantSpecs::ModuleA.send(:remove_const, :CS_CONST205)
end
it "searches the lexical scope of the method not the receiver's immediate class" do
@@ -334,6 +359,9 @@ describe "Constant resolution within methods" do
end
ConstantSpecs::ContainerB::ChildB.const206.should == :const206_1
+ ensure
+ ConstantSpecs::ContainerB::ChildB.send(:remove_const, :CS_CONST206)
+ ConstantSpecs::ContainerB::ChildB.singleton_class.send(:remove_const, :CS_CONST206)
end
it "searches the lexical scope of a singleton method" do
@@ -341,12 +369,17 @@ describe "Constant resolution within methods" do
ConstantSpecs::ClassB::CS_CONST207 = :const207_2
ConstantSpecs::CS_CONST208.const207.should == :const207_1
+ ensure
+ ConstantSpecs.send(:remove_const, :CS_CONST207)
+ ConstantSpecs::ClassB.send(:remove_const, :CS_CONST207)
end
it "does not search the lexical scope of the caller" do
ConstantSpecs::ClassB::CS_CONST209 = :const209
-> { ConstantSpecs::ClassB.const209 }.should raise_error(NameError)
+ ensure
+ ConstantSpecs::ClassB.send(:remove_const, :CS_CONST209)
end
it "searches the lexical scope of a block" do
@@ -354,6 +387,9 @@ describe "Constant resolution within methods" do
ConstantSpecs::ParentB::CS_CONST210 = :const210_2
ConstantSpecs::ClassB.const210.should == :const210_1
+ ensure
+ ConstantSpecs::ClassB.send(:remove_const, :CS_CONST210)
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST210)
end
it "searches Object as a lexical scope only if Object is explicitly opened" do
@@ -364,6 +400,11 @@ describe "Constant resolution within methods" do
Object::CS_CONST212 = :const212_2
ConstantSpecs::ParentB::CS_CONST212 = :const212_1
ConstantSpecs::ContainerB::ChildB.const212.should == :const212_1
+ ensure
+ Object.send(:remove_const, :CS_CONST211)
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST211)
+ Object.send(:remove_const, :CS_CONST212)
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST212)
end
it "returns the updated value when a constant is reassigned" do
@@ -376,6 +417,8 @@ describe "Constant resolution within methods" do
}.should complain(/already initialized constant/)
ConstantSpecs::ContainerB::ChildB.const213.should == :const213_2
ConstantSpecs::ContainerB::ChildB.new.const213.should == :const213_2
+ ensure
+ ConstantSpecs::ParentB.send(:remove_const, :CS_CONST213)
end
it "does not search the lexical scope of qualifying modules" do
@@ -384,6 +427,8 @@ describe "Constant resolution within methods" do
-> do
ConstantSpecs::ContainerB::ChildB.const214
end.should raise_error(NameError)
+ ensure
+ ConstantSpecs::ContainerB.send(:remove_const, :CS_CONST214)
end
end
@@ -484,6 +529,10 @@ describe "Module#private_constant marked constants" do
PrivateModule::X.should == 1
end
+ ensure
+ module ::ConstantVisibility::ModuleContainer
+ PrivateModule.send(:remove_const, :X)
+ end
end
it "can be reopened as a class where constant is not private" do
@@ -494,6 +543,10 @@ describe "Module#private_constant marked constants" do
PrivateClass::X.should == 1
end
+ ensure
+ module ::ConstantVisibility::ModuleContainer
+ PrivateClass.send(:remove_const, :X)
+ end
end
it "is not defined? with A::B form" do
@@ -565,6 +618,10 @@ describe "Module#private_constant marked constants" do
PrivateModule::X.should == 1
end
+ ensure
+ class ::ConstantVisibility::ClassContainer
+ PrivateModule.send(:remove_const, :X)
+ end
end
it "can be reopened as a class where constant is not private" do
@@ -575,6 +632,10 @@ describe "Module#private_constant marked constants" do
PrivateClass::X.should == 1
end
+ ensure
+ class ::ConstantVisibility::ClassContainer
+ PrivateClass.send(:remove_const, :X)
+ end
end
it "is not defined? with A::B form" do
diff --git a/spec/ruby/language/def_spec.rb b/spec/ruby/language/def_spec.rb
index c8531343c0..0cf1790791 100644
--- a/spec/ruby/language/def_spec.rb
+++ b/spec/ruby/language/def_spec.rb
@@ -97,7 +97,8 @@ describe "An instance method" do
def foo; end
end
}.should raise_error(FrozenError) { |e|
- e.message.should.start_with? "can't modify frozen module"
+ msg_class = ruby_version_is("4.0") ? "Module" : "module"
+ e.message.should == "can't modify frozen #{msg_class}: #{e.receiver}"
}
-> {
@@ -106,7 +107,8 @@ describe "An instance method" do
def foo; end
end
}.should raise_error(FrozenError){ |e|
- e.message.should.start_with? "can't modify frozen class"
+ msg_class = ruby_version_is("4.0") ? "Class" : "class"
+ e.message.should == "can't modify frozen #{msg_class}: #{e.receiver}"
}
end
end
@@ -197,15 +199,25 @@ describe "An instance method with a default argument" do
foo(2,3,3).should == [2,3,[3]]
end
- it "raises a SyntaxError when there is an existing method with the same name as the local variable" do
- def bar
- 1
+ ruby_version_is ""..."3.4" do
+ it "raises a SyntaxError if using the argument in its default value" do
+ -> {
+ eval "def foo(bar = bar)
+ bar
+ end"
+ }.should raise_error(SyntaxError)
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "is nil if using the argument in its default value" do
+ -> {
+ eval "def foo(bar = bar)
+ bar
+ end
+ foo"
+ }.call.should == nil
end
- -> {
- eval "def foo(bar = bar)
- bar
- end"
- }.should raise_error(SyntaxError)
end
it "calls a method with the same name as the local when explicitly using ()" do
@@ -238,7 +250,7 @@ describe "A singleton method definition" do
end
it "can be declared for a global variable" do
- $__a__ = "hi"
+ $__a__ = +"hi"
def $__a__.foo
7
end
@@ -273,20 +285,21 @@ describe "A singleton method definition" do
it "raises FrozenError with the correct class name" do
obj = Object.new
obj.freeze
- -> { def obj.foo; end }.should raise_error(FrozenError){ |e|
- e.message.should.start_with? "can't modify frozen object"
- }
+ msg_class = ruby_version_is("4.0") ? "Object" : "object"
+ -> { def obj.foo; end }.should raise_error(FrozenError, "can't modify frozen #{msg_class}: #{obj}")
+ obj = Object.new
c = obj.singleton_class
- -> { def c.foo; end }.should raise_error(FrozenError){ |e|
- e.message.should.start_with? "can't modify frozen Class"
- }
+ c.singleton_class.freeze
+ -> { def c.foo; end }.should raise_error(FrozenError, "can't modify frozen Class: #{c}")
+
+ c = Class.new
+ c.freeze
+ -> { def c.foo; end }.should raise_error(FrozenError, "can't modify frozen Class: #{c}")
m = Module.new
m.freeze
- -> { def m.foo; end }.should raise_error(FrozenError){ |e|
- e.message.should.start_with? "can't modify frozen Module"
- }
+ -> { def m.foo; end }.should raise_error(FrozenError, "can't modify frozen Module: #{m}")
end
end
@@ -514,6 +527,8 @@ describe "A nested method definition" do
obj = DefSpecNested.new
obj.inherited_method.should == obj
+ ensure
+ DefSpecNested.send(:remove_const, :TARGET)
end
# See http://yugui.jp/articles/846#label-3
@@ -535,6 +550,8 @@ describe "A nested method definition" do
DefSpecNested.should_not have_instance_method :arg_method
DefSpecNested.should_not have_instance_method :body_method
+ ensure
+ DefSpecNested.send(:remove_const, :OBJ)
end
it "creates an instance method inside Class.new" do
diff --git a/spec/ruby/language/defined_spec.rb b/spec/ruby/language/defined_spec.rb
index b84fe9394a..80ad1818b1 100644
--- a/spec/ruby/language/defined_spec.rb
+++ b/spec/ruby/language/defined_spec.rb
@@ -116,6 +116,11 @@ describe "The defined? keyword when called with a method name" do
defined?(obj.a_defined_method).should == "method"
end
+ it "returns 'method' for []=" do
+ a = []
+ defined?(a[0] = 1).should == "method"
+ end
+
it "returns nil if the method is not defined" do
obj = DefinedSpecs::Basic.new
defined?(obj.an_undefined_method).should be_nil
@@ -231,6 +236,14 @@ describe "The defined? keyword for an expression" do
defined?(@@defined_specs_x = 2).should == "assignment"
end
+ it "returns 'assignment' for assigning a constant" do
+ defined?(A = 2).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning a fully qualified constant" do
+ defined?(Object::A = 2).should == "assignment"
+ end
+
it "returns 'assignment' for assigning multiple variables" do
defined?((a, b = 1, 2)).should == "assignment"
end
@@ -248,7 +261,27 @@ describe "The defined? keyword for an expression" do
end
it "returns 'assignment' for an expression with '+='" do
- defined?(x += 2).should == "assignment"
+ defined?(a += 1).should == "assignment"
+ defined?(@a += 1).should == "assignment"
+ defined?(@@a += 1).should == "assignment"
+ defined?($a += 1).should == "assignment"
+ defined?(A += 1).should == "assignment"
+ # fully qualified constant check is moved out into a separate test case
+ defined?(a.b += 1).should == "assignment"
+ defined?(a[:b] += 1).should == "assignment"
+ end
+
+ # https://bugs.ruby-lang.org/issues/20111
+ ruby_version_is ""..."3.4" do
+ it "returns 'expression' for an assigning a fully qualified constant with '+='" do
+ defined?(Object::A += 1).should == "expression"
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "returns 'assignment' for an assigning a fully qualified constant with '+='" do
+ defined?(Object::A += 1).should == "assignment"
+ end
end
it "returns 'assignment' for an expression with '*='" do
@@ -279,12 +312,90 @@ describe "The defined? keyword for an expression" do
defined?(x >>= 2).should == "assignment"
end
- it "returns 'assignment' for an expression with '||='" do
- defined?(x ||= 2).should == "assignment"
+ context "||=" do
+ it "returns 'assignment' for assigning a local variable with '||='" do
+ defined?(a ||= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning an instance variable with '||='" do
+ defined?(@a ||= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning a class variable with '||='" do
+ defined?(@@a ||= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning a global variable with '||='" do
+ defined?($a ||= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning a constant with '||='" do
+ defined?(A ||= true).should == "assignment"
+ end
+
+ # https://bugs.ruby-lang.org/issues/20111
+ ruby_version_is ""..."3.4" do
+ it "returns 'expression' for assigning a fully qualified constant with '||='" do
+ defined?(Object::A ||= true).should == "expression"
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "returns 'assignment' for assigning a fully qualified constant with '||='" do
+ defined?(Object::A ||= true).should == "assignment"
+ end
+ end
+
+ it "returns 'assignment' for assigning an attribute with '||='" do
+ defined?(a.b ||= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning a referenced element with '||='" do
+ defined?(a[:b] ||= true).should == "assignment"
+ end
end
- it "returns 'assignment' for an expression with '&&='" do
- defined?(x &&= 2).should == "assignment"
+ context "&&=" do
+ it "returns 'assignment' for assigning a local variable with '&&='" do
+ defined?(a &&= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning an instance variable with '&&='" do
+ defined?(@a &&= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning a class variable with '&&='" do
+ defined?(@@a &&= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning a global variable with '&&='" do
+ defined?($a &&= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning a constant with '&&='" do
+ defined?(A &&= true).should == "assignment"
+ end
+
+ # https://bugs.ruby-lang.org/issues/20111
+ ruby_version_is ""..."3.4" do
+ it "returns 'expression' for assigning a fully qualified constant with '&&='" do
+ defined?(Object::A &&= true).should == "expression"
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "returns 'assignment' for assigning a fully qualified constant with '&&='" do
+ defined?(Object::A &&= true).should == "assignment"
+ end
+ end
+
+ it "returns 'assignment' for assigning an attribute with '&&='" do
+ defined?(a.b &&= true).should == "assignment"
+ end
+
+ it "returns 'assignment' for assigning a referenced element with '&&='" do
+ defined?(a[:b] &&= true).should == "assignment"
+ end
end
it "returns 'assignment' for an expression with '**='" do
@@ -789,6 +900,10 @@ describe "The defined? keyword for a scoped constant" do
defined?(DefinedSpecs::Undefined).should be_nil
end
+ it "returns nil when the constant is not defined and the outer module implements .const_missing" do
+ defined?(DefinedSpecs::ModuleWithConstMissing::Undefined).should be_nil
+ end
+
it "does not call .const_missing if the constant is not defined" do
DefinedSpecs.should_not_receive(:const_missing)
defined?(DefinedSpecs::UnknownChild).should be_nil
diff --git a/spec/ruby/language/delegation_spec.rb b/spec/ruby/language/delegation_spec.rb
index 3f24a79d5c..c711a536c2 100644
--- a/spec/ruby/language/delegation_spec.rb
+++ b/spec/ruby/language/delegation_spec.rb
@@ -1,6 +1,7 @@
require_relative '../spec_helper'
require_relative 'fixtures/delegation'
+# Forwarding anonymous parameters
describe "delegation with def(...)" do
it "delegates rest and kwargs" do
a = Class.new(DelegationSpecs::Target)
@@ -10,10 +11,10 @@ describe "delegation with def(...)" do
end
RUBY
- a.new.delegate(1, b: 2).should == [[1], {b: 2}]
+ a.new.delegate(1, b: 2).should == [[1], {b: 2}, nil]
end
- it "delegates block" do
+ it "delegates a block literal" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate_block(...)
@@ -24,6 +25,18 @@ describe "delegation with def(...)" do
a.new.delegate_block(1, b: 2) { |x| x }.should == [{b: 2}, [1]]
end
+ it "delegates a block argument" do
+ a = Class.new(DelegationSpecs::Target)
+ a.class_eval(<<-RUBY)
+ def delegate(...)
+ target(...)
+ end
+ RUBY
+
+ block = proc {}
+ a.new.delegate(1, b: 2, &block).should == [[1], {b: 2}, block]
+ end
+
it "parses as open endless Range when brackets are omitted" do
a = Class.new(DelegationSpecs::Target)
suppress_warning do
@@ -31,35 +44,115 @@ describe "delegation with def(...)" do
def delegate(...)
target ...
end
- RUBY
- end
+ RUBY
+ end
- a.new.delegate(1, b: 2).should == Range.new([[], {}], nil, true)
+ a.new.delegate(1, b: 2).should == Range.new([[], {}, nil], nil, true)
end
end
-ruby_version_is "2.7.3" do
- describe "delegation with def(x, ...)" do
- it "delegates rest and kwargs" do
- a = Class.new(DelegationSpecs::Target)
- a.class_eval(<<-RUBY)
- def delegate(x, ...)
- target(...)
- end
- RUBY
+describe "delegation with def(x, ...)" do
+ it "delegates rest and kwargs" do
+ a = Class.new(DelegationSpecs::Target)
+ a.class_eval(<<-RUBY)
+ def delegate(x, ...)
+ target(...)
+ end
+ RUBY
+
+ a.new.delegate(0, 1, b: 2).should == [[1], {b: 2}, nil]
+ end
+
+ it "delegates a block literal" do
+ a = Class.new(DelegationSpecs::Target)
+ a.class_eval(<<-RUBY)
+ def delegate_block(x, ...)
+ target_block(...)
+ end
+ RUBY
+
+ a.new.delegate_block(0, 1, b: 2) { |x| x }.should == [{b: 2}, [1]]
+ end
+
+ it "delegates a block argument" do
+ a = Class.new(DelegationSpecs::Target)
+ a.class_eval(<<-RUBY)
+ def delegate(...)
+ target(...)
+ end
+ RUBY
- a.new.delegate(0, 1, b: 2).should == [[1], {b: 2}]
+ block = proc {}
+ a.new.delegate(1, b: 2, &block).should == [[1], {b: 2}, block]
+ end
+end
+
+describe "delegation with def(*)" do
+ it "delegates rest" do
+ a = Class.new(DelegationSpecs::Target)
+ a.class_eval(<<-RUBY)
+ def delegate(*)
+ target(*)
end
+ RUBY
- it "delegates block" do
- a = Class.new(DelegationSpecs::Target)
- a.class_eval(<<-RUBY)
- def delegate_block(x, ...)
- target_block(...)
- end
- RUBY
+ a.new.delegate(0, 1).should == [[0, 1], {}, nil]
+ end
+
+ ruby_version_is "3.3" do
+ context "within a block that accepts anonymous rest within a method that accepts anonymous rest" do
+ it "does not allow delegating rest" do
+ -> {
+ eval "def m(*); proc { |*| n(*) } end"
+ }.should raise_error(SyntaxError, /anonymous rest parameter is also used within block/)
+ end
+ end
+ end
+end
+
+describe "delegation with def(**)" do
+ it "delegates kwargs" do
+ a = Class.new(DelegationSpecs::Target)
+ a.class_eval(<<-RUBY)
+ def delegate(**)
+ target(**)
+ end
+ RUBY
+
+ a.new.delegate(a: 1) { |x| x }.should == [[], {a: 1}, nil]
+ end
+
+ ruby_version_is "3.3" do
+ context "within a block that accepts anonymous kwargs within a method that accepts anonymous kwargs" do
+ it "does not allow delegating kwargs" do
+ -> {
+ eval "def m(**); proc { |**| n(**) } end"
+ }.should raise_error(SyntaxError, /anonymous keyword rest parameter is also used within block/)
+ end
+ end
+ end
+end
- a.new.delegate_block(0, 1, b: 2) { |x| x }.should == [{b: 2}, [1]]
+describe "delegation with def(&)" do
+ it "delegates an anonymous block parameter" do
+ a = Class.new(DelegationSpecs::Target)
+ a.class_eval(<<-RUBY)
+ def delegate(&)
+ target(&)
+ end
+ RUBY
+
+ block = proc {}
+ a.new.delegate(&block).should == [[], {}, block]
+ end
+
+ ruby_version_is "3.3" do
+ context "within a block that accepts anonymous block within a method that accepts anonymous block" do
+ it "does not allow delegating a block" do
+ -> {
+ eval "def m(&); proc { |&| n(&) } end"
+ }.should raise_error(SyntaxError, /anonymous block parameter is also used within block/)
+ end
end
end
end
diff --git a/spec/ruby/language/encoding_spec.rb b/spec/ruby/language/encoding_spec.rb
index 5430c9cb98..e761a53cb6 100644
--- a/spec/ruby/language/encoding_spec.rb
+++ b/spec/ruby/language/encoding_spec.rb
@@ -13,15 +13,15 @@ describe "The __ENCODING__ pseudo-variable" do
end
it "is the evaluated strings's one inside an eval" do
- eval("__ENCODING__".force_encoding("US-ASCII")).should == Encoding::US_ASCII
- eval("__ENCODING__".force_encoding("BINARY")).should == Encoding::BINARY
+ eval("__ENCODING__".dup.force_encoding("US-ASCII")).should == Encoding::US_ASCII
+ eval("__ENCODING__".dup.force_encoding("BINARY")).should == Encoding::BINARY
end
it "is the encoding specified by a magic comment inside an eval" do
- code = "# encoding: BINARY\n__ENCODING__".force_encoding("US-ASCII")
+ code = "# encoding: BINARY\n__ENCODING__".dup.force_encoding("US-ASCII")
eval(code).should == Encoding::BINARY
- code = "# encoding: us-ascii\n__ENCODING__".force_encoding("BINARY")
+ code = "# encoding: us-ascii\n__ENCODING__".dup.force_encoding("BINARY")
eval(code).should == Encoding::US_ASCII
end
diff --git a/spec/ruby/language/ensure_spec.rb b/spec/ruby/language/ensure_spec.rb
index e893904bcb..b76292c007 100644
--- a/spec/ruby/language/ensure_spec.rb
+++ b/spec/ruby/language/ensure_spec.rb
@@ -328,4 +328,19 @@ describe "An ensure block inside 'do end' block" do
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[0].should =~ /#{__FILE__}:#{line-3}:in 'foo'/
+ foo[1].should =~ /#{__FILE__}:#{line+2}:in 'block/
+ end
+ end
end
diff --git a/spec/ruby/language/execution_spec.rb b/spec/ruby/language/execution_spec.rb
index 4e0310946d..51bcde62e8 100644
--- a/spec/ruby/language/execution_spec.rb
+++ b/spec/ruby/language/execution_spec.rb
@@ -5,6 +5,45 @@ describe "``" do
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
@@ -12,4 +51,43 @@ describe "%x" 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
diff --git a/spec/ruby/language/fixtures/class_with_class_variable.rb b/spec/ruby/language/fixtures/class_with_class_variable.rb
new file mode 100644
index 0000000000..0b07f16d30
--- /dev/null
+++ b/spec/ruby/language/fixtures/class_with_class_variable.rb
@@ -0,0 +1,9 @@
+module StringSpecs
+ class ClassWithClassVariable
+ @@a = "xxx"
+
+ def foo
+ "#@@a"
+ end
+ end
+end
diff --git a/spec/ruby/language/fixtures/defined.rb b/spec/ruby/language/fixtures/defined.rb
index a9552619bf..3761cfa5bd 100644
--- a/spec/ruby/language/fixtures/defined.rb
+++ b/spec/ruby/language/fixtures/defined.rb
@@ -285,6 +285,12 @@ module DefinedSpecs
end
end
+ module ModuleWithConstMissing
+ def self.const_missing(const)
+ const
+ end
+ end
+
class SuperWithIntermediateModules
include IntermediateModule1
include IntermediateModule2
diff --git a/spec/ruby/language/fixtures/delegation.rb b/spec/ruby/language/fixtures/delegation.rb
index 527d928390..da2b024791 100644
--- a/spec/ruby/language/fixtures/delegation.rb
+++ b/spec/ruby/language/fixtures/delegation.rb
@@ -1,7 +1,7 @@
module DelegationSpecs
class Target
- def target(*args, **kwargs)
- [args, kwargs]
+ def target(*args, **kwargs, &block)
+ [args, kwargs, block]
end
def target_block(*args, **kwargs)
diff --git a/spec/ruby/language/fixtures/module.rb b/spec/ruby/language/fixtures/module.rb
index 33d323846e..75eee77791 100644
--- a/spec/ruby/language/fixtures/module.rb
+++ b/spec/ruby/language/fixtures/module.rb
@@ -12,13 +12,4 @@ module ModuleSpecs
module Anonymous
end
-
- module IncludedInObject
- module IncludedModuleSpecs
- end
- end
-end
-
-class Object
- include ModuleSpecs::IncludedInObject
end
diff --git a/spec/ruby/language/fixtures/private.rb b/spec/ruby/language/fixtures/private.rb
index 96f73cea3f..da3e0a97f9 100644
--- a/spec/ruby/language/fixtures/private.rb
+++ b/spec/ruby/language/fixtures/private.rb
@@ -43,17 +43,17 @@ module Private
end
end
- class E
- include D
- end
-
- class G
- def foo
- "foo"
- end
- end
-
- class H < A
- private :foo
- end
+ class E
+ include D
+ end
+
+ class G
+ def foo
+ "foo"
+ end
+ end
+
+ class H < A
+ private :foo
+ end
end
diff --git a/spec/ruby/language/fixtures/rescue/top_level.rb b/spec/ruby/language/fixtures/rescue/top_level.rb
new file mode 100644
index 0000000000..59e78ef1d6
--- /dev/null
+++ b/spec/ruby/language/fixtures/rescue/top_level.rb
@@ -0,0 +1,7 @@
+# capturing in local variable at top-level
+
+begin
+ raise "message"
+rescue => e
+ ScratchPad << e.message
+end
diff --git a/spec/ruby/language/fixtures/send.rb b/spec/ruby/language/fixtures/send.rb
index 918241e171..4787abee5c 100644
--- a/spec/ruby/language/fixtures/send.rb
+++ b/spec/ruby/language/fixtures/send.rb
@@ -43,9 +43,9 @@ module LangSendSpecs
attr_writer :foo
private :foo=
- def call_self_foo_equals(value)
- self.foo = value
- end
+ def call_self_foo_equals(value)
+ self.foo = value
+ end
def call_self_foo_equals_masgn(value)
a, self.foo = 1, value
@@ -81,6 +81,16 @@ module LangSendSpecs
end
end
+ class RawToProc
+ def initialize(to_proc)
+ @to_proc = to_proc
+ end
+
+ def to_proc
+ @to_proc
+ end
+ end
+
class ToAry
def initialize(obj)
@obj = obj
diff --git a/spec/ruby/language/fixtures/super.rb b/spec/ruby/language/fixtures/super.rb
index 94a2a91be0..c5bdcf0e40 100644
--- a/spec/ruby/language/fixtures/super.rb
+++ b/spec/ruby/language/fixtures/super.rb
@@ -539,6 +539,30 @@ module SuperSpecs
args
end
+ def m3(*args)
+ args
+ end
+
+ def m4(*args)
+ args
+ end
+
+ def m_default(*args)
+ args
+ end
+
+ def m_rest(*args)
+ args
+ end
+
+ def m_pre_default_rest_post(*args)
+ args
+ end
+
+ def m_kwrest(**kw)
+ kw
+ end
+
def m_modified(*args)
args
end
@@ -549,6 +573,30 @@ module SuperSpecs
super
end
+ def m3(_, _, _)
+ super
+ end
+
+ def m4(_, _, _, _)
+ super
+ end
+
+ def m_default(_ = 0)
+ super
+ end
+
+ def m_rest(*_)
+ super
+ end
+
+ def m_pre_default_rest_post(_, _, _=:a, _=:b, *_, _, _)
+ super
+ end
+
+ def m_kwrest(**_)
+ super
+ end
+
def m_modified(_, _)
_ = 14
super
diff --git a/spec/ruby/language/for_spec.rb b/spec/ruby/language/for_spec.rb
index 0ad5ea88af..b8ddfe5f0d 100644
--- a/spec/ruby/language/for_spec.rb
+++ b/spec/ruby/language/for_spec.rb
@@ -19,6 +19,27 @@ describe "The for expression" do
end
end
+ it "iterates over a list of arrays and destructures with an empty splat" do
+ for i, * in [[1,2]]
+ i.should == 1
+ end
+ end
+
+ it "iterates over a list of arrays and destructures with a splat" do
+ for i, *j in [[1,2]]
+ i.should == 1
+ j.should == [2]
+ end
+ end
+
+ it "iterates over a list of arrays and destructures with a splat and additional targets" do
+ for i, *j, k in [[1,2,3,4]]
+ i.should == 1
+ j.should == [2,3]
+ k.should == 4
+ end
+ end
+
it "iterates over an Hash passing each key-value pair to the block" do
k = 0
l = 0
@@ -81,6 +102,88 @@ describe "The for expression" do
end
end
+ it "allows a global variable as an iterator name" do
+ old_global_var = $var
+ m = [1,2,3]
+ n = 0
+ for $var in m
+ n += 1
+ end
+ $var.should == 3
+ n.should == 3
+ $var = old_global_var
+ end
+
+ it "allows an attribute as an iterator name" do
+ class OFor
+ attr_accessor :target
+ end
+
+ ofor = OFor.new
+ m = [1,2,3]
+ n = 0
+ for ofor.target in m
+ n += 1
+ end
+ ofor.target.should == 3
+ n.should == 3
+ end
+
+ # Segfault in MRI 3.3 and lower: https://bugs.ruby-lang.org/issues/20468
+ ruby_bug "#20468", ""..."3.4" do
+ it "allows an attribute with safe navigation as an iterator name" do
+ class OFor
+ attr_accessor :target
+ end
+
+ ofor = OFor.new
+ m = [1,2,3]
+ n = 0
+ eval <<~RUBY
+ for ofor&.target in m
+ n += 1
+ end
+ RUBY
+ ofor.target.should == 3
+ n.should == 3
+ end
+
+ it "allows an attribute with safe navigation on a nil base as an iterator name" do
+ ofor = nil
+ m = [1,2,3]
+ n = 0
+ eval <<~RUBY
+ for ofor&.target in m
+ n += 1
+ end
+ RUBY
+ ofor.should be_nil
+ n.should == 3
+ end
+ end
+
+ it "allows an array index writer as an iterator name" do
+ arr = [:a, :b, :c]
+ m = [1,2,3]
+ n = 0
+ for arr[1] in m
+ n += 1
+ end
+ arr.should == [:a, 3, :c]
+ n.should == 3
+ end
+
+ it "allows a hash index writer as an iterator name" do
+ hash = { a: 10, b: 20, c: 30 }
+ m = [1,2,3]
+ n = 0
+ for hash[:b] in m
+ n += 1
+ end
+ hash.should == { a: 10, b: 3, c: 30 }
+ n.should == 3
+ end
+
# 1.9 behaviour verified by nobu in
# http://redmine.ruby-lang.org/issues/show/2053
it "yields only as many values as there are arguments" do
diff --git a/spec/ruby/language/hash_spec.rb b/spec/ruby/language/hash_spec.rb
index 9e2b9bd4c5..668716e2e3 100644
--- a/spec/ruby/language/hash_spec.rb
+++ b/spec/ruby/language/hash_spec.rb
@@ -33,7 +33,7 @@ describe "Hash literal" do
end
it "freezes string keys on initialization" do
- key = "foo"
+ key = +"foo"
h = {key => "bar"}
key.reverse!
h["foo"].should == "bar"
@@ -60,14 +60,12 @@ describe "Hash literal" do
@h.should == {1000 => :foo}
end
- ruby_version_is "3.1" do
- it "checks duplicated float keys on initialization" do
- -> {
- @h = eval "{1.0 => :bar, 1.0 => :foo}"
- }.should complain(/key 1.0 is duplicated|duplicated key/)
- @h.keys.size.should == 1
- @h.should == {1.0 => :foo}
- end
+ it "checks duplicated float keys on initialization" do
+ -> {
+ @h = eval "{1.0 => :bar, 1.0 => :foo}"
+ }.should complain(/key 1.0 is duplicated|duplicated key/)
+ @h.keys.size.should == 1
+ @h.should == {1.0 => :foo}
end
it "accepts a hanging comma" do
@@ -77,15 +75,39 @@ describe "Hash literal" do
end
it "recognizes '=' at the end of the key" do
- eval("{:a==>1}").should == {:"a=" => 1}
- eval("{:a= =>1}").should == {:"a=" => 1}
- eval("{:a= => 1}").should == {:"a=" => 1}
+ {:a==>1}.should == {:"a=" => 1}
+ {:a= =>1}.should == {:"a=" => 1}
+ {:a= => 1}.should == {:"a=" => 1}
end
it "with '==>' in the middle raises SyntaxError" do
-> { eval("{:a ==> 1}") }.should raise_error(SyntaxError)
end
+ it "recognizes '!' at the end of the key" do
+ {:a! =>1}.should == {:"a!" => 1}
+ {:a! => 1}.should == {:"a!" => 1}
+
+ {a!:1}.should == {:"a!" => 1}
+ {a!: 1}.should == {:"a!" => 1}
+ end
+
+ it "raises a SyntaxError if there is no space between `!` and `=>`" do
+ -> { eval("{:a!=> 1}") }.should raise_error(SyntaxError)
+ end
+
+ it "recognizes '?' at the end of the key" do
+ {:a? =>1}.should == {:"a?" => 1}
+ {:a? => 1}.should == {:"a?" => 1}
+
+ {a?:1}.should == {:"a?" => 1}
+ {a?: 1}.should == {:"a?" => 1}
+ end
+
+ it "raises a SyntaxError if there is no space between `?` and `=>`" do
+ -> { eval("{:a?=> 1}") }.should raise_error(SyntaxError)
+ end
+
it "constructs a new hash with the given elements" do
{foo: 123}.should == {foo: 123}
h = {rbx: :cool, specs: 'fail_sometimes'}
@@ -105,7 +127,7 @@ describe "Hash literal" do
it "accepts mixed 'key: value', 'key => value' and '\"key\"': value' syntax" do
h = {:a => 1, :b => 2, "c" => 3, :d => 4}
- eval('{a: 1, :b => 2, "c" => 3, "d": 4}').should == h
+ {a: 1, :b => 2, "c" => 3, "d": 4}.should == h
end
it "expands an '**{}' element into the containing Hash literal initialization" do
@@ -127,6 +149,26 @@ describe "Hash literal" do
{a: 1, **h, c: 4}.should == {a: 1, b: 2, c: 4}
end
+ ruby_version_is ""..."3.4" do
+ it "does not expand nil using ** into {} and raises TypeError" do
+ h = nil
+ -> { {a: 1, **h} }.should raise_error(TypeError, "no implicit conversion of nil into Hash")
+
+ -> { {a: 1, **nil} }.should raise_error(TypeError, "no implicit conversion of nil into Hash")
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "expands nil using ** into {}" do
+ h = nil
+ {**h}.should == {}
+ {a: 1, **h}.should == {a: 1}
+
+ {**nil}.should == {}
+ {a: 1, **nil}.should == {a: 1}
+ end
+ end
+
it "expands an '**{}' or '**obj' element with the last key/value pair taking precedence" do
-> {
@h = eval "{a: 1, **{a: 2, b: 3, c: 1}, c: 3}"
@@ -191,20 +233,22 @@ describe "Hash literal" do
usascii_hash.keys.first.encoding.should == Encoding::US_ASCII
end
- it "raises an EncodingError at parse time when Symbol key with invalid bytes" do
- ScratchPad.record []
- -> {
- eval 'ScratchPad << 1; {:"\xC3" => 1}'
- }.should raise_error(EncodingError, 'invalid symbol in encoding UTF-8 :"\xC3"')
- ScratchPad.recorded.should == []
- end
+ ruby_bug "#20280", ""..."3.4" do
+ it "raises a SyntaxError at parse time when Symbol key with invalid bytes" do
+ ScratchPad.record []
+ -> {
+ eval 'ScratchPad << 1; {:"\xC3" => 1}'
+ }.should raise_error(SyntaxError, /invalid symbol/)
+ ScratchPad.recorded.should == []
+ end
- it "raises an EncodingError at parse time when Symbol key with invalid bytes and 'key: value' syntax used" do
- ScratchPad.record []
- -> {
- eval 'ScratchPad << 1; {"\xC3": 1}'
- }.should raise_error(EncodingError, 'invalid symbol in encoding UTF-8 :"\xC3"')
- ScratchPad.recorded.should == []
+ it "raises a SyntaxError at parse time when Symbol key with invalid bytes and 'key: value' syntax used" do
+ ScratchPad.record []
+ -> {
+ eval 'ScratchPad << 1; {"\xC3": 1}'
+ }.should raise_error(SyntaxError, /invalid symbol/)
+ ScratchPad.recorded.should == []
+ end
end
end
@@ -220,53 +264,61 @@ describe "The ** operator" do
h.should == { one: 1, two: 2 }
end
- it "does not copy when calling a method taking a positional Hash" do
- def m(h)
- h.delete(:one); h
+ ruby_bug "#20012", ""..."3.3" do
+ it "makes a copy when calling a method taking a positional Hash" do
+ def m(h)
+ h.delete(:one); h
+ end
+
+ h = { one: 1, two: 2 }
+ m(**h).should == { two: 2 }
+ m(**h).should_not.equal?(h)
+ h.should == { one: 1, two: 2 }
end
+ end
- h = { one: 1, two: 2 }
- m(**h).should == { two: 2 }
- m(**h).should.equal?(h)
- h.should == { two: 2 }
- end
-
- ruby_version_is "3.1" do
- describe "hash with omitted value" do
- it "accepts short notation 'key' for 'key: value' syntax" do
- a, b, c = 1, 2, 3
- h = eval('{a:}')
- {a: 1}.should == h
- h = eval('{a:, b:, c:}')
- {a: 1, b: 2, c: 3}.should == h
- end
+ describe "hash with omitted value" do
+ it "accepts short notation 'key' for 'key: value' syntax" do
+ a, b, c = 1, 2, 3
+ h = {a:}
+ {a: 1}.should == h
+ h = {a:, b:, c:}
+ {a: 1, b: 2, c: 3}.should == h
+ end
- it "ignores hanging comma on short notation" do
- a, b, c = 1, 2, 3
- h = eval('{a:, b:, c:,}')
- {a: 1, b: 2, c: 3}.should == h
- end
+ it "ignores hanging comma on short notation" do
+ a, b, c = 1, 2, 3
+ h = {a:, b:, c:,}
+ {a: 1, b: 2, c: 3}.should == h
+ end
- it "accepts mixed syntax" do
- a, e = 1, 5
- h = eval('{a:, b: 2, "c" => 3, :d => 4, e:}')
- eval('{a: 1, :b => 2, "c" => 3, "d": 4, e: 5}').should == h
- end
+ it "accepts mixed syntax" do
+ a, e = 1, 5
+ h = {a:, b: 2, "c" => 3, :d => 4, e:}
+ {a: 1, :b => 2, "c" => 3, "d": 4, e: 5}.should == h
+ end
- it "works with methods and local vars" do
- a = Class.new
- a.class_eval(<<-RUBY)
- def bar
- "baz"
- end
+ it "works with methods and local vars" do
+ a = Class.new
+ a.class_eval(<<-RUBY)
+ def bar
+ "baz"
+ end
- def foo(val)
- {bar:, val:}
- end
- RUBY
+ def foo(val)
+ {bar:, val:}
+ end
+ RUBY
- a.new.foo(1).should == {bar: "baz", val: 1}
- end
+ a.new.foo(1).should == {bar: "baz", val: 1}
+ end
+
+ it "raises a SyntaxError when the hash key ends with `!`" do
+ -> { eval("{a!:}") }.should raise_error(SyntaxError, /identifier a! is not valid to get/)
+ end
+
+ it "raises a SyntaxError when the hash key ends with `?`" do
+ -> { eval("{a?:}") }.should raise_error(SyntaxError, /identifier a\? is not valid to get/)
end
end
end
diff --git a/spec/ruby/language/heredoc_spec.rb b/spec/ruby/language/heredoc_spec.rb
index b3b160fd11..47ee9c2c51 100644
--- a/spec/ruby/language/heredoc_spec.rb
+++ b/spec/ruby/language/heredoc_spec.rb
@@ -106,4 +106,14 @@ HERE
SquigglyHeredocSpecs.least_indented_on_the_first_line_single.should == "a\n b\n c\n"
SquigglyHeredocSpecs.least_indented_on_the_last_line_single.should == " a\n b\nc\n"
end
+
+ it "reports line numbers inside HEREDOC with method call" do
+ -> {
+ <<-HERE.chomp
+ a
+ b
+ #{c}
+ HERE
+ }.should raise_error(NameError) { |e| e.backtrace[0].should.start_with?("#{__FILE__}:#{__LINE__ - 2}") }
+ end
end
diff --git a/spec/ruby/language/if_spec.rb b/spec/ruby/language/if_spec.rb
index a5da696000..2d1a89f081 100644
--- a/spec/ruby/language/if_spec.rb
+++ b/spec/ruby/language/if_spec.rb
@@ -305,6 +305,16 @@ describe "The if expression" do
6.times(&b)
ScratchPad.recorded.should == [4, 5, 4, 5]
end
+
+ it "warns when Integer literals are used instead of predicates" do
+ -> {
+ eval <<~RUBY
+ $. = 0
+ 10.times { |i| ScratchPad << i if 4..5 }
+ RUBY
+ }.should complain(/warning: integer literal in flip-flop/, verbose: true)
+ ScratchPad.recorded.should == []
+ end
end
describe "when a branch syntactically does not return a value" do
diff --git a/spec/ruby/language/it_parameter_spec.rb b/spec/ruby/language/it_parameter_spec.rb
new file mode 100644
index 0000000000..72023180d9
--- /dev/null
+++ b/spec/ruby/language/it_parameter_spec.rb
@@ -0,0 +1,66 @@
+require_relative '../spec_helper'
+
+ruby_version_is "3.4" do
+ describe "The `it` parameter" do
+ it "provides it in a block" do
+ -> { it }.call("a").should == "a"
+ proc { it }.call("a").should == "a"
+ lambda { it }.call("a").should == "a"
+ ["a"].map { it }.should == ["a"]
+ end
+
+ it "assigns nil to not passed parameters" do
+ proc { it }.call().should == nil
+ end
+
+ it "can be used in both outer and nested blocks at the same time" do
+ -> { it + -> { it * it }.call(2) }.call(3).should == 7
+ end
+
+ it "is a regular local variable if there is already a 'it' local variable" do
+ it = 0
+ proc { it }.call("a").should == 0
+ end
+
+ it "raises SyntaxError when block parameters are specified explicitly" do
+ -> { eval("-> () { it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
+ -> { eval("-> (x) { it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
+
+ -> { eval("proc { || it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
+ -> { eval("proc { |x| it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
+
+ -> { eval("lambda { || it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
+ -> { eval("lambda { |x| it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
+
+ -> { eval("['a'].map { || it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
+ -> { eval("['a'].map { |x| it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
+ end
+
+ it "affects block arity" do
+ -> {}.arity.should == 0
+ -> { it }.arity.should == 1
+ end
+
+ it "affects block parameters" do
+ -> { it }.parameters.should == [[:req]]
+
+ ruby_version_is ""..."4.0" do
+ proc { it }.parameters.should == [[:opt, nil]]
+ end
+ ruby_version_is "4.0" do
+ proc { it }.parameters.should == [[:opt]]
+ end
+ end
+
+ it "does not affect binding local variables" do
+ -> { it; binding.local_variables }.call("a").should == []
+ end
+
+ it "does not work in methods" do
+ obj = Object.new
+ def obj.foo; it; end
+
+ -> { obj.foo("a") }.should raise_error(ArgumentError, /wrong number of arguments/)
+ end
+ end
+end
diff --git a/spec/ruby/language/keyword_arguments_spec.rb b/spec/ruby/language/keyword_arguments_spec.rb
index e2c816f622..4f6370d419 100644
--- a/spec/ruby/language/keyword_arguments_spec.rb
+++ b/spec/ruby/language/keyword_arguments_spec.rb
@@ -87,13 +87,16 @@ describe "Keyword arguments" do
end
context "**" do
- it "does not copy a non-empty Hash for a method taking (*args)" do
- def m(*args)
- args[0]
- end
+ ruby_version_is "3.3" do
+ it "copies a non-empty Hash for a method taking (*args)" do
+ def m(*args)
+ args[0]
+ end
- h = {a: 1}
- m(**h).should.equal?(h)
+ h = {a: 1}
+ m(**h).should_not.equal?(h)
+ h.should == {a: 1}
+ end
end
it "copies the given Hash for a method taking (**kwargs)" do
@@ -320,76 +323,64 @@ describe "Keyword arguments" do
m({a: 1}).should == [[{a: 1}], {}]
end
- ruby_version_is "3.1" do
- describe "omitted values" do
- it "accepts short notation 'key' for 'key: value' syntax" do
- def m(a:, b:)
- [a, b]
- end
+ describe "omitted values" do
+ it "accepts short notation 'key' for 'key: value' syntax" do
+ def m(a:, b:)
+ [a, b]
+ end
- a = 1
- b = 2
+ a = 1
+ b = 2
- eval('m(a:, b:).should == [1, 2]')
- end
+ m(a:, b:).should == [1, 2]
end
end
- ruby_version_is "3.2" do
- it "does not work with call(*ruby2_keyword_args) with missing ruby2_keywords in between" do
- class << self
- def n(*args) # Note the missing ruby2_keywords here
- target(*args)
- end
+ it "does not work with call(*ruby2_keyword_args) with missing ruby2_keywords in between" do
+ class << self
+ def n(*args) # Note the missing ruby2_keywords here
+ target(*args)
+ end
- ruby2_keywords def m(*args)
- n(*args)
- end
+ ruby2_keywords def m(*args)
+ n(*args)
end
+ end
- empty = {}
- m(**empty).should == [[], {}]
- m(empty).should == [[{}], {}]
+ empty = {}
+ m(**empty).should == [[], {}]
+ m(empty).should == [[{}], {}]
- m(a: 1).should == [[{a: 1}], {}]
- m({a: 1}).should == [[{a: 1}], {}]
- end
+ m(a: 1).should == [[{a: 1}], {}]
+ m({a: 1}).should == [[{a: 1}], {}]
end
+ end
- ruby_version_is ""..."3.2" do
- # https://bugs.ruby-lang.org/issues/18625
- it "works with call(*ruby2_keyword_args) with missing ruby2_keywords in between due to CRuby bug #18625" do
- class << self
- def n(*args) # Note the missing ruby2_keywords here
- target(*args)
- end
-
- ruby2_keywords def m(*args)
- n(*args)
- end
+ context "in define_method(name, &proc)" do
+ # This tests that a free-standing proc used in define_method and converted to ruby2_keywords adopts that logic.
+ # See jruby/jruby#8119 for a case where aggressive JIT optimization broke later ruby2_keywords changes.
+ it "works with ruby2_keywords" do
+ m = Class.new do
+ def bar(a, foo: nil)
+ [a, foo]
end
- empty = {}
- m(**empty).should == [[], {}]
- Hash.ruby2_keywords_hash?(empty).should == false
- m(empty).should == [[{}], {}]
- Hash.ruby2_keywords_hash?(empty).should == false
-
- m(a: 1).should == [[], {a: 1}]
- m({a: 1}).should == [[{a: 1}], {}]
+ # define_method and ruby2_keywords using send to avoid peephole optimizations
+ def self.setup
+ pr = make_proc
+ send :define_method, :foo, &pr
+ send :ruby2_keywords, :foo
+ end
- kw = {a: 1}
+ # create proc in isolated method to force jit compilation on some implementations
+ def self.make_proc
+ proc { |a, *args| bar(a, *args) }
+ end
+ end
- m(**kw).should == [[], {a: 1}]
- m(**kw)[1].should == kw
- m(**kw)[1].should_not.equal?(kw)
- Hash.ruby2_keywords_hash?(kw).should == false
- Hash.ruby2_keywords_hash?(m(**kw)[1]).should == false
+ m.setup
- m(kw).should == [[{a: 1}], {}]
- m(kw)[0][0].should.equal?(kw)
- Hash.ruby2_keywords_hash?(kw).should == false
- end
+ m.new.foo(1, foo:2).should == [1, 2]
end
end
end
diff --git a/spec/ruby/language/lambda_spec.rb b/spec/ruby/language/lambda_spec.rb
index 3ab3569ebe..ed5a1c69e8 100644
--- a/spec/ruby/language/lambda_spec.rb
+++ b/spec/ruby/language/lambda_spec.rb
@@ -263,18 +263,21 @@ describe "A lambda literal -> () { }" do
end
describe "with circular optional argument reference" do
- it "raises a SyntaxError if using an existing local with the same name as the argument" do
- a = 1
- -> {
- @proc = eval "-> (a=a) { a }"
- }.should raise_error(SyntaxError)
+ ruby_version_is ""..."3.4" do
+ it "raises a SyntaxError if using the argument in its default value" do
+ a = 1
+ -> {
+ eval "-> (a=a) { a }"
+ }.should raise_error(SyntaxError)
+ end
end
- it "raises a SyntaxError if there is an existing method with the same name as the argument" do
- def a; 1; end
- -> {
- @proc = eval "-> (a=a) { a }"
- }.should raise_error(SyntaxError)
+ ruby_version_is "3.4" do
+ it "is nil if using the argument in its default value" do
+ -> {
+ eval "-> (a=a) { a }.call"
+ }.call.should == nil
+ end
end
it "calls an existing method with the same name as the argument if explicitly using ()" do
diff --git a/spec/ruby/language/magic_comment_spec.rb b/spec/ruby/language/magic_comment_spec.rb
index f2bf3a08e5..af9c9dbfd0 100644
--- a/spec/ruby/language/magic_comment_spec.rb
+++ b/spec/ruby/language/magic_comment_spec.rb
@@ -45,7 +45,8 @@ end
describe "Magic comments" do
describe "in stdin" do
- it_behaves_like :magic_comments, :locale, -> file {
+ default = (platform_is :windows and ruby_version_is "4.0") ? :UTF8 : :locale
+ it_behaves_like :magic_comments, default, -> file {
print_at_exit = fixture(__FILE__, "print_magic_comment_result_at_exit.rb")
ruby_exe(nil, args: "< #{fixture(__FILE__, file)}", options: "-r#{print_at_exit}")
}
diff --git a/spec/ruby/language/method_spec.rb b/spec/ruby/language/method_spec.rb
index b1d169aba8..8f72bd45ed 100644
--- a/spec/ruby/language/method_spec.rb
+++ b/spec/ruby/language/method_spec.rb
@@ -1107,9 +1107,9 @@ describe "A method" do
m({a: 1}).should == {a: 1}
m({"a" => 1}).should == {"a" => 1}
- -> { m(a: 1) }.should raise_error(ArgumentError)
- -> { m(**{a: 1}) }.should raise_error(ArgumentError)
- -> { m("a" => 1) }.should raise_error(ArgumentError)
+ -> { m(a: 1) }.should raise_error(ArgumentError, 'no keywords accepted')
+ -> { m(**{a: 1}) }.should raise_error(ArgumentError, 'no keywords accepted')
+ -> { m("a" => 1) }.should raise_error(ArgumentError, 'no keywords accepted')
end
evaluate <<-ruby do
@@ -1175,6 +1175,31 @@ describe "A method" do
end
end
+context "when passing **nil into a method that accepts keyword arguments" do
+ ruby_version_is ""..."3.4" do
+ it "raises TypeError" do
+ def m(**kw) kw; end
+
+ h = nil
+ -> { m(a: 1, **h) }.should raise_error(TypeError, "no implicit conversion of nil into Hash")
+ -> { m(a: 1, **nil) }.should raise_error(TypeError, "no implicit conversion of nil into Hash")
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "expands nil using ** into {}" do
+ def m(**kw) kw; end
+
+ h = nil
+ m(**h).should == {}
+ m(a: 1, **h).should == {a: 1}
+
+ m(**nil).should == {}
+ m(a: 1, **nil).should == {a: 1}
+ end
+ end
+end
+
describe "A method call with a space between method name and parentheses" do
before(:each) do
def m(*args)
@@ -1193,14 +1218,42 @@ describe "A method call with a space between method name and parentheses" do
end
end
- context "when a single argument provided" do
- it "assigns it" do
+ context "when a single argument is provided" do
+ it "assigns a simple expression" do
+ args = m (1)
+ args.should == [1]
+ end
+
+ it "assigns an expression consisting of multiple statements" do
+ args = m ((0; 1))
+ args.should == [1]
+ end
+
+ it "assigns one single statement, without the need of parentheses" do
args = m (1 == 1 ? true : false)
args.should == [true]
end
+
+ ruby_version_is "3.3" do
+ it "supports multiple statements" do
+ eval("m (1; 2)").should == [2]
+ end
+ end
+ end
+
+ context "when multiple arguments are provided" do
+ it "assigns simple expressions" do
+ args = m (1), (2)
+ args.should == [1, 2]
+ end
+
+ it "assigns expressions consisting of multiple statements" do
+ args = m ((0; 1)), ((2; 3))
+ args.should == [1, 3]
+ end
end
- context "when 2+ arguments provided" do
+ context "when the argument looks like an argument list" do
it "raises a syntax error" do
-> {
eval("m (1, 2)")
@@ -1307,6 +1360,7 @@ describe "An endless method definition" do
end
end
+ # tested more thoroughly in language/delegation_spec.rb
context "with args forwarding" do
evaluate <<-ruby do
def mm(word, num:)
@@ -1387,53 +1441,209 @@ describe "Keyword arguments are now separated from positional arguments" do
end
end
-ruby_version_is "3.1" do
- describe "kwarg with omitted value in a method call" do
- context "accepts short notation 'kwarg' in method call" do
- evaluate <<-ruby do
- def call(*args, **kwargs) = [args, kwargs]
- ruby
+describe "kwarg with omitted value in a method call" do
+ context "accepts short notation 'kwarg' in method call" do
+ evaluate <<-ruby do
+ def call(*args, **kwargs) = [args, kwargs]
+ ruby
+
+ a, b, c = 1, 2, 3
+ arr, h = call(a:)
+ h.should == {a: 1}
+ arr.should == []
+
+ arr, h = call(a:, b:, c:)
+ h.should == {a: 1, b: 2, c: 3}
+ arr.should == []
+
+ arr, h = call(a:, b: 10, c:)
+ h.should == {a: 1, b: 10, c: 3}
+ arr.should == []
+ end
+ end
+
+ context "with methods and local variables" do
+ evaluate <<-ruby do
+ def call(*args, **kwargs) = [args, kwargs]
+
+ def bar
+ "baz"
+ end
+
+ def foo(val)
+ call bar:, val:
+ end
+ ruby
- a, b, c = 1, 2, 3
- arr, h = eval('call a:')
- h.should == {a: 1}
- arr.should == []
+ foo(1).should == [[], {bar: "baz", val: 1}]
+ end
+ end
+end
+
+describe "Inside 'endless' method definitions" do
+ it "allows method calls without parenthesis" do
+ def greet(person) = "Hi, ".dup.concat person
- arr, h = eval('call(a:, b:, c:)')
- h.should == {a: 1, b: 2, c: 3}
- arr.should == []
+ greet("Homer").should == "Hi, Homer"
+ end
+end
- arr, h = eval('call(a:, b: 10, c:)')
- h.should == {a: 1, b: 10, c: 3}
- arr.should == []
+describe "warning about not used block argument" do
+ ruby_version_is "3.4" do
+ it "warns when passing a block argument to a method that never uses it" do
+ def m_that_does_not_use_block
+ 42
end
+
+ -> {
+ m_that_does_not_use_block { }
+ }.should complain(
+ /#{__FILE__}:#{__LINE__ - 2}: warning: the block passed to 'm_that_does_not_use_block' defined at #{__FILE__}:#{__LINE__ - 7} may be ignored/,
+ verbose: true)
end
- context "with methods and local variables" do
- evaluate <<-ruby do
- def call(*args, **kwargs) = [args, kwargs]
+ it "does not warn when passing a block argument to a method that declares a block parameter" do
+ def m_with_block_parameter(&block)
+ 42
+ end
- def bar
- "baz"
- end
+ -> { m_with_block_parameter { } }.should_not complain(verbose: true)
+ end
- def foo(val)
- call bar:, val:
- end
- ruby
+ it "does not warn when passing a block argument to a method that declares an anonymous block parameter" do
+ def m_with_anonymous_block_parameter(&)
+ 42
+ end
+
+ -> { m_with_anonymous_block_parameter { } }.should_not complain(verbose: true)
+ end
- foo(1).should == [[], {bar: "baz", val: 1}]
+ it "does not warn when passing a block argument to a method that yields an implicit block parameter" do
+ def m_with_yield
+ yield 42
end
+
+ -> { m_with_yield { } }.should_not complain(verbose: true)
end
- end
- describe "Inside 'endless' method definitions" do
- it "allows method calls without parenthesis" do
- eval <<-ruby
- def greet(person) = "Hi, ".concat person
- ruby
+ it "warns when passing a block argument to a method that calls #block_given?" do
+ def m_with_block_given
+ block_given?
+ end
+
+ -> {
+ m_with_block_given { }
+ }.should complain(
+ /#{__FILE__}:#{__LINE__ - 2}: warning: the block passed to 'm_with_block_given' defined at #{__FILE__}:#{__LINE__ - 7} may be ignored/,
+ verbose: true)
+ end
+
+ it "does not warn when passing a block argument to a method that calls super" do
+ parent = Class.new do
+ def m
+ end
+ end
+
+ child = Class.new(parent) do
+ def m
+ super
+ end
+ end
+
+ obj = child.new
+ -> { obj.m { } }.should_not complain(verbose: true)
+ end
+
+ it "does not warn when passing a block argument to a method that calls super(...)" do
+ parent = Class.new do
+ def m(a)
+ end
+ end
+
+ child = Class.new(parent) do
+ def m(...)
+ super(...)
+ end
+ end
+
+ obj = child.new
+ -> { obj.m(42) { } }.should_not complain(verbose: true)
+ end
+
+ it "does not warn when called #initialize()" do
+ klass = Class.new do
+ def initialize
+ end
+ end
+
+ -> { klass.new {} }.should_not complain(verbose: true)
+ end
+
+ it "does not warn when passing a block argument to a method that calls super()" do
+ parent = Class.new do
+ def m
+ end
+ end
+
+ child = Class.new(parent) do
+ def m
+ super()
+ end
+ end
+
+ obj = child.new
+ -> { obj.m { } }.should_not complain(verbose: true)
+ end
+
+ it "warns only once per call site" do
+ def m_that_does_not_use_block
+ 42
+ end
+
+ def call_m_that_does_not_use_block
+ m_that_does_not_use_block {}
+ end
+
+ -> {
+ m_that_does_not_use_block { }
+ }.should complain(/the block passed to 'm_that_does_not_use_block' defined at .+ may be ignored/, verbose: true)
+
+ -> {
+ m_that_does_not_use_block { }
+ }.should_not complain(verbose: true)
+ end
+
+ it "can be disabled with :strict_unused_block warning category" do
+ def m_that_does_not_use_block
+ 42
+ end
+
+ # ensure that warning is emitted
+ -> { m_that_does_not_use_block { } }.should complain(verbose: true)
- greet("Homer").should == "Hi, Homer"
+ warn_strict_unused_block = Warning[:strict_unused_block]
+ Warning[:strict_unused_block] = false
+ begin
+ -> { m_that_does_not_use_block { } }.should_not complain(verbose: true)
+ ensure
+ Warning[:strict_unused_block] = warn_strict_unused_block
+ end
+ end
+
+ it "can be enabled with :strict_unused_block = true warning category in not verbose mode" do
+ def m_that_does_not_use_block
+ 42
+ end
+
+ warn_strict_unused_block = Warning[:strict_unused_block]
+ Warning[:strict_unused_block] = true
+ begin
+ -> {
+ m_that_does_not_use_block { }
+ }.should complain(/the block passed to 'm_that_does_not_use_block' defined at .+ may be ignored/)
+ ensure
+ Warning[:strict_unused_block] = warn_strict_unused_block
+ end
end
end
end
diff --git a/spec/ruby/language/module_spec.rb b/spec/ruby/language/module_spec.rb
index fffcf9c90d..fba4aa8c6e 100644
--- a/spec/ruby/language/module_spec.rb
+++ b/spec/ruby/language/module_spec.rb
@@ -26,20 +26,39 @@ describe "The module keyword" do
it "reopens an existing module" do
module ModuleSpecs; Reopened = true; end
ModuleSpecs::Reopened.should be_true
- end
-
- ruby_version_is '3.2' do
- it "does not reopen a module included in Object" do
- module IncludedModuleSpecs; Reopened = true; end
- ModuleSpecs::IncludedInObject::IncludedModuleSpecs.should_not == Object::IncludedModuleSpecs
- end
- end
-
- ruby_version_is ''...'3.2' do
- it "reopens a module included in Object" do
- module IncludedModuleSpecs; Reopened = true; end
- ModuleSpecs::IncludedInObject::IncludedModuleSpecs::Reopened.should be_true
- end
+ ensure
+ ModuleSpecs.send(:remove_const, :Reopened)
+ end
+
+ it "does not reopen a module included in Object" do
+ ruby_exe(<<~RUBY).should == "false"
+ module IncludedInObject
+ module IncludedModule; end
+ end
+ class Object
+ include IncludedInObject
+ end
+ module IncludedModule; end
+ print IncludedInObject::IncludedModule == Object::IncludedModule
+ RUBY
+ end
+
+ it "does not reopen a module included in non-Object modules" do
+ ruby_exe(<<~RUBY).should == "false/false"
+ module Included
+ module IncludedModule; end
+ end
+ module M
+ include Included
+ module IncludedModule; end
+ end
+ class C
+ include Included
+ module IncludedModule; end
+ end
+ print Included::IncludedModule == M::IncludedModule, "/",
+ Included::IncludedModule == C::IncludedModule
+ RUBY
end
it "raises a TypeError if the constant is a Class" do
@@ -76,6 +95,8 @@ describe "Assigning an anonymous module to a constant" do
::ModuleSpecs_CS1 = mod
mod.name.should == "ModuleSpecs_CS1"
+ ensure
+ Object.send(:remove_const, :ModuleSpecs_CS1)
end
it "sets the name of a module scoped by an anonymous module" do
@@ -96,5 +117,7 @@ describe "Assigning an anonymous module to a constant" do
b.name.should == "ModuleSpecs_CS2::B"
c.name.should == "ModuleSpecs_CS2::B::C"
d.name.should == "ModuleSpecs_CS2::D"
+ ensure
+ Object.send(:remove_const, :ModuleSpecs_CS2)
end
end
diff --git a/spec/ruby/language/numbered_parameters_spec.rb b/spec/ruby/language/numbered_parameters_spec.rb
index 9d2bd7ff92..de532c326d 100644
--- a/spec/ruby/language/numbered_parameters_spec.rb
+++ b/spec/ruby/language/numbered_parameters_spec.rb
@@ -22,7 +22,7 @@ describe "Numbered parameters" do
it "does not support more than 9 parameters" do
-> {
proc { [_10] }.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
- }.should raise_error(NameError, /undefined local variable or method `_10'/)
+ }.should raise_error(NameError, /undefined local variable or method [`']_10'/)
end
it "can not be used in both outer and nested blocks at the same time" do
@@ -82,6 +82,28 @@ describe "Numbered parameters" do
lambda { _9 }.arity.should == 9
end
+ it "affects block parameters" do
+ -> { _1 }.parameters.should == [[:req, :_1]]
+ -> { _2 }.parameters.should == [[:req, :_1], [:req, :_2]]
+
+ proc { _1 }.parameters.should == [[:opt, :_1]]
+ proc { _2 }.parameters.should == [[:opt, :_1], [:opt, :_2]]
+ end
+
+ ruby_version_is ""..."4.0" do
+ it "affects binding local variables" do
+ -> { _1; binding.local_variables }.call("a").should == [:_1]
+ -> { _2; binding.local_variables }.call("a", "b").should == [:_1, :_2]
+ end
+ end
+
+ ruby_version_is "4.0" do
+ it "does not affect binding local variables" do
+ -> { _1; binding.local_variables }.call("a").should == []
+ -> { _2; binding.local_variables }.call("a", "b").should == []
+ end
+ end
+
it "does not work in methods" do
obj = Object.new
def obj.foo; _1 end
diff --git a/spec/ruby/language/optional_assignments_spec.rb b/spec/ruby/language/optional_assignments_spec.rb
index 02461655d6..5fe3e3671b 100644
--- a/spec/ruby/language/optional_assignments_spec.rb
+++ b/spec/ruby/language/optional_assignments_spec.rb
@@ -57,7 +57,7 @@ describe 'Optional variable assignments' do
end
end
- describe 'using a accessor' do
+ describe 'using an accessor' do
before do
klass = Class.new { attr_accessor :b }
@a = klass.new
@@ -103,6 +103,16 @@ describe 'Optional variable assignments' do
@a.b.should == 10
end
+ it 'does evaluate receiver only once when assigns' do
+ ScratchPad.record []
+ @a.b = nil
+
+ (ScratchPad << :evaluated; @a).b ||= 10
+
+ ScratchPad.recorded.should == [:evaluated]
+ @a.b.should == 10
+ end
+
it 'returns the new value if set to false' do
def @a.b=(x)
:v
@@ -122,29 +132,148 @@ describe 'Optional variable assignments' do
(@a.b ||= 20).should == 10
end
- it 'works when writer is private' do
+ it 'ignores method visibility when receiver is self' do
+ klass_with_private_methods = Class.new do
+ def initialize(v) @a = v end
+ def public_method(v); self.a ||= v end
+ private
+ def a; @a end
+ def a=(v) @a = v; 42 end
+ end
+
+ a = klass_with_private_methods.new(false)
+ a.public_method(10).should == 10
+ end
+ end
+
+ describe 'using a #[]' do
+ before do
+ @a = {}
klass = Class.new do
- def t
- self.b = false
- (self.b ||= 10).should == 10
- (self.b ||= 20).should == 10
+ def [](k)
+ @hash ||= {}
+ @hash[k]
+ end
+
+ def []=(k, v)
+ @hash ||= {}
+ @hash[k] = v
+ 7
end
+ end
+ @b = klass.new
+ end
+
+ it 'returns the assigned value, not the result of the []= method with ||=' do
+ (@b[:k] ||= 12).should == 12
+ end
+
+ it "evaluates the index precisely once" do
+ ary = [:x, :y]
+ @a[:x] = 15
+ @a[ary.pop] ||= 25
+ ary.should == [:x]
+ @a.should == { x: 15, y: 25 }
+ end
- def b
- @b
+ it "evaluates the index arguments in the correct order" do
+ ary = Class.new(Array) do
+ def [](x, y)
+ super(x + 3 * y)
end
- def b=(x)
- @b = x
- :v
+ def []=(x, y, value)
+ super(x + 3 * y, value)
end
+ end.new
+ ary[0, 0] = 1
+ ary[1, 0] = 1
+ ary[2, 0] = nil
+ ary[3, 0] = 1
+ ary[4, 0] = 1
+ ary[5, 0] = 1
+ ary[6, 0] = nil
+
+ foo = [0, 2]
+
+ ary[foo.pop, foo.pop] ||= 2 # expected `ary[2, 0] ||= 2`
+
+ ary[2, 0].should == 2
+ ary[6, 0].should == nil # returns the same element as `ary[0, 2]`
+ end
+
+ it 'evaluates receiver only once when assigns' do
+ ScratchPad.record []
+ @a[:k] = nil
- private :b=
+ (ScratchPad << :evaluated; @a)[:k] ||= 2
+
+ ScratchPad.recorded.should == [:evaluated]
+ @a[:k].should == 2
+ end
+
+ it 'ignores method visibility when receiver is self' do
+ klass_with_private_methods = Class.new do
+ def initialize(h) @a = h end
+ def public_method(k, v); self[k] ||= v end
+ private
+ def [](k) @a[k] end
+ def []=(k, v) @a[k] = v; 42 end
end
- klass.new.t
+ a = klass_with_private_methods.new(k: false)
+ a.public_method(:k, 10).should == 10
end
+ context 'splatted argument' do
+ it 'correctly handles it' do
+ (@b[*[:m]] ||= 10).should == 10
+ @b[:m].should == 10
+
+ (@b[*(1; [:n])] ||= 10).should == 10
+ @b[:n].should == 10
+
+ (@b[*begin 1; [:k] end] ||= 10).should == 10
+ @b[:k].should == 10
+ end
+
+ it 'calls #to_a only once' do
+ k = Object.new
+ def k.to_a
+ ScratchPad << :to_a
+ [:k]
+ end
+
+ ScratchPad.record []
+ (@b[*k] ||= 20).should == 20
+ @b[:k].should == 20
+ ScratchPad.recorded.should == [:to_a]
+ end
+
+ it 'correctly handles a nested splatted argument' do
+ (@b[*[*[:k]]] ||= 20).should == 20
+ @b[:k].should == 20
+ end
+
+ it 'correctly handles multiple nested splatted arguments' do
+ klass_with_multiple_parameters = Class.new do
+ def [](k1, k2, k3)
+ @hash ||= {}
+ @hash[:"#{k1}#{k2}#{k3}"]
+ end
+
+ def []=(k1, k2, k3, v)
+ @hash ||= {}
+ @hash[:"#{k1}#{k2}#{k3}"] = v
+ 7
+ end
+ end
+ a = klass_with_multiple_parameters.new
+
+ (a[*[:a], *[:b], *[:c]] ||= 20).should == 20
+ a[:a, :b, :c].should == 20
+ end
+ end
end
end
@@ -191,7 +320,7 @@ describe 'Optional variable assignments' do
end
end
- describe 'using a single variable' do
+ describe 'using an accessor' do
before do
klass = Class.new { attr_accessor :b }
@a = klass.new
@@ -236,6 +365,29 @@ describe 'Optional variable assignments' do
@a.b.should == 20
end
+
+ it 'does evaluate receiver only once when assigns' do
+ ScratchPad.record []
+ @a.b = 10
+
+ (ScratchPad << :evaluated; @a).b &&= 20
+
+ ScratchPad.recorded.should == [:evaluated]
+ @a.b.should == 20
+ end
+
+ it 'ignores method visibility when receiver is self' do
+ klass_with_private_methods = Class.new do
+ def initialize(v) @a = v end
+ def public_method(v); self.a &&= v end
+ private
+ def a; @a end
+ def a=(v) @a = v; 42 end
+ end
+
+ a = klass_with_private_methods.new(true)
+ a.public_method(10).should == 10
+ end
end
describe 'using a #[]' do
@@ -297,17 +449,15 @@ describe 'Optional variable assignments' do
end
it 'returns the assigned value, not the result of the []= method with ||=' do
- (@b[:k] ||= 12).should == 12
- end
-
- it 'correctly handles a splatted argument for the index' do
- (@b[*[:k]] ||= 12).should == 12
+ @b[:k] = 10
+ (@b[:k] &&= 12).should == 12
end
it "evaluates the index precisely once" do
ary = [:x, :y]
@a[:x] = 15
- @a[ary.pop] ||= 25
+ @a[:y] = 20
+ @a[ary.pop] &&= 25
ary.should == [:x]
@a.should == { x: 15, y: 25 }
end
@@ -324,24 +474,103 @@ describe 'Optional variable assignments' do
end.new
ary[0, 0] = 1
ary[1, 0] = 1
- ary[2, 0] = nil
+ ary[2, 0] = 1
ary[3, 0] = 1
ary[4, 0] = 1
ary[5, 0] = 1
- ary[6, 0] = nil
+ ary[6, 0] = 1
foo = [0, 2]
- ary[foo.pop, foo.pop] ||= 2
+ ary[foo.pop, foo.pop] &&= 2 # expected `ary[2, 0] &&= 2`
ary[2, 0].should == 2
- ary[6, 0].should == nil
+ ary[6, 0].should == 1 # returns the same element as `ary[0, 2]`
+ end
+
+ it 'evaluates receiver only once when assigns' do
+ ScratchPad.record []
+ @a[:k] = 1
+
+ (ScratchPad << :evaluated; @a)[:k] &&= 2
+
+ ScratchPad.recorded.should == [:evaluated]
+ @a[:k].should == 2
end
it 'returns the assigned value, not the result of the []= method with +=' do
@b[:k] = 17
(@b[:k] += 12).should == 29
end
+
+ it 'ignores method visibility when receiver is self' do
+ klass_with_private_methods = Class.new do
+ def initialize(h) @a = h end
+ def public_method(k, v); self[k] &&= v end
+ private
+ def [](k) @a[k] end
+ def []=(k, v) @a[k] = v; 42 end
+ end
+
+ a = klass_with_private_methods.new(k: true)
+ a.public_method(:k, 10).should == 10
+ end
+
+ context 'splatted argument' do
+ it 'correctly handles it' do
+ @b[:m] = 0
+ (@b[*[:m]] &&= 10).should == 10
+ @b[:m].should == 10
+
+ @b[:n] = 0
+ (@b[*(1; [:n])] &&= 10).should == 10
+ @b[:n].should == 10
+
+ @b[:k] = 0
+ (@b[*begin 1; [:k] end] &&= 10).should == 10
+ @b[:k].should == 10
+ end
+
+ it 'calls #to_a only once' do
+ k = Object.new
+ def k.to_a
+ ScratchPad << :to_a
+ [:k]
+ end
+
+ ScratchPad.record []
+ @b[:k] = 10
+ (@b[*k] &&= 20).should == 20
+ @b[:k].should == 20
+ ScratchPad.recorded.should == [:to_a]
+ end
+
+ it 'correctly handles a nested splatted argument' do
+ @b[:k] = 10
+ (@b[*[*[:k]]] &&= 20).should == 20
+ @b[:k].should == 20
+ end
+
+ it 'correctly handles multiple nested splatted arguments' do
+ klass_with_multiple_parameters = Class.new do
+ def [](k1, k2, k3)
+ @hash ||= {}
+ @hash[:"#{k1}#{k2}#{k3}"]
+ end
+
+ def []=(k1, k2, k3, v)
+ @hash ||= {}
+ @hash[:"#{k1}#{k2}#{k3}"] = v
+ 7
+ end
+ end
+ a = klass_with_multiple_parameters.new
+
+ a[:a, :b, :c] = 10
+ (a[*[:a], *[:b], *[:c]] &&= 20).should == 20
+ a[:a, :b, :c].should == 20
+ end
+ end
end
end
@@ -434,7 +663,7 @@ describe 'Optional constant assignment' do
ConstantSpecs::ClassA::OR_ASSIGNED_CONSTANT2.should == :assigned
end
- it 'causes side-effects of the module part to be applied (for nil constant)' do
+ it 'causes side-effects of the module part to be applied only once (for nil constant)' do
suppress_warning do # already initialized constant
ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT2 = nil
x = 0
@@ -469,6 +698,8 @@ describe 'Optional constant assignment' do
x.should == 1
y.should == 0
ConstantSpecs::ClassA::NIL_OR_ASSIGNED_CONSTANT3.should == nil
+ ensure
+ ConstantSpecs::ClassA.send(:remove_const, :NIL_OR_ASSIGNED_CONSTANT3)
end
end
@@ -492,5 +723,20 @@ describe 'Optional constant assignment' do
ConstantSpecs::OpAssignFalse.should == false
ConstantSpecs.send :remove_const, :OpAssignFalse
end
+
+ it 'causes side-effects of the module part to be applied only once (when assigns)' do
+ module ConstantSpecs
+ OpAssignTrue = true
+ end
+
+ suppress_warning do # already initialized constant
+ x = 0
+ (x += 1; ConstantSpecs)::OpAssignTrue &&= :assigned
+ x.should == 1
+ ConstantSpecs::OpAssignTrue.should == :assigned
+ end
+
+ ConstantSpecs.send :remove_const, :OpAssignTrue
+ end
end
end
diff --git a/spec/ruby/language/pattern_matching_spec.rb b/spec/ruby/language/pattern_matching_spec.rb
index 050a8a052d..c1a6f0e4d6 100644
--- a/spec/ruby/language/pattern_matching_spec.rb
+++ b/spec/ruby/language/pattern_matching_spec.rb
@@ -1,155 +1,143 @@
require_relative '../spec_helper'
describe "Pattern matching" do
- # TODO: Remove excessive eval calls when Ruby 3 is the minimum version.
- # It is best to keep the eval's longer if other Ruby impls cannot parse pattern matching yet.
-
before :each do
ScratchPad.record []
end
- describe "can be standalone assoc operator that" do
+ describe "Rightward assignment (`=>`) that can be standalone assoc operator that" do
it "deconstructs value" do
suppress_warning do
- eval(<<-RUBY).should == [0, 1]
- [0, 1] => [a, b]
- [a, b]
- RUBY
+ [0, 1] => [a, b]
+ [a, b].should == [0, 1]
end
end
it "deconstructs value and properly scopes variables" do
suppress_warning do
- eval(<<-RUBY).should == [0, nil]
- a = nil
- eval(<<-PATTERN)
- [0, 1] => [a, b]
- PATTERN
- [a, defined?(b)]
- RUBY
+ a = nil
+ 1.times {
+ [0, 1] => [a, b]
+ }
+ [a, defined?(b)].should == [0, nil]
end
end
+
+ it "can work with keywords" do
+ { a: 0, b: 1 } => { a:, b: }
+ [a, b].should == [0, 1]
+ end
+ end
+
+ describe "One-line pattern matching" do
+ it "can be used to check if a pattern matches for Array-like entities" do
+ ([0, 1] in [a, b]).should == true
+ ([0, 1] in [a, b, c]).should == false
+ end
+
+ it "can be used to check if a pattern matches for Hash-like entities" do
+ ({ a: 0, b: 1 } in { a:, b: }).should == true
+ ({ a: 0, b: 1 } in { a:, b:, c: }).should == false
+ end
end
describe "find pattern" do
it "captures preceding elements to the pattern" do
- eval(<<~RUBY).should == [0, 1]
- case [0, 1, 2, 3]
- in [*pre, 2, 3]
- pre
- else
- false
- end
- RUBY
+ case [0, 1, 2, 3]
+ in [*pre, 2, 3]
+ pre
+ else
+ false
+ end.should == [0, 1]
end
it "captures following elements to the pattern" do
- eval(<<~RUBY).should == [2, 3]
- case [0, 1, 2, 3]
- in [0, 1, *post]
- post
- else
- false
- end
- RUBY
+ case [0, 1, 2, 3]
+ in [0, 1, *post]
+ post
+ else
+ false
+ end.should == [2, 3]
end
it "captures both preceding and following elements to the pattern" do
- eval(<<~RUBY).should == [[0, 1], [3, 4]]
- case [0, 1, 2, 3, 4]
- in [*pre, 2, *post]
- [pre, post]
- else
- false
- end
- RUBY
+ case [0, 1, 2, 3, 4]
+ in [*pre, 2, *post]
+ [pre, post]
+ else
+ false
+ end.should == [[0, 1], [3, 4]]
end
it "can capture the entirety of the pattern" do
- eval(<<~RUBY).should == [0, 1, 2, 3, 4]
- case [0, 1, 2, 3, 4]
- in [*everything]
- everything
- else
- false
- end
- RUBY
+ case [0, 1, 2, 3, 4]
+ in [*everything]
+ everything
+ else
+ false
+ end.should == [0, 1, 2, 3, 4]
end
it "will match an empty Array-like structure" do
- eval(<<~RUBY).should == []
- case []
- in [*everything]
- everything
- else
- false
- end
- RUBY
+ case []
+ in [*everything]
+ everything
+ else
+ false
+ end.should == []
end
it "can be nested" do
- eval(<<~RUBY).should == [[0, [2, 4, 6]], [[4, 16, 64]], 27]
- case [0, [2, 4, 6], [3, 9, 27], [4, 16, 64]]
- in [*pre, [*, 9, a], *post]
- [pre, post, a]
- else
- false
- end
- RUBY
+ case [0, [2, 4, 6], [3, 9, 27], [4, 16, 64]]
+ in [*pre, [*, 9, a], *post]
+ [pre, post, a]
+ else
+ false
+ end.should == [[0, [2, 4, 6]], [[4, 16, 64]], 27]
end
it "can be nested with an array pattern" do
- eval(<<~RUBY).should == [[4, 16, 64]]
- case [0, [2, 4, 6], [3, 9, 27], [4, 16, 64]]
- in [_, _, [*, 9, *], *post]
- post
- else
- false
- end
- RUBY
+ case [0, [2, 4, 6], [3, 9, 27], [4, 16, 64]]
+ in [_, _, [*, 9, *], *post]
+ post
+ else
+ false
+ end.should == [[4, 16, 64]]
end
it "can be nested within a hash pattern" do
- eval(<<~RUBY).should == [27]
- case {a: [3, 9, 27]}
- in {a: [*, 9, *post]}
- post
- else
- false
- end
- RUBY
+ case {a: [3, 9, 27]}
+ in {a: [*, 9, *post]}
+ post
+ else
+ false
+ end.should == [27]
end
it "can nest hash and array patterns" do
- eval(<<~RUBY).should == [42, 2]
- case [0, {a: 42, b: [0, 1]}, {a: 42, b: [1, 2]}]
- in [*, {a:, b: [1, c]}, *]
- [a, c]
- else
- false
- end
- RUBY
+ case [0, {a: 42, b: [0, 1]}, {a: 42, b: [1, 2]}]
+ in [*, {a:, b: [1, c]}, *]
+ [a, c]
+ else
+ false
+ end.should == [42, 2]
end
end
it "extends case expression with case/in construction" do
- eval(<<~RUBY).should == :bar
- case [0, 1]
- in [0]
- :foo
- in [0, 1]
- :bar
- end
- RUBY
+ case [0, 1]
+ in [0]
+ :foo
+ in [0, 1]
+ :bar
+ end.should == :bar
end
it "allows using then operator" do
- eval(<<~RUBY).should == :bar
- case [0, 1]
- in [0] then :foo
- in [0, 1] then :bar
- end
- RUBY
+ case [0, 1]
+ in [0] then :foo
+ in [0, 1] then :bar
+ end.should == :bar
end
describe "warning" do
@@ -176,27 +164,17 @@ describe "Pattern matching" do
@src = '[0, 1] => [a, b]'
end
- ruby_version_is ""..."3.1" do
- it "warns about pattern matching is experimental feature" do
- -> { eval @src }.should complain(/pattern matching is experimental, and the behavior may change in future versions of Ruby!/i)
- end
- end
-
- ruby_version_is "3.1" do
- it "does not warn about pattern matching is experimental feature" do
- -> { eval @src }.should_not complain
- end
+ it "does not warn about pattern matching is experimental feature" do
+ -> { eval @src }.should_not complain
end
end
end
it "binds variables" do
- eval(<<~RUBY).should == 1
- case [0, 1]
- in [0, a]
- a
- end
- RUBY
+ case [0, 1]
+ in [0, a]
+ a
+ end.should == 1
end
it "cannot mix in and when operators" do
@@ -207,7 +185,7 @@ describe "Pattern matching" do
in []
end
RUBY
- }.should raise_error(SyntaxError, /syntax error, unexpected `in'|\(eval\):3: syntax error, unexpected keyword_in/)
+ }.should raise_error(SyntaxError, /syntax error, unexpected `in'|\(eval\):3: syntax error, unexpected keyword_in|unexpected 'in'/)
-> {
eval <<~RUBY
@@ -216,51 +194,50 @@ describe "Pattern matching" do
when 1 == 1
end
RUBY
- }.should raise_error(SyntaxError, /syntax error, unexpected `when'|\(eval\):3: syntax error, unexpected keyword_when/)
+ }.should raise_error(SyntaxError, /syntax error, unexpected `when'|\(eval\):3: syntax error, unexpected keyword_when|unexpected 'when'/)
end
it "checks patterns until the first matching" do
- eval(<<~RUBY).should == :bar
- case [0, 1]
- in [0]
- :foo
- in [0, 1]
- :bar
- in [0, 1]
- :baz
- end
- RUBY
+ case [0, 1]
+ in [0]
+ :foo
+ in [0, 1]
+ :bar
+ in [0, 1]
+ :baz
+ end.should == :bar
end
it "executes else clause if no pattern matches" do
- eval(<<~RUBY).should == false
- case [0, 1]
- in [0]
- true
- else
- false
- end
- RUBY
+ case [0, 1]
+ in [0]
+ true
+ else
+ false
+ end.should == false
end
it "raises NoMatchingPatternError if no pattern matches and no else clause" do
-> {
- eval <<~RUBY
- case [0, 1]
- in [0]
- end
- RUBY
+ case [0, 1]
+ in [0]
+ end
}.should raise_error(NoMatchingPatternError, /\[0, 1\]/)
+
+ error_pattern = ruby_version_is("3.4") ? /\{a: 0, b: 1\}/ : /\{:a=>0, :b=>1\}/
+ -> {
+ case {a: 0, b: 1}
+ in a: 1, b: 1
+ end
+ }.should raise_error(NoMatchingPatternError, error_pattern)
end
it "raises NoMatchingPatternError if no pattern matches and evaluates the expression only once" do
evals = 0
-> {
- eval <<~RUBY
- case (evals += 1; [0, 1])
- in [0]
- end
- RUBY
+ case (evals += 1; [0, 1])
+ in [0]
+ end
}.should raise_error(NoMatchingPatternError, /\[0, 1\]/)
evals.should == 1
end
@@ -273,230 +250,185 @@ describe "Pattern matching" do
true
end
RUBY
- }.should raise_error(SyntaxError, /unexpected/)
+ }.should raise_error(SyntaxError, /unexpected|expected a delimiter after the patterns of an `in` clause/)
end
it "evaluates the case expression once for multiple patterns, caching the result" do
- eval(<<~RUBY).should == true
- case (ScratchPad << :foo; 1)
- in 0
- false
- in 1
- true
- end
- RUBY
+ case (ScratchPad << :foo; 1)
+ in 0
+ false
+ in 1
+ true
+ end.should == true
ScratchPad.recorded.should == [:foo]
end
describe "guards" do
it "supports if guard" do
- eval(<<~RUBY).should == false
- case 0
- in 0 if false
- true
- else
- false
- end
- RUBY
+ case 0
+ in 0 if false
+ true
+ else
+ false
+ end.should == false
- eval(<<~RUBY).should == true
- case 0
- in 0 if true
- true
- else
- false
- end
- RUBY
+ case 0
+ in 0 if true
+ true
+ else
+ false
+ end.should == true
end
it "supports unless guard" do
- eval(<<~RUBY).should == false
- case 0
- in 0 unless true
- true
- else
- false
- end
- RUBY
+ case 0
+ in 0 unless true
+ true
+ else
+ false
+ end.should == false
- eval(<<~RUBY).should == true
- case 0
- in 0 unless false
- true
- else
- false
- end
- RUBY
+ case 0
+ in 0 unless false
+ true
+ else
+ false
+ end.should == true
end
it "makes bound variables visible in guard" do
- eval(<<~RUBY).should == true
- case [0, 1]
- in [a, 1] if a >= 0
- true
- end
- RUBY
+ case [0, 1]
+ in [a, 1] if a >= 0
+ true
+ end.should == true
end
it "does not evaluate guard if pattern does not match" do
- eval <<~RUBY
- case 0
- in 1 if (ScratchPad << :foo) || true
- else
- end
- RUBY
+ case 0
+ in 1 if (ScratchPad << :foo) || true
+ else
+ end
ScratchPad.recorded.should == []
end
it "takes guards into account when there are several matching patterns" do
- eval(<<~RUBY).should == :bar
- case 0
- in 0 if false
- :foo
- in 0 if true
- :bar
- end
- RUBY
+ case 0
+ in 0 if false
+ :foo
+ in 0 if true
+ :bar
+ end.should == :bar
end
it "executes else clause if no guarded pattern matches" do
- eval(<<~RUBY).should == false
- case 0
- in 0 if false
- true
- else
- false
- end
- RUBY
+ case 0
+ in 0 if false
+ true
+ else
+ false
+ end.should == false
end
it "raises NoMatchingPatternError if no guarded pattern matches and no else clause" do
-> {
- eval <<~RUBY
- case [0, 1]
- in [0, 1] if false
- end
- RUBY
+ case [0, 1]
+ in [0, 1] if false
+ end
}.should raise_error(NoMatchingPatternError, /\[0, 1\]/)
end
end
describe "value pattern" do
it "matches an object such that pattern === object" do
- eval(<<~RUBY).should == true
- case 0
- in 0
- true
- end
- RUBY
+ case 0
+ in 0
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case 0
- in (-1..1)
- true
- end
- RUBY
+ case 0
+ in (
+ -1..1)
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case 0
- in Integer
- true
- end
- RUBY
+ case 0
+ in Integer
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case "0"
- in /0/
- true
- end
- RUBY
+ case "0"
+ in /0/
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case "0"
- in ->(s) { s == "0" }
- true
- end
- RUBY
+ case "0"
+ in -> s { s == "0" }
+ true
+ end.should == true
end
it "allows string literal with interpolation" do
x = "x"
- eval(<<~RUBY).should == true
- case "x"
- in "#{x + ""}"
- true
- end
- RUBY
+ case "x"
+ in "#{x + ""}"
+ true
+ end.should == true
end
end
describe "variable pattern" do
it "matches a value and binds variable name to this value" do
- eval(<<~RUBY).should == 0
- case 0
- in a
- a
- end
- RUBY
+ case 0
+ in a
+ a
+ end.should == 0
end
it "makes bounded variable visible outside a case statement scope" do
- eval(<<~RUBY).should == 0
- case 0
- in a
- end
+ case 0
+ in a
+ end
- a
- RUBY
+ a.should == 0
end
it "create local variables even if a pattern doesn't match" do
- eval(<<~RUBY).should == [0, nil, nil]
- case 0
- in a
- in b
- in c
- end
+ case 0
+ in a
+ in b
+ in c
+ end
- [a, b, c]
- RUBY
+ [a, b, c].should == [0, nil, nil]
end
it "allow using _ name to drop values" do
- eval(<<~RUBY).should == 0
- case [0, 1]
- in [a, _]
- a
- end
- RUBY
+ case [0, 1]
+ in [a, _]
+ a
+ end.should == 0
end
it "supports using _ in a pattern several times" do
- eval(<<~RUBY).should == true
- case [0, 1, 2]
- in [0, _, _]
- true
- end
- RUBY
+ case [0, 1, 2]
+ in [0, _, _]
+ true
+ end.should == true
end
it "supports using any name with _ at the beginning in a pattern several times" do
- eval(<<~RUBY).should == true
- case [0, 1, 2]
- in [0, _x, _x]
- true
- end
- RUBY
+ case [0, 1, 2]
+ in [0, _x, _x]
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case {a: 0, b: 1, c: 2}
- in {a: 0, b: _x, c: _x}
- true
- end
- RUBY
+ case {a: 0, b: 1, c: 2}
+ in {a: 0, b: _x, c: _x}
+ true
+ end.should == true
end
it "does not support using variable name (except _) several times" do
@@ -512,30 +444,24 @@ describe "Pattern matching" do
it "supports existing variables in a pattern specified with ^ operator" do
a = 0
- eval(<<~RUBY).should == true
- case 0
- in ^a
- true
- end
- RUBY
+ case 0
+ in ^a
+ true
+ end.should == true
end
it "allows applying ^ operator to bound variables" do
- eval(<<~RUBY).should == 1
- case [1, 1]
- in [n, ^n]
- n
- end
- RUBY
+ case [1, 1]
+ in [n, ^n]
+ n
+ end.should == 1
- eval(<<~RUBY).should == false
- case [1, 2]
- in [n, ^n]
- true
- else
- false
- end
- RUBY
+ case [1, 2]
+ in [n, ^n]
+ true
+ else
+ false
+ end.should == false
end
it "requires bound variable to be specified in a pattern before ^ operator when it relies on a bound variable" do
@@ -554,12 +480,10 @@ describe "Pattern matching" do
describe "alternative pattern" do
it "matches if any of patterns matches" do
- eval(<<~RUBY).should == true
- case 0
- in 0 | 1 | 2
- true
- end
- RUBY
+ case 0
+ in 0 | 1 | 2
+ true
+ end.should == true
end
it "does not support variable binding" do
@@ -569,124 +493,103 @@ describe "Pattern matching" do
in [0, 0] | [0, a]
end
RUBY
- }.should raise_error(SyntaxError, /illegal variable in alternative pattern/)
+ }.should raise_error(SyntaxError)
end
it "support underscore prefixed variables in alternation" do
- eval(<<~RUBY).should == true
- case [0, 1]
- in [1, _]
- false
- in [0, 0] | [0, _a]
- true
- end
- RUBY
+ case [0, 1]
+ in [1, _]
+ false
+ in [0, 0] | [0, _a]
+ true
+ end.should == true
end
it "can be used as a nested pattern" do
- eval(<<~RUBY).should == true
- case [[1], ["2"]]
- in [[0] | nil, _]
- false
- in [[1], [1]]
- false
- in [[1], [2 | "2"]]
- true
- end
- RUBY
+ case [[1], ["2"]]
+ in [[0] | nil, _]
+ false
+ in [[1], [1]]
+ false
+ in [[1], [2 | "2"]]
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case [1, 2]
- in [0, _] | {a: 0}
- false
- in {a: 1, b: 2} | [1, 2]
- true
- end
- RUBY
+ case [1, 2]
+ in [0, _] | {a: 0}
+ false
+ in {a: 1, b: 2} | [1, 2]
+ true
+ end.should == true
end
end
describe "AS pattern" do
it "binds a variable to a value if pattern matches" do
- eval(<<~RUBY).should == 0
- case 0
- in Integer => n
- n
- end
- RUBY
+ case 0
+ in Integer => n
+ n
+ end.should == 0
end
it "can be used as a nested pattern" do
- eval(<<~RUBY).should == [2, 3]
- case [1, [2, 3]]
- in [1, Array => ary]
- ary
- end
- RUBY
+ case [1, [2, 3]]
+ in [1, Array => ary]
+ ary
+ end.should == [2, 3]
end
end
describe "Array pattern" do
it "supports form Constant(pat, pat, ...)" do
- eval(<<~RUBY).should == true
- case [0, 1, 2]
- in Array(0, 1, 2)
- true
- end
- RUBY
+ case [0, 1, 2]
+ in Array(0, 1, 2)
+ true
+ end.should == true
end
it "supports form Constant[pat, pat, ...]" do
- eval(<<~RUBY).should == true
- case [0, 1, 2]
- in Array[0, 1, 2]
- true
- end
- RUBY
+ case [0, 1, 2]
+ in Array[0, 1, 2]
+ true
+ end.should == true
end
it "supports form [pat, pat, ...]" do
- eval(<<~RUBY).should == true
- case [0, 1, 2]
- in [0, 1, 2]
- true
- end
- RUBY
+ case [0, 1, 2]
+ in [0, 1, 2]
+ true
+ end.should == true
end
it "supports form pat, pat, ..." do
- eval(<<~RUBY).should == true
- case [0, 1, 2]
- in 0, 1, 2
- true
- end
- RUBY
+ case [0, 1, 2]
+ in 0, 1, 2
+ true
+ end.should == true
- eval(<<~RUBY).should == 1
- case [0, 1, 2]
- in 0, a, 2
- a
- end
- RUBY
+ case [0, 1, 2]
+ in 0, a, 2
+ a
+ end.should == 1
- eval(<<~RUBY).should == [1, 2]
- case [0, 1, 2]
- in 0, *rest
- rest
- end
- RUBY
+ case [0, 1, 2]
+ in 0, *rest
+ rest
+ end.should == [1, 2]
end
it "matches an object with #deconstruct method which returns an array and each element in array matches element in pattern" do
obj = Object.new
- def obj.deconstruct; [0, 1] end
- eval(<<~RUBY).should == true
- case obj
- in [Integer, Integer]
- true
- end
- RUBY
+ def obj.deconstruct
+ [0, 1]
+ end
+
+ case obj
+ in [Integer, Integer]
+ true
+ end.should == true
end
it "calls #deconstruct once for multiple patterns, caching the result" do
@@ -697,304 +600,267 @@ describe "Pattern matching" do
[0, 1]
end
- eval(<<~RUBY).should == true
- case obj
- in [1, 2]
- false
- in [0, 1]
- true
- end
- RUBY
+ case obj
+ in [1, 2]
+ false
+ in [0, 1]
+ true
+ end.should == true
ScratchPad.recorded.should == [:deconstruct]
end
it "calls #deconstruct even on objects that are already an array" do
obj = [1, 2]
+
def obj.deconstruct
ScratchPad << :deconstruct
[3, 4]
end
- eval(<<~RUBY).should == true
- case obj
- in [3, 4]
- true
- else
- false
- end
- RUBY
+ case obj
+ in [3, 4]
+ true
+ else
+ false
+ end.should == true
ScratchPad.recorded.should == [:deconstruct]
end
it "does not match object if Constant === object returns false" do
- eval(<<~RUBY).should == false
- case [0, 1, 2]
- in String[0, 1, 2]
- true
- else
- false
- end
- RUBY
+ case [0, 1, 2]
+ in String[0, 1, 2]
+ true
+ else
+ false
+ end.should == false
+ end
+
+ it "checks Constant === object before calling #deconstruct" do
+ c1 = Class.new
+ obj = c1.new
+ obj.should_not_receive(:deconstruct)
+
+ case obj
+ in String[1]
+ true
+ else
+ false
+ end.should == false
end
it "does not match object without #deconstruct method" do
obj = Object.new
obj.should_receive(:respond_to?).with(:deconstruct)
- eval(<<~RUBY).should == false
- case obj
- in Object[]
- true
- else
- false
- end
- RUBY
+ case obj
+ in Object[]
+ true
+ else
+ false
+ end.should == false
end
it "raises TypeError if #deconstruct method does not return array" do
obj = Object.new
- def obj.deconstruct; "" end
+
+ def obj.deconstruct
+ ""
+ end
-> {
- eval <<~RUBY
- case obj
- in Object[]
- else
- end
- RUBY
+ case obj
+ in Object[]
+ else
+ end
}.should raise_error(TypeError, /deconstruct must return Array/)
end
it "accepts a subclass of Array from #deconstruct" do
obj = Object.new
+
def obj.deconstruct
- subarray = Class.new(Array).new(2)
- def subarray.[](n)
- n
- end
- subarray
+ Class.new(Array).new([0, 1])
end
- eval(<<~RUBY).should == true
- case obj
- in [1, 2]
- false
- in [0, 1]
- true
- end
- RUBY
+ case obj
+ in [1, 2]
+ false
+ in [0, 1]
+ true
+ end.should == true
end
it "does not match object if elements of array returned by #deconstruct method does not match elements in pattern" do
obj = Object.new
- def obj.deconstruct; [1] end
- eval(<<~RUBY).should == false
- case obj
- in Object[0]
- true
- else
- false
- end
- RUBY
+ def obj.deconstruct
+ [1]
+ end
+
+ case obj
+ in Object[0]
+ true
+ else
+ false
+ end.should == false
end
it "binds variables" do
- eval(<<~RUBY).should == [0, 1, 2]
- case [0, 1, 2]
- in [a, b, c]
- [a, b, c]
- end
- RUBY
+ case [0, 1, 2]
+ in [a, b, c]
+ [a, b, c]
+ end.should == [0, 1, 2]
end
it "supports splat operator *rest" do
- eval(<<~RUBY).should == [1, 2]
- case [0, 1, 2]
- in [0, *rest]
- rest
- end
- RUBY
+ case [0, 1, 2]
+ in [0, *rest]
+ rest
+ end.should == [1, 2]
end
it "does not match partially by default" do
- eval(<<~RUBY).should == false
- case [0, 1, 2, 3]
- in [1, 2]
- true
- else
- false
- end
- RUBY
+ case [0, 1, 2, 3]
+ in [1, 2]
+ true
+ else
+ false
+ end.should == false
end
it "does match partially from the array beginning if list + , syntax used" do
- eval(<<~RUBY).should == true
- case [0, 1, 2, 3]
- in [0, 1,]
- true
- end
- RUBY
+ case [0, 1, 2, 3]
+ in [0, 1, ]
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case [0, 1, 2, 3]
- in 0, 1,;
- true
- end
- RUBY
+ case [0, 1, 2, 3]
+ in 0, 1,;
+ true
+ end.should == true
end
it "matches [] with []" do
- eval(<<~RUBY).should == true
- case []
- in []
- true
- end
- RUBY
+ case []
+ in []
+ true
+ end.should == true
end
it "matches anything with *" do
- eval(<<~RUBY).should == true
- case [0, 1]
- in *;
- true
- end
- RUBY
+ case [0, 1]
+ in *;
+ true
+ end.should == true
end
it "can be used as a nested pattern" do
- eval(<<~RUBY).should == true
- case [[1], ["2"]]
- in [[0] | nil, _]
- false
- in [[1], [1]]
- false
- in [[1], [2 | "2"]]
- true
- end
- RUBY
+ case [[1], ["2"]]
+ in [[0] | nil, _]
+ false
+ in [[1], [1]]
+ false
+ in [[1], [2 | "2"]]
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case [1, 2]
- in [0, _] | {a: 0}
- false
- in {a: 1, b: 2} | [1, 2]
- true
- end
- RUBY
+ case [1, 2]
+ in [0, _] | {a: 0}
+ false
+ in {a: 1, b: 2} | [1, 2]
+ true
+ end.should == true
end
end
describe "Hash pattern" do
it "supports form Constant(id: pat, id: pat, ...)" do
- eval(<<~RUBY).should == true
- case {a: 0, b: 1}
- in Hash(a: 0, b: 1)
- true
- end
- RUBY
+ case {a: 0, b: 1}
+ in Hash(a: 0, b: 1)
+ true
+ end.should == true
end
it "supports form Constant[id: pat, id: pat, ...]" do
- eval(<<~RUBY).should == true
- case {a: 0, b: 1}
- in Hash[a: 0, b: 1]
- true
- end
- RUBY
+ case {a: 0, b: 1}
+ in Hash[a: 0, b: 1]
+ true
+ end.should == true
end
it "supports form {id: pat, id: pat, ...}" do
- eval(<<~RUBY).should == true
- case {a: 0, b: 1}
- in {a: 0, b: 1}
- true
- end
- RUBY
+ case {a: 0, b: 1}
+ in {a: 0, b: 1}
+ true
+ end.should == true
end
it "supports form id: pat, id: pat, ..." do
- eval(<<~RUBY).should == true
- case {a: 0, b: 1}
- in a: 0, b: 1
- true
- end
- RUBY
+ case {a: 0, b: 1}
+ in a: 0, b: 1
+ true
+ end.should == true
- eval(<<~RUBY).should == [0, 1]
- case {a: 0, b: 1}
- in a: a, b: b
- [a, b]
- end
- RUBY
+ case {a: 0, b: 1}
+ in a: a, b: b
+ [a, b]
+ end.should == [0, 1]
- eval(<<~RUBY).should == { b: 1, c: 2 }
- case {a: 0, b: 1, c: 2}
- in a: 0, **rest
- rest
- end
- RUBY
+ case {a: 0, b: 1, c: 2}
+ in a: 0, **rest
+ rest
+ end.should == {b: 1, c: 2}
end
it "supports a: which means a: a" do
- eval(<<~RUBY).should == [0, 1]
- case {a: 0, b: 1}
- in Hash(a:, b:)
- [a, b]
- end
- RUBY
+ case {a: 0, b: 1}
+ in Hash(a:, b:)
+ [a, b]
+ end.should == [0, 1]
a = b = nil
- eval(<<~RUBY).should == [0, 1]
- case {a: 0, b: 1}
- in Hash[a:, b:]
- [a, b]
- end
- RUBY
+
+ case {a: 0, b: 1}
+ in Hash[a:, b:]
+ [a, b]
+ end.should == [0, 1]
a = b = nil
- eval(<<~RUBY).should == [0, 1]
- case {a: 0, b: 1}
- in {a:, b:}
- [a, b]
- end
- RUBY
+
+ case {a: 0, b: 1}
+ in {a:, b:}
+ [a, b]
+ end.should == [0, 1]
a = nil
- eval(<<~RUBY).should == [0, {b: 1, c: 2}]
- case {a: 0, b: 1, c: 2}
- in {a:, **rest}
- [a, rest]
- end
- RUBY
+
+ case {a: 0, b: 1, c: 2}
+ in {a:, **rest}
+ [a, rest]
+ end.should == [0, {b: 1, c: 2}]
a = b = nil
- eval(<<~RUBY).should == [0, 1]
- case {a: 0, b: 1}
- in a:, b:
- [a, b]
- end
- RUBY
+
+ case {a: 0, b: 1}
+ in a:, b:
+ [a, b]
+ end.should == [0, 1]
end
it "can mix key (a:) and key-value (a: b) declarations" do
- eval(<<~RUBY).should == [0, 1]
- case {a: 0, b: 1}
- in Hash(a:, b: x)
- [a, x]
- end
- RUBY
+ case {a: 0, b: 1}
+ in Hash(a:, b: x)
+ [a, x]
+ end.should == [0, 1]
end
it "supports 'string': key literal" do
- eval(<<~RUBY).should == true
- case {a: 0}
- in {"a": 0}
- true
- end
- RUBY
+ case {a: 0}
+ in {"a": 0}
+ true
+ end.should == true
end
it "does not support non-symbol keys" do
@@ -1004,19 +870,17 @@ describe "Pattern matching" do
in {"a" => 1}
end
RUBY
- }.should raise_error(SyntaxError, /unexpected/)
+ }.should raise_error(SyntaxError, /unexpected|expected a label as the key in the hash pattern/)
end
it "does not support string interpolation in keys" do
- x = "a"
-
-> {
eval <<~'RUBY'
case {a: 1}
in {"#{x}": 1}
end
RUBY
- }.should raise_error(SyntaxError, /symbol literal with interpolation is not allowed/)
+ }.should raise_error(SyntaxError, /symbol literal with interpolation is not allowed|expected a label as the key in the hash pattern/)
end
it "raise SyntaxError when keys duplicate in pattern" do
@@ -1031,14 +895,15 @@ describe "Pattern matching" do
it "matches an object with #deconstruct_keys method which returns a Hash with equal keys and each value in Hash matches value in pattern" do
obj = Object.new
- def obj.deconstruct_keys(*); {a: 1} end
- eval(<<~RUBY).should == true
- case obj
- in {a: 1}
- true
- end
- RUBY
+ def obj.deconstruct_keys(*)
+ {a: 1}
+ end
+
+ case obj
+ in {a: 1}
+ true
+ end.should == true
end
it "calls #deconstruct_keys per pattern" do
@@ -1049,82 +914,92 @@ describe "Pattern matching" do
{a: 1}
end
- eval(<<~RUBY).should == true
- case obj
- in {b: 1}
- false
- in {a: 1}
- true
- end
- RUBY
+ case obj
+ in {b: 1}
+ false
+ in {a: 1}
+ true
+ end.should == true
ScratchPad.recorded.should == [:deconstruct_keys, :deconstruct_keys]
end
it "does not match object if Constant === object returns false" do
- eval(<<~RUBY).should == false
- case {a: 1}
- in String[a: 1]
- true
- else
- false
- end
- RUBY
+ case {a: 1}
+ in String[a: 1]
+ true
+ else
+ false
+ end.should == false
+ end
+
+ it "checks Constant === object before calling #deconstruct_keys" do
+ c1 = Class.new
+ obj = c1.new
+ obj.should_not_receive(:deconstruct_keys)
+
+ case obj
+ in String(a: 1)
+ true
+ else
+ false
+ end.should == false
end
it "does not match object without #deconstruct_keys method" do
obj = Object.new
obj.should_receive(:respond_to?).with(:deconstruct_keys)
- eval(<<~RUBY).should == false
- case obj
- in Object[a: 1]
- true
- else
- false
- end
- RUBY
+ case obj
+ in Object[a: 1]
+ true
+ else
+ false
+ end.should == false
end
it "does not match object if #deconstruct_keys method does not return Hash" do
obj = Object.new
- def obj.deconstruct_keys(*); "" end
+
+ def obj.deconstruct_keys(*)
+ ""
+ end
-> {
- eval <<~RUBY
- case obj
- in Object[a: 1]
- end
- RUBY
+ case obj
+ in Object[a: 1]
+ end
}.should raise_error(TypeError, /deconstruct_keys must return Hash/)
end
it "does not match object if #deconstruct_keys method returns Hash with non-symbol keys" do
obj = Object.new
- def obj.deconstruct_keys(*); {"a" => 1} end
- eval(<<~RUBY).should == false
- case obj
- in Object[a: 1]
- true
- else
- false
- end
- RUBY
+ def obj.deconstruct_keys(*)
+ {"a" => 1}
+ end
+
+ case obj
+ in Object[a: 1]
+ true
+ else
+ false
+ end.should == false
end
it "does not match object if elements of Hash returned by #deconstruct_keys method does not match values in pattern" do
obj = Object.new
- def obj.deconstruct_keys(*); {a: 1} end
- eval(<<~RUBY).should == false
- case obj
- in Object[a: 2]
- true
- else
- false
- end
- RUBY
+ def obj.deconstruct_keys(*)
+ {a: 1}
+ end
+
+ case obj
+ in Object[a: 2]
+ true
+ else
+ false
+ end.should == false
end
it "passes keys specified in pattern as arguments to #deconstruct_keys method" do
@@ -1135,11 +1010,9 @@ describe "Pattern matching" do
{a: 1, b: 2, c: 3}
end
- eval <<~RUBY
- case obj
- in Object[a: 1, b: 2, c: 3]
- end
- RUBY
+ case obj
+ in Object[a: 1, b: 2, c: 3]
+ end
ScratchPad.recorded.sort.should == [[[:a, :b, :c]]]
end
@@ -1152,11 +1025,9 @@ describe "Pattern matching" do
{a: 1, b: 2, c: 3}
end
- eval <<~RUBY
- case obj
- in Object[a: 1, b: 2, **]
- end
- RUBY
+ case obj
+ in Object[a: 1, b: 2, **]
+ end
ScratchPad.recorded.sort.should == [[[:a, :b]]]
end
@@ -1169,100 +1040,105 @@ describe "Pattern matching" do
{a: 1, b: 2}
end
- eval <<~RUBY
- case obj
- in Object[a: 1, **rest]
- end
- RUBY
+ case obj
+ in Object[a: 1, **rest]
+ end
ScratchPad.recorded.should == [[nil]]
end
it "binds variables" do
- eval(<<~RUBY).should == [0, 1, 2]
- case {a: 0, b: 1, c: 2}
- in {a: x, b: y, c: z}
- [x, y, z]
- end
- RUBY
+ case {a: 0, b: 1, c: 2}
+ in {a: x, b: y, c: z}
+ [x, y, z]
+ end.should == [0, 1, 2]
end
it "supports double splat operator **rest" do
- eval(<<~RUBY).should == {b: 1, c: 2}
- case {a: 0, b: 1, c: 2}
- in {a: 0, **rest}
- rest
- end
- RUBY
+ case {a: 0, b: 1, c: 2}
+ in {a: 0, **rest}
+ rest
+ end.should == {b: 1, c: 2}
end
it "treats **nil like there should not be any other keys in a matched Hash" do
- eval(<<~RUBY).should == true
- case {a: 1, b: 2}
- in {a: 1, b: 2, **nil}
- true
- end
- RUBY
+ case {a: 1, b: 2}
+ in {a: 1, b: 2, **nil}
+ true
+ end.should == true
- eval(<<~RUBY).should == false
- case {a: 1, b: 2}
- in {a: 1, **nil}
- true
- else
- false
- end
- RUBY
+ case {a: 1, b: 2}
+ in {a: 1, **nil}
+ true
+ else
+ false
+ end.should == false
end
it "can match partially" do
- eval(<<~RUBY).should == true
- case {a: 1, b: 2}
- in {a: 1}
- true
- end
- RUBY
+ case {a: 1, b: 2}
+ in {a: 1}
+ true
+ end.should == true
end
it "matches {} with {}" do
- eval(<<~RUBY).should == true
- case {}
- in {}
- true
- end
- RUBY
+ case {}
+ in {}
+ true
+ end.should == true
+ end
+
+ it "in {} only matches empty hashes" do
+ case {a: 1}
+ in {}
+ true
+ else
+ false
+ end.should == false
+ end
+
+ it "in {**nil} only matches empty hashes" do
+ case {}
+ in {**nil}
+ true
+ else
+ false
+ end.should == true
+
+ case {a: 1}
+ in {**nil}
+ true
+ else
+ false
+ end.should == false
end
it "matches anything with **" do
- eval(<<~RUBY).should == true
- case {a: 1}
- in **;
- true
- end
- RUBY
+ case {a: 1}
+ in **;
+ true
+ end.should == true
end
it "can be used as a nested pattern" do
- eval(<<~RUBY).should == true
- case {a: {a: 1, b: 1}, b: {a: 1, b: 2}}
- in {a: {a: 0}}
- false
- in {a: {a: 1}, b: {b: 1}}
- false
- in {a: {a: 1}, b: {b: 2}}
- true
- end
- RUBY
+ case {a: {a: 1, b: 1}, b: {a: 1, b: 2}}
+ in {a: {a: 0}}
+ false
+ in {a: {a: 1}, b: {b: 1}}
+ false
+ in {a: {a: 1}, b: {b: 2} }
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case [{a: 1, b: [1]}, {a: 1, c: ["2"]}]
- in [{a:, c:},]
- false
- in [{a: 1, b:}, {a: 1, c: [Integer]}]
- false
- in [_, {a: 1, c: [String]}]
- true
- end
- RUBY
+ case [{a: 1, b: [1]}, {a: 1, c: ["2"]}]
+ in [{a:, c:}, ]
+ false
+ in [{a: 1, b:}, {a: 1, c: [Integer]}]
+ false
+ in [_, {a: 1, c: [String]}]
+ true
+ end.should == true
end
end
@@ -1280,12 +1156,11 @@ describe "Pattern matching" do
Module.new do
using refinery
- result = eval(<<~RUBY)
+ result =
case []
in [0]
true
end
- RUBY
end
result.should == true
@@ -1304,12 +1179,11 @@ describe "Pattern matching" do
Module.new do
using refinery
- result = eval(<<~RUBY)
+ result =
case {}
in a: 0
true
end
- RUBY
end
result.should == true
@@ -1328,44 +1202,38 @@ describe "Pattern matching" do
Module.new do
using refinery
- result = eval(<<~RUBY)
+ result =
case {}
in Array
true
end
- RUBY
end
result.should == true
end
end
- ruby_version_is "3.1" do
+ describe "Ruby 3.1 improvements" do
it "can omit parentheses in one line pattern matching" do
- eval(<<~RUBY).should == [1, 2]
- [1, 2] => a, b
- [a, b]
- RUBY
+ [1, 2] => a, b
+ [a, b].should == [1, 2]
- eval(<<~RUBY).should == 1
- {a: 1} => a:
- a
- RUBY
+ {a: 1} => a:
+ a.should == 1
end
it "supports pinning instance variables" do
- eval(<<~RUBY).should == true
- @a = /a/
- case 'abc'
- in ^@a
- true
- end
- RUBY
+ @a = /a/
+ case 'abc'
+ in ^@a
+ true
+ end.should == true
end
it "supports pinning class variables" do
result = nil
Module.new do
+ # avoid "class variable access from toplevel" runtime error with #module_eval
result = module_eval(<<~RUBY)
@@a = 0..10
@@ -1380,36 +1248,63 @@ describe "Pattern matching" do
end
it "supports pinning global variables" do
- eval(<<~RUBY).should == true
- $a = /a/
- case 'abc'
- in ^$a
- true
- end
- RUBY
+ $a = /a/
+ case 'abc'
+ in ^$a
+ true
+ end.should == true
end
it "supports pinning expressions" do
- eval(<<~RUBY).should == true
- case 'abc'
- in ^(/a/)
- true
- end
- RUBY
+ case 'abc'
+ in ^(/a/)
+ true
+ end.should == true
- eval(<<~RUBY).should == true
- case {name: '2.6', released_at: Time.new(2018, 12, 25)}
- in {released_at: ^(Time.new(2010)..Time.new(2020))}
- true
- end
- RUBY
+ case 0
+ in ^(0 + 0)
+ true
+ end.should == true
+ end
- eval(<<~RUBY).should == true
- case 0
- in ^(0+0)
- true
- end
- RUBY
+ it "supports pinning expressions in array pattern" do
+ case [3]
+ in [^(1 + 2)]
+ true
+ end.should == true
+ end
+
+ it "supports pinning expressions in hash pattern" do
+ case {name: '2.6', released_at: Time.new(2018, 12, 25)}
+ in {released_at: ^(Time.new(2010)..Time.new(2020))}
+ true
+ end.should == true
+ end
+ end
+
+ describe "value in pattern" do
+ it "returns true if the pattern matches" do
+ (1 in 1).should == true
+
+ (1 in Integer).should == true
+
+ e = nil
+ ([1, 2] in [1, e]).should == true
+ e.should == 2
+
+ k = nil
+ ({k: 1} in {k:}).should == true
+ k.should == 1
+ end
+
+ it "returns false if the pattern does not match" do
+ (1 in 2).should == false
+
+ (1 in Float).should == false
+
+ ([1, 2] in [2, e]).should == false
+
+ ({k: 1} in {k: 2}).should == false
end
end
end
diff --git a/spec/ruby/language/precedence_spec.rb b/spec/ruby/language/precedence_spec.rb
index c5adcca2c0..5e606c16d8 100644
--- a/spec/ruby/language/precedence_spec.rb
+++ b/spec/ruby/language/precedence_spec.rb
@@ -294,14 +294,14 @@ describe "Operators" do
-> { eval("1...2...3") }.should raise_error(SyntaxError)
end
- it ".. ... have higher precedence than ? :" do
- # Use variables to avoid warnings
- from = 1
- to = 2
- # These are flip-flop, not Range instances
- (from..to ? 3 : 4).should == 3
- (from...to ? 3 : 4).should == 3
- end
+ it ".. ... have higher precedence than ? :" do
+ # Use variables to avoid warnings
+ from = 1
+ to = 2
+ # These are flip-flop, not Range instances
+ (from..to ? 3 : 4).should == 3
+ (from...to ? 3 : 4).should == 3
+ end
it "? : is right-associative" do
(true ? 2 : 3 ? 4 : 5).should == 2
diff --git a/spec/ruby/language/predefined_spec.rb b/spec/ruby/language/predefined_spec.rb
index 6f779b55f5..fc1667a38f 100644
--- a/spec/ruby/language/predefined_spec.rb
+++ b/spec/ruby/language/predefined_spec.rb
@@ -1,4 +1,5 @@
require_relative '../spec_helper'
+require_relative '../core/exception/shared/set_backtrace'
require 'stringio'
# The following tables are excerpted from Programming Ruby: The Pragmatic Programmer's Guide'
@@ -71,7 +72,7 @@ describe "Predefined global $~" do
match2.should_not == nil
$~.should == match2
- eval 'match3 = /baz/.match("baz")'
+ match3 = /baz/.match("baz")
match3.should_not == nil
$~.should == match3
@@ -88,8 +89,8 @@ describe "Predefined global $~" do
$~ = /foo/.match("foo")
$~.should be_an_instance_of(MatchData)
- -> { $~ = Object.new }.should raise_error(TypeError)
- -> { $~ = 1 }.should raise_error(TypeError)
+ -> { $~ = Object.new }.should raise_error(TypeError, 'wrong argument type Object (expected MatchData)')
+ -> { $~ = 1 }.should raise_error(TypeError, 'wrong argument type Integer (expected MatchData)')
end
it "changes the value of derived capture globals when assigned" do
@@ -133,9 +134,22 @@ describe "Predefined global $&" do
end
it "sets the encoding to the encoding of the source String" do
- "abc".force_encoding(Encoding::EUC_JP) =~ /b/
+ "abc".dup.force_encoding(Encoding::EUC_JP) =~ /b/
$&.encoding.should equal(Encoding::EUC_JP)
end
+
+ it "is read-only" do
+ -> {
+ eval %q{$& = ""}
+ }.should raise_error(SyntaxError, /Can't set variable \$&/)
+ end
+
+ it "is read-only when aliased" do
+ alias $predefined_spec_ampersand $&
+ -> {
+ $predefined_spec_ampersand = ""
+ }.should raise_error(NameError, '$predefined_spec_ampersand is a read-only variable')
+ end
end
describe "Predefined global $`" do
@@ -146,14 +160,27 @@ describe "Predefined global $`" do
end
it "sets the encoding to the encoding of the source String" do
- "abc".force_encoding(Encoding::EUC_JP) =~ /b/
+ "abc".dup.force_encoding(Encoding::EUC_JP) =~ /b/
$`.encoding.should equal(Encoding::EUC_JP)
end
it "sets an empty result to the encoding of the source String" do
- "abc".force_encoding(Encoding::ISO_8859_1) =~ /a/
+ "abc".dup.force_encoding(Encoding::ISO_8859_1) =~ /a/
$`.encoding.should equal(Encoding::ISO_8859_1)
end
+
+ it "is read-only" do
+ -> {
+ eval %q{$` = ""}
+ }.should raise_error(SyntaxError, /Can't set variable \$`/)
+ end
+
+ it "is read-only when aliased" do
+ alias $predefined_spec_backquote $`
+ -> {
+ $predefined_spec_backquote = ""
+ }.should raise_error(NameError, '$predefined_spec_backquote is a read-only variable')
+ end
end
describe "Predefined global $'" do
@@ -164,14 +191,27 @@ describe "Predefined global $'" do
end
it "sets the encoding to the encoding of the source String" do
- "abc".force_encoding(Encoding::EUC_JP) =~ /b/
+ "abc".dup.force_encoding(Encoding::EUC_JP) =~ /b/
$'.encoding.should equal(Encoding::EUC_JP)
end
it "sets an empty result to the encoding of the source String" do
- "abc".force_encoding(Encoding::ISO_8859_1) =~ /c/
+ "abc".dup.force_encoding(Encoding::ISO_8859_1) =~ /c/
$'.encoding.should equal(Encoding::ISO_8859_1)
end
+
+ it "is read-only" do
+ -> {
+ eval %q{$' = ""}
+ }.should raise_error(SyntaxError, /Can't set variable \$'/)
+ end
+
+ it "is read-only when aliased" do
+ alias $predefined_spec_single_quote $'
+ -> {
+ $predefined_spec_single_quote = ""
+ }.should raise_error(NameError, '$predefined_spec_single_quote is a read-only variable')
+ end
end
describe "Predefined global $+" do
@@ -187,9 +227,22 @@ describe "Predefined global $+" do
end
it "sets the encoding to the encoding of the source String" do
- "abc".force_encoding(Encoding::EUC_JP) =~ /(b)/
+ "abc".dup.force_encoding(Encoding::EUC_JP) =~ /(b)/
$+.encoding.should equal(Encoding::EUC_JP)
end
+
+ it "is read-only" do
+ -> {
+ eval %q{$+ = ""}
+ }.should raise_error(SyntaxError, /Can't set variable \$\+/)
+ end
+
+ it "is read-only when aliased" do
+ alias $predefined_spec_plus $+
+ -> {
+ $predefined_spec_plus = ""
+ }.should raise_error(NameError, '$predefined_spec_plus is a read-only variable')
+ end
end
describe "Predefined globals $1..N" do
@@ -214,7 +267,7 @@ describe "Predefined globals $1..N" do
end
it "sets the encoding to the encoding of the source String" do
- "abc".force_encoding(Encoding::EUC_JP) =~ /(b)/
+ "abc".dup.force_encoding(Encoding::EUC_JP) =~ /(b)/
$1.encoding.should equal(Encoding::EUC_JP)
end
end
@@ -229,7 +282,7 @@ describe "Predefined global $stdout" do
end
it "raises TypeError error if assigned to nil" do
- -> { $stdout = nil }.should raise_error(TypeError)
+ -> { $stdout = nil }.should raise_error(TypeError, '$stdout must have write method, NilClass given')
end
it "raises TypeError error if assigned to object that doesn't respond to #write" do
@@ -243,6 +296,22 @@ describe "Predefined global $stdout" do
end
describe "Predefined global $!" do
+ it "is Fiber-local" do
+ Fiber.new do
+ raise "hi"
+ rescue
+ Fiber.yield
+ end.resume
+
+ $!.should == nil
+ end
+
+ it "is read-only" do
+ -> {
+ $! = []
+ }.should raise_error(NameError, '$! is a read-only variable')
+ end
+
# See http://jira.codehaus.org/browse/JRUBY-5550
it "remains nil after a failed core class \"checked\" coercion against a class that defines method_missing" do
$!.should == nil
@@ -502,6 +571,75 @@ describe "Predefined global $!" do
end
end
+describe "Predefined global $@" do
+ it "is Fiber-local" do
+ Fiber.new do
+ raise "hi"
+ rescue
+ Fiber.yield
+ end.resume
+
+ $@.should == nil
+ end
+
+ it "is set to a backtrace of a rescued exception" do
+ begin
+ raise
+ rescue
+ $@.should be_an_instance_of(Array)
+ $@.should == $!.backtrace
+ end
+ end
+
+ it "is cleared when an exception is rescued" do
+ begin
+ raise
+ rescue
+ end
+
+ $@.should == nil
+ end
+
+ it "is not set when there is no current exception" do
+ $@.should == nil
+ end
+
+ it "is set to a backtrace of a rescued exception" do
+ begin
+ raise
+ rescue
+ $@.should be_an_instance_of(Array)
+ $@.should == $!.backtrace
+ end
+ end
+
+ it "is not read-only" do
+ begin
+ raise
+ rescue
+ $@ = []
+ $@.should == []
+ end
+ end
+
+ it_behaves_like :exception_set_backtrace, -> backtrace {
+ exception = nil
+ begin
+ raise
+ rescue
+ $@ = backtrace
+ exception = $!
+ end
+ exception
+ }
+
+ it "cannot be assigned when there is no a rescued exception" do
+ -> {
+ $@ = []
+ }.should raise_error(ArgumentError, '$! not set')
+ end
+end
+
# Input/Output Variables
# ---------------------------------------------------------------------------------------------------
#
@@ -549,12 +687,39 @@ describe "Predefined global $/" do
$VERBOSE = @verbose
end
- it "can be assigned a String" do
- str = "abc"
- $/ = str
- $/.should equal(str)
+ ruby_version_is ""..."4.0" do
+ it "can be assigned a String" do
+ str = +"abc"
+ $/ = str
+ $/.should equal(str)
+ end
end
+ ruby_version_is "4.0" do
+ it "makes a new frozen String from the assigned String" do
+ string_subclass = Class.new(String)
+ str = string_subclass.new("abc")
+ str.instance_variable_set(:@ivar, 1)
+ $/ = str
+ $/.should.frozen?
+ $/.should be_an_instance_of(String)
+ $/.should_not.instance_variable_defined?(:@ivar)
+ $/.should == str
+ end
+
+ it "makes a new frozen String if it's not frozen" do
+ str = +"abc"
+ $/ = str
+ $/.should.frozen?
+ $/.should == str
+ end
+
+ it "assigns the given String if it's frozen and has no instance variables" do
+ str = "abc".freeze
+ $/ = str
+ $/.should equal(str)
+ end
+ end
it "can be assigned nil" do
$/ = nil
$/.should be_nil
@@ -573,15 +738,19 @@ describe "Predefined global $/" do
obj = mock("$/ value")
obj.should_not_receive(:to_str)
- -> { $/ = obj }.should raise_error(TypeError)
+ -> { $/ = obj }.should raise_error(TypeError, 'value of $/ must be String')
end
it "raises a TypeError if assigned an Integer" do
- -> { $/ = 1 }.should raise_error(TypeError)
+ -> { $/ = 1 }.should raise_error(TypeError, 'value of $/ must be String')
end
it "raises a TypeError if assigned a boolean" do
- -> { $/ = true }.should raise_error(TypeError)
+ -> { $/ = true }.should raise_error(TypeError, 'value of $/ must be String')
+ end
+
+ it "warns if assigned non-nil" do
+ -> { $/ = "_" }.should complain(/warning: (?:non-nil )?[`']\$\/' is deprecated/)
end
end
@@ -598,10 +767,38 @@ describe "Predefined global $-0" do
$VERBOSE = @verbose
end
- it "can be assigned a String" do
- str = "abc"
- $-0 = str
- $-0.should equal(str)
+ ruby_version_is ""..."4.0" do
+ it "can be assigned a String" do
+ str = +"abc"
+ $-0 = str
+ $-0.should equal(str)
+ end
+ end
+
+ ruby_version_is "4.0" do
+ it "makes a new frozen String from the assigned String" do
+ string_subclass = Class.new(String)
+ str = string_subclass.new("abc")
+ str.instance_variable_set(:@ivar, 1)
+ $-0 = str
+ $-0.should.frozen?
+ $-0.should be_an_instance_of(String)
+ $-0.should_not.instance_variable_defined?(:@ivar)
+ $-0.should == str
+ end
+
+ it "makes a new frozen String if it's not frozen" do
+ str = +"abc"
+ $-0 = str
+ $-0.should.frozen?
+ $-0.should == str
+ end
+
+ it "assigns the given String if it's frozen and has no instance variables" do
+ str = "abc".freeze
+ $-0 = str
+ $-0.should equal(str)
+ end
end
it "can be assigned nil" do
@@ -622,15 +819,19 @@ describe "Predefined global $-0" do
obj = mock("$-0 value")
obj.should_not_receive(:to_str)
- -> { $-0 = obj }.should raise_error(TypeError)
+ -> { $-0 = obj }.should raise_error(TypeError, 'value of $-0 must be String')
end
it "raises a TypeError if assigned an Integer" do
- -> { $-0 = 1 }.should raise_error(TypeError)
+ -> { $-0 = 1 }.should raise_error(TypeError, 'value of $-0 must be String')
end
it "raises a TypeError if assigned a boolean" do
- -> { $-0 = true }.should raise_error(TypeError)
+ -> { $-0 = true }.should raise_error(TypeError, 'value of $-0 must be String')
+ end
+
+ it "warns if assigned non-nil" do
+ -> { $-0 = "_" }.should complain(/warning: (?:non-nil )?[`']\$-0' is deprecated/)
end
end
@@ -664,12 +865,16 @@ describe "Predefined global $\\" do
obj = mock("$\\ value")
obj.should_not_receive(:to_str)
- -> { $\ = obj }.should raise_error(TypeError)
+ -> { $\ = obj }.should raise_error(TypeError, 'value of $\ must be String')
end
it "raises a TypeError if assigned not String" do
- -> { $\ = 1 }.should raise_error(TypeError)
- -> { $\ = true }.should raise_error(TypeError)
+ -> { $\ = 1 }.should raise_error(TypeError, 'value of $\ must be String')
+ -> { $\ = true }.should raise_error(TypeError, 'value of $\ must be String')
+ end
+
+ it "warns if assigned non-nil" do
+ -> { $\ = "_" }.should complain(/warning: (?:non-nil )?[`']\$\\' is deprecated/)
end
end
@@ -683,11 +888,11 @@ describe "Predefined global $," do
end
it "raises TypeError if assigned a non-String" do
- -> { $, = Object.new }.should raise_error(TypeError)
+ -> { $, = Object.new }.should raise_error(TypeError, 'value of $, must be String')
end
it "warns if assigned non-nil" do
- -> { $, = "_" }.should complain(/warning: `\$,' is deprecated/)
+ -> { $, = "_" }.should complain(/warning: (?:non-nil )?[`']\$,' is deprecated/)
end
end
@@ -724,7 +929,7 @@ describe "Predefined global $;" do
end
it "warns if assigned non-nil" do
- -> { $; = "_" }.should complain(/warning: `\$;' is deprecated/)
+ -> { $; = "_" }.should complain(/warning: (?:non-nil )?[`']\$;' is deprecated/)
end
end
@@ -758,7 +963,7 @@ describe "Predefined global $_" do
match.should == "bar\n"
$_.should == match
- eval 'match = stdin.gets'
+ match = stdin.gets
match.should == "baz\n"
$_.should == match
@@ -868,22 +1073,28 @@ describe "Execution variable $:" do
it "is read-only" do
-> {
$: = []
- }.should raise_error(NameError)
+ }.should raise_error(NameError, '$: is a read-only variable')
-> {
$LOAD_PATH = []
- }.should raise_error(NameError)
+ }.should raise_error(NameError, '$LOAD_PATH is a read-only variable')
-> {
$-I = []
- }.should raise_error(NameError)
+ }.should raise_error(NameError, '$-I is a read-only variable')
end
it "default $LOAD_PATH entries until sitelibdir included have @gem_prelude_index set" do
skip "no sense in ruby itself" if MSpecScript.instance_variable_defined?(:@testing_ruby)
- $:.should.include?(RbConfig::CONFIG['sitelibdir'])
- idx = $:.index(RbConfig::CONFIG['sitelibdir'])
+ if platform_is :windows
+ # See https://github.com/ruby/setup-ruby/pull/762#issuecomment-2917460440
+ $:.should.find { |e| File.realdirpath(e) == RbConfig::CONFIG['sitelibdir'] }
+ idx = $:.index { |e| File.realdirpath(e) == RbConfig::CONFIG['sitelibdir'] }
+ else
+ $:.should.include?(RbConfig::CONFIG['sitelibdir'])
+ idx = $:.index(RbConfig::CONFIG['sitelibdir'])
+ end
$:[idx..-1].all? { |p| p.instance_variable_defined?(:@gem_prelude_index) }.should be_true
$:[0...idx].all? { |p| !p.instance_variable_defined?(:@gem_prelude_index) }.should be_true
@@ -898,11 +1109,11 @@ describe "Global variable $\"" do
it "is read-only" do
-> {
$" = []
- }.should raise_error(NameError)
+ }.should raise_error(NameError, '$" is a read-only variable')
-> {
$LOADED_FEATURES = []
- }.should raise_error(NameError)
+ }.should raise_error(NameError, '$LOADED_FEATURES is a read-only variable')
end
end
@@ -910,7 +1121,7 @@ describe "Global variable $<" do
it "is read-only" do
-> {
$< = nil
- }.should raise_error(NameError)
+ }.should raise_error(NameError, '$< is a read-only variable')
end
end
@@ -918,7 +1129,7 @@ describe "Global variable $FILENAME" do
it "is read-only" do
-> {
$FILENAME = "-"
- }.should raise_error(NameError)
+ }.should raise_error(NameError, '$FILENAME is a read-only variable')
end
end
@@ -926,7 +1137,7 @@ describe "Global variable $?" do
it "is read-only" do
-> {
$? = nil
- }.should raise_error(NameError)
+ }.should raise_error(NameError, '$? is a read-only variable')
end
it "is thread-local" do
@@ -937,19 +1148,19 @@ end
describe "Global variable $-a" do
it "is read-only" do
- -> { $-a = true }.should raise_error(NameError)
+ -> { $-a = true }.should raise_error(NameError, '$-a is a read-only variable')
end
end
describe "Global variable $-l" do
it "is read-only" do
- -> { $-l = true }.should raise_error(NameError)
+ -> { $-l = true }.should raise_error(NameError, '$-l is a read-only variable')
end
end
describe "Global variable $-p" do
it "is read-only" do
- -> { $-p = true }.should raise_error(NameError)
+ -> { $-p = true }.should raise_error(NameError, '$-p is a read-only variable')
end
end
@@ -1037,7 +1248,7 @@ describe "Global variable $0" do
it "is the path given as the main script and the same as __FILE__" do
script = "fixtures/dollar_zero.rb"
- Dir.chdir(File.dirname(__FILE__)) do
+ Dir.chdir(__dir__) do
ruby_exe(script).should == "#{script}\n#{script}\nOK"
end
end
@@ -1100,7 +1311,7 @@ describe "The predefined standard object nil" do
end
it "raises a SyntaxError if assigned to" do
- -> { eval("nil = true") }.should raise_error(SyntaxError)
+ -> { eval("nil = true") }.should raise_error(SyntaxError, /Can't assign to nil/)
end
end
@@ -1110,7 +1321,7 @@ describe "The predefined standard object true" do
end
it "raises a SyntaxError if assigned to" do
- -> { eval("true = false") }.should raise_error(SyntaxError)
+ -> { eval("true = false") }.should raise_error(SyntaxError, /Can't assign to true/)
end
end
@@ -1120,13 +1331,13 @@ describe "The predefined standard object false" do
end
it "raises a SyntaxError if assigned to" do
- -> { eval("false = nil") }.should raise_error(SyntaxError)
+ -> { eval("false = nil") }.should raise_error(SyntaxError, /Can't assign to false/)
end
end
describe "The self pseudo-variable" do
it "raises a SyntaxError if assigned to" do
- -> { eval("self = 1") }.should raise_error(SyntaxError)
+ -> { eval("self = 1") }.should raise_error(SyntaxError, /Can't change the value of self/)
end
end
@@ -1317,9 +1528,9 @@ end
describe "$LOAD_PATH.resolve_feature_path" do
it "returns what will be loaded without actual loading, .rb file" do
- extension, path = $LOAD_PATH.resolve_feature_path('set')
+ extension, path = $LOAD_PATH.resolve_feature_path('pp')
extension.should == :rb
- path.should.end_with?('/set.rb')
+ path.should.end_with?('/pp.rb')
end
it "returns what will be loaded without actual loading, .so file" do
@@ -1331,16 +1542,8 @@ describe "$LOAD_PATH.resolve_feature_path" do
path.should.end_with?("/etc.#{RbConfig::CONFIG['DLEXT']}")
end
- ruby_version_is ""..."3.1" do
- it "raises LoadError if feature cannot be found" do
- -> { $LOAD_PATH.resolve_feature_path('noop') }.should raise_error(LoadError)
- end
- end
-
- ruby_version_is "3.1" do
- it "return nil if feature cannot be found" do
- $LOAD_PATH.resolve_feature_path('noop').should be_nil
- end
+ it "return nil if feature cannot be found" do
+ $LOAD_PATH.resolve_feature_path('noop').should be_nil
end
end
diff --git a/spec/ruby/language/private_spec.rb b/spec/ruby/language/private_spec.rb
index ddf185e6d2..b04aa25c9e 100644
--- a/spec/ruby/language/private_spec.rb
+++ b/spec/ruby/language/private_spec.rb
@@ -34,7 +34,7 @@ describe "The private keyword" do
it "changes visibility of previously called method" do
klass = Class.new do
def foo
- "foo"
+ "foo"
end
end
f = klass.new
diff --git a/spec/ruby/language/proc_spec.rb b/spec/ruby/language/proc_spec.rb
index cc69b7799c..ca9a13aa61 100644
--- a/spec/ruby/language/proc_spec.rb
+++ b/spec/ruby/language/proc_spec.rb
@@ -104,7 +104,7 @@ describe "A Proc" do
end
it "assigns all passed values after the first to the rest argument" do
- @l.call(1, 2, 3).should == [1, [2, 3]]
+ @l.call(1, 2, 3).should == [1, [2, 3]]
end
it "does not call #to_ary to convert a single passed object to an Array" do
diff --git a/spec/ruby/language/regexp/anchors_spec.rb b/spec/ruby/language/regexp/anchors_spec.rb
index 0129e255da..cdc06c0b4d 100644
--- a/spec/ruby/language/regexp/anchors_spec.rb
+++ b/spec/ruby/language/regexp/anchors_spec.rb
@@ -124,10 +124,10 @@ describe "Regexps with anchors" do
/foo\b/.match("foo").to_a.should == ["foo"]
/foo\b/.match("foo\n").to_a.should == ["foo"]
LanguageSpecs.white_spaces.scan(/./).each do |c|
- /foo\b/.match("foo" + c).to_a.should == ["foo"]
+ /foo\b/.match("foo" + c).to_a.should == ["foo"]
end
LanguageSpecs.non_alphanum_non_space.scan(/./).each do |c|
- /foo\b/.match("foo" + c).to_a.should == ["foo"]
+ /foo\b/.match("foo" + c).to_a.should == ["foo"]
end
/foo\b/.match("foo\0").to_a.should == ["foo"]
# Basic non-matching
@@ -145,10 +145,10 @@ describe "Regexps with anchors" do
/foo\B/.match("foo").should be_nil
/foo\B/.match("foo\n").should be_nil
LanguageSpecs.white_spaces.scan(/./).each do |c|
- /foo\B/.match("foo" + c).should be_nil
+ /foo\B/.match("foo" + c).should be_nil
end
LanguageSpecs.non_alphanum_non_space.scan(/./).each do |c|
- /foo\B/.match("foo" + c).should be_nil
+ /foo\B/.match("foo" + c).should be_nil
end
/foo\B/.match("foo\0").should be_nil
end
diff --git a/spec/ruby/language/regexp/back-references_spec.rb b/spec/ruby/language/regexp/back-references_spec.rb
index 26750c20c5..627c8daace 100644
--- a/spec/ruby/language/regexp/back-references_spec.rb
+++ b/spec/ruby/language/regexp/back-references_spec.rb
@@ -22,6 +22,15 @@ describe "Regexps with back-references" do
$10.should == "0"
end
+ it "returns nil for numbered variable with too large index" do
+ -> {
+ eval(<<~CODE).should == nil
+ "a" =~ /(.)/
+ eval('$4294967296')
+ CODE
+ }.should complain(/warning: ('|`)\$4294967296' is too big for a number variable, always nil/)
+ end
+
it "will not clobber capture variables across threads" do
cap1, cap2, cap3 = nil
"foo" =~ /(o+)/
diff --git a/spec/ruby/language/regexp/character_classes_spec.rb b/spec/ruby/language/regexp/character_classes_spec.rb
index 98d431a817..018757db41 100644
--- a/spec/ruby/language/regexp/character_classes_spec.rb
+++ b/spec/ruby/language/regexp/character_classes_spec.rb
@@ -113,7 +113,7 @@ describe "Regexp with character classes" do
end
it "doesn't matches Unicode marks with [[:alnum:]]" do
- "\u{36F}".match(/[[:alnum:]]/).should be_nil
+ "\u{3099}".match(/[[:alnum:]]/).should be_nil
end
it "doesn't match Unicode control characters with [[:alnum:]]" do
@@ -133,7 +133,7 @@ describe "Regexp with character classes" do
end
it "doesn't matches Unicode marks with [[:alpha:]]" do
- "\u{36F}".match(/[[:alpha:]]/).should be_nil
+ "\u{3099}".match(/[[:alpha:]]/).should be_nil
end
it "doesn't match Unicode control characters with [[:alpha:]]" do
@@ -226,7 +226,7 @@ describe "Regexp with character classes" do
end
it "matches Unicode letter characters with [[:graph:]]" do
- "à".match(/[[:graph:]]/).to_a.should == ["à"]
+ "à".match(/[[:graph:]]/).to_a.should == ["à"]
end
it "matches Unicode digits with [[:graph:]]" do
@@ -562,6 +562,13 @@ describe "Regexp with character classes" do
"\u{16EE}".match(/[[:word:]]/).to_a.should == ["\u{16EE}"]
end
+ ruby_bug "#19417", ""..."3.4.6" do
+ it "matches Unicode join control characters with [[:word:]]" do
+ "\u{200C}".match(/[[:word:]]/).to_a.should == ["\u{200C}"]
+ "\u{200D}".match(/[[:word:]]/).to_a.should == ["\u{200D}"]
+ end
+ end
+
it "doesn't match Unicode No characters with [[:word:]]" do
"\u{17F0}".match(/[[:word:]]/).should be_nil
end
diff --git a/spec/ruby/language/regexp/encoding_spec.rb b/spec/ruby/language/regexp/encoding_spec.rb
index febc3fdb37..ceb9cf823a 100644
--- a/spec/ruby/language/regexp/encoding_spec.rb
+++ b/spec/ruby/language/regexp/encoding_spec.rb
@@ -1,21 +1,21 @@
-# -*- encoding: binary -*-
+# encoding: binary
require_relative '../../spec_helper'
require_relative '../fixtures/classes'
describe "Regexps with encoding modifiers" do
it "supports /e (EUC encoding)" do
- match = /./e.match("\303\251".force_encoding(Encoding::EUC_JP))
- match.to_a.should == ["\303\251".force_encoding(Encoding::EUC_JP)]
+ match = /./e.match("\303\251".dup.force_encoding(Encoding::EUC_JP))
+ match.to_a.should == ["\303\251".dup.force_encoding(Encoding::EUC_JP)]
end
it "supports /e (EUC encoding) with interpolation" do
- match = /#{/./}/e.match("\303\251".force_encoding(Encoding::EUC_JP))
- match.to_a.should == ["\303\251".force_encoding(Encoding::EUC_JP)]
+ match = /#{/./}/e.match("\303\251".dup.force_encoding(Encoding::EUC_JP))
+ match.to_a.should == ["\303\251".dup.force_encoding(Encoding::EUC_JP)]
end
it "supports /e (EUC encoding) with interpolation /o" do
- match = /#{/./}/e.match("\303\251".force_encoding(Encoding::EUC_JP))
- match.to_a.should == ["\303\251".force_encoding(Encoding::EUC_JP)]
+ match = /#{/./}/e.match("\303\251".dup.force_encoding(Encoding::EUC_JP))
+ match.to_a.should == ["\303\251".dup.force_encoding(Encoding::EUC_JP)]
end
it 'uses EUC-JP as /e encoding' do
@@ -39,7 +39,11 @@ describe "Regexps with encoding modifiers" do
end
it "warns when using /n with a match string with non-ASCII characters and an encoding other than ASCII-8BIT" do
- -> { /./n.match("\303\251".force_encoding('utf-8')) }.should complain(%r{historical binary regexp match /.../n against UTF-8 string})
+ -> {
+ eval <<~RUBY
+ /./n.match("\303\251".dup.force_encoding('utf-8'))
+ RUBY
+ }.should complain(%r{historical binary regexp match /.../n against UTF-8 string})
end
it 'uses US-ASCII as /n encoding if all chars are 7-bit' do
@@ -63,18 +67,18 @@ describe "Regexps with encoding modifiers" do
end
it "supports /s (Windows_31J encoding)" do
- match = /./s.match("\303\251".force_encoding(Encoding::Windows_31J))
- match.to_a.should == ["\303".force_encoding(Encoding::Windows_31J)]
+ match = /./s.match("\303\251".dup.force_encoding(Encoding::Windows_31J))
+ match.to_a.should == ["\303".dup.force_encoding(Encoding::Windows_31J)]
end
it "supports /s (Windows_31J encoding) with interpolation" do
- match = /#{/./}/s.match("\303\251".force_encoding(Encoding::Windows_31J))
- match.to_a.should == ["\303".force_encoding(Encoding::Windows_31J)]
+ match = /#{/./}/s.match("\303\251".dup.force_encoding(Encoding::Windows_31J))
+ match.to_a.should == ["\303".dup.force_encoding(Encoding::Windows_31J)]
end
it "supports /s (Windows_31J encoding) with interpolation and /o" do
- match = /#{/./}/s.match("\303\251".force_encoding(Encoding::Windows_31J))
- match.to_a.should == ["\303".force_encoding(Encoding::Windows_31J)]
+ match = /#{/./}/s.match("\303\251".dup.force_encoding(Encoding::Windows_31J))
+ match.to_a.should == ["\303".dup.force_encoding(Encoding::Windows_31J)]
end
it 'uses Windows-31J as /s encoding' do
@@ -86,15 +90,15 @@ describe "Regexps with encoding modifiers" do
end
it "supports /u (UTF8 encoding)" do
- /./u.match("\303\251".force_encoding('utf-8')).to_a.should == ["\u{e9}"]
+ /./u.match("\303\251".dup.force_encoding('utf-8')).to_a.should == ["\u{e9}"]
end
it "supports /u (UTF8 encoding) with interpolation" do
- /#{/./}/u.match("\303\251".force_encoding('utf-8')).to_a.should == ["\u{e9}"]
+ /#{/./}/u.match("\303\251".dup.force_encoding('utf-8')).to_a.should == ["\u{e9}"]
end
it "supports /u (UTF8 encoding) with interpolation and /o" do
- /#{/./}/u.match("\303\251".force_encoding('utf-8')).to_a.should == ["\u{e9}"]
+ /#{/./}/u.match("\303\251".dup.force_encoding('utf-8')).to_a.should == ["\u{e9}"]
end
it 'uses UTF-8 as /u encoding' do
@@ -122,26 +126,26 @@ describe "Regexps with encoding modifiers" do
end
it "raises Encoding::CompatibilityError when the regexp has a fixed, non-ASCII-compatible encoding" do
- -> { Regexp.new("".force_encoding("UTF-16LE"), Regexp::FIXEDENCODING) =~ " ".encode("UTF-8") }.should raise_error(Encoding::CompatibilityError)
+ -> { Regexp.new("".dup.force_encoding("UTF-16LE"), Regexp::FIXEDENCODING) =~ " ".encode("UTF-8") }.should raise_error(Encoding::CompatibilityError)
end
it "raises Encoding::CompatibilityError when the regexp has a fixed encoding and the match string has non-ASCII characters" do
- -> { Regexp.new("".force_encoding("US-ASCII"), Regexp::FIXEDENCODING) =~ "\303\251".force_encoding('UTF-8') }.should raise_error(Encoding::CompatibilityError)
+ -> { Regexp.new("".dup.force_encoding("US-ASCII"), Regexp::FIXEDENCODING) =~ "\303\251".dup.force_encoding('UTF-8') }.should raise_error(Encoding::CompatibilityError)
end
it "raises ArgumentError when trying to match a broken String" do
- s = "\x80".force_encoding('UTF-8')
+ s = "\x80".dup.force_encoding('UTF-8')
-> { s =~ /./ }.should raise_error(ArgumentError, "invalid byte sequence in UTF-8")
end
it "computes the Regexp Encoding for each interpolated Regexp instance" do
make_regexp = -> str { /#{str}/ }
- r = make_regexp.call("été".force_encoding(Encoding::UTF_8))
+ r = make_regexp.call("été".dup.force_encoding(Encoding::UTF_8))
r.should.fixed_encoding?
r.encoding.should == Encoding::UTF_8
- r = make_regexp.call("abc".force_encoding(Encoding::UTF_8))
+ r = make_regexp.call("abc".dup.force_encoding(Encoding::UTF_8))
r.should_not.fixed_encoding?
r.encoding.should == Encoding::US_ASCII
end
diff --git a/spec/ruby/language/regexp/escapes_spec.rb b/spec/ruby/language/regexp/escapes_spec.rb
index 16a4d8c23b..541998b937 100644
--- a/spec/ruby/language/regexp/escapes_spec.rb
+++ b/spec/ruby/language/regexp/escapes_spec.rb
@@ -1,4 +1,4 @@
-# -*- encoding: binary -*-
+# encoding: binary
require_relative '../../spec_helper'
require_relative '../fixtures/classes'
diff --git a/spec/ruby/language/regexp/grouping_spec.rb b/spec/ruby/language/regexp/grouping_spec.rb
index 2f04a04018..313858f714 100644
--- a/spec/ruby/language/regexp/grouping_spec.rb
+++ b/spec/ruby/language/regexp/grouping_spec.rb
@@ -12,7 +12,7 @@ describe "Regexps with grouping" do
end
it "raises a SyntaxError when parentheses aren't balanced" do
- -> { eval "/(hay(st)ack/" }.should raise_error(SyntaxError)
+ -> { eval "/(hay(st)ack/" }.should raise_error(SyntaxError)
end
it "supports (?: ) (non-capturing group)" do
diff --git a/spec/ruby/language/regexp_spec.rb b/spec/ruby/language/regexp_spec.rb
index cdafd29f49..ce344b5b05 100644
--- a/spec/ruby/language/regexp_spec.rb
+++ b/spec/ruby/language/regexp_spec.rb
@@ -62,7 +62,7 @@ describe "Literal Regexps" do
end
end
- it "supports non-paired delimiters delimiters with %r" do
+ it "supports non-paired delimiters with %r" do
LanguageSpecs.non_paired_delimiters.each do |c|
eval("%r#{c} foo #{c}").should == / foo /
end
@@ -112,7 +112,7 @@ describe "Literal Regexps" do
/foo.(?<=\d)/.match("fooA foo1").to_a.should == ["foo1"]
end
- ruby_bug "#13671", ""..."3.4" do # https://bugs.ruby-lang.org/issues/13671
+ ruby_bug "#13671", ""..."4.0" do # https://bugs.ruby-lang.org/issues/13671
it "handles a lookbehind with ss characters" do
r = Regexp.new("(?<!dss)", Regexp::IGNORECASE)
r.should =~ "✨"
diff --git a/spec/ruby/language/rescue_spec.rb b/spec/ruby/language/rescue_spec.rb
index b91b52fa0f..6be3bfd023 100644
--- a/spec/ruby/language/rescue_spec.rb
+++ b/spec/ruby/language/rescue_spec.rb
@@ -52,6 +52,16 @@ describe "The rescue keyword" do
RescueSpecs::SafeNavigationSetterCaptor.should_capture_exception
end
+ it 'using a safely navigated setter method on a nil target' do
+ target = nil
+ begin
+ raise SpecificExampleException, "Raising this to be handled below"
+ rescue SpecificExampleException => target&.captured_error
+ :caught
+ end.should == :caught
+ target.should be_nil
+ end
+
it 'using a setter method' do
RescueSpecs::SetterCaptor.should_capture_exception
end
@@ -61,6 +71,82 @@ describe "The rescue keyword" do
end
end
+ describe 'capturing in a local variable (that defines it)' do
+ it 'captures successfully in a method' do
+ ScratchPad.record []
+
+ def a
+ raise "message"
+ rescue => e
+ ScratchPad << e.message
+ end
+
+ a
+ ScratchPad.recorded.should == ["message"]
+ end
+
+ it 'captures successfully in a block' do
+ ScratchPad.record []
+
+ p = proc do
+ raise "message"
+ rescue => e
+ ScratchPad << e.message
+ end
+
+ p.call
+ ScratchPad.recorded.should == ["message"]
+ end
+
+ it 'captures successfully in a class' do
+ ScratchPad.record []
+
+ class RescueSpecs::C
+ raise "message"
+ rescue => e
+ ScratchPad << e.message
+ end
+
+ ScratchPad.recorded.should == ["message"]
+ end
+
+ it 'captures successfully in a module' do
+ ScratchPad.record []
+
+ module RescueSpecs::M
+ raise "message"
+ rescue => e
+ ScratchPad << e.message
+ end
+
+ ScratchPad.recorded.should == ["message"]
+ end
+
+ it 'captures sucpcessfully in a singleton class' do
+ ScratchPad.record []
+
+ class << Object.new
+ raise "message"
+ rescue => e
+ ScratchPad << e.message
+ end
+
+ ScratchPad.recorded.should == ["message"]
+ end
+
+ it 'captures successfully at the top-level' do
+ ScratchPad.record []
+ loaded_features = $".dup
+ begin
+ require_relative 'fixtures/rescue/top_level'
+
+ ScratchPad.recorded.should == ["message"]
+ ensure
+ $".replace loaded_features
+ end
+ end
+ end
+
it "returns value from `rescue` if an exception was raised" do
begin
raise
@@ -191,7 +277,7 @@ describe "The rescue keyword" do
rescue ArgumentError
end
rescue StandardError => e
- e.backtrace.first.should include ":in `raise_standard_error'"
+ e.backtrace.first.should =~ /:in [`'](?:RescueSpecs\.)?raise_standard_error'/
else
fail("exception wasn't handled by the correct rescue block")
end
@@ -481,6 +567,21 @@ describe "The rescue keyword" do
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[0].should =~ /#{__FILE__}:#{line-3}:in 'foo'/
+ foo[1].should =~ /#{__FILE__}:#{line+2}:in 'block/
+ end
+ end
+
describe "inline form" do
it "can be inlined" do
a = 1/0 rescue 1
diff --git a/spec/ruby/language/reserved_keywords.rb b/spec/ruby/language/reserved_keywords.rb
new file mode 100644
index 0000000000..6c40e34ccc
--- /dev/null
+++ b/spec/ruby/language/reserved_keywords.rb
@@ -0,0 +1,149 @@
+require_relative '../spec_helper'
+
+describe "Ruby's reserved keywords" do
+ # Copied from https://github.com/ruby/ruby/blob/master/defs/keywords
+ keywords = %w[
+ alias
+ and
+ begin
+ BEGIN
+ break
+ case
+ class
+ def
+ defined?
+ do
+ else
+ elsif
+ end
+ END
+ ensure
+ false
+ for
+ if
+ in
+ module
+ next
+ nil
+ not
+ or
+ redo
+ rescue
+ retry
+ return
+ self
+ super
+ then
+ true
+ undef
+ unless
+ until
+ when
+ while
+ yield
+ __ENCODING__
+ __FILE__
+ __LINE__
+ ]
+
+ keywords.each do |name|
+ describe "keyword '#{name}'" do
+ it "can't be used as local variable name" do
+ -> { eval(<<~RUBY) }.should raise_error(SyntaxError)
+ #{name} = :local_variable
+ RUBY
+ end
+
+ if name == "defined?"
+ it "can't be used as an instance variable name" do
+ -> { eval(<<~RUBY) }.should raise_error(SyntaxError)
+ @#{name} = :instance_variable
+ RUBY
+ end
+
+ it "can't be used as a class variable name" do
+ -> { eval(<<~RUBY) }.should raise_error(SyntaxError)
+ class C
+ @@#{name} = :class_variable
+ end
+ RUBY
+ end
+
+ it "can't be used as a global variable name" do
+ -> { eval(<<~RUBY) }.should raise_error(SyntaxError)
+ $#{name} = :global_variable
+ RUBY
+ end
+ else
+ it "can be used as an instance variable name" do
+ result = eval <<~RUBY
+ @#{name} = :instance_variable
+ @#{name}
+ RUBY
+
+ result.should == :instance_variable
+ end
+
+ it "can be used as a class variable name" do
+ result = eval <<~RUBY
+ class C
+ @@#{name} = :class_variable
+ @@#{name}
+ end
+ RUBY
+
+ result.should == :class_variable
+ end
+
+ it "can be used as a global variable name" do
+ result = eval <<~RUBY
+ $#{name} = :global_variable
+ $#{name}
+ RUBY
+
+ result.should == :global_variable
+ end
+ end
+
+ it "can't be used as a positional parameter name" do
+ -> { eval(<<~RUBY) }.should raise_error(SyntaxError)
+ def x(#{name}); end
+ RUBY
+ end
+
+ invalid_kw_param_names = ["BEGIN","END","defined?"]
+
+ if invalid_kw_param_names.include?(name)
+ it "can't be used a keyword parameter name" do
+ -> { eval(<<~RUBY) }.should raise_error(SyntaxError)
+ def m(#{name}:); end
+ RUBY
+ end
+ else
+ it "can be used a keyword parameter name" do
+ result = instance_eval <<~RUBY
+ def m(#{name}:)
+ binding.local_variable_get(:#{name})
+ end
+
+ m(#{name}: :argument)
+ RUBY
+
+ result.should == :argument
+ end
+ end
+
+ it "can be used as a method name" do
+ result = instance_eval <<~RUBY
+ def #{name}
+ :method_return_value
+ end
+
+ send(:#{name})
+ RUBY
+
+ result.should == :method_return_value
+ end
+ end
+ end
+end
diff --git a/spec/ruby/language/retry_spec.rb b/spec/ruby/language/retry_spec.rb
index ee5377946f..669d5f0ff5 100644
--- a/spec/ruby/language/retry_spec.rb
+++ b/spec/ruby/language/retry_spec.rb
@@ -31,8 +31,11 @@ describe "The retry statement" do
results.should == [1, 2, 3, 1, 2, 4, 5, 6, 4, 5]
end
- it "raises a SyntaxError when used outside of a begin statement" do
+ it "raises a SyntaxError when used outside of a rescue statement" do
-> { eval 'retry' }.should raise_error(SyntaxError)
+ -> { eval 'begin; retry; end' }.should raise_error(SyntaxError)
+ -> { eval 'def m; retry; end' }.should raise_error(SyntaxError)
+ -> { eval 'module RetrySpecs; retry; end' }.should raise_error(SyntaxError)
end
end
diff --git a/spec/ruby/language/safe_navigator_spec.rb b/spec/ruby/language/safe_navigator_spec.rb
index c3aecff2dd..b1e28c3963 100644
--- a/spec/ruby/language/safe_navigator_spec.rb
+++ b/spec/ruby/language/safe_navigator_spec.rb
@@ -7,43 +7,43 @@ describe "Safe navigator" do
context "when context is nil" do
it "always returns nil" do
- eval("nil&.unknown").should == nil
- eval("[][10]&.unknown").should == nil
+ nil&.unknown.should == nil
+ [][10]&.unknown.should == nil
end
it "can be chained" do
- eval("nil&.one&.two&.three").should == nil
+ nil&.one&.two&.three.should == nil
end
it "doesn't evaluate arguments" do
obj = Object.new
obj.should_not_receive(:m)
- eval("nil&.unknown(obj.m) { obj.m }")
+ nil&.unknown(obj.m) { obj.m }
end
end
context "when context is false" do
it "calls the method" do
- eval("false&.to_s").should == "false"
+ false&.to_s.should == "false"
- -> { eval("false&.unknown") }.should raise_error(NoMethodError)
+ -> { false&.unknown }.should raise_error(NoMethodError)
end
end
context "when context is truthy" do
it "calls the method" do
- eval("1&.to_s").should == "1"
+ 1&.to_s.should == "1"
- -> { eval("1&.unknown") }.should raise_error(NoMethodError)
+ -> { 1&.unknown }.should raise_error(NoMethodError)
end
end
it "takes a list of arguments" do
- eval("[1,2,3]&.first(2)").should == [1,2]
+ [1,2,3]&.first(2).should == [1,2]
end
it "takes a block" do
- eval("[1,2]&.map { |i| i * 2 }").should == [2, 4]
+ [1,2]&.map { |i| i * 2 }.should == [2, 4]
end
it "allows assignment methods" do
@@ -56,29 +56,77 @@ describe "Safe navigator" do
end
obj = klass.new
- eval("obj&.foo = 3").should == 3
+ (obj&.foo = 3).should == 3
obj.foo.should == 3
obj = nil
- eval("obj&.foo = 3").should == nil
+ (obj&.foo = 3).should == nil
end
it "allows assignment operators" do
klass = Class.new do
- attr_accessor :m
+ attr_reader :m
def initialize
@m = 0
end
+
+ def m=(v)
+ @m = v
+ 42
+ end
end
obj = klass.new
- eval("obj&.m += 3")
+ obj&.m += 3
obj.m.should == 3
obj = nil
- eval("obj&.m += 3").should == nil
+ (obj&.m += 3).should == nil
+ end
+
+ it "allows ||= operator" do
+ klass = Class.new do
+ attr_reader :m
+
+ def initialize
+ @m = false
+ end
+
+ def m=(v)
+ @m = v
+ 42
+ end
+ end
+
+ obj = klass.new
+
+ (obj&.m ||= true).should == true
+ obj.m.should == true
+
+ obj = nil
+ (obj&.m ||= true).should == nil
+ obj.should == nil
+ end
+
+ it "allows &&= operator" do
+ klass = Class.new do
+ attr_accessor :m
+
+ def initialize
+ @m = true
+ end
+ end
+
+ obj = klass.new
+
+ (obj&.m &&= false).should == false
+ obj.m.should == false
+
+ obj = nil
+ (obj&.m &&= false).should == nil
+ obj.should == nil
end
it "does not call the operator method lazily with an assignment operator" do
@@ -91,7 +139,7 @@ describe "Safe navigator" do
obj = klass.new
-> {
- eval("obj&.foo += 3")
+ obj&.foo += 3
}.should raise_error(NoMethodError) { |e|
e.name.should == :+
}
diff --git a/spec/ruby/language/send_spec.rb b/spec/ruby/language/send_spec.rb
index a1656559fe..5d6340ffc5 100644
--- a/spec/ruby/language/send_spec.rb
+++ b/spec/ruby/language/send_spec.rb
@@ -43,7 +43,7 @@ describe "Invoking a method" do
end
describe "with optional arguments" do
- it "uses the optional argument if none is is passed" do
+ it "uses the optional argument if none is passed" do
specs.fooM0O1.should == [1]
end
@@ -106,6 +106,24 @@ describe "Invoking a method" do
specs.yield_now(&o).should == :from_to_proc
end
+ ruby_version_is "4.0" do
+ it "raises TypeError if 'to_proc' doesn't return a Proc" do
+ o = LangSendSpecs::RawToProc.new(42)
+
+ -> {
+ specs.makeproc(&o)
+ }.should raise_error(TypeError, "can't convert LangSendSpecs::RawToProc to Proc (LangSendSpecs::RawToProc#to_proc gives Integer)")
+ end
+
+ it "raises TypeError if block object isn't a Proc and doesn't respond to `to_proc`" do
+ o = Object.new
+
+ -> {
+ specs.makeproc(&o)
+ }.should raise_error(TypeError, "no implicit conversion of Object into Proc")
+ end
+ end
+
it "raises a SyntaxError with both a literal block and an object as block" do
-> {
eval "specs.oneb(10, &l){ 42 }"
diff --git a/spec/ruby/language/singleton_class_spec.rb b/spec/ruby/language/singleton_class_spec.rb
index 7512f0eb39..45e1f7f3ad 100644
--- a/spec/ruby/language/singleton_class_spec.rb
+++ b/spec/ruby/language/singleton_class_spec.rb
@@ -70,7 +70,7 @@ describe "A singleton class" do
end
it "has class String as the superclass of a String instance" do
- "blah".singleton_class.superclass.should == String
+ "blah".dup.singleton_class.superclass.should == String
end
it "doesn't have singleton class" do
@@ -307,4 +307,11 @@ describe "Frozen properties" do
o.freeze
klass.frozen?.should == true
end
+
+ it "will be unfrozen if the frozen object is cloned with freeze set to false" do
+ o = Object.new
+ o.freeze
+ o2 = o.clone(freeze: false)
+ o2.singleton_class.frozen?.should == false
+ end
end
diff --git a/spec/ruby/language/string_spec.rb b/spec/ruby/language/string_spec.rb
index 418dc2ca7d..f287731bed 100644
--- a/spec/ruby/language/string_spec.rb
+++ b/spec/ruby/language/string_spec.rb
@@ -1,6 +1,7 @@
-# -*- encoding: binary -*-
+# encoding: binary
require_relative '../spec_helper'
+require_relative 'fixtures/class_with_class_variable'
# TODO: rewrite these horrid specs. it "are..." seriously?!
@@ -27,6 +28,11 @@ describe "Ruby character strings" do
"#$ip".should == 'xxx'
end
+ it "interpolate class variables just with the # character" do
+ object = StringSpecs::ClassWithClassVariable.new
+ object.foo.should == 'xxx'
+ end
+
it "allows underscore as part of a variable name in a simple interpolation" do
@my_ip = 'xxx'
"#@my_ip".should == 'xxx'
@@ -231,8 +237,16 @@ describe "Ruby String literals" do
ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files.rb")).chomp.should == "true"
end
- it "produce different objects for literals with the same content in different files if the other file doesn't have the comment" do
- ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files_no_comment.rb")).chomp.should == "true"
+ 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
+ ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files_no_comment.rb")).chomp.should == "true"
+ end
+ end
+
+ 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
+ ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files_no_comment.rb")).chomp.should == "false"
+ end
end
it "produce different objects for literals with the same content in different files if they have different encodings" do
@@ -251,12 +265,12 @@ describe "Ruby String interpolation" do
it "returns a string with the source encoding by default" do
"a#{"b"}c".encoding.should == Encoding::BINARY
- eval('"a#{"b"}c"'.force_encoding("us-ascii")).encoding.should == Encoding::US_ASCII
+ eval('"a#{"b"}c"'.dup.force_encoding("us-ascii")).encoding.should == Encoding::US_ASCII
eval("# coding: US-ASCII \n 'a#{"b"}c'").encoding.should == Encoding::US_ASCII
end
it "returns a string with the source encoding, even if the components have another encoding" do
- a = "abc".force_encoding("euc-jp")
+ a = "abc".dup.force_encoding("euc-jp")
"#{a}".encoding.should == Encoding::BINARY
b = "abc".encode("utf-8")
@@ -265,22 +279,22 @@ describe "Ruby String interpolation" do
it "raises an Encoding::CompatibilityError if the Encodings are not compatible" do
a = "\u3042"
- b = "\xff".force_encoding "binary"
+ b = "\xff".dup.force_encoding "binary"
-> { "#{a} #{b}" }.should raise_error(Encoding::CompatibilityError)
end
it "creates a non-frozen String" do
code = <<~'RUBY'
- "a#{6*7}c"
+ "a#{6*7}c"
RUBY
eval(code).should_not.frozen?
end
it "creates a non-frozen String when # frozen-string-literal: true is used" do
code = <<~'RUBY'
- # frozen-string-literal: true
- "a#{6*7}c"
+ # frozen-string-literal: true
+ "a#{6*7}c"
RUBY
eval(code).should_not.frozen?
end
diff --git a/spec/ruby/language/super_spec.rb b/spec/ruby/language/super_spec.rb
index d22c603605..7d9e896d8b 100644
--- a/spec/ruby/language/super_spec.rb
+++ b/spec/ruby/language/super_spec.rb
@@ -70,7 +70,7 @@ describe "The super keyword" do
SuperSpecs::S4::B.new.foo([],"test").should == ["B#foo(a,test)", "A#foo"]
end
- it "raises an error error when super method does not exist" do
+ it "raises an error when super method does not exist" do
sup = Class.new
sub_normal = Class.new(sup) do
def foo
@@ -335,6 +335,13 @@ describe "The super keyword" do
it "without explicit arguments that are '_'" do
SuperSpecs::ZSuperWithUnderscores::B.new.m(1, 2).should == [1, 2]
+ SuperSpecs::ZSuperWithUnderscores::B.new.m3(1, 2, 3).should == [1, 2, 3]
+ SuperSpecs::ZSuperWithUnderscores::B.new.m4(1, 2, 3, 4).should == [1, 2, 3, 4]
+ SuperSpecs::ZSuperWithUnderscores::B.new.m_default(1).should == [1]
+ SuperSpecs::ZSuperWithUnderscores::B.new.m_default.should == [0]
+ SuperSpecs::ZSuperWithUnderscores::B.new.m_pre_default_rest_post(1, 2, 3, 4, 5, 6, 7).should == [1, 2, 3, 4, 5, 6, 7]
+ SuperSpecs::ZSuperWithUnderscores::B.new.m_rest(1, 2).should == [1, 2]
+ SuperSpecs::ZSuperWithUnderscores::B.new.m_kwrest(a: 1).should == {a: 1}
end
it "without explicit arguments that are '_' including any modifications" do
diff --git a/spec/ruby/language/symbol_spec.rb b/spec/ruby/language/symbol_spec.rb
index 7c1898efc2..0801d3223e 100644
--- a/spec/ruby/language/symbol_spec.rb
+++ b/spec/ruby/language/symbol_spec.rb
@@ -96,11 +96,13 @@ describe "A Symbol literal" do
%I{a b #{"c"}}.should == [:a, :b, :c]
end
- it "raises an EncodingError at parse time when Symbol with invalid bytes" do
- ScratchPad.record []
- -> {
- eval 'ScratchPad << 1; :"\xC3"'
- }.should raise_error(EncodingError, 'invalid symbol in encoding UTF-8 :"\xC3"')
- ScratchPad.recorded.should == []
+ ruby_bug "#20280", ""..."3.4" do
+ it "raises an SyntaxError at parse time when Symbol with invalid bytes" do
+ ScratchPad.record []
+ -> {
+ eval 'ScratchPad << 1; :"\xC3"'
+ }.should raise_error(SyntaxError, /invalid symbol/)
+ ScratchPad.recorded.should == []
+ end
end
end
diff --git a/spec/ruby/language/undef_spec.rb b/spec/ruby/language/undef_spec.rb
index 29dba4afb4..268c0b84c3 100644
--- a/spec/ruby/language/undef_spec.rb
+++ b/spec/ruby/language/undef_spec.rb
@@ -69,7 +69,7 @@ describe "The undef keyword" do
it "raises a NameError when passed a missing name" do
Class.new do
-> {
- undef not_exist
+ undef not_exist
}.should raise_error(NameError) { |e|
# a NameError and not a NoMethodError
e.class.should == NameError
diff --git a/spec/ruby/language/variables_spec.rb b/spec/ruby/language/variables_spec.rb
index 23c2cdb557..e134271939 100644
--- a/spec/ruby/language/variables_spec.rb
+++ b/spec/ruby/language/variables_spec.rb
@@ -14,69 +14,34 @@ describe "Evaluation order during assignment" do
end
context "with multiple assignment" do
- ruby_version_is ""..."3.1" do
- it "does not evaluate from left to right" do
- obj = VariablesSpecs::EvalOrder.new
-
- obj.instance_eval do
- foo[0], bar.baz = a, b
- end
-
- obj.order.should == ["a", "b", "foo", "foo[]=", "bar", "bar.baz="]
+ it "evaluates from left to right, receivers first then methods" do
+ obj = VariablesSpecs::EvalOrder.new
+ obj.instance_eval do
+ foo[0], bar.baz = a, b
end
- it "cannot be used to swap variables with nested method calls" do
- node = VariablesSpecs::EvalOrder.new.node
-
- original_node = node
- original_node_left = node.left
- original_node_left_right = node.left.right
-
- node.left, node.left.right, node = node.left.right, node, node.left
- # Should evaluate in the order of:
- # RHS: node.left.right, node, node.left
- # LHS:
- # * node(original_node), original_node.left = original_node_left_right
- # * node(original_node), node.left(changed in the previous assignment to original_node_left_right),
- # original_node_left_right.right = original_node
- # * node = original_node_left
-
- node.should == original_node_left
- node.right.should_not == original_node
- node.right.left.should_not == original_node_left_right
- end
+ obj.order.should == ["foo", "bar", "a", "b", "foo[]=", "bar.baz="]
end
- ruby_version_is "3.1" do
- it "evaluates from left to right, receivers first then methods" do
- obj = VariablesSpecs::EvalOrder.new
- obj.instance_eval do
- foo[0], bar.baz = a, b
- end
+ it "can be used to swap variables with nested method calls" do
+ node = VariablesSpecs::EvalOrder.new.node
- obj.order.should == ["foo", "bar", "a", "b", "foo[]=", "bar.baz="]
- end
+ original_node = node
+ original_node_left = node.left
+ original_node_left_right = node.left.right
- it "can be used to swap variables with nested method calls" do
- node = VariablesSpecs::EvalOrder.new.node
-
- original_node = node
- original_node_left = node.left
- original_node_left_right = node.left.right
-
- node.left, node.left.right, node = node.left.right, node, node.left
- # Should evaluate in the order of:
- # LHS: node, node.left(original_node_left)
- # RHS: original_node_left_right, original_node, original_node_left
- # Ops:
- # * node(original_node), original_node.left = original_node_left_right
- # * original_node_left.right = original_node
- # * node = original_node_left
-
- node.should == original_node_left
- node.right.should == original_node
- node.right.left.should == original_node_left_right
- end
+ node.left, node.left.right, node = node.left.right, node, node.left
+ # Should evaluate in the order of:
+ # LHS: node, node.left(original_node_left)
+ # RHS: original_node_left_right, original_node, original_node_left
+ # Ops:
+ # * node(original_node), original_node.left = original_node_left_right
+ # * original_node_left.right = original_node
+ # * node = original_node_left
+
+ node.should == original_node_left
+ node.right.should == original_node
+ node.right.left.should == original_node_left_right
end
end
end
@@ -367,8 +332,13 @@ describe "Multiple assignment" do
it "assigns indexed elements" do
a = []
- a[1], a[2] = 1
- a.should == [nil, 1, nil]
+ a[1], a[2] = 1, 2
+ a.should == [nil, 1, 2]
+
+ # with splatted argument
+ a = []
+ a[*[1]], a[*[2]] = 1, 2
+ a.should == [nil, 1, 2]
end
it "assigns constants" do
@@ -376,6 +346,9 @@ describe "Multiple assignment" do
SINGLE_RHS_1, SINGLE_RHS_2 = 1
[SINGLE_RHS_1, SINGLE_RHS_2].should == [1, nil]
end
+ ensure
+ VariableSpecs.send(:remove_const, :SINGLE_RHS_1)
+ VariableSpecs.send(:remove_const, :SINGLE_RHS_2)
end
end
@@ -390,11 +363,22 @@ describe "Multiple assignment" do
a.should == []
end
- it "calls #to_a to convert nil to an empty Array" do
- nil.should_receive(:to_a).and_return([])
+ ruby_version_is "4.0" do
+ it "converts nil to empty array without calling a method" do
+ nil.should_not_receive(:to_a)
- (*a = *nil).should == []
- a.should == []
+ (*a = *nil).should == []
+ a.should == []
+ end
+ end
+
+ ruby_version_is ""..."4.0" do
+ it "calls #to_a to convert nil to an empty Array" do
+ nil.should_receive(:to_a).and_return([])
+
+ (*a = *nil).should == []
+ a.should == []
+ end
end
it "does not call #to_a on an Array" do
@@ -614,6 +598,8 @@ describe "Multiple assignment" do
(*SINGLE_SPLATTED_RHS) = *1
SINGLE_SPLATTED_RHS.should == [1]
end
+ ensure
+ VariableSpecs.send(:remove_const, :SINGLE_SPLATTED_RHS)
end
end
@@ -813,6 +799,9 @@ describe "Multiple assignment" do
MRHS_VALUES_1.should == 1
MRHS_VALUES_2.should == 2
end
+ ensure
+ VariableSpecs.send(:remove_const, :MRHS_VALUES_1)
+ VariableSpecs.send(:remove_const, :MRHS_VALUES_2)
end
it "assigns all RHS values as an array to a single LHS constant" do
@@ -820,6 +809,8 @@ describe "Multiple assignment" do
MRHS_VALUES = 1, 2, 3
MRHS_VALUES.should == [1, 2, 3]
end
+ ensure
+ VariableSpecs.send(:remove_const, :MRHS_VALUES)
end
end
@@ -925,7 +916,7 @@ describe "Instance variables" do
obj = Object.new
def obj.foobar; a = $specs_uninitialized_global_variable; end
- -> { obj.foobar }.should complain(/warning: global variable `\$specs_uninitialized_global_variable' not initialized/, verbose: true)
+ -> { obj.foobar }.should complain(/warning: global variable [`']\$specs_uninitialized_global_variable' not initialized/, verbose: true)
end
it "doesn't warn at lazy initialization" do
diff --git a/spec/ruby/language/yield_spec.rb b/spec/ruby/language/yield_spec.rb
index 5283517636..e125cf8e73 100644
--- a/spec/ruby/language/yield_spec.rb
+++ b/spec/ruby/language/yield_spec.rb
@@ -206,3 +206,15 @@ describe "Using yield in non-lambda block" do
-> { eval(code) }.should raise_error(SyntaxError, /Invalid yield/)
end
end
+
+describe "Using yield in a module literal" do
+ it 'raises a SyntaxError' do
+ code = <<~RUBY
+ module YieldSpecs::ModuleWithYield
+ yield
+ end
+ RUBY
+
+ -> { eval(code) }.should raise_error(SyntaxError, /Invalid yield/)
+ end
+end