class CallbacksTest::ConditionalTests

Public Instance Methods

build_class(callback) click to toggle source
# File activesupport/test/callbacks_test.rb, line 980
def build_class(callback)
  Class.new {
    include ActiveSupport::Callbacks
    define_callbacks :foo
    set_callback :foo, :before, :foo, if: callback
    def foo; end
    def run; run_callbacks :foo; end
  }
end
foo() click to toggle source
# File activesupport/test/callbacks_test.rb, line 985
def foo; end
run() click to toggle source
# File activesupport/test/callbacks_test.rb, line 986
def run; run_callbacks :foo; end
test_class() click to toggle source

FIXME: do we really want to support classes as conditionals? There were no tests for it previous to this.

# File activesupport/test/callbacks_test.rb, line 1012
def test_class
  z = []
  klass = build_class Class.new {
    define_singleton_method(:before) { |o| z << o }
  }
  object = klass.new
  object.run
  assert_equal [object], z
end
test_class_conditional_with_scope() click to toggle source

FIXME: do we really want to support classes as conditionals? There were no tests for it previous to this.

# File activesupport/test/callbacks_test.rb, line 992
def test_class_conditional_with_scope
  z = []
  callback = Class.new {
    define_singleton_method(:foo) { |o| z << o }
  }
  klass = Class.new {
    include ActiveSupport::Callbacks
    define_callbacks :foo, scope: [:name]
    set_callback :foo, :before, :foo, if: callback
    def run; run_callbacks :foo; end
    private
      def foo; end
  }
  object = klass.new
  object.run
  assert_equal [object], z
end
test_proc_arity0() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1029
def test_proc_arity0
  z = []
  object = build_class(->() { z << 0 }).new
  object.run
  assert_equal [0], z
end
test_proc_arity1() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1036
def test_proc_arity1
  z = []
  object = build_class(->(x) { z << x }).new
  object.run
  assert_equal [object], z
end
test_proc_arity2() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1043
def test_proc_arity2
  assert_raises(ArgumentError) do
    object = build_class(->(a, b) {}).new
    object.run
  end
end
test_proc_negative_arity() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1022
def test_proc_negative_arity # passes an empty list if *args
  z = []
  object = build_class(->(*args) { z << args }).new
  object.run
  assert_equal [], z.flatten
end