class ModuleTest

Public Class Methods

new(client) click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 232
def initialize(client)
  @client = client
end
parent_method() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 286
def self.parent_method; end

Public Instance Methods

setup() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 147
def setup
  @david = Someone.new("David", Somewhere.new("Paulina", "Chicago"))
end
test_delegate_line_with_nil() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 303
def test_delegate_line_with_nil
  _, line = Someone.instance_method(:bar).source_location
  assert_equal Someone::FAILED_DELEGATE_LINE_2, line
end
test_delegate_missing_to_affects_respond_to() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 385
def test_delegate_missing_to_affects_respond_to
  assert DecoratedTester.new(@david).respond_to?(:name)
  assert_not DecoratedTester.new(@david).respond_to?(:private_name)
  assert_not DecoratedTester.new(@david).respond_to?(:my_fake_method)

  assert DecoratedTester.new(@david).respond_to?(:name, true)
  assert_not DecoratedTester.new(@david).respond_to?(:private_name, true)
  assert_not DecoratedTester.new(@david).respond_to?(:my_fake_method, true)
end
test_delegate_missing_to_does_not_delegate_to_fake_methods() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 369
def test_delegate_missing_to_does_not_delegate_to_fake_methods
  e = assert_raises(NoMethodError) do
    DecoratedReserved.new(@david).my_fake_method
  end

  assert_match(/undefined method `my_fake_method' for/, e.message)
end
test_delegate_missing_to_does_not_delegate_to_private_methods() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 361
def test_delegate_missing_to_does_not_delegate_to_private_methods
  e = assert_raises(NoMethodError) do
    DecoratedReserved.new(@david).private_name
  end

  assert_match(/undefined method `private_name' for/, e.message)
end
test_delegate_missing_to_raises_delegation_error_if_target_nil() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 377
def test_delegate_missing_to_raises_delegation_error_if_target_nil
  e = assert_raises(Module::DelegationError) do
    DecoratedTester.new(nil).name
  end

  assert_equal "name delegated to client, but client is nil", e.message
end
test_delegate_missing_to_respects_superclass_missing() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 395
def test_delegate_missing_to_respects_superclass_missing
  assert_equal 42, DecoratedTester.new(@david).extra_missing

  assert_respond_to DecoratedTester.new(@david), :extra_missing
end
test_delegate_missing_to_with_method() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 353
def test_delegate_missing_to_with_method
  assert_equal "David", DecoratedTester.new(@david).name
end
test_delegate_missing_to_with_reserved_methods() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 357
def test_delegate_missing_to_with_reserved_methods
  assert_equal "David", DecoratedReserved.new(@david).name
end
test_delegate_with_case() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 401
def test_delegate_with_case
  event = Event.new(Tester.new)
  assert_equal 1, event.foo
end
test_delegation_custom_prefix() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 217
def test_delegation_custom_prefix
  invoice = Invoice.new(@david)
  assert_equal "David", invoice.customer_name
  assert_equal "Paulina", invoice.customer_street
  assert_equal "Chicago", invoice.customer_city
end
test_delegation_does_not_raise_error_when_removing_singleton_instance_methods() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 284
def test_delegation_does_not_raise_error_when_removing_singleton_instance_methods
  parent = Class.new do
    def self.parent_method; end
  end

  assert_nothing_raised do
    Class.new(parent) do
      class << self
        delegate :parent_method, to: :superclass
      end
    end
  end
end
test_delegation_doesnt_mask_nested_no_method_error_on_nil_receiver() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 338
def test_delegation_doesnt_mask_nested_no_method_error_on_nil_receiver
  product = Product.new("Widget")

  # Nested NoMethodError is a different name from the delegation
  assert_raise(NoMethodError) { product.manufacturer_name }

  # Nested NoMethodError is the same name as the delegation
  assert_raise(NoMethodError) { product.type_name }
end
test_delegation_down_hierarchy() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 172
def test_delegation_down_hierarchy
  assert_equal "CHICAGO", @david.upcase
end
test_delegation_exception_backtrace() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 308
def test_delegation_exception_backtrace
  someone = Someone.new("foo", "bar")
  someone.foo
rescue NoMethodError => e
  file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE}"
  # We can't simply check the first line of the backtrace, because JRuby reports the call to __send__ in the backtrace.
  assert e.backtrace.any? { |a| a.include?(file_and_line) },
         "[#{e.backtrace.inspect}] did not include [#{file_and_line}]"
end
test_delegation_exception_backtrace_with_allow_nil() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 318
def test_delegation_exception_backtrace_with_allow_nil
  someone = Someone.new("foo", "bar")
  someone.bar
rescue NoMethodError => e
  file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE_2}"
  # We can't simply check the first line of the backtrace, because JRuby reports the call to __send__ in the backtrace.
  assert e.backtrace.any? { |a| a.include?(file_and_line) },
         "[#{e.backtrace.inspect}] did not include [#{file_and_line}]"
end
test_delegation_invokes_the_target_exactly_once() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 328
def test_delegation_invokes_the_target_exactly_once
  se = SideEffect.new

  assert_equal 1, se.to_i
  assert_equal [2, 3], se.ints

  assert_equal "2", se.to_s
  assert_equal [3], se.ints
end
test_delegation_line_number() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 298
def test_delegation_line_number
  _, line = Someone.instance_method(:foo).source_location
  assert_equal Someone::FAILED_DELEGATE_LINE, line
end
test_delegation_prefix() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 210
def test_delegation_prefix
  invoice = Invoice.new(@david)
  assert_equal "David", invoice.client_name
  assert_equal "Paulina", invoice.client_street
  assert_equal "Chicago", invoice.client_city
end
test_delegation_prefix_with_instance_variable() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 229
def test_delegation_prefix_with_instance_variable
  assert_raise ArgumentError do
    Class.new do
      def initialize(client)
        @client = client
      end
      delegate :name, :address, to: :@client, prefix: true
    end
  end
end
test_delegation_prefix_with_nil_or_false() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 224
def test_delegation_prefix_with_nil_or_false
  assert_equal "David", Developer.new(@david).name
  assert_equal "David", Tester.new(@david).name
end
test_delegation_target_when_prefix_is_true() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 195
def test_delegation_target_when_prefix_is_true
  assert_nothing_raised do
    Name.send :delegate, :go, to: :you, prefix: true
  end
  assert_nothing_raised do
    Name.send :delegate, :go, to: :_you, prefix: true
  end
  assert_raise(ArgumentError) do
    Name.send :delegate, :go, to: :You, prefix: true
  end
  assert_raise(ArgumentError) do
    Name.send :delegate, :go, to: :@you, prefix: true
  end
end
test_delegation_to_assignment_method() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 156
def test_delegation_to_assignment_method
  @david.place_name = "Fred"
  assert_equal "Fred", @david.place.name
end
test_delegation_to_class_method() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 181
def test_delegation_to_class_method
  assert_equal "some_table", @david.table_name
  assert_equal "some_table", @david.class_table_name
end
test_delegation_to_index_get_method() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 161
def test_delegation_to_index_get_method
  @params = ParameterSet.new
  assert_equal "bar", @params[:foo]
end
test_delegation_to_index_set_method() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 166
def test_delegation_to_index_set_method
  @params = ParameterSet.new
  @params[:foo] = "baz"
  assert_equal "baz", @params[:foo]
end
test_delegation_to_instance_variable() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 176
def test_delegation_to_instance_variable
  david = Name.new("David", "Hansson")
  assert_equal "DAVID HANSSON", david.upcase
end
test_delegation_to_method_that_exists_on_nil() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 274
def test_delegation_to_method_that_exists_on_nil
  nil_person = Someone.new(nil)
  assert_equal 0.0, nil_person.to_f
end
test_delegation_to_method_that_exists_on_nil_when_allowing_nil() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 279
def test_delegation_to_method_that_exists_on_nil_when_allowing_nil
  nil_project = Project.new(nil)
  assert_equal 0.0, nil_project.to_f
end
test_delegation_to_methods() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 151
def test_delegation_to_methods
  assert_equal "Paulina", @david.street
  assert_equal "Chicago", @david.city
end
test_delegation_with_allow_nil() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 240
def test_delegation_with_allow_nil
  quails = Project.new("Quails", Someone.new("David"))
  assert_equal "David", quails.name
end
test_delegation_with_allow_nil_and_false_value() click to toggle source

Ensures with check for nil, not for a falseish target.

# File activesupport/test/core_ext/module_test.rb, line 251
def test_delegation_with_allow_nil_and_false_value
  project = Project.new(false, false)
  assert_raise(NoMethodError) { project.name }
end
test_delegation_with_allow_nil_and_invalid_value() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 256
def test_delegation_with_allow_nil_and_invalid_value
  quails = Project.new("Quails", "David")
  assert_raise(NoMethodError) { quails.name }
end
test_delegation_with_allow_nil_and_nil_value() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 245
def test_delegation_with_allow_nil_and_nil_value
  quails = Project.new("Quails")
  assert_nil quails.name
end
test_delegation_with_allow_nil_and_nil_value_and_prefix() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 261
def test_delegation_with_allow_nil_and_nil_value_and_prefix
  Project.class_eval do
    delegate :name, to: :person, allow_nil: true, prefix: true
  end
  quails = Project.new("Quails")
  assert_nil quails.person_name
end
test_delegation_with_method_arguments() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 348
def test_delegation_with_method_arguments
  has_block = HasBlock.new(Block.new)
  assert has_block.hello?
end
test_delegation_without_allow_nil_and_nil_value() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 269
def test_delegation_without_allow_nil_and_nil_value
  david = Someone.new("David")
  assert_raise(Module::DelegationError) { david.street }
end
test_missing_delegation_target() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 186
def test_missing_delegation_target
  assert_raise(ArgumentError) do
    Name.send :delegate, :nowhere
  end
  assert_raise(ArgumentError) do
    Name.send :delegate, :noplace, tos: :hollywood
  end
end
test_private_delegate() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 406
def test_private_delegate
  location = Class.new do
    def initialize(place)
      @place = place
    end

    private(*delegate(:street, :city, to: :@place))
  end

  place = location.new(Somewhere.new("Such street", "Sad city"))

  assert_not place.respond_to?(:street)
  assert_not place.respond_to?(:city)

  assert place.respond_to?(:street, true) # Asking for private method
  assert place.respond_to?(:city, true)
end
test_private_delegate_prefixed() click to toggle source
# File activesupport/test/core_ext/module_test.rb, line 424
def test_private_delegate_prefixed
  location = Class.new do
    def initialize(place)
      @place = place
    end

    private(*delegate(:street, :city, to: :@place, prefix: :the))
  end

  place = location.new(Somewhere.new("Such street", "Sad city"))

  assert_not place.respond_to?(:street)
  assert_not place.respond_to?(:city)

  assert_not place.respond_to?(:the_street)
  assert place.respond_to?(:the_street, true)
  assert_not place.respond_to?(:the_city)
  assert place.respond_to?(:the_city, true)
end