summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2021-02-23 12:21:41 -0800
committerJeremy Evans <code@jeremyevans.net>2021-03-29 07:45:15 -0700
commit62e66aedb08745eb7ff95575b5555c72baf1d961 (patch)
tree5def57ccde8f3de6d08a4e12619bf8c1f372975f
parent7b3c5ab8a5825a2b960e639d257f0c8a69c4186c (diff)
Add more tests for defined? with method calls
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4213
-rw-r--r--test/ruby/test_defined.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/ruby/test_defined.rb b/test/ruby/test_defined.rb
index 73c192a..3324a09 100644
--- a/test/ruby/test_defined.rb
+++ b/test/ruby/test_defined.rb
@@ -134,6 +134,55 @@ class TestDefined < Test::Unit::TestCase
assert_equal("expression", defined?(1))
end
+ def test_defined_method
+ self_ = self
+ assert_equal("method", defined?(test_defined_method))
+ assert_equal("method", defined?(self.test_defined_method))
+ assert_equal("method", defined?(self_.test_defined_method))
+
+ assert_equal(nil, defined?(1.test_defined_method))
+ assert_equal("method", defined?(1.to_i))
+ assert_equal(nil, defined?(1.to_i.test_defined_method))
+ assert_equal(nil, defined?(1.test_defined_method.to_i))
+
+ assert_equal("method", defined?("x".reverse))
+ assert_equal("method", defined?("x".reverse(1)))
+ assert_equal("method", defined?("x".reverse.reverse))
+ assert_equal(nil, defined?("x".reverse(1).reverse))
+
+ assert_equal("method", defined?(1.to_i(10)))
+ assert_equal("method", defined?(1.to_i("x")))
+ assert_equal(nil, defined?(1.to_i("x").undefined))
+ assert_equal(nil, defined?(1.to_i(undefined).to_i))
+ assert_equal(nil, defined?(1.to_i("x").undefined.to_i))
+ assert_equal(nil, defined?(1.to_i(undefined).to_i.to_i))
+ end
+
+ def test_defined_method_single_call
+ times_called = 0
+ define_singleton_method(:t) do
+ times_called += 1
+ self
+ end
+ assert_equal("method", defined?(t))
+ assert_equal(0, times_called)
+
+ assert_equal("method", defined?(t.t))
+ assert_equal(1, times_called)
+
+ times_called = 0
+ assert_equal("method", defined?(t.t.t))
+ assert_equal(2, times_called)
+
+ times_called = 0
+ assert_equal("method", defined?(t.t.t.t))
+ assert_equal(3, times_called)
+
+ times_called = 0
+ assert_equal("method", defined?(t.t.t.t.t))
+ assert_equal(4, times_called)
+ end
+
def test_defined_empty_paren_expr
bug8224 = '[ruby-core:54024] [Bug #8224]'
(1..3).each do |level|