From 783a3000f3112dbcdf97c7bdfcd6625a671e3ceb Mon Sep 17 00:00:00 2001 From: Syed Aslam Date: Tue, 26 Oct 2021 21:30:01 +0200 Subject: [PATCH 1/4] add spec confirming Runtime error when a class variable is overtaken --- language/class_variable_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/language/class_variable_spec.rb b/language/class_variable_spec.rb index dffab47a6b..85c94abe1e 100644 --- a/language/class_variable_spec.rb +++ b/language/class_variable_spec.rb @@ -82,3 +82,13 @@ c.class_variable_get(:@@cv).should == :next end end + +describe 'Class variable overtaking' do + it "raises Runtime error when a defined class variable is overtaken" do + -> do + eval <<-CODE + @@cvar_a = :new_val + CODE + end.should raise_error(RuntimeError, /class variable access from toplevel/) + end +end From b878ebeaac444c2c09d9f65d085fc7bb82244ff7 Mon Sep 17 00:00:00 2001 From: Syed Aslam Date: Tue, 26 Oct 2021 21:33:39 +0200 Subject: [PATCH 2/4] add version guard block --- language/class_variable_spec.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/language/class_variable_spec.rb b/language/class_variable_spec.rb index 85c94abe1e..e84d46b121 100644 --- a/language/class_variable_spec.rb +++ b/language/class_variable_spec.rb @@ -83,12 +83,14 @@ end end -describe 'Class variable overtaking' do - it "raises Runtime error when a defined class variable is overtaken" do - -> do - eval <<-CODE - @@cvar_a = :new_val - CODE - end.should raise_error(RuntimeError, /class variable access from toplevel/) +ruby_version_is "3.0" do + describe 'Class variable overtaking' do + it "raises Runtime error when a defined class variable is overtaken" do + -> do + eval <<-CODE + @@cvar_a = :new_val + CODE + end.should raise_error(RuntimeError, /class variable access from toplevel/) + end end end From 414756ca577a2b33cc45ee7f09acc910dd760e8e Mon Sep 17 00:00:00 2001 From: Syed Aslam Date: Wed, 27 Oct 2021 21:59:18 +0200 Subject: [PATCH 3/4] test raisin RuntimeError when class var is overtaken by parent - also update test definitions to better describe the test --- language/class_variable_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/language/class_variable_spec.rb b/language/class_variable_spec.rb index e84d46b121..33848c3116 100644 --- a/language/class_variable_spec.rb +++ b/language/class_variable_spec.rb @@ -92,5 +92,14 @@ CODE end.should raise_error(RuntimeError, /class variable access from toplevel/) end + + it "raises Runtime error when a child class variable is overtaken" do + a = Class.new() + b = Class.new(a) + b.class_variable_set(:@@cvar, :value) + a.class_variable_set(:@@cvar, :new_value) + + -> { b.class_variable_get(:@@cvar) }.should raise_error(RuntimeError) + end end end From 76d3a0d18f5ee7ce086597bbde5c241666f9554b Mon Sep 17 00:00:00 2001 From: Syed Aslam Date: Wed, 27 Oct 2021 23:01:04 +0200 Subject: [PATCH 4/4] update test descriptions --- language/class_variable_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/language/class_variable_spec.rb b/language/class_variable_spec.rb index 33848c3116..231d613a21 100644 --- a/language/class_variable_spec.rb +++ b/language/class_variable_spec.rb @@ -85,7 +85,7 @@ ruby_version_is "3.0" do describe 'Class variable overtaking' do - it "raises Runtime error when a defined class variable is overtaken" do + it "accessing a class variable from the toplevel scope raises a RuntimeError" do -> do eval <<-CODE @@cvar_a = :new_val @@ -93,7 +93,7 @@ end.should raise_error(RuntimeError, /class variable access from toplevel/) end - it "raises Runtime error when a child class variable is overtaken" do + it "raises a RuntimeError when a class variable is overtaken by the same definition in an ancestor class" do a = Class.new() b = Class.new(a) b.class_variable_set(:@@cvar, :value)