/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

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