1 # frozen_string_literal: false
4 class TestVariable < Test::Unit::TestCase
11 def self.ruler1 # <= per method definition style
14 class << self # <= multiple method definition style
29 @@rule = "Cronus" # modifies @@rule in Gods
36 def test_setting_class_variable_on_module_through_inheritance
38 mod.class_variable_set(:@@foo, 1)
40 c = Class.new { include(mod) }
41 assert_raise(FrozenError) { c.class_variable_set(:@@foo, 2) }
42 assert_raise(FrozenError) { c.class_eval("@@foo = 2") }
43 assert_equal(1, c.class_variable_get(:@@foo))
46 def test_singleton_class_included_class_variable
49 assert_empty(c.singleton_class.class_variables)
50 assert_raise(NameError){ c.singleton_class.class_variable_get(:@@rule) }
51 c.class_variable_set(:@@foo, 1)
52 assert_equal([:@@foo], c.singleton_class.class_variables)
53 assert_equal(1, c.singleton_class.class_variable_get(:@@foo))
58 assert_empty(sc.singleton_class.class_variables)
59 assert_raise(NameError){ sc.singleton_class.class_variable_get(:@@rule) }
60 c.class_variable_set(:@@foo, 1)
61 assert_equal([:@@foo], sc.singleton_class.class_variables)
62 assert_equal(1, sc.singleton_class.class_variable_get(:@@foo))
67 assert_equal([:@@rule], o.singleton_class.class_variables)
68 assert_equal("Zeus", o.singleton_class.class_variable_get(:@@rule))
69 c.class_variable_set(:@@foo, 1)
70 assert_equal([:@@foo, :@@rule], o.singleton_class.class_variables.sort)
71 assert_equal(1, o.singleton_class.class_variable_get(:@@foo))
74 def test_cvar_overtaken_by_parent_class
87 assert_equal 1, Child.cvar
93 assert_raise RuntimeError do
98 assert_equal "class variable @@cvar of TestVariable::Child is overtaken by TestVariable::Parent", error.message
100 TestVariable.send(:remove_const, :Child) rescue nil
101 TestVariable.send(:remove_const, :Parent) rescue nil
104 def test_cvar_overtaken_by_module
106 class ParentForModule
114 assert_equal 1, ParentForModule.cvar
120 class ParentForModule
124 assert_raise RuntimeError do
129 assert_equal "class variable @@cvar of TestVariable::ParentForModule is overtaken by TestVariable::Mixin", error.message
131 TestVariable.send(:remove_const, :Mixin) rescue nil
132 TestVariable.send(:remove_const, :ParentForModule) rescue nil
135 class IncludeRefinedModuleClassVariableNoWarning
137 @@_test_include_refined_module_class_variable = true
148 @@_test_include_refined_module_class_variable
152 def test_include_refined_module_class_variable
153 assert_warning('') do
154 IncludeRefinedModuleClassVariableNoWarning.new.t
159 assert_instance_of(Integer, $$)
162 assert_raise(NameError) do
165 assert_normal_exit("$*=0; $*", "[ruby-dev:36698]")
169 assert_equal(foobar, $_)
171 assert_equal("Cronus", Gods.new.ruler0)
172 assert_equal("Cronus", Gods.ruler1)
173 assert_equal("Cronus", Gods.ruler2)
174 assert_equal("Cronus", Titans.ruler1)
175 assert_equal("Cronus", Titans.ruler2)
177 assert_equal("Cronus", atlas.ruler0)
178 assert_equal("Zeus", atlas.ruler3)
179 assert_raise(RuntimeError) { atlas.ruler4 }
180 assert_nothing_raised do
182 defined?(@@rule) && @@rule
187 def test_local_variables
189 assert_instance_of(Symbol, local_variables[0], "[ruby-dev:34008]")
193 def test_local_variables2
196 assert_equal([:x, :y], local_variables.sort)
201 def test_local_variables3
205 assert_equal([:x, :y, :z], local_variables.sort)
211 def test_shadowing_local_variables
212 bug9486 = '[ruby-core:60501] [Bug #9486]'
213 assert_equal([:x, :bug9486], tap {|x| break local_variables}, bug9486)
216 def test_shadowing_block_local_variables
217 bug9486 = '[ruby-core:60501] [Bug #9486]'
218 assert_equal([:x, :bug9486], tap {|;x| x = x; break local_variables}, bug9486)
221 def test_global_variables
222 gv = global_variables
223 assert_empty(gv.grep(/\A(?!\$)/))
225 assert_not_include(gv, :$1)
226 /(\w)(\d)?(.)(.)(.)(.)(.)(.)(.)(.)(\d)?(.)/ =~ "globalglobalglobal"
228 gv = global_variables - gv
229 assert_include(gv, :$1)
230 assert_not_include(gv, :$2)
231 assert_not_include(gv, :$11)
232 assert_include(gv, :$12)
235 def test_global_variable_0
236 assert_in_out_err(["-e", "$0='t'*1000;print $0"], "", /\At+\z/, [])
239 def test_global_variable_popped
240 assert_nothing_raised {
241 EnvUtil.suppress_warning {
247 def test_constant_popped
248 assert_nothing_raised {
249 EnvUtil.suppress_warning {
250 eval("TestVariable::Gods; 1")
255 def test_special_constant_ivars
256 [ true, false, :symbol, "dsym#{rand(9999)}".to_sym, 1, 1.0 ].each do |v|
257 assert_empty v.instance_variables
258 msg = "can't modify frozen #{v.class}: #{v.inspect}"
260 assert_raise_with_message(FrozenError, msg) do
261 v.instance_variable_set(:@foo, :bar)
264 assert_nil EnvUtil.suppress_warning {v.instance_variable_get(:@foo)}
265 assert_not_send([v, :instance_variable_defined?, :@foo])
267 assert_raise_with_message(FrozenError, msg) do
268 v.remove_instance_variable(:@foo)
285 def test_external_ivars
287 # check inline cache for external ivar access
288 assert_equal [1, 2, 3], ExIvar.new.ivars
292 def test_local_variables_with_kwarg
293 bug11674 = '[ruby-core:71437] [Bug #11674]'
294 v = with_kwargs_11(v1:1,v2:2,v3:3,v4:4,v5:5,v6:6,v7:7,v8:8,v9:9,v10:10,v11:11)
295 assert_equal(%i(v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11), v, bug11674)
299 def with_kwargs_11(v1:, v2:, v3:, v4:, v5:, v6:, v7:, v8:, v9:, v10:, v11:)