aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/object/try_test.rb
diff options
context:
space:
mode:
authorAkshay Vishnoi <akshay.vishnoi@vinsol.com>2014-06-12 07:54:41 +0530
committerAkshay Vishnoi <akshay.vishnoi@vinsol.com>2014-07-29 05:01:50 +0530
commita45d1f6dfdb22a9d3e61397030b98a5d4d0c5114 (patch)
treee82f7a076ecf131c3b1499181be578bbc434e04f /activesupport/test/core_ext/object/try_test.rb
parent55cedf288e958cb79cd4a94b08bf7da5e1c3d3f8 (diff)
downloadrails-a45d1f6dfdb22a9d3e61397030b98a5d4d0c5114.tar.gz
rails-a45d1f6dfdb22a9d3e61397030b98a5d4d0c5114.tar.bz2
rails-a45d1f6dfdb22a9d3e61397030b98a5d4d0c5114.zip
Move object test files under object
Diffstat (limited to 'activesupport/test/core_ext/object/try_test.rb')
-rw-r--r--activesupport/test/core_ext/object/try_test.rb91
1 files changed, 91 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/object/try_test.rb b/activesupport/test/core_ext/object/try_test.rb
new file mode 100644
index 0000000000..8b754ced53
--- /dev/null
+++ b/activesupport/test/core_ext/object/try_test.rb
@@ -0,0 +1,91 @@
+require 'abstract_unit'
+require 'active_support/core_ext/object'
+
+class ObjectTryTest < ActiveSupport::TestCase
+ def setup
+ @string = "Hello"
+ end
+
+ def test_nonexisting_method
+ method = :undefined_method
+ assert !@string.respond_to?(method)
+ assert_nil @string.try(method)
+ end
+
+ def test_nonexisting_method_with_arguments
+ method = :undefined_method
+ assert !@string.respond_to?(method)
+ assert_nil @string.try(method, 'llo', 'y')
+ end
+
+ def test_nonexisting_method_bang
+ method = :undefined_method
+ assert !@string.respond_to?(method)
+ assert_raise(NoMethodError) { @string.try!(method) }
+ end
+
+ def test_nonexisting_method_with_arguments_bang
+ method = :undefined_method
+ assert !@string.respond_to?(method)
+ assert_raise(NoMethodError) { @string.try!(method, 'llo', 'y') }
+ end
+
+ def test_try_only_block_bang
+ assert_equal @string.reverse, @string.try! { |s| s.reverse }
+ end
+
+ def test_valid_method
+ assert_equal 5, @string.try(:size)
+ end
+
+ def test_argument_forwarding
+ assert_equal 'Hey', @string.try(:sub, 'llo', 'y')
+ end
+
+ def test_block_forwarding
+ assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
+ end
+
+ def test_nil_to_type
+ assert_nil nil.try(:to_s)
+ assert_nil nil.try(:to_i)
+ end
+
+ def test_false_try
+ assert_equal 'false', false.try(:to_s)
+ end
+
+ def test_try_only_block
+ assert_equal @string.reverse, @string.try { |s| s.reverse }
+ end
+
+ def test_try_only_block_nil
+ ran = false
+ nil.try { ran = true }
+ assert_equal false, ran
+ end
+
+ def test_try_with_private_method_bang
+ klass = Class.new do
+ private
+
+ def private_method
+ 'private method'
+ end
+ end
+
+ assert_raise(NoMethodError) { klass.new.try!(:private_method) }
+ end
+
+ def test_try_with_private_method
+ klass = Class.new do
+ private
+
+ def private_method
+ 'private method'
+ end
+ end
+
+ assert_nil klass.new.try(:private_method)
+ end
+end