summaryrefslogtreecommitdiff
path: root/spec/ruby/language
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2023-02-27 21:02:44 +0100
committerBenoit Daloze <eregontp@gmail.com>2023-02-27 21:02:44 +0100
commit18b4def471bb901d0baa4a1307185484cb05815f (patch)
tree779b24566144e9ee06c6f8de35bd2f7fd74ccdb4 /spec/ruby/language
parentde60139053fa7c561858c5c5556d61c82f361dd9 (diff)
downloadruby-18b4def471bb901d0baa4a1307185484cb05815f.tar.gz
Update to ruby/spec@e7dc804
Diffstat (limited to 'spec/ruby/language')
-rw-r--r--spec/ruby/language/END_spec.rb26
-rw-r--r--spec/ruby/language/fixtures/super.rb14
-rw-r--r--spec/ruby/language/hash_spec.rb31
-rw-r--r--spec/ruby/language/if_spec.rb43
-rw-r--r--spec/ruby/language/regexp/character_classes_spec.rb4
-rw-r--r--spec/ruby/language/super_spec.rb4
-rw-r--r--spec/ruby/language/symbol_spec.rb4
7 files changed, 115 insertions, 11 deletions
diff --git a/spec/ruby/language/END_spec.rb b/spec/ruby/language/END_spec.rb
index 787f602d88..c84f0cc9ac 100644
--- a/spec/ruby/language/END_spec.rb
+++ b/spec/ruby/language/END_spec.rb
@@ -1,19 +1,33 @@
require_relative '../spec_helper'
+require_relative '../shared/kernel/at_exit'
describe "The END keyword" do
+ it_behaves_like :kernel_at_exit, :END
+
it "runs only once for multiple calls" do
ruby_exe("10.times { END { puts 'foo' }; } ").should == "foo\n"
end
- it "runs last in a given code unit" do
- ruby_exe("END { puts 'bar' }; puts'foo'; ").should == "foo\nbar\n"
+ it "is affected by the toplevel assignment" do
+ ruby_exe("foo = 'foo'; END { puts foo }").should == "foo\n"
end
- it "runs multiple ends in LIFO order" do
- ruby_exe("END { puts 'foo' }; END { puts 'bar' }").should == "bar\nfoo\n"
+ it "warns when END is used in a method" do
+ ruby_exe(<<~ruby, args: "2>&1").should =~ /warning: END in method; use at_exit/
+ def foo
+ END { }
+ end
+ ruby
end
- it "is affected by the toplevel assignment" do
- ruby_exe("foo = 'foo'; END { puts foo }").should == "foo\n"
+ context "END blocks and at_exit callbacks are mixed" do
+ it "runs them all in reverse order of registration" do
+ ruby_exe(<<~ruby).should == "at_exit#2\nEND#2\nat_exit#1\nEND#1\n"
+ END { puts 'END#1' }
+ at_exit { puts 'at_exit#1' }
+ END { puts 'END#2' }
+ at_exit { puts 'at_exit#2' }
+ ruby
+ end
end
end
diff --git a/spec/ruby/language/fixtures/super.rb b/spec/ruby/language/fixtures/super.rb
index 218f1e4970..94a2a91be0 100644
--- a/spec/ruby/language/fixtures/super.rb
+++ b/spec/ruby/language/fixtures/super.rb
@@ -556,6 +556,20 @@ module SuperSpecs
end
end
+ module ZSuperInBlock
+ class A
+ def m(arg:)
+ arg
+ end
+ end
+
+ class B < A
+ def m(arg:)
+ proc { super }.call
+ end
+ end
+ end
+
module Keywords
class Arguments
def foo(**args)
diff --git a/spec/ruby/language/hash_spec.rb b/spec/ruby/language/hash_spec.rb
index c84564d3ea..fa5c8723e9 100644
--- a/spec/ruby/language/hash_spec.rb
+++ b/spec/ruby/language/hash_spec.rb
@@ -127,11 +127,24 @@ describe "Hash literal" do
{a: 1, **h, c: 4}.should == {a: 1, b: 2, c: 4}
end
- it "expands an '**{}' element with the last key/value pair taking precedence" do
+ 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}"
}.should complain(/key :a is duplicated|duplicated key/)
@h.should == {a: 2, b: 3, c: 3}
+
+ -> {
+ h = {a: 2, b: 3, c: 1}
+ @h = eval "{a: 1, **h, c: 3}"
+ }.should_not complain
+ @h.should == {a: 2, b: 3, c: 3}
+ end
+
+ it "expands an '**{}' and warns when finding an additional duplicate key afterwards" do
+ -> {
+ @h = eval "{d: 1, **{a: 2, b: 3, c: 1}, c: 3}"
+ }.should complain(/key :c is duplicated|duplicated key/)
+ @h.should == {a: 2, b: 3, c: 3, d: 1}
end
it "merges multiple nested '**obj' in Hash literals" do
@@ -177,6 +190,22 @@ describe "Hash literal" do
utf8_hash.keys.first.should == usascii_hash.keys.first
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
+
+ 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 == []
+ end
end
describe "The ** operator" do
diff --git a/spec/ruby/language/if_spec.rb b/spec/ruby/language/if_spec.rb
index d1d95c1607..a5da696000 100644
--- a/spec/ruby/language/if_spec.rb
+++ b/spec/ruby/language/if_spec.rb
@@ -306,6 +306,49 @@ describe "The if expression" do
ScratchPad.recorded.should == [4, 5, 4, 5]
end
end
+
+ describe "when a branch syntactically does not return a value" do
+ it "raises SyntaxError if both do not return a value" do
+ -> {
+ eval <<~RUBY
+ def m
+ a = if rand
+ return
+ else
+ return
+ end
+ a
+ end
+ RUBY
+ }.should raise_error(SyntaxError, /void value expression/)
+ end
+
+ it "does not raise SyntaxError if one branch returns a value" do
+ eval(<<~RUBY).should == 1
+ def m
+ a = if false # using false to make it clear that's not checked for
+ 42
+ else
+ return 1
+ end
+ a
+ end
+ m
+ RUBY
+
+ eval(<<~RUBY).should == 1
+ def m
+ a = if true # using true to make it clear that's not checked for
+ return 1
+ else
+ 42
+ end
+ a
+ end
+ m
+ RUBY
+ end
+ end
end
describe "The postfix if form" do
diff --git a/spec/ruby/language/regexp/character_classes_spec.rb b/spec/ruby/language/regexp/character_classes_spec.rb
index 12a51178b2..a86200ff34 100644
--- a/spec/ruby/language/regexp/character_classes_spec.rb
+++ b/spec/ruby/language/regexp/character_classes_spec.rb
@@ -610,8 +610,8 @@ describe "Regexp with character classes" do
end
it "supports negated property condition" do
- "a".match(/\P{L}/).should be_nil
- "1".match(/\P{N}/).should be_nil
+ "a".match(eval("/\P{L}/")).should be_nil
+ "1".match(eval("/\P{N}/")).should be_nil
end
ruby_bug "#17340", ''...'3.0' do
diff --git a/spec/ruby/language/super_spec.rb b/spec/ruby/language/super_spec.rb
index 1ac5c5e1be..1fd7acc727 100644
--- a/spec/ruby/language/super_spec.rb
+++ b/spec/ruby/language/super_spec.rb
@@ -322,6 +322,10 @@ describe "The super keyword" do
SuperSpecs::ZSuperWithUnderscores::B.new.m_modified(1, 2).should == [14, 2]
end
+ it "should pass method arguments when called within a closure" do
+ SuperSpecs::ZSuperInBlock::B.new.m(arg: 1).should == 1
+ end
+
describe 'when using keyword arguments' do
before :each do
@req = SuperSpecs::Keywords::RequiredArguments.new
diff --git a/spec/ruby/language/symbol_spec.rb b/spec/ruby/language/symbol_spec.rb
index d6a41d3059..7c1898efc2 100644
--- a/spec/ruby/language/symbol_spec.rb
+++ b/spec/ruby/language/symbol_spec.rb
@@ -96,11 +96,11 @@ describe "A Symbol literal" do
%I{a b #{"c"}}.should == [:a, :b, :c]
end
- it "with invalid bytes raises an EncodingError at parse time" do
+ it "raises an EncodingError at parse time when Symbol with invalid bytes" do
ScratchPad.record []
-> {
eval 'ScratchPad << 1; :"\xC3"'
- }.should raise_error(EncodingError, /invalid/)
+ }.should raise_error(EncodingError, 'invalid symbol in encoding UTF-8 :"\xC3"')
ScratchPad.recorded.should == []
end
end