summaryrefslogtreecommitdiff
path: root/spec/ruby/language
diff options
context:
space:
mode:
authorAndrew Konchin <[email protected]>2024-07-01 15:38:25 +0300
committerBenoit Daloze <[email protected]>2024-07-02 13:33:48 +0200
commitcee62c6738c42ce774e96e180cf2d46afb8e9cbe (patch)
tree52a37ef94e90f99f8471dbba4d6659ca5dc6c532 /spec/ruby/language
parentd7af8afe1b85b8de04cd77c673b0f6ef3f3627fa (diff)
Update to ruby/spec@f8987ac
Diffstat (limited to 'spec/ruby/language')
-rw-r--r--spec/ruby/language/for_spec.rb103
-rw-r--r--spec/ruby/language/heredoc_spec.rb10
-rw-r--r--spec/ruby/language/pattern_matching_spec.rb19
-rw-r--r--spec/ruby/language/rescue_spec.rb10
4 files changed, 141 insertions, 1 deletions
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/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/pattern_matching_spec.rb b/spec/ruby/language/pattern_matching_spec.rb
index 8c5df06a17..52608b48be 100644
--- a/spec/ruby/language/pattern_matching_spec.rb
+++ b/spec/ruby/language/pattern_matching_spec.rb
@@ -5,7 +5,7 @@ describe "Pattern matching" 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
[0, 1] => [a, b]
@@ -22,6 +22,23 @@ describe "Pattern matching" do
[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
diff --git a/spec/ruby/language/rescue_spec.rb b/spec/ruby/language/rescue_spec.rb
index a3ee4807ac..4dc25a5b45 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