PageRenderTime 54ms CodeModel.GetById 13ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

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