PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/activesupport/test/callback_inheritance_test.rb

https://bitbucket.org/druly/rails_cherry_pick
Ruby | 179 lines | 140 code | 39 blank | 0 comment | 10 complexity | f78358d2ae1c744ab444019645417883 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'test/unit'
  3. class GrandParent
  4. include ActiveSupport::Callbacks
  5. attr_reader :log, :action_name
  6. def initialize(action_name)
  7. @action_name, @log = action_name, []
  8. end
  9. define_callbacks :dispatch
  10. set_callback :dispatch, :before, :before1, :before2, :per_key => {:if => proc {|c| c.action_name == "index" || c.action_name == "update" }}
  11. set_callback :dispatch, :after, :after1, :after2, :per_key => {:if => proc {|c| c.action_name == "update" || c.action_name == "delete" }}
  12. def before1
  13. @log << "before1"
  14. end
  15. def before2
  16. @log << "before2"
  17. end
  18. def after1
  19. @log << "after1"
  20. end
  21. def after2
  22. @log << "after2"
  23. end
  24. def dispatch
  25. run_callbacks(:dispatch, action_name) do
  26. @log << action_name
  27. end
  28. self
  29. end
  30. end
  31. class Parent < GrandParent
  32. skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}
  33. skip_callback :dispatch, :after, :after2, :per_key => {:unless => proc {|c| c.action_name == "delete" }}
  34. end
  35. class Child < GrandParent
  36. skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}, :if => :state_open?
  37. def state_open?
  38. @state == :open
  39. end
  40. def initialize(action_name, state)
  41. super(action_name)
  42. @state = state
  43. end
  44. end
  45. class EmptyParent
  46. include ActiveSupport::Callbacks
  47. def performed?
  48. @performed ||= false
  49. end
  50. define_callbacks :dispatch
  51. def perform!
  52. @performed = true
  53. end
  54. def dispatch
  55. run_callbacks :dispatch
  56. self
  57. end
  58. end
  59. class EmptyChild < EmptyParent
  60. set_callback :dispatch, :before, :do_nothing
  61. def do_nothing
  62. end
  63. end
  64. class CountingParent
  65. include ActiveSupport::Callbacks
  66. attr_reader :count
  67. define_callbacks :dispatch
  68. def initialize
  69. @count = 0
  70. end
  71. def count!
  72. @count += 1
  73. end
  74. def dispatch
  75. run_callbacks(:dispatch)
  76. self
  77. end
  78. end
  79. class CountingChild < CountingParent
  80. end
  81. class BasicCallbacksTest < Test::Unit::TestCase
  82. def setup
  83. @index = GrandParent.new("index").dispatch
  84. @update = GrandParent.new("update").dispatch
  85. @delete = GrandParent.new("delete").dispatch
  86. @unknown = GrandParent.new("unknown").dispatch
  87. end
  88. def test_basic_per_key1
  89. assert_equal %w(before1 before2 index), @index.log
  90. end
  91. def test_basic_per_key2
  92. assert_equal %w(before1 before2 update after2 after1), @update.log
  93. end
  94. def test_basic_per_key3
  95. assert_equal %w(delete after2 after1), @delete.log
  96. end
  97. end
  98. class InheritedCallbacksTest < Test::Unit::TestCase
  99. def setup
  100. @index = Parent.new("index").dispatch
  101. @update = Parent.new("update").dispatch
  102. @delete = Parent.new("delete").dispatch
  103. @unknown = Parent.new("unknown").dispatch
  104. end
  105. def test_inherited_excluded
  106. assert_equal %w(before1 index), @index.log
  107. end
  108. def test_inherited_not_excluded
  109. assert_equal %w(before1 before2 update after1), @update.log
  110. end
  111. def test_partially_excluded
  112. assert_equal %w(delete after2 after1), @delete.log
  113. end
  114. end
  115. class InheritedCallbacksTest2 < Test::Unit::TestCase
  116. def setup
  117. @update1 = Child.new("update", :open).dispatch
  118. @update2 = Child.new("update", :closed).dispatch
  119. end
  120. def test_crazy_mix_on
  121. assert_equal %w(before1 update after2 after1), @update1.log
  122. end
  123. def test_crazy_mix_off
  124. assert_equal %w(before1 before2 update after2 after1), @update2.log
  125. end
  126. end
  127. class DynamicInheritedCallbacks < Test::Unit::TestCase
  128. def test_callbacks_looks_to_the_superclass_before_running
  129. child = EmptyChild.new.dispatch
  130. assert !child.performed?
  131. EmptyParent.set_callback :dispatch, :before, :perform!
  132. child = EmptyChild.new.dispatch
  133. assert child.performed?
  134. end
  135. def test_callbacks_should_be_performed_once_in_child_class
  136. CountingParent.set_callback(:dispatch, :before) { count! }
  137. child = CountingChild.new.dispatch
  138. assert_equal 1, child.count
  139. end
  140. end