class CallbacksTest::CallbackTypeTest

Public Class Methods

skip(*things) click to toggle source
# File activesupport/test/callbacks_test.rb, line 1098
def self.skip(*things); skip_callback :foo, :before, *things; end

Public Instance Methods

build_class(callback, n = 10) click to toggle source
# File activesupport/test/callbacks_test.rb, line 1092
def build_class(callback, n = 10)
  Class.new {
    include ActiveSupport::Callbacks
    define_callbacks :foo
    n.times { set_callback :foo, :before, callback }
    def run; run_callbacks :foo; end
    def self.skip(*things); skip_callback :foo, :before, *things; end
  }
end
run() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1097
def run; run_callbacks :foo; end
test_add_class() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1102
def test_add_class
  calls = []
  callback = Class.new {
    define_singleton_method(:before) { |o| calls << o }
  }
  build_class(callback).new.run
  assert_equal 10, calls.length
end
test_add_lambda() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1111
def test_add_lambda
  calls = []
  build_class(->(o) { calls << o }).new.run
  assert_equal 10, calls.length
end
test_add_symbol() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1117
def test_add_symbol
  calls = []
  klass = build_class(:bar)
  klass.class_eval { define_method(:bar) { calls << klass } }
  klass.new.run
  assert_equal 1, calls.length
end
test_skip_class() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1125
def test_skip_class # removes one at a time
  calls = []
  callback = Class.new {
    define_singleton_method(:before) { |o| calls << o }
  }
  klass = build_class(callback)
  9.downto(0) { |i|
    klass.skip callback
    klass.new.run
    assert_equal i, calls.length
    calls.clear
  }
end
test_skip_lambda() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1139
def test_skip_lambda # raises error
  calls = []
  callback = ->(o) { calls << o }
  klass = build_class(callback)
  assert_raises(ArgumentError) { klass.skip callback }
  klass.new.run
  assert_equal 10, calls.length
end
test_skip_string() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1157
def test_skip_string # raises error
  calls = []
  klass = build_class(:bar)
  klass.class_eval { define_method(:bar) { calls << klass } }
  assert_raises(ArgumentError) { klass.skip "bar" }
  klass.new.run
  assert_equal 1, calls.length
end
test_skip_symbol() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1148
def test_skip_symbol # removes all
  calls = []
  klass = build_class(:bar)
  klass.class_eval { define_method(:bar) { calls << klass } }
  klass.skip :bar
  klass.new.run
  assert_equal 0, calls.length
end
test_skip_undefined_callback() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1166
def test_skip_undefined_callback # raises error
  calls = []
  klass = build_class(:bar)
  klass.class_eval { define_method(:bar) { calls << klass } }
  assert_raises(ArgumentError) { klass.skip :qux }
  klass.new.run
  assert_equal 1, calls.length
end
test_skip_without_raise() click to toggle source
# File activesupport/test/callbacks_test.rb, line 1175
def test_skip_without_raise # removes nothing
  calls = []
  klass = build_class(:bar)
  klass.class_eval { define_method(:bar) { calls << klass } }
  klass.skip :qux, raise: false
  klass.new.run
  assert_equal 1, calls.length
end