class ObjectTryTest

Public Instance Methods

private_method() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 82
def private_method
  "private method"
end
setup() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 7
def setup
  @string = "Hello"
end
test_argument_forwarding() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 39
def test_argument_forwarding
  assert_equal "Hey", @string.try(:sub, "llo", "y")
end
test_block_forwarding() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 43
def test_block_forwarding
  assert_equal "Hey", @string.try(:sub, "llo") { |match| "y" }
end
test_false_try() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 52
def test_false_try
  assert_equal "false", false.try(:to_s)
end
test_nil_to_type() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 47
def test_nil_to_type
  assert_nil nil.try(:to_s)
  assert_nil nil.try(:to_i)
end
test_nonexisting_method() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 11
def test_nonexisting_method
  method = :undefined_method
  assert !@string.respond_to?(method)
  assert_nil @string.try(method)
end
test_nonexisting_method_bang() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 23
def test_nonexisting_method_bang
  method = :undefined_method
  assert !@string.respond_to?(method)
  assert_raise(NoMethodError) { @string.try!(method) }
end
test_nonexisting_method_with_arguments() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 17
def test_nonexisting_method_with_arguments
  method = :undefined_method
  assert !@string.respond_to?(method)
  assert_nil @string.try(method, "llo", "y")
end
test_nonexisting_method_with_arguments_bang() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 29
def test_nonexisting_method_with_arguments_bang
  method = :undefined_method
  assert !@string.respond_to?(method)
  assert_raise(NoMethodError) { @string.try!(method, "llo", "y") }
end
test_try_only_block() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 56
def test_try_only_block
  assert_equal @string.reverse, @string.try(&:reverse)
end
test_try_only_block_bang() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 60
def test_try_only_block_bang
  assert_equal @string.reverse, @string.try!(&:reverse)
end
test_try_only_block_nil() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 64
def test_try_only_block_nil
  ran = false
  nil.try { ran = true }
  assert_equal false, ran
end
test_try_with_instance_eval_block() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 70
def test_try_with_instance_eval_block
  assert_equal @string.reverse, @string.try { reverse }
end
test_try_with_instance_eval_block_bang() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 74
def test_try_with_instance_eval_block_bang
  assert_equal @string.reverse, @string.try! { reverse }
end
test_try_with_method_on_delegator() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 118
def test_try_with_method_on_delegator
  assert_equal "delegator method", Decorator.new(@string).try(:delegator_method)
end
test_try_with_method_on_delegator_target() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 122
def test_try_with_method_on_delegator_target
  assert_equal 5, Decorator.new(@string).size
end
test_try_with_overridden_method_on_delegator() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 126
def test_try_with_overridden_method_on_delegator
  assert_equal "overridden reverse", Decorator.new(@string).reverse
end
test_try_with_private_method() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 90
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
test_try_with_private_method_bang() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 78
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
test_try_with_private_method_on_delegator() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 130
def test_try_with_private_method_on_delegator
  assert_nil Decorator.new(@string).try(:private_delegator_method)
end
test_try_with_private_method_on_delegator_bang() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 134
def test_try_with_private_method_on_delegator_bang
  assert_raise(NoMethodError) do
    Decorator.new(@string).try!(:private_delegator_method)
  end
end
test_try_with_private_method_on_delegator_target() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 140
def test_try_with_private_method_on_delegator_target
  klass = Class.new do
    private

      def private_method
        "private method"
      end
  end

  assert_nil Decorator.new(klass.new).try(:private_method)
end
test_try_with_private_method_on_delegator_target_bang() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 152
def test_try_with_private_method_on_delegator_target_bang
  klass = Class.new do
    private

      def private_method
        "private method"
      end
  end

  assert_raise(NoMethodError) do
    Decorator.new(klass.new).try!(:private_method)
  end
end
test_valid_method() click to toggle source
# File activesupport/test/core_ext/object/try_test.rb, line 35
def test_valid_method
  assert_equal 5, @string.try(:size)
end