PageRenderTime 74ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/callback_inheritance_test.rb

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