PageRenderTime 43ms CodeModel.GetById 16ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/delowong/logstash
Ruby | 120 lines | 111 code | 9 blank | 0 comment | 0 complexity | 742318b98a5e722b5ddcdf753b2c9265 MD5 | raw file
  1require 'spec_helper'
  2
  3module RandomTopLevelModule
  4  def self.setup!
  5    shared_examples_for("top level in module") {}
  6  end
  7end
  8
  9module RSpec::Core
 10  describe SharedExampleGroup do
 11
 12    ExampleModule = Module.new
 13    ExampleClass = Class.new
 14
 15    it 'does not add a bunch of private methods to Module' do
 16      seg_methods = RSpec::Core::SharedExampleGroup.private_instance_methods
 17      expect(Module.private_methods & seg_methods).to eq([])
 18    end
 19
 20    module SharedExampleGroup
 21      describe Registry do
 22        it "can safely be reset when there aren't any shared groups" do
 23          expect { Registry.new.clear }.to_not raise_error
 24        end
 25      end
 26    end
 27
 28    before(:all) do
 29      # this is a work around as SharedExampleGroup is not world safe
 30      RandomTopLevelModule.setup!
 31    end
 32
 33    %w[share_examples_for shared_examples_for shared_examples shared_context].each do |shared_method_name|
 34      describe shared_method_name do
 35        it "is exposed to the global namespace" do
 36          expect(Kernel).to respond_to(shared_method_name)
 37        end
 38
 39        it "displays a warning when adding a second shared example group with the same name" do
 40          group = ExampleGroup.describe('example group')
 41          group.send(shared_method_name, 'some shared group') {}
 42          original_declaration = [__FILE__, __LINE__ - 1].join(':')
 43
 44          warning = nil
 45          Kernel.stub(:warn) { |msg| warning = msg }
 46
 47          group.send(shared_method_name, 'some shared group') {}
 48          second_declaration = [__FILE__, __LINE__ - 1].join(':')
 49          expect(warning).to include('some shared group', original_declaration, second_declaration)
 50        end
 51
 52        it 'works with top level defined examples in modules' do
 53          expect(RSpec::configuration.reporter).to_not receive(:deprecation)
 54          group = ExampleGroup.describe('example group') { include_context 'top level in module' }
 55        end
 56
 57        ["name", :name, ExampleModule, ExampleClass].each do |object|
 58          type = object.class.name.downcase
 59          context "given a #{type}" do
 60            it "captures the given #{type} and block in the collection of shared example groups" do
 61              implementation = lambda {}
 62              send(shared_method_name, object, &implementation)
 63              expect(SharedExampleGroup.registry.shared_example_groups[self][object]).to eq implementation
 64            end
 65          end
 66        end
 67
 68        context "given a hash" do
 69          it "delegates extend on configuration" do
 70            implementation = Proc.new { def bar; 'bar'; end }
 71            send(shared_method_name, :foo => :bar, &implementation)
 72            a = RSpec.configuration.include_or_extend_modules.first
 73            expect(a[0]).to eq(:extend)
 74            expect(Class.new.extend(a[1]).new.bar).to eq('bar')
 75            expect(a[2]).to eq(:foo => :bar)
 76          end
 77        end
 78
 79        context "given a string and a hash" do
 80          it "captures the given string and block in the World's collection of shared example groups" do
 81            implementation = lambda {}
 82            send(shared_method_name, "name", :foo => :bar, &implementation)
 83            expect(SharedExampleGroup.registry.shared_example_groups[self]["name"]).to eq implementation
 84          end
 85
 86          it "delegates extend on configuration" do
 87            implementation = Proc.new { def bar; 'bar'; end }
 88            send(shared_method_name, "name", :foo => :bar, &implementation)
 89            a = RSpec.configuration.include_or_extend_modules.first
 90            expect(a[0]).to eq(:extend)
 91            expect(Class.new.extend(a[1]).new.bar).to eq('bar')
 92            expect(a[2]).to eq(:foo => :bar)
 93          end
 94        end
 95      end
 96    end
 97
 98    describe "#share_as" do
 99      before { allow(RSpec).to receive(:deprecate) }
100
101      it "is exposed to the global namespace" do
102        expect(Kernel).to respond_to("share_as")
103      end
104
105      it "adds examples to current example_group using include", :compat => 'rspec-1.2' do
106        share_as('Cornucopia') do
107          it "is plentiful" do
108            expect(5).to eq(4)
109          end
110        end
111        group = ExampleGroup.describe('group') { include Cornucopia }
112        phantom_group = group.children.first
113        expect(phantom_group.description).to eql("")
114        expect(phantom_group.metadata[:shared_group_name]).to eql('Cornucopia')
115        expect(phantom_group.examples.length).to eq(1)
116        expect(phantom_group.examples.first.metadata[:description]).to eq("is plentiful")
117      end
118    end
119  end
120end