summaryrefslogtreecommitdiff
path: root/test/ruby/test_primitive.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_primitive.rb')
-rw-r--r--test/ruby/test_primitive.rb115
1 files changed, 85 insertions, 30 deletions
diff --git a/test/ruby/test_primitive.rb b/test/ruby/test_primitive.rb
index 8ffbe549d6..f1db934000 100644
--- a/test/ruby/test_primitive.rb
+++ b/test/ruby/test_primitive.rb
@@ -1,7 +1,8 @@
+# frozen_string_literal: false
require 'test/unit'
class TestRubyPrimitive < Test::Unit::TestCase
-
+
def test_not
assert_equal false, !true
assert_equal true, !false
@@ -25,24 +26,31 @@ class TestRubyPrimitive < Test::Unit::TestCase
assert_equal 4, c
end
- C = 1
- class A
- Const = 1
- class B
- Const = 2
- class C
- Const = 3
- def const
- Const
+ C_Setup = -> do
+ remove_const :C if defined? ::TestRubyPrimitive::C
+ remove_const :A if defined? ::TestRubyPrimitive::A
+
+ C = 1
+ class A
+ Const = 1
+ class B
+ Const = 2
+ class C
+ Const = 3
+ def const
+ Const
+ end
end
end
end
+ (1..2).map {
+ A::B::C::Const
+ }
end
- (1..2).map {
- A::B::C::Const
- }
def test_constant
+ C_Setup.call
+
assert_equal 1, C
assert_equal 1, C
assert_equal 1, A::Const
@@ -81,7 +89,7 @@ class TestRubyPrimitive < Test::Unit::TestCase
end
i = 0
while i < 3
- r = A3::B3::C # cache
+ r = r = A3::B3::C # cache
class A3::B3
remove_const :C
end
@@ -143,48 +151,66 @@ class TestRubyPrimitive < Test::Unit::TestCase
assert_equal 7, ($test_ruby_primitive_gvar = 7)
assert_equal 7, ($test_ruby_primitive_gvar = 7)
end
-
- class A7
- @@c = 1
- def m
- @@c += 1
+
+ A7_Setup = -> do
+ remove_const :A7 if defined? TestRubyPrimitive::A7
+
+ class A7
+ @@c = 1
+ def m
+ @@c += 1
+ end
end
end
def test_cvar_from_instance_method
+ A7_Setup.call
+
assert_equal 2, A7.new.m
assert_equal 3, A7.new.m
assert_equal 4, A7.new.m
end
- class A8
- @@c = 1
- class << self
- def m
- @@c += 1
+ A8_Setup = -> do
+ remove_const :A8 if defined? TestRubyPrimitive::A8
+
+ class A8
+ @@c = 1
+ class << self
+ def m
+ @@c += 1
+ end
end
end
end
def test_cvar_from_singleton_method
+ A8_Setup.call
+
assert_equal 2, A8.m
assert_equal 3, A8.m
assert_equal 4, A8.m
end
- class A9
- @@c = 1
- def self.m
- @@c += 1
+ A9_Setup = -> do
+ remove_const :A8 if defined? TestRubyPrimitive::A8
+
+ class A9
+ @@c = 1
+ def self.m
+ @@c += 1
+ end
end
end
def test_cvar_from_singleton_method2
+ A9_Setup.call
+
assert_equal 2, A9.m
assert_equal 3, A9.m
assert_equal 4, A9.m
end
-
+
class A10
attr_accessor :a
end
@@ -198,6 +224,9 @@ class TestRubyPrimitive < Test::Unit::TestCase
@iv += 2
assert_equal 4, @iv
+ # init @@cv
+ @@cv = nil
+
@@cv ||= 1
assert_equal 1, @@cv
@@cv &&= 2
@@ -228,6 +257,12 @@ class TestRubyPrimitive < Test::Unit::TestCase
assert_equal 7, a[0]
a[0] ||= 3
assert_equal 7, a[0]
+
+ a = [0, 1, nil, 3, 4]
+ a[*[2]] ||= :foo
+ assert_equal [0, 1, :foo, 3, 4], a
+ a[*[1,3]] &&= [:bar]
+ assert_equal [0, :bar, 4], a
end
def test_opassign_and_or
@@ -257,7 +292,7 @@ class TestRubyPrimitive < Test::Unit::TestCase
h[0] &&= 1
assert_equal 1, h[0]
end
-
+
def test_backref
/a(b)(c)d/ =~ 'xyzabcdefgabcdefg'
assert_equal 'b', $1
@@ -394,4 +429,24 @@ class TestRubyPrimitive < Test::Unit::TestCase
#assert_equal [0,1,2,3,4], [0, *a, 4]
end
+ def test_concatarray_ruby_dev_41933
+ bug3658 = '[ruby-dev:41933]'
+ [0, *x=1]
+ assert_equal(1, x, bug3658)
+ [0, *x=1, 2]
+ assert_equal(1, x, bug3658)
+ class << (x = Object.new)
+ attr_accessor :to_a_called
+ def to_a
+ @to_a_called = true
+ [self]
+ end
+ end
+ x.to_a_called = false
+ [0, *x]
+ assert_predicate(x, :to_a_called, bug3658)
+ x.to_a_called = false
+ [0, *x, 2]
+ assert_predicate(x, :to_a_called, bug3658)
+ end
end