/vendor/bundle/jruby/2.1/gems/rspec-core-2.14.8/spec/rspec/core/hooks_filtering_spec.rb

https://github.com/delowong/logstash · Ruby · 227 lines · 202 code · 25 blank · 0 comment · 3 complexity · 06cf90bf249da6e64da63f09ddef28a5 MD5 · raw file

  1. require "spec_helper"
  2. module RSpec::Core
  3. describe "config block hook filtering" do
  4. describe "unfiltered hooks" do
  5. it "is run" do
  6. filters = []
  7. RSpec.configure do |c|
  8. c.before(:all) { filters << "before all in config"}
  9. c.around(:each) {|example| filters << "around each in config"; example.run}
  10. c.before(:each) { filters << "before each in config"}
  11. c.after(:each) { filters << "after each in config"}
  12. c.after(:all) { filters << "after all in config"}
  13. end
  14. group = ExampleGroup.describe
  15. group.example("example") {}
  16. group.run
  17. expect(filters).to eq([
  18. "before all in config",
  19. "around each in config",
  20. "before each in config",
  21. "after each in config",
  22. "after all in config"
  23. ])
  24. end
  25. end
  26. describe "hooks with single filters" do
  27. context "with no scope specified" do
  28. it "is run around|before|after :each if the filter matches the example group's filter" do
  29. filters = []
  30. RSpec.configure do |c|
  31. c.around(:match => true) {|example| filters << "around each in config"; example.run}
  32. c.before(:match => true) { filters << "before each in config"}
  33. c.after(:match => true) { filters << "after each in config"}
  34. end
  35. group = ExampleGroup.describe(:match => true)
  36. group.example("example") {}
  37. group.run
  38. expect(filters).to eq([
  39. "around each in config",
  40. "before each in config",
  41. "after each in config"
  42. ])
  43. end
  44. end
  45. it "is run if the filter matches the example group's filter" do
  46. filters = []
  47. RSpec.configure do |c|
  48. c.before(:all, :match => true) { filters << "before all in config"}
  49. c.around(:each, :match => true) {|example| filters << "around each in config"; example.run}
  50. c.before(:each, :match => true) { filters << "before each in config"}
  51. c.after(:each, :match => true) { filters << "after each in config"}
  52. c.after(:all, :match => true) { filters << "after all in config"}
  53. end
  54. group = ExampleGroup.describe(:match => true)
  55. group.example("example") {}
  56. group.run
  57. expect(filters).to eq([
  58. "before all in config",
  59. "around each in config",
  60. "before each in config",
  61. "after each in config",
  62. "after all in config"
  63. ])
  64. end
  65. it "runs before|after :all hooks on matching nested example groups" do
  66. filters = []
  67. RSpec.configure do |c|
  68. c.before(:all, :match => true) { filters << :before_all }
  69. c.after(:all, :match => true) { filters << :after_all }
  70. end
  71. example_1_filters = example_2_filters = nil
  72. group = ExampleGroup.describe "group" do
  73. it("example 1") { example_1_filters = filters.dup }
  74. describe "subgroup", :match => true do
  75. it("example 2") { example_2_filters = filters.dup }
  76. end
  77. end
  78. group.run
  79. expect(example_1_filters).to be_empty
  80. expect(example_2_filters).to eq([:before_all])
  81. expect(filters).to eq([:before_all, :after_all])
  82. end
  83. it "runs before|after :all hooks only on the highest level group that matches the filter" do
  84. filters = []
  85. RSpec.configure do |c|
  86. c.before(:all, :match => true) { filters << :before_all }
  87. c.after(:all, :match => true) { filters << :after_all }
  88. end
  89. example_1_filters = example_2_filters = example_3_filters = nil
  90. group = ExampleGroup.describe "group", :match => true do
  91. it("example 1") { example_1_filters = filters.dup }
  92. describe "subgroup", :match => true do
  93. it("example 2") { example_2_filters = filters.dup }
  94. describe "sub-subgroup", :match => true do
  95. it("example 3") { example_3_filters = filters.dup }
  96. end
  97. end
  98. end
  99. group.run
  100. expect(example_1_filters).to eq([:before_all])
  101. expect(example_2_filters).to eq([:before_all])
  102. expect(example_3_filters).to eq([:before_all])
  103. expect(filters).to eq([:before_all, :after_all])
  104. end
  105. it "does not run if the filter doesn't match the example group's filter" do
  106. filters = []
  107. RSpec.configure do |c|
  108. c.before(:all, :match => false) { filters << "before all in config"}
  109. c.around(:each, :match => false) {|example| filters << "around each in config"; example.run}
  110. c.before(:each, :match => false) { filters << "before each in config"}
  111. c.after(:each, :match => false) { filters << "after each in config"}
  112. c.after(:all, :match => false) { filters << "after all in config"}
  113. end
  114. group = ExampleGroup.describe(:match => true)
  115. group.example("example") {}
  116. group.run
  117. expect(filters).to eq([])
  118. end
  119. context "when the hook filters apply to individual examples instead of example groups" do
  120. let(:each_filters) { [] }
  121. let(:all_filters) { [] }
  122. let(:group) do
  123. md = example_metadata
  124. ExampleGroup.describe do
  125. it("example", md) { }
  126. end
  127. end
  128. def filters
  129. each_filters + all_filters
  130. end
  131. before(:each) do
  132. af, ef = all_filters, each_filters
  133. RSpec.configure do |c|
  134. c.before(:all, :foo => :bar) { af << "before all in config"}
  135. c.around(:each, :foo => :bar) {|example| ef << "around each in config"; example.run}
  136. c.before(:each, :foo => :bar) { ef << "before each in config"}
  137. c.after(:each, :foo => :bar) { ef << "after each in config"}
  138. c.after(:all, :foo => :bar) { af << "after all in config"}
  139. end
  140. group.run
  141. end
  142. describe 'an example with matching metadata' do
  143. let(:example_metadata) { { :foo => :bar } }
  144. it "runs the `:each` hooks" do
  145. expect(each_filters).to eq([
  146. 'around each in config',
  147. 'before each in config',
  148. 'after each in config'
  149. ])
  150. end
  151. it "does not run the `:all` hooks" do
  152. expect(all_filters).to be_empty
  153. end
  154. end
  155. describe 'an example without matching metadata' do
  156. let(:example_metadata) { { :foo => :bazz } }
  157. it "does not run any of the hooks" do
  158. expect(filters).to be_empty
  159. end
  160. end
  161. end
  162. end
  163. describe "hooks with multiple filters" do
  164. it "is run if all hook filters match the group's filters" do
  165. filters = []
  166. RSpec.configure do |c|
  167. c.before(:all, :one => 1) { filters << "before all in config"}
  168. c.around(:each, :two => 2, :one => 1) {|example| filters << "around each in config"; example.run}
  169. c.before(:each, :one => 1, :two => 2) { filters << "before each in config"}
  170. c.after(:each, :one => 1, :two => 2, :three => 3) { filters << "after each in config"}
  171. c.after(:all, :one => 1, :three => 3) { filters << "after all in config"}
  172. end
  173. group = ExampleGroup.describe(:one => 1, :two => 2, :three => 3)
  174. group.example("example") {}
  175. group.run
  176. expect(filters).to eq([
  177. "before all in config",
  178. "around each in config",
  179. "before each in config",
  180. "after each in config",
  181. "after all in config"
  182. ])
  183. end
  184. it "does not run if some hook filters don't match the group's filters" do
  185. filters = []
  186. RSpec.configure do |c|
  187. c.before(:all, :one => 1, :four => 4) { filters << "before all in config"}
  188. c.around(:each, :two => 2, :four => 4) {|example| filters << "around each in config"; example.run}
  189. c.before(:each, :one => 1, :two => 2, :four => 4) { filters << "before each in config"}
  190. c.after(:each, :one => 1, :two => 2, :three => 3, :four => 4) { filters << "after each in config"}
  191. c.after(:all, :one => 1, :three => 3, :four => 4) { filters << "after all in config"}
  192. end
  193. group = ExampleGroup.describe(:one => 1, :two => 2, :three => 3)
  194. group.example("example") {}
  195. group.run
  196. expect(filters).to eq([])
  197. end
  198. end
  199. end
  200. end