rubocop -a --only Style/StringLiterals

This commit is contained in:
k0va1 2020-05-30 12:09:29 +03:00
parent 3feae81442
commit 38284c1fd6
16 changed files with 250 additions and 250 deletions

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true
source 'https://rubygems.org'
source "https://rubygems.org"
eval_gemfile 'Gemfile.devtools'
eval_gemfile "Gemfile.devtools"
gemspec
group :tools do
gem 'pry-byebug', platform: :mri
gem "pry-byebug", platform: :mri
end

View File

@ -1,14 +1,14 @@
#!/usr/bin/env rake
# frozen_string_literal: true
require 'bundler/gem_tasks'
require "bundler/gem_tasks"
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
require 'rspec/core'
require 'rspec/core/rake_task'
require "rspec/core"
require "rspec/core/rake_task"
task default: :spec
desc 'Run all specs in spec directory'
desc "Run all specs in spec directory"
RSpec::Core::RakeTask.new(:spec)

View File

@ -1,3 +1,3 @@
# frozen_string_literal: true
require 'dry/container'
require "dry/container"

View File

@ -1,13 +1,13 @@
# frozen_string_literal: true
require 'dry-configurable'
require 'dry/container/error'
require 'dry/container/namespace'
require 'dry/container/registry'
require 'dry/container/resolver'
require 'dry/container/namespace_dsl'
require 'dry/container/mixin'
require 'dry/container/version'
require "dry-configurable"
require "dry/container/error"
require "dry/container/namespace"
require "dry/container/registry"
require "dry/container/resolver"
require "dry/container/namespace_dsl"
require "dry/container/mixin"
require "dry/container/version"
# A collection of micro-libraries, each intended to encapsulate
# a common task in Ruby

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'dry/container/item'
require "dry/container/item"
module Dry
class Container

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'dry/container/item/memoizable'
require 'dry/container/item/callable'
require "dry/container/item/memoizable"
require "dry/container/item/callable"
module Dry
class Container

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'dry/container/item'
require "dry/container/item"
module Dry
class Container
@ -41,7 +41,7 @@ module Dry
# @private
def raise_not_supported_error
raise ::Dry::Container::Error, 'Memoize only supported for a block or a proc'.freeze
raise ::Dry::Container::Error, "Memoize only supported for a block or a proc".freeze
end
end
end

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'concurrent/hash'
require "concurrent/hash"
module Dry
class Container
@ -49,7 +49,7 @@ module Dry
setting :registry, ::Dry::Container::Registry.new
setting :resolver, ::Dry::Container::Resolver.new
setting :namespace_separator, '.'
setting :namespace_separator, "."
@_container = ::Concurrent::Hash.new
end
@ -71,7 +71,7 @@ module Dry
setting :registry, ::Dry::Container::Registry.new
setting :resolver, ::Dry::Container::Resolver.new
setting :namespace_separator, '.'
setting :namespace_separator, "."
def config
self.class.config
@ -221,7 +221,7 @@ module Dry
if with.is_a?(Class)
decorator = with.method(:new)
elsif block.nil? && !with.respond_to?(:call)
raise Error, 'Decorator needs to be a Class, block, or respond to the `call` method'
raise Error, "Decorator needs to be a Class, block, or respond to the `call` method"
else
decorator = with || block
end

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'delegate'
require "delegate"
module Dry
class Container

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'dry/container/item/factory'
require "dry/container/item/factory"
module Dry
class Container

View File

@ -3,6 +3,6 @@
module Dry
class Container
# @api public
VERSION = '0.7.2'.freeze
VERSION = "0.7.2".freeze
end
end

View File

@ -1,18 +1,18 @@
# frozen_string_literal: true
begin
require 'rubocop/rake_task'
require "rubocop/rake_task"
Rake::Task[:default].enhance [:rubocop]
RuboCop::RakeTask.new do |task|
task.options << '--display-cop-names'
task.options << "--display-cop-names"
end
namespace :rubocop do
desc 'Generate a configuration file acting as a TODO list.'
desc "Generate a configuration file acting as a TODO list."
task :auto_gen_config do
exec 'bundle exec rubocop --auto-gen-config'
exec "bundle exec rubocop --auto-gen-config"
end
end

View File

@ -4,10 +4,10 @@ RSpec.describe Dry::Container do
let(:klass) { Dry::Container }
let(:container) { klass.new }
it_behaves_like 'a container'
it_behaves_like "a container"
describe 'inheritance' do
it 'sets up a container for a child class' do
describe "inheritance" do
it "sets up a container for a child class" do
parent = Class.new { extend Dry::Container::Mixin }
child = Class.new(parent)

View File

@ -1,24 +1,24 @@
# frozen_string_literal: true
RSpec.describe Dry::Container::Mixin do
describe 'extended' do
describe "extended" do
let(:klass) do
Class.new { extend Dry::Container::Mixin }
end
let(:container) { klass }
it_behaves_like 'a container'
it_behaves_like "a container"
end
describe 'included' do
describe "included" do
let(:klass) do
Class.new { include Dry::Container::Mixin }
end
let(:container) { klass.new }
it_behaves_like 'a container'
it_behaves_like "a container"
context 'into a class with a custom .initialize method' do
context "into a class with a custom .initialize method" do
let(:klass) do
Class.new do
include Dry::Container::Mixin
@ -26,7 +26,7 @@ RSpec.describe Dry::Container::Mixin do
end
end
it 'does not fail on missing member variable' do
it "does not fail on missing member variable" do
expect { container.register :key, -> {} }.to_not raise_error
end
end

View File

@ -1,20 +1,20 @@
# frozen_string_literal: true
require_relative 'support/coverage'
require_relative "support/coverage"
begin
require 'pry-byebug'
require "pry-byebug"
rescue LoadError
end
require 'pathname'
require "pathname"
Dir[Pathname(__FILE__).dirname.join('support/**/*.rb').to_s].each do |file|
Dir[Pathname(__FILE__).dirname.join("support/**/*.rb").to_s].each do |file|
require file
end
require 'dry/container'
require 'dry/container/stub'
require "dry/container"
require "dry/container/stub"
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
@ -53,7 +53,7 @@ RSpec.configure do |config|
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = 'spec/examples.txt'
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
@ -73,7 +73,7 @@ RSpec.configure do |config|
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
config.default_formatter = "doc"
end
# Print the n slowest examples and example groups at the

View File

@ -1,14 +1,14 @@
# frozen_string_literal: true
RSpec.shared_examples 'a container' do
describe 'configuration' do
describe 'registry' do
describe 'default' do
RSpec.shared_examples "a container" do
describe "configuration" do
describe "registry" do
describe "default" do
it { expect(klass.config.registry).to be_a(Dry::Container::Registry) }
end
describe 'custom' do
let(:custom_registry) { double('Registry') }
describe "custom" do
let(:custom_registry) { double("Registry") }
let(:key) { :key }
let(:item) { :item }
let(:options) { {} }
@ -42,14 +42,14 @@ RSpec.shared_examples 'a container' do
end
end
describe 'resolver' do
describe 'default' do
describe "resolver" do
describe "default" do
it { expect(klass.config.resolver).to be_a(Dry::Container::Resolver) }
end
describe 'custom' do
let(:custom_resolver) { double('Resolver') }
let(:item) { double('Item') }
describe "custom" do
let(:custom_resolver) { double("Resolver") }
let(:item) { double("Item") }
let(:key) { :key }
before do
@ -75,16 +75,16 @@ RSpec.shared_examples 'a container' do
end
end
describe 'namespace_separator' do
describe 'default' do
it { expect(klass.config.namespace_separator).to eq('.') }
describe "namespace_separator" do
describe "default" do
it { expect(klass.config.namespace_separator).to eq(".") }
end
describe 'custom' do
let(:custom_registry) { double('Registry') }
let(:key) { 'key' }
let(:namespace_separator) { '-' }
let(:namespace) { 'one' }
describe "custom" do
let(:custom_registry) { double("Registry") }
let(:key) { "key" }
let(:namespace_separator) { "-" }
let(:namespace) { "one" }
before do
klass.configure do |config|
@ -92,7 +92,7 @@ RSpec.shared_examples 'a container' do
end
container.namespace(namespace) do
register('key', 'item')
register("key", "item")
end
end
@ -100,106 +100,106 @@ RSpec.shared_examples 'a container' do
# HACK: Have to reset the configuration so that it doesn't
# interfere with other specs
klass.configure do |config|
config.namespace_separator = '.'
config.namespace_separator = "."
end
end
subject! { container.resolve([namespace, key].join(namespace_separator)) }
it { is_expected.to eq('item') }
it { is_expected.to eq("item") }
end
end
end
context 'with default configuration' do
describe 'registering a block' do
context 'without options' do
context 'without arguments' do
it 'registers and resolves an object' do
container.register(:item) { 'item' }
context "with default configuration" do
describe "registering a block" do
context "without options" do
context "without arguments" do
it "registers and resolves an object" do
container.register(:item) { "item" }
expect(container.keys).to eq(['item'])
expect(container.keys).to eq(["item"])
expect(container.key?(:item)).to be true
expect(container.resolve(:item)).to eq('item')
expect(container.resolve(:item)).to eq("item")
end
end
context 'with arguments' do
it 'registers and resolves a proc' do
context "with arguments" do
it "registers and resolves a proc" do
container.register(:item) { |item| item }
expect(container.resolve(:item).call('item')).to eq('item')
expect(container.resolve(:item).call("item")).to eq("item")
end
it 'does not call a proc on resolving if one accepts an arbitrary number of keyword arguments' do
container.register(:item) { |*| 'item' }
it "does not call a proc on resolving if one accepts an arbitrary number of keyword arguments" do
container.register(:item) { |*| "item" }
expect(container.resolve(:item)).to be_a_kind_of Proc
expect(container.resolve(:item).call).to eq('item')
expect(container.resolve(:item).call).to eq("item")
end
end
end
context 'with option call: false' do
it 'registers and resolves a proc' do
container.register(:item, call: false) { 'item' }
context "with option call: false" do
it "registers and resolves a proc" do
container.register(:item, call: false) { "item" }
expect(container.keys).to eq(['item'])
expect(container.keys).to eq(["item"])
expect(container.key?(:item)).to be true
expect(container.resolve(:item).call).to eq('item')
expect(container[:item].call).to eq('item')
expect(container.resolve(:item).call).to eq("item")
expect(container[:item].call).to eq("item")
end
end
end
describe 'registering a proc' do
context 'without options' do
context 'without arguments' do
it 'registers and resolves an object' do
container.register(:item, proc { 'item' })
describe "registering a proc" do
context "without options" do
context "without arguments" do
it "registers and resolves an object" do
container.register(:item, proc { "item" })
expect(container.keys).to eq(['item'])
expect(container.keys).to eq(["item"])
expect(container.key?(:item)).to be true
expect(container.resolve(:item)).to eq('item')
expect(container[:item]).to eq('item')
expect(container.resolve(:item)).to eq("item")
expect(container[:item]).to eq("item")
end
end
context 'with arguments' do
it 'registers and resolves a proc' do
context "with arguments" do
it "registers and resolves a proc" do
container.register(:item, proc { |item| item })
expect(container.keys).to eq(['item'])
expect(container.keys).to eq(["item"])
expect(container.key?(:item)).to be true
expect(container.resolve(:item).call('item')).to eq('item')
expect(container[:item].call('item')).to eq('item')
expect(container.resolve(:item).call("item")).to eq("item")
expect(container[:item].call("item")).to eq("item")
end
end
end
context 'with option call: false' do
it 'registers and resolves a proc' do
container.register(:item, proc { 'item' }, call: false)
context "with option call: false" do
it "registers and resolves a proc" do
container.register(:item, proc { "item" }, call: false)
expect(container.keys).to eq(['item'])
expect(container.keys).to eq(["item"])
expect(container.key?(:item)).to be true
expect(container.resolve(:item).call).to eq('item')
expect(container[:item].call).to eq('item')
expect(container.resolve(:item).call).to eq("item")
expect(container[:item].call).to eq("item")
end
end
context 'with option memoize: true' do
it 'registers and resolves a proc' do
container.register(:item, proc { 'item' }, memoize: true)
context "with option memoize: true" do
it "registers and resolves a proc" do
container.register(:item, proc { "item" }, memoize: true)
expect(container[:item]).to be container[:item]
expect(container.keys).to eq(['item'])
expect(container.keys).to eq(["item"])
expect(container.key?(:item)).to be true
expect(container.resolve(:item)).to eq('item')
expect(container[:item]).to eq('item')
expect(container.resolve(:item)).to eq("item")
expect(container[:item]).to eq("item")
end
it 'only resolves the proc once' do
it "only resolves the proc once" do
resolved_times = 0
container.register(:item, proc { resolved_times += 1 }, memoize: true)
@ -208,33 +208,33 @@ RSpec.shared_examples 'a container' do
expect(container.resolve(:item)).to be 1
end
context 'when receiving something other than a proc' do
context "when receiving something other than a proc" do
it do
expect { container.register(:item, 'Hello!', memoize: true) }.to raise_error(Dry::Container::Error)
expect { container.register(:item, "Hello!", memoize: true) }.to raise_error(Dry::Container::Error)
end
end
end
end
describe 'registering an object' do
context 'without options' do
it 'registers and resolves the object' do
item = 'item'
describe "registering an object" do
context "without options" do
it "registers and resolves the object" do
item = "item"
container.register(:item, item)
expect(container.keys).to eq(['item'])
expect(container.keys).to eq(["item"])
expect(container.key?(:item)).to be true
expect(container.resolve(:item)).to be(item)
expect(container[:item]).to be(item)
end
end
context 'with option call: false' do
it 'registers and resolves an object' do
item = -> { 'test' }
context "with option call: false" do
it "registers and resolves an object" do
item = -> { "test" }
container.register(:item, item, call: false)
expect(container.keys).to eq(['item'])
expect(container.keys).to eq(["item"])
expect(container.key?(:item)).to be true
expect(container.resolve(:item)).to eq(item)
expect(container[:item]).to eq(item)
@ -242,29 +242,29 @@ RSpec.shared_examples 'a container' do
end
end
describe 'registering with the same key multiple times' do
describe "registering with the same key multiple times" do
it do
container.register(:item, proc { 'item' })
container.register(:item, proc { "item" })
expect { container.register(:item, proc { 'item' }) }.to raise_error(Dry::Container::Error)
expect { container.register(:item, proc { "item" }) }.to raise_error(Dry::Container::Error)
end
end
describe 'resolving with a key that has not been registered' do
describe "resolving with a key that has not been registered" do
it do
expect(container.key?(:item)).to be false
expect { container.resolve(:item) }.to raise_error(Dry::Container::Error)
end
end
describe 'mixing Strings and Symbols' do
describe "mixing Strings and Symbols" do
it do
container.register(:item, 'item')
expect(container.resolve('item')).to eql('item')
container.register(:item, "item")
expect(container.resolve("item")).to eql("item")
end
end
describe '#merge' do
describe "#merge" do
let(:key) { :key }
let(:other) { Dry::Container.new }
@ -272,25 +272,25 @@ RSpec.shared_examples 'a container' do
other.register(key) { :item }
end
context 'without namespace argument' do
context "without namespace argument" do
subject! { container.merge(other) }
it { expect(container.resolve(key)).to be(:item) }
it { expect(container[key]).to be(:item) }
end
context 'with namespace argument' do
context "with namespace argument" do
subject! { container.merge(other, namespace: namespace) }
context 'when namespace is nil' do
context "when namespace is nil" do
let(:namespace) { nil }
it { expect(container.resolve(key)).to be(:item) }
it { expect(container[key]).to be(:item) }
end
context 'when namespace is not nil' do
let(:namespace) { 'namespace' }
context "when namespace is not nil" do
let(:namespace) { "namespace" }
it { expect(container.resolve("#{namespace}.#{key}")).to be(:item) }
it { expect(container["#{namespace}.#{key}"]).to be(:item) }
@ -298,7 +298,7 @@ RSpec.shared_examples 'a container' do
end
end
describe '#key?' do
describe "#key?" do
let(:key) { :key }
before do
@ -307,22 +307,22 @@ RSpec.shared_examples 'a container' do
subject! { container.key?(resolve_key) }
context 'when key exists in container' do
context "when key exists in container" do
let(:resolve_key) { key }
it { is_expected.to be true }
end
context 'when key does not exist in container' do
context "when key does not exist in container" do
let(:resolve_key) { :random }
it { is_expected.to be false }
end
end
describe '#keys' do
describe "#keys" do
let(:keys) { [:key_1, :key_2] }
let(:expected_keys) { ['key_1', 'key_2'] }
let(:expected_keys) { ["key_1", "key_2"] }
before do
keys.each do |key|
@ -332,14 +332,14 @@ RSpec.shared_examples 'a container' do
subject! { container.keys }
it 'returns stringified versions of all registered keys' do
it "returns stringified versions of all registered keys" do
is_expected.to match_array(expected_keys)
end
end
describe '#each_key' do
describe "#each_key" do
let(:keys) { [:key_1, :key_2] }
let(:expected_keys) { ['key_1', 'key_2'] }
let(:expected_keys) { ["key_1", "key_2"] }
let!(:yielded_keys) { [] }
before do
@ -352,18 +352,18 @@ RSpec.shared_examples 'a container' do
container.each_key { |key| yielded_keys << key }
end
it 'yields stringified versions of all registered keys to the block' do
it "yields stringified versions of all registered keys to the block" do
expect(yielded_keys).to match_array(expected_keys)
end
it 'returns the container' do
it "returns the container" do
is_expected.to eq(container)
end
end
describe '#each' do
describe "#each" do
let(:keys) { [:key_1, :key_2] }
let(:expected_key_value_pairs) { [['key_1', 'value_for_key_1'], ['key_2', 'value_for_key_2']] }
let(:expected_key_value_pairs) { [["key_1", "value_for_key_1"], ["key_2", "value_for_key_2"]] }
let!(:yielded_key_value_pairs) { [] }
before do
@ -376,33 +376,33 @@ RSpec.shared_examples 'a container' do
container.each { |key, value| yielded_key_value_pairs << [key, value] }
end
it 'yields stringified versions of all registered keys to the block' do
it "yields stringified versions of all registered keys to the block" do
expect(yielded_key_value_pairs).to match_array(expected_key_value_pairs)
end
it 'returns the container' do
it "returns the container" do
is_expected.to eq(expected_key_value_pairs)
end
end
describe '#decorate' do
require 'delegate'
describe "#decorate" do
require "delegate"
let(:key) { :key }
let(:decorated_class_spy) { spy(:decorated_class_spy) }
let(:decorated_class) { Class.new }
context 'for callable item' do
context "for callable item" do
before do
allow(decorated_class_spy).to receive(:new) { decorated_class.new }
container.register(key, memoize: memoize) { decorated_class_spy.new }
container.decorate(key, with: SimpleDelegator)
end
context 'memoize false' do
context "memoize false" do
let(:memoize) { false }
it 'does not call the block until the key is resolved' do
it "does not call the block until the key is resolved" do
expect(decorated_class_spy).not_to have_received(:new)
container.resolve(key)
expect(decorated_class_spy).to have_received(:new)
@ -416,7 +416,7 @@ RSpec.shared_examples 'a container' do
end
end
context 'memoize true' do
context "memoize true" do
let(:memoize) { true }
specify do
@ -427,23 +427,23 @@ RSpec.shared_examples 'a container' do
end
end
context 'for not callable item' do
describe 'wrapping' do
context "for not callable item" do
describe "wrapping" do
before do
container.register(key, call: false) { 'value' }
container.register(key, call: false) { "value" }
container.decorate(key, with: SimpleDelegator)
end
it 'expected to be an instance of SimpleDelegator' do
it "expected to be an instance of SimpleDelegator" do
expect(container.resolve(key)).to be_instance_of(SimpleDelegator)
expect(container.resolve(key).__getobj__.call).to eql('value')
expect(container.resolve(key).__getobj__.call).to eql("value")
end
end
describe 'memoization' do
describe "memoization" do
before do
@called = 0
container.register(key, 'value')
container.register(key, "value")
container.decorate(key) do |value|
@called += 1
@ -451,15 +451,15 @@ RSpec.shared_examples 'a container' do
end
end
it 'decorates static value only once' do
expect(container.resolve(key)).to eql('<value>')
expect(container.resolve(key)).to eql('<value>')
it "decorates static value only once" do
expect(container.resolve(key)).to eql("<value>")
expect(container.resolve(key)).to eql("<value>")
expect(@called).to be(1)
end
end
end
context 'with an instance as a decorator' do
context "with an instance as a decorator" do
let(:decorator) do
double.tap do |decorator|
allow(decorator).to receive(:call) { |input| "decorated #{input}" }
@ -467,200 +467,200 @@ RSpec.shared_examples 'a container' do
end
before do
container.register(key) { 'value' }
container.register(key) { "value" }
container.decorate(key, with: decorator)
end
it 'expected to pass original value to decorator#call method' do
expect(container.resolve(key)).to eq('decorated value')
it "expected to pass original value to decorator#call method" do
expect(container.resolve(key)).to eq("decorated value")
end
end
end
describe 'namespace' do
context 'when block does not take arguments' do
describe "namespace" do
context "when block does not take arguments" do
before do
container.namespace('one') do
register('two', 2)
container.namespace("one") do
register("two", 2)
end
end
subject! { container.resolve('one.two') }
subject! { container.resolve("one.two") }
it 'registers items under the given namespace' do
it "registers items under the given namespace" do
is_expected.to eq(2)
end
end
context 'when block takes arguments' do
context "when block takes arguments" do
before do
container.namespace('one') do |c|
c.register('two', 2)
container.namespace("one") do |c|
c.register("two", 2)
end
end
subject! { container.resolve('one.two') }
subject! { container.resolve("one.two") }
it 'registers items under the given namespace' do
it "registers items under the given namespace" do
is_expected.to eq(2)
end
end
context 'with nesting' do
context "with nesting" do
before do
container.namespace('one') do
namespace('two') do
register('three', 3)
container.namespace("one") do
namespace("two") do
register("three", 3)
end
end
end
subject! { container.resolve('one.two.three') }
subject! { container.resolve("one.two.three") }
it 'registers items under the given namespaces' do
it "registers items under the given namespaces" do
is_expected.to eq(3)
end
end
context 'with nesting and when block takes arguments' do
context "with nesting and when block takes arguments" do
before do
container.namespace('one') do |c|
c.register('two', 2)
c.register('three', c.resolve('two'))
container.namespace("one") do |c|
c.register("two", 2)
c.register("three", c.resolve("two"))
end
end
subject! { container.resolve('one.three') }
subject! { container.resolve("one.three") }
it 'resolves items relative to the namespace' do
it "resolves items relative to the namespace" do
is_expected.to eq(2)
end
end
end
describe 'import' do
it 'allows importing of namespaces' do
ns = Dry::Container::Namespace.new('one') do
register('two', 2)
describe "import" do
it "allows importing of namespaces" do
ns = Dry::Container::Namespace.new("one") do
register("two", 2)
end
container.import(ns)
expect(container.resolve('one.two')).to eq(2)
expect(container.resolve("one.two")).to eq(2)
end
it 'allows importing of nested namespaces' do
ns = Dry::Container::Namespace.new('two') do
register('three', 3)
it "allows importing of nested namespaces" do
ns = Dry::Container::Namespace.new("two") do
register("three", 3)
end
container.namespace('one') do
container.namespace("one") do
import(ns)
end
expect(container.resolve('one.two.three')).to eq(3)
expect(container.resolve("one.two.three")).to eq(3)
end
end
end
describe 'stubbing' do
describe "stubbing" do
before do
container.enable_stubs!
container.register(:item, 'item')
container.register(:foo, 'bar')
container.register(:item, "item")
container.register(:foo, "bar")
end
after do
container.unstub
end
it 'keys can be stubbed' do
container.stub(:item, 'stub')
expect(container.resolve(:item)).to eql('stub')
expect(container[:item]).to eql('stub')
it "keys can be stubbed" do
container.stub(:item, "stub")
expect(container.resolve(:item)).to eql("stub")
expect(container[:item]).to eql("stub")
end
it 'only other keys remain accesible' do
container.stub(:item, 'stub')
expect(container.resolve(:foo)).to eql('bar')
expect(container[:foo]).to eql('bar')
it "only other keys remain accesible" do
container.stub(:item, "stub")
expect(container.resolve(:foo)).to eql("bar")
expect(container[:foo]).to eql("bar")
end
it 'keys can be reverted back to their original value' do
container.stub(:item, 'stub')
it "keys can be reverted back to their original value" do
container.stub(:item, "stub")
container.unstub(:item)
expect(container.resolve(:item)).to eql('item')
expect(container[:item]).to eql('item')
expect(container.resolve(:item)).to eql("item")
expect(container[:item]).to eql("item")
end
describe 'with block argument' do
it 'executes the block with the given stubs' do
expect { |b| container.stub(:item, 'stub', &b) }.to yield_control
describe "with block argument" do
it "executes the block with the given stubs" do
expect { |b| container.stub(:item, "stub", &b) }.to yield_control
end
it 'keys are stubbed only while inside the block' do
container.stub(:item, 'stub') do
expect(container.resolve(:item)).to eql('stub')
it "keys are stubbed only while inside the block" do
container.stub(:item, "stub") do
expect(container.resolve(:item)).to eql("stub")
end
expect(container.resolve(:item)).to eql('item')
expect(container.resolve(:item)).to eql("item")
end
end
describe 'mixing Strings and Symbols' do
describe "mixing Strings and Symbols" do
it do
container.stub(:item, 'stub')
expect(container.resolve('item')).to eql('stub')
container.stub(:item, "stub")
expect(container.resolve("item")).to eql("stub")
end
end
it 'raises an error when key is missing' do
expect { container.stub(:non_existing, 'something') }
it "raises an error when key is missing" do
expect { container.stub(:non_existing, "something") }
.to raise_error(ArgumentError, 'cannot stub "non_existing" - no such key in container')
end
end
describe '.freeze' do
describe ".freeze" do
before do
container.register(:foo, 'bar')
container.register(:foo, "bar")
end
it 'allows to freeze a container so that nothing can be registered later' do
it "allows to freeze a container so that nothing can be registered later" do
container.freeze
error = RUBY_VERSION >= '2.5' ? FrozenError : RuntimeError
expect { container.register(:baz, 'quux') }.to raise_error(error)
error = RUBY_VERSION >= "2.5" ? FrozenError : RuntimeError
expect { container.register(:baz, "quux") }.to raise_error(error)
expect(container).to be_frozen
end
it 'returns self back' do
it "returns self back" do
expect(container.freeze).to be(container)
end
end
describe '.dup' do
describe ".dup" do
it "returns a copy that doesn't share registered keys with the parent" do
container.dup.register(:foo, 'bar')
container.dup.register(:foo, "bar")
expect(container.key?(:foo)).to be false
end
end
describe '.clone' do
describe ".clone" do
it "returns a copy that doesn't share registered keys with the parent" do
container.clone.register(:foo, 'bar')
container.clone.register(:foo, "bar")
expect(container.key?(:foo)).to be false
end
it 're-uses frozen container' do
it "re-uses frozen container" do
expect(container.freeze.clone).to be_frozen
expect(container.clone._container).to be(container._container)
end
end
describe '.resolve' do
it 'accepts a fallback block' do
expect(container.resolve('missing') { :fallback }).to be(:fallback)
describe ".resolve" do
it "accepts a fallback block" do
expect(container.resolve("missing") { :fallback }).to be(:fallback)
end
end
end