|
1 | 1 | module RSpec::Core
|
2 | 2 | RSpec.describe "config block hook filtering" do
|
| 3 | + context "when hooks are defined after a group has been defined" do |
| 4 | + it "still applies" do |
| 5 | + sequence = [] |
| 6 | + |
| 7 | + group = RSpec.describe do |
| 8 | + example { sequence << :ex_1 } |
| 9 | + example { sequence << :ex_2 } |
| 10 | + end |
| 11 | + |
| 12 | + RSpec.configure do |c| |
| 13 | + c.before(:context) { sequence << :before_cont_2 } |
| 14 | + c.prepend_before(:context) { sequence << :before_cont_1 } |
| 15 | + |
| 16 | + c.before(:example) { sequence << :before_ex_2 } |
| 17 | + c.prepend_before(:example) { sequence << :before_ex_1 } |
| 18 | + |
| 19 | + c.after(:context) { sequence << :after_cont_1 } |
| 20 | + c.append_after(:context) { sequence << :after_cont_2 } |
| 21 | + |
| 22 | + c.after(:example) { sequence << :after_ex_1 } |
| 23 | + c.append_after(:example) { sequence << :after_ex_2 } |
| 24 | + |
| 25 | + c.around(:example) do |ex| |
| 26 | + sequence << :around_before_ex |
| 27 | + ex.run |
| 28 | + sequence << :around_after_ex |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + group.run |
| 33 | + |
| 34 | + expect(sequence).to eq [ |
| 35 | + :before_cont_1, :before_cont_2, |
| 36 | + :around_before_ex, :before_ex_1, :before_ex_2, :ex_1, :after_ex_1, :after_ex_2, :around_after_ex, |
| 37 | + :around_before_ex, :before_ex_1, :before_ex_2, :ex_2, :after_ex_1, :after_ex_2, :around_after_ex, |
| 38 | + :after_cont_1, :after_cont_2 |
| 39 | + ] |
| 40 | + end |
| 41 | + |
| 42 | + it "applies only to groups with matching metadata" do |
| 43 | + sequence = [] |
| 44 | + |
| 45 | + unmatching_group = RSpec.describe do |
| 46 | + example { } |
| 47 | + example { } |
| 48 | + end |
| 49 | + |
| 50 | + matching_group = RSpec.describe "", :run_hooks do |
| 51 | + example { sequence << :ex_1 } |
| 52 | + example { sequence << :ex_2 } |
| 53 | + end |
| 54 | + |
| 55 | + RSpec.configure do |c| |
| 56 | + c.before(:context, :run_hooks) { sequence << :before_cont_2 } |
| 57 | + c.prepend_before(:context, :run_hooks) { sequence << :before_cont_1 } |
| 58 | + |
| 59 | + c.before(:example, :run_hooks) { sequence << :before_ex_2 } |
| 60 | + c.prepend_before(:example, :run_hooks) { sequence << :before_ex_1 } |
| 61 | + |
| 62 | + c.after(:context, :run_hooks) { sequence << :after_cont_1 } |
| 63 | + c.append_after(:context, :run_hooks) { sequence << :after_cont_2 } |
| 64 | + |
| 65 | + c.after(:example, :run_hooks) { sequence << :after_ex_1 } |
| 66 | + c.append_after(:example, :run_hooks) { sequence << :after_ex_2 } |
| 67 | + |
| 68 | + c.around(:example, :run_hooks) do |ex| |
| 69 | + sequence << :around_before_ex |
| 70 | + ex.run |
| 71 | + sequence << :around_after_ex |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + expect { unmatching_group.run }.not_to change { sequence }.from([]) |
| 76 | + |
| 77 | + matching_group.run |
| 78 | + expect(sequence).to eq [ |
| 79 | + :before_cont_1, :before_cont_2, |
| 80 | + :around_before_ex, :before_ex_1, :before_ex_2, :ex_1, :after_ex_1, :after_ex_2, :around_after_ex, |
| 81 | + :around_before_ex, :before_ex_1, :before_ex_2, :ex_2, :after_ex_1, :after_ex_2, :around_after_ex, |
| 82 | + :after_cont_1, :after_cont_2 |
| 83 | + ] |
| 84 | + end |
| 85 | + |
| 86 | + it "applies only to examples with matching metadata" do |
| 87 | + sequence = [] |
| 88 | + |
| 89 | + group = RSpec.describe do |
| 90 | + example("") { sequence << :ex_1 } |
| 91 | + example("", :run_hooks) { sequence << :ex_2 } |
| 92 | + end |
| 93 | + |
| 94 | + RSpec.configure do |c| |
| 95 | + c.before(:example, :run_hooks) { sequence << :before_ex_2 } |
| 96 | + c.prepend_before(:example, :run_hooks) { sequence << :before_ex_1 } |
| 97 | + |
| 98 | + c.after(:example, :run_hooks) { sequence << :after_ex_1 } |
| 99 | + c.append_after(:example, :run_hooks) { sequence << :after_ex_2 } |
| 100 | + |
| 101 | + c.around(:example, :run_hooks) do |ex| |
| 102 | + sequence << :around_before_ex |
| 103 | + ex.run |
| 104 | + sequence << :around_after_ex |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + group.run |
| 109 | + expect(sequence).to eq [ |
| 110 | + :ex_1, |
| 111 | + :around_before_ex, :before_ex_1, :before_ex_2, :ex_2, :after_ex_1, :after_ex_2, :around_after_ex, |
| 112 | + ] |
| 113 | + end |
| 114 | + end |
| 115 | + |
3 | 116 | describe "unfiltered hooks" do
|
4 | 117 | it "is run" do
|
5 | 118 | filters = []
|
|
0 commit comments