[前][次][番号順一覧][スレッド一覧]

ruby-changes:65436

From: eileencodes <ko1@a...>
Date: Thu, 11 Mar 2021 02:40:11 +0900 (JST)
Subject: [ruby-changes:65436] cbc7c1c061 (master): Add cvar overtaken tests

https://git.ruby-lang.org/ruby.git/commit/?id=cbc7c1c061

From cbc7c1c06145b02b3d887a6fb2c3b6d9e4bb22dd Mon Sep 17 00:00:00 2001
From: eileencodes <eileencodes@g...>
Date: Thu, 25 Feb 2021 12:42:10 -0500
Subject: Add cvar overtaken tests

While working on another project we noticed that there were no tests for
the cvar overtaken exception when using classes. This change adds a test
for cvar overtaken with classes and moves the cvar overtaken test for
modules into the new file.

Co-authored-by: Aaron Patterson <tenderlove@r...>
---
 test/ruby/test_variable.rb | 55 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/test/ruby/test_variable.rb b/test/ruby/test_variable.rb
index 9795223..bedddf0 100644
--- a/test/ruby/test_variable.rb
+++ b/test/ruby/test_variable.rb
@@ -71,6 +71,61 @@ class TestVariable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_variable.rb#L71
     assert_equal(1, o.singleton_class.class_variable_get(:@@foo))
   end
 
+  def test_cvar_overtaken_by_parent_class
+    error = eval <<~EORB
+      class Parent
+      end
+
+      class Child < Parent
+        @@cvar = 1
+
+        def self.cvar
+          @@cvar
+        end
+      end
+
+      assert_equal 1, Child.cvar
+
+      class Parent
+        @@cvar = 2
+      end
+
+      assert_raise RuntimeError do
+        Child.cvar
+      end
+    EORB
+
+    assert_equal "class variable @@cvar of TestVariable::Child is overtaken by TestVariable::Parent", error.message
+  end
+
+  def test_cvar_overtaken_by_module
+    error = eval <<~EORB
+      class ParentForModule
+        @@cvar = 1
+
+        def self.cvar
+          @@cvar
+        end
+      end
+
+      assert_equal 1, ParentForModule.cvar
+
+      module Mixin
+        @@cvar = 2
+      end
+
+      class ParentForModule
+        include Mixin
+      end
+
+      assert_raise RuntimeError do
+        ParentForModule.cvar
+      end
+    EORB
+
+    assert_equal "class variable @@cvar of TestVariable::ParentForModule is overtaken by TestVariable::Mixin", error.message
+  end
+
   class IncludeRefinedModuleClassVariableNoWarning
     module Mod
       @@_test_include_refined_module_class_variable = true
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]