diff options
author | Benoit Daloze <eregontp@gmail.com> | 2021-10-28 18:54:01 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2021-10-28 18:54:01 +0200 |
commit | c75df796d875b02d7b97974c7fe840f0a9de171f (patch) | |
tree | ea9a4e1b98cdd1be834fe608e24ba20399184e5a /spec/ruby/language | |
parent | 3bf36979d888cfa958c22fbf094ad10dabfe624d (diff) | |
download | ruby-c75df796d875b02d7b97974c7fe840f0a9de171f.tar.gz |
Update to ruby/spec@21a48d9
Diffstat (limited to 'spec/ruby/language')
-rw-r--r-- | spec/ruby/language/class_variable_spec.rb | 26 | ||||
-rw-r--r-- | spec/ruby/language/numbered_parameters_spec.rb | 14 | ||||
-rw-r--r-- | spec/ruby/language/safe_spec.rb | 10 | ||||
-rw-r--r-- | spec/ruby/language/variables_spec.rb | 29 |
4 files changed, 79 insertions, 0 deletions
diff --git a/spec/ruby/language/class_variable_spec.rb b/spec/ruby/language/class_variable_spec.rb index dffab47a6b..9a4b36e82b 100644 --- a/spec/ruby/language/class_variable_spec.rb +++ b/spec/ruby/language/class_variable_spec.rb @@ -82,3 +82,29 @@ describe 'A class variable definition' do c.class_variable_get(:@@cv).should == :next end end + +ruby_version_is "3.0" do + describe 'Accessing a class variable' do + it "raises a RuntimeError when accessed from the toplevel scope (not in some module or class)" do + -> { + eval "@@cvar_toplevel1" + }.should raise_error(RuntimeError, 'class variable access from toplevel') + -> { + eval "@@cvar_toplevel2 = 2" + }.should raise_error(RuntimeError, 'class variable access from toplevel') + end + + it "raises a RuntimeError when a class variable is overtaken in an ancestor class" do + parent = Class.new() + subclass = Class.new(parent) + subclass.class_variable_set(:@@cvar_overtaken, :subclass) + parent.class_variable_set(:@@cvar_overtaken, :parent) + + -> { + subclass.class_variable_get(:@@cvar_overtaken) + }.should raise_error(RuntimeError, /class variable @@cvar_overtaken of .+ is overtaken by .+/) + + parent.class_variable_get(:@@cvar_overtaken).should == :parent + end + end +end diff --git a/spec/ruby/language/numbered_parameters_spec.rb b/spec/ruby/language/numbered_parameters_spec.rb index cd1ddf4555..838822b2d6 100644 --- a/spec/ruby/language/numbered_parameters_spec.rb +++ b/spec/ruby/language/numbered_parameters_spec.rb @@ -80,6 +80,20 @@ ruby_version_is "2.7" do -> { eval("['a'].map { |x| _1 }") }.should raise_error(SyntaxError, /ordinary parameter is defined/) end + describe "assigning to a numbered parameter" do + ruby_version_is '2.7'...'3.0' do + it "warns" do + -> { eval("proc { _1 = 0 }") }.should complain(/warning: `_1' is reserved for numbered parameter; consider another name/) + end + end + + ruby_version_is '3.0' do + it "raises SyntaxError" do + -> { eval("proc { _1 = 0 }") }.should raise_error(SyntaxError, /_1 is reserved for numbered parameter/) + end + end + end + it "affects block arity" do -> { _1 }.arity.should == 1 -> { _2 }.arity.should == 2 diff --git a/spec/ruby/language/safe_spec.rb b/spec/ruby/language/safe_spec.rb index 062381d729..89830a2069 100644 --- a/spec/ruby/language/safe_spec.rb +++ b/spec/ruby/language/safe_spec.rb @@ -106,4 +106,14 @@ describe "The $SAFE variable" do }.should complain(/\$SAFE will become a normal global variable in Ruby 3.0/) end end + + ruby_version_is "3.0" do + it "$SAFE is a regular global variable" do + $SAFE.should == nil + $SAFE = 42 + $SAFE.should == 42 + ensure + $SAFE = nil + end + end end diff --git a/spec/ruby/language/variables_spec.rb b/spec/ruby/language/variables_spec.rb index 7d6969e659..699187335c 100644 --- a/spec/ruby/language/variables_spec.rb +++ b/spec/ruby/language/variables_spec.rb @@ -822,3 +822,32 @@ describe 'Allowed characters' do result.should == 1 end end + +describe "Instance variables" do + context "when instance variable is uninitialized" do + ruby_version_is ""..."3.0" do + it "warns about accessing uninitialized instance variable" do + obj = Object.new + def obj.foobar; a = @a; end + + -> { obj.foobar }.should complain(/warning: instance variable @a not initialized/, verbose: true) + end + end + + ruby_version_is "3.0" do + it "doesn't warn about accessing uninitialized instance variable" do + obj = Object.new + def obj.foobar; a = @a; end + + -> { obj.foobar }.should_not complain(verbose: true) + end + end + + it "doesn't warn at lazy initialization" do + obj = Object.new + def obj.foobar; @a ||= 42; end + + -> { obj.foobar }.should_not complain(verbose: true) + end + end +end |