Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spec confirming Runtime error when a class variable is overtaken #888

Merged
merged 4 commits into from
Oct 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions language/class_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,24 @@
c.class_variable_get(:@@cv).should == :next
end
end

ruby_version_is "3.0" do
describe 'Class variable overtaking' do
it "accessing a class variable from the toplevel scope raises a RuntimeError" do
-> do
eval <<-CODE
@@cvar_a = :new_val
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks accessing a class variable from the toplevel (i.e., not under some class/module), but not overtaking itself, which is something different: setting a class var in a parent class when it's already defined in a child class.
Could you try to add a separate example (it) for that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍, initially I was reading the description as "it should raise an error when the parent class tries to define the class variable already defined by the child".

Added the needed test.

CODE
end.should raise_error(RuntimeError, /class variable access from toplevel/)
end

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)
a.class_variable_set(:@@cvar, :new_value)

-> { b.class_variable_get(:@@cvar) }.should raise_error(RuntimeError)
end
end
end