diff options
Diffstat (limited to 'test/ruby/test_basicinstructions.rb')
| -rw-r--r-- | test/ruby/test_basicinstructions.rb | 189 |
1 files changed, 143 insertions, 46 deletions
diff --git a/test/ruby/test_basicinstructions.rb b/test/ruby/test_basicinstructions.rb index 3e52ef62d3..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 @@ -7,7 +8,7 @@ class Class end class TestBasicInstructions < Test::Unit::TestCase - + def test_immediates assert_equal((1==1), true) assert_equal((1==2), false) @@ -30,7 +31,7 @@ class TestBasicInstructions < Test::Unit::TestCase assert_equal false, (self == nil) assert_equal false, (self == 0) end - + def test_string expected = "str" + "ing" assert_equal expected, 'string' @@ -55,16 +56,16 @@ class TestBasicInstructions < Test::Unit::TestCase assert_equal :sym, :"#{s}" assert_equal :sym, :"#{"#{"#{s}"}"}" end - + def test_xstr assert_equal 'hoge', `echo hoge`.chomp assert_equal '3', `echo #{1 + 2}`.chomp hoge = 'huga' assert_equal 'huga', `echo #{hoge}`.chomp end - + def test_regexp - assert_equal /test/, /test/ + assert_equal(/test/, /test/) assert_equal 'test', /test/.source assert_equal 'TEST', /TEST/.source assert_equal true, !!(/test/ =~ 'test') @@ -76,16 +77,18 @@ class TestBasicInstructions < Test::Unit::TestCase assert_equal true, !!(re =~ 'test') assert_equal false, !!(re =~ 'does not match') - assert_equal /x#{1+1}x/, /x#{1+1}x/ + assert_equal(/x#{1+1}x/, /x#{1+1}x/) s = "OK" - assert_equal /x#{s}x/, /x#{s}x/ + assert_equal(/x#{s}x/, /x#{s}x/) assert_equal true, !!(/x#{s}x/ =~ "xOKx") assert_equal false, !!(/x#{s}x/ =~ "does not match") 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 @@ -108,13 +111,12 @@ class TestBasicInstructions < Test::Unit::TestCase assert_equal 'c', a[2] assert_nil a[3] end - + def test_hash assert_equal({}, {}) 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}) assert_equal({"key"=>"val"}, {"key"=>"val"}) end @@ -127,7 +129,7 @@ class TestBasicInstructions < Test::Unit::TestCase assert_equal((1...3), (1...2+1)) assert_equal(('a'..'z'), ('a'..'z')) end - + def test_not assert_equal true, !nil assert_equal true, !false @@ -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 @@ -476,78 +481,144 @@ class TestBasicInstructions < Test::Unit::TestCase assert_equal 'B/instance', CVarA.new.cv CVarA.cv = 'CVarA@@cv' - assert_raise(NameError) { CVarB.cvB } - assert_raise(NameError) { CVarB.cvB2 } - assert_raise(NameError) { CVarB.new.cvB } + assert_equal('CVarA@@cv', CVarB.cvB) + assert_equal('CVarA@@cv', CVarB.cvB2) + assert_equal('CVarA@@cv', CVarB.new.cvB) CVarB.cvB = 'B/singleton' assert_equal 'B/singleton', CVarB.cvB assert_equal 'B/singleton', CVarB.cvB2 assert_equal 'B/singleton', CVarB.new.cvB - assert_equal 'CVarA@@cv', CVarA.cv - assert_equal 'CVarA@@cv', CVarA.cv2 - assert_equal 'CVarA@@cv', CVarA.new.cv + assert_equal 'B/singleton', CVarA.cv + assert_equal 'B/singleton', CVarA.cv2 + assert_equal 'B/singleton', CVarA.new.cv CVarB.new.cvB = 'B/instance' assert_equal 'B/instance', CVarB.cvB assert_equal 'B/instance', CVarB.cvB2 assert_equal 'B/instance', CVarB.new.cvB - assert_equal 'CVarA@@cv', CVarA.cv - assert_equal 'CVarA@@cv', CVarA.cv2 - assert_equal 'CVarA@@cv', CVarA.new.cv + assert_equal 'B/instance', CVarA.cv + assert_equal 'B/instance', CVarA.cv2 + assert_equal 'B/instance', CVarA.new.cv CVarA.cv = 'CVarA@@cv' CVarB.cvB = 'CVarB@@cv' end class OP - attr_accessor :x + attr_reader :x + attr_accessor :foo + def x=(x) + @x = x + :Bug1996 + end + Bug1996 = '[ruby-dev:39163], [ruby-core:25143]' + def [](i) + @x + end + def []=(i, x) + @x = x + :Bug2050 + end + Bug2050 = '[ruby-core:25387]' end - def test_opassign + def test_opassign2_1 x = nil - x ||= 1 + assert_equal 1, x ||= 1 assert_equal 1, x - x &&= 2 + assert_equal 2, x &&= 2 assert_equal 2, x - x ||= 3 + assert_equal 2, x ||= 3 assert_equal 2, x - x &&= 4 + assert_equal 4, x &&= 4 + assert_equal 4, x + assert_equal 5, x += 1 + assert_equal 5, x + assert_equal 4, x -= 1 assert_equal 4, x + end + def test_opassign2_2 y = OP.new y.x = nil - y.x ||= 1 + assert_equal 1, y.x ||= 1, OP::Bug1996 assert_equal 1, y.x - y.x &&= 2 + assert_equal 2, y.x &&= 2, OP::Bug1996 assert_equal 2, y.x - y.x ||= 3 + assert_equal 2, y.x ||= 3 assert_equal 2, y.x - y.x &&= 4 + assert_equal 4, y.x &&= 4, OP::Bug1996 + assert_equal 4, y.x + assert_equal 5, y.x += 1, OP::Bug1996 + assert_equal 5, y.x + assert_equal 4, y.x -= 1, OP::Bug1996 assert_equal 4, y.x + end + def test_opassign2_3 z = OP.new - z.x = y + z.x = OP.new z.x.x = nil - z.x.x ||= 1 + assert_equal 1, z.x.x ||= 1, OP::Bug1996 assert_equal 1, z.x.x - z.x.x &&= 2 + assert_equal 2, z.x.x &&= 2, OP::Bug1996 assert_equal 2, z.x.x - z.x.x ||= 3 + assert_equal 2, z.x.x ||= 3 assert_equal 2, z.x.x - z.x.x &&= 4 + assert_equal 4, z.x.x &&= 4, OP::Bug1996 + assert_equal 4, z.x.x + assert_equal 5, z.x.x += 1, OP::Bug1996 + assert_equal 5, z.x.x + assert_equal 4, z.x.x -= 1, OP::Bug1996 assert_equal 4, z.x.x + end + def test_opassign1_1 a = [] a[0] = nil - a[0] ||= 1 + assert_equal 1, a[0] ||= 1 assert_equal 1, a[0] - a[0] &&= 2 + assert_equal 2, a[0] &&= 2 assert_equal 2, a[0] - a[0] ||= 3 + assert_equal 2, a[0] ||= 3 assert_equal 2, a[0] - a[0] &&= 4 + assert_equal 4, a[0] &&= 4 + assert_equal 4, a[0] + assert_equal 5, a[0] += 1 + assert_equal 5, a[0] + assert_equal 4, a[0] -= 1 assert_equal 4, a[0] end + def test_opassign1_2 + x = OP.new + x[0] = nil + assert_equal 1, x[0] ||= 1, OP::Bug2050 + assert_equal 1, x[0] + assert_equal 2, x[0] &&= 2, OP::Bug2050 + assert_equal 2, x[0] + assert_equal 2, x[0] ||= 3, OP::Bug2050 + assert_equal 2, x[0] + assert_equal 4, x[0] &&= 4, OP::Bug2050 + assert_equal 4, x[0] + assert_equal 5, x[0] += 1, OP::Bug2050 + assert_equal 5, x[0] + assert_equal 4, x[0] -= 1, OP::Bug2050 + 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 $~ @@ -580,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 @@ -610,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 [nil], [*a] # FIXME: []? [nil]? error? - assert_equal [1], [1, *a] # FIXME: [1, nil]? error? + 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 |
