summaryrefslogtreecommitdiff
path: root/test/ruby/test_basicinstructions.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_basicinstructions.rb')
-rw-r--r--test/ruby/test_basicinstructions.rb59
1 files changed, 52 insertions, 7 deletions
diff --git a/test/ruby/test_basicinstructions.rb b/test/ruby/test_basicinstructions.rb
index ff14e4a7a6..f6b69cc1e5 100644
--- a/test/ruby/test_basicinstructions.rb
+++ b/test/ruby/test_basicinstructions.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
require 'test/unit'
ConstTest = 3
@@ -85,7 +86,9 @@ class TestBasicInstructions < Test::Unit::TestCase
s = "OK"
prev = nil
3.times do
- assert_equal prev.object_id, (prev ||= /#{s}/o).object_id if prev
+ re = /#{s}/o
+ assert_same prev, re if prev
+ prev = re
end
end
@@ -114,7 +117,6 @@ class TestBasicInstructions < Test::Unit::TestCase
assert_equal({1=>2}, {1=>2})
assert_equal({1=>2, 3=>4}, {1=>2, 3=>4})
assert_equal({1=>2, 3=>4}, {3=>4, 1=>2})
- # assert_equal({1=>2, 3=>4}, {1,2, 3,4}) # 1.9 doesn't support
assert_equal({"key"=>"val"}, {"key"=>"val"})
end
@@ -208,9 +210,9 @@ class TestBasicInstructions < Test::Unit::TestCase
assert_raise(NameError) { a }
assert_raise(NameError) { b }
assert_raise(NameError) { c }
- a = "NOT OK"
- b = "NOT OK"
- c = "NOT OK"
+ a = a = "NOT OK"
+ b = b = "NOT OK"
+ c = c = "NOT OK"
end
class Const
@@ -426,7 +428,9 @@ class TestBasicInstructions < Test::Unit::TestCase
end
class CVarA
- @@cv = 'CVarA@@cv'
+ def self.setup
+ @@cv = 'CVarA@@cv'
+ end
def self.cv() @@cv end
def self.cv=(v) @@cv = v end
class << self
@@ -447,6 +451,7 @@ class TestBasicInstructions < Test::Unit::TestCase
end
def test_class_variable
+ CVarA.setup
assert_equal 'CVarA@@cv', CVarA.cv
assert_equal 'CVarA@@cv', CVarA.cv2
assert_equal 'CVarA@@cv', CVarA.new.cv
@@ -500,6 +505,7 @@ class TestBasicInstructions < Test::Unit::TestCase
class OP
attr_reader :x
+ attr_accessor :foo
def x=(x)
@x = x
:Bug1996
@@ -600,6 +606,19 @@ class TestBasicInstructions < Test::Unit::TestCase
assert_equal 4, x[0]
end
+ def test_send_opassign
+ return if defined?(RUBY_ENGINE) and RUBY_ENGINE != "ruby"
+
+ bug7773 = '[ruby-core:51821]'
+ x = OP.new
+ assert_equal 42, x.foo = 42, bug7773
+ assert_equal 42, x.foo, bug7773
+ assert_equal (-6), x.send(:foo=, -6), bug7773
+ assert_equal (-6), x.foo, bug7773
+ assert_equal :Bug1996, x.send(:x=, :case_when_setter_returns_other_value), bug7773
+ assert_equal :case_when_setter_returns_other_value, x.x, bug7773
+ end
+
def test_backref
/re/ =~ 'not match'
assert_nil $~
@@ -632,7 +651,7 @@ class TestBasicInstructions < Test::Unit::TestCase
assert_equal 'i', $~[9]
assert_equal 'x', $`
assert_equal 'abcdefghi', $&
- assert_equal 'y', $'
+ assert_equal "y", $'
assert_equal 'i', $+
assert_equal 'a', $1
assert_equal 'b', $2
@@ -662,19 +681,45 @@ class TestBasicInstructions < Test::Unit::TestCase
end
def test_array_splat
+ feature1125 = '[ruby-core:21901]'
+
a = []
assert_equal [], [*a]
assert_equal [1], [1, *a]
+ assert_not_same(a, [*a], feature1125)
a = [2]
assert_equal [2], [*a]
assert_equal [1, 2], [1, *a]
+ assert_not_same(a, [*a], feature1125)
a = [2, 3]
assert_equal [2, 3], [*a]
assert_equal [1, 2, 3], [1, *a]
+ assert_not_same(a, [*a], feature1125)
a = nil
assert_equal [], [*a]
assert_equal [1], [1, *a]
end
+ def test_special_const_instance_variables
+ assert_separately(%w(-W0), <<-INPUT, timeout: 60)
+ module M
+ def get
+ # we can not set instance variables on special const objects.
+ # However, we can access instance variables with default value (nil).
+ @ivar
+ end
+ end
+ class Integer; include M; end
+ class Float; include M; end
+ class Symbol; include M; end
+ class FalseClass; include M; end
+ class TrueClass; include M; end
+ class NilClass; include M; end
+
+ [123, 1.2, :sym, false, true, nil].each{|obj|
+ assert_equal(nil, obj.get)
+ }
+ INPUT
+ end
end