Sha256: b67c4fd89d352423945179f296b6b08d023b8c7c0d512c3d62647f585a3d7975

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module RSpec
  module Core
    module SharedExampleGroup
      class Collection

        def initialize(sources, examples)
          @sources, @examples = sources, examples
        end

        def [](key)
          fetch_examples(key) || warn_deprecation_and_fetch_anyway(key)
        end

        private

          def fetch_examples(key)
            @examples[source_for key][key]
          end

          def source_for(key)
            @sources.reverse.find { |source| @examples[source].has_key? key }
          end

          def fetch_anyway(key)
            @examples.values.inject({}, &:merge)[key]
          end

          def warn_deprecation_and_fetch_anyway(key)
            if (example = fetch_anyway key)
              RSpec.warn_deprecation <<-WARNING.gsub(/^ /, '')
                Accessing shared_examples defined across contexts is deprecated.
                Please declare shared_examples within a shared context, or at the top level.
                This message was generated at: #{caller(0)[8]}
              WARNING
              example
            end
          end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec-core-2.14.0 lib/rspec/core/shared_example_group/collection.rb
rspec-core-2.14.0.rc1 lib/rspec/core/shared_example_group/collection.rb