rubocop -A lib spec

This commit is contained in:
Piotr Solnica 2021-06-03 12:58:44 +02:00
parent d887d1479b
commit 2471df9850
No known key found for this signature in database
GPG Key ID: 9445E5CB758E470D
20 changed files with 332 additions and 330 deletions

View File

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

View File

@ -1,13 +1,13 @@
# frozen_string_literal: true
require 'concurrent/array'
require "concurrent/array"
require 'dry/configurable/constants'
require 'dry/configurable/class_methods'
require 'dry/configurable/instance_methods'
require 'dry/configurable/config'
require 'dry/configurable/setting'
require 'dry/configurable/errors'
require "dry/configurable/constants"
require "dry/configurable/class_methods"
require "dry/configurable/instance_methods"
require "dry/configurable/config"
require "dry/configurable/setting"
require "dry/configurable/errors"
module Dry
# A simple configuration mixin

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true
require 'set'
require "set"
require 'dry/configurable/constants'
require 'dry/configurable/dsl'
require 'dry/configurable/methods'
require 'dry/configurable/settings'
require "dry/configurable/constants"
require "dry/configurable/dsl"
require "dry/configurable/methods"
require "dry/configurable/settings"
module Dry
module Configurable
@ -18,7 +18,7 @@ module Dry
parent_settings = (respond_to?(:config) ? config._settings : _settings)
klass.instance_variable_set('@_settings', parent_settings)
klass.instance_variable_set("@_settings", parent_settings)
end
# Add a setting to the configuration

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'dry/configurable/setting'
require 'dry/configurable/settings'
require "dry/configurable/setting"
require "dry/configurable/settings"
module Dry
module Configurable

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true
require 'concurrent/map'
require "concurrent/map"
require 'dry/core/equalizer'
require "dry/core/equalizer"
require 'dry/configurable/constants'
require 'dry/configurable/errors'
require "dry/configurable/constants"
require "dry/configurable/errors"
module Dry
module Configurable
@ -105,7 +105,7 @@ module Dry
super unless setting
if setting.writer?(meth)
raise FrozenConfig, 'Cannot modify frozen config' if frozen?
raise FrozenConfig, "Cannot modify frozen config" if frozen?
_settings << setting.with(input: args[0])
else
@ -115,7 +115,7 @@ module Dry
# @api private
def resolve(meth)
_resolved.fetch(meth) { _resolved[meth] = meth.to_s.tr('=', '').to_sym }
_resolved.fetch(meth) { _resolved[meth] = meth.to_s.tr("=", "").to_sym }
end
# @api private

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'dry/core/constants'
require "dry/core/constants"
module Dry
# Shared constants

View File

@ -1,9 +1,9 @@
# frozen_string_literal: true
require 'dry/configurable/constants'
require 'dry/configurable/setting'
require 'dry/configurable/settings'
require 'dry/configurable/compiler'
require "dry/configurable/constants"
require "dry/configurable/setting"
require "dry/configurable/settings"
require "dry/configurable/compiler"
require "dry/core/deprecations"
module Dry

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'dry/configurable/config'
require 'dry/configurable/methods'
require "dry/configurable/config"
require "dry/configurable/methods"
module Dry
module Configurable

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'dry/configurable/errors'
require "dry/configurable/errors"
module Dry
module Configurable
@ -10,7 +10,7 @@ module Dry
module Methods
# @api public
def configure(&block)
raise FrozenConfig, 'Cannot modify frozen config' if frozen?
raise FrozenConfig, "Cannot modify frozen config" if frozen?
yield(config) if block
self

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true
require 'set'
require "set"
require 'dry/core/equalizer'
require "dry/core/equalizer"
require 'dry/configurable/constants'
require 'dry/configurable/config'
require "dry/configurable/constants"
require "dry/configurable/config"
module Dry
module Configurable

View File

@ -1,9 +1,9 @@
# frozen_string_literal: true
require 'concurrent/map'
require "concurrent/map"
require 'dry/core/equalizer'
require 'dry/configurable/constants'
require "dry/core/equalizer"
require "dry/configurable/constants"
module Dry
module Configurable

View File

@ -3,6 +3,6 @@
module Dry
module Configurable
# @api public
VERSION = '0.13.0'
VERSION = "0.13.0"
end
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'pathname'
require 'set'
require "pathname"
require "set"
RSpec.describe Dry::Configurable::Config do
subject(:config) do
@ -14,52 +14,52 @@ RSpec.describe Dry::Configurable::Config do
end
end
describe '#update' do
it 'sets new config values in a flat config' do
describe "#update" do
it "sets new config values in a flat config" do
klass.setting :db
config = klass.config.update(db: 'sqlite')
config = klass.config.update(db: "sqlite")
expect(klass.config).to be(config)
expect(config.db).to eql('sqlite')
expect(config.db).to eql("sqlite")
end
it 'sets new config values in a nested config' do
it "sets new config values in a nested config" do
klass.setting :db do
setting :user, default: 'root'
setting :pass, default: 'secret'
setting :user, default: "root"
setting :pass, default: "secret"
end
klass.config.update(db: { user: 'jane', pass: 'supersecret' })
klass.config.update(db: {user: "jane", pass: "supersecret"})
expect(klass.config.db.user).to eql('jane')
expect(klass.config.db.pass).to eql('supersecret')
expect(klass.config.db.user).to eql("jane")
expect(klass.config.db.pass).to eql("supersecret")
end
end
describe '#to_h' do
describe "#to_h" do
before do
klass.setting :db do
setting :user, default: 'root'
setting :pass, default: 'secret'
setting :user, default: "root"
setting :pass, default: "secret"
setting :ports, default: Set[123, 321]
end
end
it 'is used for equality' do
it "is used for equality" do
expect(klass.config).to eql(klass.config.dup)
end
end
describe '#dup' do
context 'with a class' do
it 'returns a deep-copy' do
describe "#dup" do
context "with a class" do
it "returns a deep-copy" do
klass = Class.new do
include Dry::Configurable
setting :db do
setting :user, default: 'root'
setting :pass, default: 'secret'
setting :user, default: "root"
setting :pass, default: "secret"
setting :ports, default: Set[123]
end
end
@ -80,11 +80,11 @@ RSpec.describe Dry::Configurable::Config do
end
end
context 'with an object' do
it 'returns a deep-copy' do
context "with an object" do
it "returns a deep-copy" do
klass.setting :db do
setting :user, default: 'root'
setting :pass, default: 'secret'
setting :user, default: "root"
setting :pass, default: "secret"
setting :ports, default: Set[123]
end
@ -107,26 +107,26 @@ RSpec.describe Dry::Configurable::Config do
end
end
describe '#[]' do
it 'coerces name from string' do
describe "#[]" do
it "coerces name from string" do
klass.setting :db, default: :sqlite
expect(klass.config['db']).to eql(:sqlite)
expect(klass.config["db"]).to eql(:sqlite)
end
it 'raises ArgumentError when name is not valid' do
it "raises ArgumentError when name is not valid" do
expect { klass.config[:hello] }.to raise_error(ArgumentError, /hello/)
end
end
describe '#method_missing' do
it 'provides access to reader methods' do
describe "#method_missing" do
it "provides access to reader methods" do
klass.setting :hello
expect { klass.config.method(:hello) }.to_not raise_error
end
it 'provides access to writer methods' do
it "provides access to writer methods" do
klass.setting :hello
expect { klass.config.method(:hello=) }.to_not raise_error

View File

@ -1,21 +1,21 @@
# frozen_string_literal: true
RSpec.describe Dry::Configurable, '.configure' do
shared_context 'configurable behavior' do
RSpec.describe Dry::Configurable, ".configure" do
shared_context "configurable behavior" do
before do
klass.setting :db
object.configure do |config|
config.db = 'postgresql'
config.db = "postgresql"
end
end
it 'sets the values' do
expect(object.config.db).to eql('postgresql')
it "sets the values" do
expect(object.config.db).to eql("postgresql")
end
end
context 'when extended' do
context "when extended" do
subject(:object) do
klass
end
@ -26,10 +26,10 @@ RSpec.describe Dry::Configurable, '.configure' do
end
end
include_context 'configurable behavior'
include_context "configurable behavior"
end
context 'when included' do
context "when included" do
subject(:object) do
klass.new
end
@ -40,10 +40,10 @@ RSpec.describe Dry::Configurable, '.configure' do
end
end
include_context 'configurable behavior'
include_context "configurable behavior"
it 'defines a constructor that sets the config' do
expect(object.config.db).to eql('postgresql')
it "defines a constructor that sets the config" do
expect(object.config.db).to eql("postgresql")
end
end
end

View File

@ -1,41 +1,41 @@
# frozen_string_literal: true
RSpec.describe Dry::Configurable, '.included' do
shared_examples 'configure' do
it 'extends ClassMethods' do
RSpec.describe Dry::Configurable, ".included" do
shared_examples "configure" do
it "extends ClassMethods" do
expect(configurable_klass.singleton_class.included_modules)
.to include(Dry::Configurable::ClassMethods)
end
it 'includes InstanceMethods' do
it "includes InstanceMethods" do
expect(configurable_klass.included_modules)
.to include(Dry::Configurable::InstanceMethods)
end
it 'raises when Dry::Configurable has already been included' do
it "raises when Dry::Configurable has already been included" do
expect {
configurable_klass.include(Dry::Configurable)
}.to raise_error(Dry::Configurable::AlreadyIncluded)
end
it 'ensures `.config` is not defined' do
it "ensures `.config` is not defined" do
expect(configurable_klass).not_to respond_to(:config)
end
it 'ensures `.configure` is not defined' do
it "ensures `.configure` is not defined" do
expect(configurable_klass).not_to respond_to(:configure)
end
it 'ensures `#config` returns instance of Dry::Configurable::Config' do
it "ensures `#config` returns instance of Dry::Configurable::Config" do
expect(configurable_klass.new.config).to be_a(Dry::Configurable::Config)
end
end
let(:configurable_klass) { Class.new.include(Dry::Configurable) }
it_behaves_like 'configure'
it_behaves_like "configure"
context 'when #initialize is defined in configurable class' do
context "when #initialize is defined in configurable class" do
let(:configurable_klass) do
Class.new do
include Dry::Configurable
@ -43,7 +43,7 @@ RSpec.describe Dry::Configurable, '.included' do
end
end
it_behaves_like 'configure'
it_behaves_like "configure"
context "required initialize parameters" do
let(:configurable_klass) do
@ -59,7 +59,7 @@ RSpec.describe Dry::Configurable, '.included' do
end
end
context 'when #finalize! is defined in configurable class' do
context "when #finalize! is defined in configurable class" do
let(:instance) do
Class.new do
include Dry::Configurable
@ -72,7 +72,7 @@ RSpec.describe Dry::Configurable, '.included' do
end.new
end
it 'calls finalize! in configurable class' do
it "calls finalize! in configurable class" do
instance.finalize!
expect(instance.finalized).to be(true)
end

View File

@ -1,69 +1,69 @@
# frozen_string_literal: true
require 'pathname'
require "pathname"
RSpec.describe Dry::Configurable, '.setting' do
shared_context 'configurable behavior' do
context 'without a default value' do
RSpec.describe Dry::Configurable, ".setting" do
shared_context "configurable behavior" do
context "without a default value" do
before do
klass.setting :db
end
it 'sets nil as the default' do
it "sets nil as the default" do
expect(object.config.db).to be(nil)
end
it 'allows configuring a setting using a writer' do
object.config.db = 'sqlite'
it "allows configuring a setting using a writer" do
object.config.db = "sqlite"
expect(object.config.db).to eql('sqlite')
expect(object.config.db).to eql("sqlite")
end
it 'allows configuring a setting using a square-bracket writer' do
object.config[:db] = 'sqlite'
it "allows configuring a setting using a square-bracket writer" do
object.config[:db] = "sqlite"
expect(object.config.db).to eql('sqlite')
expect(object.config.db).to eql("sqlite")
object.config[:db] = 'mariadb'
object.config[:db] = "mariadb"
expect(object.config.db).to eql('mariadb')
expect(object.config.db).to eql("mariadb")
end
end
it 'raises when invalid options are passed' do
it "raises when invalid options are passed" do
expect {
klass.setting :db_config, user: 'root', password: '', reader: true
klass.setting :db_config, user: "root", password: "", reader: true
}.to raise_error(
ArgumentError, 'Invalid options: [:user, :password]'
ArgumentError, "Invalid options: [:user, :password]"
)
end
it 'stores setting name as symbol' do
klass.setting 'db', default: 'sqlite'
it "stores setting name as symbol" do
klass.setting "db", default: "sqlite"
expect(object.config.values.keys).to include(:db)
end
context 'with a default value' do
context 'string' do
context "with a default value" do
context "string" do
before do
klass.setting :db, default: 'sqlite'
klass.setting :db, default: "sqlite"
end
it 'presets the default value' do
expect(object.config.db).to eql('sqlite')
it "presets the default value" do
expect(object.config.db).to eql("sqlite")
end
end
context 'hash' do
it 'returns the default value' do
klass.setting :db_config, default: { user: 'root', password: '' }
context "hash" do
it "returns the default value" do
klass.setting :db_config, default: {user: "root", password: ""}
expect(object.config.db_config).to eql(user: 'root', password: '')
expect(object.config.db_config).to eql(user: "root", password: "")
end
it 'copies the original hash object' do
hash = { user: 'root', password: '' }
it "copies the original hash object" do
hash = {user: "root", password: ""}
klass.setting :db_config, default: hash
@ -73,10 +73,10 @@ RSpec.describe Dry::Configurable, '.setting' do
end
end
context 'with nested settings' do
context "with nested settings" do
before do
klass.setting :db do
setting :type, default: 'sqlite'
setting :type, default: "sqlite"
setting :cred do
setting :user
setting :pass
@ -84,46 +84,46 @@ RSpec.describe Dry::Configurable, '.setting' do
end
end
it 'nests values in the config' do
expect(object.config.db.type).to eql('sqlite')
it "nests values in the config" do
expect(object.config.db.type).to eql("sqlite")
expect(object.config.db.cred.user).to be(nil)
expect(object.config.db.cred.pass).to be(nil)
object.config.db.cred.user = 'root'
object.config.db.cred.pass = 'secret'
object.config.db.cred.user = "root"
object.config.db.cred.pass = "secret"
expect(object.config.db.cred.user).to eql('root')
expect(object.config.db.cred.pass).to eql('secret')
expect(object.config.db.cred.user).to eql("root")
expect(object.config.db.cred.pass).to eql("secret")
end
end
context 'with a value constructor' do
it 'constructs the value with nil default' do
context "with a value constructor" do
it "constructs the value with nil default" do
klass.setting(:path, default: nil, constructor: ->(value) { "test:#{value || "fallback"}" })
expect(object.config.path).to eql("test:fallback")
object.configure do |config|
config.path = 'foo'
config.path = "foo"
end
expect(object.config.path).to eql('test:foo')
expect(object.config.path).to eql("test:foo")
end
it 'constructs the value with undefined default' do
it "constructs the value with undefined default" do
klass.setting(:path, constructor: ->(value) { "test:#{value || "fallback"}" })
expect(object.config.path).to eql('test:fallback')
expect(object.config.path).to eql("test:fallback")
end
it 'constructs the value with non-nil default' do
klass.setting(:path, default: 'test', constructor: ->(value) { Pathname(value) })
it "constructs the value with non-nil default" do
klass.setting(:path, default: "test", constructor: ->(value) { Pathname(value) })
expect(object.config.path).to eql(Pathname('test'))
expect(object.config.path).to eql(Pathname("test"))
end
it 'raises constructor errors immediately' do
klass.setting(:failable, constructor: ->(value) { value.to_sym unless value.nil? })
it "raises constructor errors immediately" do
klass.setting(:failable, constructor: ->(value) { value&.to_sym })
expect {
object.config.failable = 12
@ -131,48 +131,48 @@ RSpec.describe Dry::Configurable, '.setting' do
end
end
context 'with reader: true' do
it 'defines a reader shortcut when there is no default' do
context "with reader: true" do
it "defines a reader shortcut when there is no default" do
klass.setting :db, reader: true
expect(object.db).to be(nil)
end
it 'defines a reader shortcut when there is default' do
klass.setting :db, default: 'sqlite', reader: true
it "defines a reader shortcut when there is default" do
klass.setting :db, default: "sqlite", reader: true
expect(object.db).to eql('sqlite')
expect(object.db).to eql("sqlite")
end
end
context 'with a ruby keyword' do
context "with a ruby keyword" do
before do
klass.setting :if, true
end
it 'works' do
it "works" do
expect(object.config.if).to be(true)
end
end
context 'with :settings as a setting name' do
context "with :settings as a setting name" do
before do
klass.setting :settings, default: true
end
it 'works' do
it "works" do
expect(object.config.settings).to be(true)
end
end
it 'rejects invalid names' do
%i(foo? bar! d'oh 7 {} - ==).each do |name|
it "rejects invalid names" do
%i[foo? bar! d'oh 7 {} - ==].each do |name|
expect { klass.setting name }.to raise_error(ArgumentError, /not a valid/)
end
end
end
context 'when extended' do
context "when extended" do
subject(:object) do
klass
end
@ -183,14 +183,14 @@ RSpec.describe Dry::Configurable, '.setting' do
end
end
include_context 'configurable behavior'
include_context "configurable behavior"
context 'with a subclass' do
context "with a subclass" do
let(:subclass) do
Class.new(klass)
end
it 'maintains mutated value in a child config' do
it "maintains mutated value in a child config" do
klass.setting :db do
setting :ports, default: Set[123]
end
@ -202,21 +202,21 @@ RSpec.describe Dry::Configurable, '.setting' do
expect(subclass.config.db.ports).to eql(Set[123, 312])
end
it 'allows defining more settings' do
klass.setting :db, default: 'sqlite'
it "allows defining more settings" do
klass.setting :db, default: "sqlite"
subclass.setting :username, 'root'
subclass.setting :username, "root"
subclass.setting :password
subclass.config.password = 'secret'
subclass.config.password = "secret"
expect(subclass.config.db).to eql('sqlite')
expect(subclass.config.username).to eql('root')
expect(subclass.config.password).to eql('secret')
expect(subclass.config.db).to eql("sqlite")
expect(subclass.config.username).to eql("root")
expect(subclass.config.password).to eql("secret")
end
it 'adding parent setting does not affect child' do
klass.setting :db, default: 'sqlite'
it "adding parent setting does not affect child" do
klass.setting :db, default: "sqlite"
expect(subclass.settings).to eql(Set[:db])
@ -225,49 +225,49 @@ RSpec.describe Dry::Configurable, '.setting' do
expect(subclass.settings).to eql(Set[:db])
end
it 'configured parent copies config to the child' do
it "configured parent copies config to the child" do
klass.setting :db
object.config.db = 'mariadb'
object.config.db = "mariadb"
expect(subclass.config.db).to eql('mariadb')
expect(subclass.config.db).to eql("mariadb")
end
it 'not configured parent does not set child config' do
it "not configured parent does not set child config" do
klass.setting :db
expect(subclass.config.db).to be(nil)
end
it 'changing child does not affect parent' do
klass.setting :db, default: 'sqlite'
it "changing child does not affect parent" do
klass.setting :db, default: "sqlite"
klass.setting :nested do
setting :test, default: 'hello'
setting :test, default: "hello"
end
subclass.configure do |config|
config.db = 'postgresql'
config.nested.test = 'woah!'
config.db = "postgresql"
config.nested.test = "woah!"
end
expect(klass.settings).to eql(Set[:db, :nested])
expect(object.config.db).to eql('sqlite')
expect(object.config.db).to eql('sqlite')
expect(object.config.nested.test).to eql('hello')
expect(object.config.db).to eql("sqlite")
expect(object.config.db).to eql("sqlite")
expect(object.config.nested.test).to eql("hello")
expect(subclass.settings).to eql(Set[:db, :nested])
expect(subclass.config.db).to eql('postgresql')
expect(subclass.config.nested.test).to eql('woah!')
expect(subclass.config.db).to eql("postgresql")
expect(subclass.config.nested.test).to eql("woah!")
end
it 'inherits readers from parent' do
klass.setting :db, default: 'sqlite', reader: true
it "inherits readers from parent" do
klass.setting :db, default: "sqlite", reader: true
expect(subclass.db).to eql('sqlite')
expect(subclass.db).to eql("sqlite")
end
it 'defines a reader shortcut for nested config' do
it "defines a reader shortcut for nested config" do
klass.setting :dsn, reader: true do
setting :pool, default: 5
end
@ -277,7 +277,7 @@ RSpec.describe Dry::Configurable, '.setting' do
end
end
context 'when included' do
context "when included" do
subject(:object) do
klass.new
end
@ -288,94 +288,94 @@ RSpec.describe Dry::Configurable, '.setting' do
end
end
include_context 'configurable behavior'
include_context "configurable behavior"
it 'creates config detached from the class settings' do
klass.setting :db, default: 'sqlite'
it "creates config detached from the class settings" do
klass.setting :db, default: "sqlite"
object.config.db = 'mariadb'
object.config.db = "mariadb"
expect(object.config.db).to eql('mariadb')
expect(klass.new.config.db).to eql('sqlite')
expect(object.config.db).to eql("mariadb")
expect(klass.new.config.db).to eql("sqlite")
end
it 'exposes `config` only at the instance-level' do
it "exposes `config` only at the instance-level" do
expect(klass).to_not respond_to(:config)
end
it 'exposes `configure` only at the instance-level' do
it "exposes `configure` only at the instance-level" do
expect(klass).to_not respond_to(:configure)
end
it 'defines a constructor that sets the config' do
klass.setting :db, 'sqlite'
it "defines a constructor that sets the config" do
klass.setting :db, "sqlite"
expect(object.config.db).to eql('sqlite')
expect(object.config.db).to eql("sqlite")
end
it 'creates distinct setting values across instances' do
klass.setting(:path, 'test', constructor: ->(m) { Pathname(m) })
it "creates distinct setting values across instances" do
klass.setting(:path, "test", constructor: ->(m) { Pathname(m) })
new_object = klass.new
expect(object.config.path).to eq Pathname('test')
expect(new_object.config.path).to eq Pathname('test')
expect(object.config.path).to eq Pathname("test")
expect(new_object.config.path).to eq Pathname("test")
expect(object.config.path).not_to be(new_object.config.path)
end
shared_examples 'copying' do
shared_examples "copying" do
before do
klass.setting :env
klass.setting :db do
setting :user, 'root'
setting :pass, 'secret'
setting :user, "root"
setting :pass, "secret"
end
end
it 'can be copied' do
it "can be copied" do
clone = object.clone
expect(object.config.env).to be(nil)
expect(clone.config.env).to be(nil)
expect(object.config.db.user).to eql('root')
expect(clone.config.db.user).to eql('root')
expect(object.config.db.user).to eql("root")
expect(clone.config.db.user).to eql("root")
object.config.env = 'production'
object.config.db.user = 'jane'
object.config.env = "production"
object.config.db.user = "jane"
expect(object.config.env).to eql('production')
expect(object.config.env).to eql("production")
expect(object.config.db.user).to eql('jane')
expect(clone.config.db.user).to eql('root')
expect(object.config.db.user).to eql("jane")
expect(clone.config.db.user).to eql("root")
expect(clone.config.db.pass).to eql(object.config.db.pass)
end
end
include_examples 'copying' do
it 'stays frozen when cloning' do
include_examples "copying" do
it "stays frozen when cloning" do
expect(object.finalize!.clone).to be_frozen
end
it 'stays unfrozen when duping' do
it "stays unfrozen when duping" do
expect(object.finalize!.dup).to_not be_frozen
end
end
it 'can be configured' do
klass.setting :db, 'sqlite'
it "can be configured" do
klass.setting :db, "sqlite"
object.configure do |config|
config.db = 'mariadb'
config.db = "mariadb"
end
expect(object.config.db).to eql('mariadb')
expect(object.config.db).to eql("mariadb")
end
it 'can be finalized' do
klass.setting :db, 'sqlite'
it "can be finalized" do
klass.setting :db, "sqlite"
object.finalize!
# becomes a no-op
@ -387,7 +387,7 @@ RSpec.describe Dry::Configurable, '.setting' do
expect { object.configure {} }.to raise_error(Dry::Configurable::FrozenConfig)
end
it 'defines a reader shortcut for nested config' do
it "defines a reader shortcut for nested config" do
klass.setting :dsn, reader: true do
setting :pool, 5
end
@ -395,9 +395,9 @@ RSpec.describe Dry::Configurable, '.setting' do
expect(object.dsn.pool).to be(5)
end
context 'Test Interface' do
describe 'reset_config' do
it 'resets configuration to default values' do
context "Test Interface" do
describe "reset_config" do
it "resets configuration to default values" do
klass.setting :dsn, nil
klass.setting :pool do
@ -406,7 +406,7 @@ RSpec.describe Dry::Configurable, '.setting' do
object.enable_test_interface
object.config.dsn = 'sqlite:memory'
object.config.dsn = "sqlite:memory"
object.config.pool.size = 5
object.reset_config

View File

@ -1,22 +1,22 @@
# frozen_string_literal: true
require_relative 'support/coverage'
require 'pathname'
require_relative "support/coverage"
require "pathname"
SPEC_ROOT = Pathname(__FILE__).dirname
begin
require 'pry-byebug'
require "pry-byebug"
rescue LoadError
end
Dir[Pathname(__FILE__).dirname.join('support/**/*.rb').to_s].each do |file|
Dir[Pathname(__FILE__).dirname.join("support/**/*.rb").to_s].sort.each do |file|
require file
end
require 'dry/configurable'
require 'dry/configurable/test_interface'
require 'dry/core/deprecations'
require "dry/configurable"
require "dry/configurable/test_interface"
require "dry/core/deprecations"
RSpec.configure do |config|
config.disable_monkey_patching!

View File

@ -1,51 +1,53 @@
require 'dry/configurable/dsl'
# frozen_string_literal: true
require "dry/configurable/dsl"
RSpec.describe Dry::Configurable::DSL do
subject(:dsl) do
Dry::Configurable::DSL.new
end
it 'compiles a setting with no options' do
it "compiles a setting with no options" do
setting = dsl.setting :user
expect(setting.name).to be(:user)
expect(setting.value).to be(nil)
end
it 'compiles a setting with default' do
setting = dsl.setting :user, default: 'root'
it "compiles a setting with default" do
setting = dsl.setting :user, default: "root"
expect(setting.name).to be(:user)
expect(setting.value).to eql('root')
expect(setting.value).to eql("root")
end
it 'compiles but deprecates giving a default as positional argument' do
it "compiles but deprecates giving a default as positional argument" do
logger = StringIO.new
Dry::Core::Deprecations.set_logger!(logger)
setting = dsl.setting :user, 'root'
setting = dsl.setting :user, "root"
expect(setting.name).to be(:user)
expect(setting.value).to eql('root')
expect(setting.value).to eql("root")
logger.rewind
expect(logger.string).to match(/#{FileUtils.pwd}.*default value as positional argument to settings is deprecated/)
end
it 'compiles a setting with a reader set' do
setting = dsl.setting(:dsn, default: 'sqlite', reader: true)
it "compiles a setting with a reader set" do
setting = dsl.setting(:dsn, default: "sqlite", reader: true)
expect(setting.name).to be(:dsn)
expect(setting).to be_reader
end
it 'compiles a setting with a default string value' do
setting = dsl.setting(:dsn, default: 'sqlite')
it "compiles a setting with a default string value" do
setting = dsl.setting(:dsn, default: "sqlite")
expect(setting.name).to be(:dsn)
expect(setting.value).to eql('sqlite')
expect(setting.value).to eql("sqlite")
end
it 'compiles a setting with a default hash value' do
default = { user: 'root', pass: 'secret' }
it "compiles a setting with a default hash value" do
default = {user: "root", pass: "secret"}
setting = dsl.setting(:dsn, default: default)
@ -53,26 +55,26 @@ RSpec.describe Dry::Configurable::DSL do
expect(setting.value).to eql(default)
end
it 'compiles a setting with a constructor' do
setting = dsl.setting(:dsn, default: 'sqlite', constructor: ->(value) { "jdbc:#{value}" })
it "compiles a setting with a constructor" do
setting = dsl.setting(:dsn, default: "sqlite", constructor: ->(value) { "jdbc:#{value}" })
expect(setting.name).to be(:dsn)
expect(setting.value).to eql('jdbc:sqlite')
expect(setting.value).to eql("jdbc:sqlite")
end
it 'supports but deprecates giving a constructor as a block' do
it "supports but deprecates giving a constructor as a block" do
logger = StringIO.new
Dry::Core::Deprecations.set_logger!(logger)
setting = dsl.setting(:dsn, default: 'sqlite') { |value| "jdbc:#{value}" }
setting = dsl.setting(:dsn, default: "sqlite") { |value| "jdbc:#{value}" }
expect(setting.name).to be(:dsn)
expect(setting.value).to eql('jdbc:sqlite')
expect(setting.value).to eql("jdbc:sqlite")
logger.rewind
expect(logger.string).to match(/#{FileUtils.pwd}.*constructor as a block is deprecated/)
end
it 'compiles a nested list of settings' do
it "compiles a nested list of settings" do
setting =
dsl.setting(:db) do
setting(:cred) do

View File

@ -1,16 +1,16 @@
# frozen_string_literal: true
require 'dry/configurable/setting'
require 'dry/configurable/settings'
require "dry/configurable/setting"
require "dry/configurable/settings"
RSpec.describe Dry::Configurable::Setting::Nested do
subject(:setting) do
Dry::Configurable::Setting::Nested.new(:db, **options)
end
shared_context 'copying' do
shared_context "copying" do
let(:options) do
{ input: settings }
{input: settings}
end
let(:settings) do
@ -19,26 +19,26 @@ RSpec.describe Dry::Configurable::Setting::Nested do
)
end
it 'maintains a copy of settings' do
it "maintains a copy of settings" do
setting.value.ports << 321
expect(copy.value.ports).to eql([123, 321])
end
end
describe '#dup' do
describe "#dup" do
let(:copy) do
setting.dup
end
include_context 'copying'
include_context "copying"
end
describe '#clone' do
describe "#clone" do
let(:copy) do
setting.clone
end
include_context 'copying'
include_context "copying"
end
end

View File

@ -1,15 +1,15 @@
# frozen_string_literal: true
require 'pathname'
require 'set'
require "pathname"
require "set"
RSpec.describe Dry::Configurable::Setting do
subject(:setting) do
Dry::Configurable::Setting.new(:test, **options)
end
describe '#initialize' do
describe 'input evaluation' do
describe "#initialize" do
describe "input evaluation" do
let(:constructor) do
# Allow constructor calls to be observed
ctx = self
@ -23,30 +23,30 @@ RSpec.describe Dry::Configurable::Setting do
@constructor_called = false
end
context 'input defined' do
context "input defined" do
let(:options) do
{input: 1, constructor: constructor}
end
it 'evaluates the input' do
it "evaluates the input" do
expect(setting).to be_evaluated
end
it 'evaluates the input through the constructor' do
it "evaluates the input through the constructor" do
expect { setting }.to change { @constructor_called }.from(false).to true
end
end
context 'input not defined' do
context "input not defined" do
let(:options) do
{constructor: constructor}
end
it 'does not evaluates the input' do
it "does not evaluates the input" do
expect(setting).not_to be_evaluated
end
it 'does not call the constructor' do
it "does not call the constructor" do
expect { setting }.not_to change { @constructor_called }
expect(@constructor_called).to be false
end
@ -54,91 +54,91 @@ RSpec.describe Dry::Configurable::Setting do
end
end
describe '#input_defined?' do
context 'no input provided' do
describe "#input_defined?" do
context "no input provided" do
let(:options) do
{}
end
it 'is false' do
it "is false" do
expect(setting.input_defined?).to be false
end
end
context 'input provided' do
context "input provided" do
let(:options) do
{input: 1}
end
it 'is true' do
it "is true" do
expect(setting.input_defined?).to be true
end
end
context 'default provided' do
context "default provided" do
let(:options) do
{default: 1}
end
it 'is false' do
it "is false" do
expect(setting.input_defined?).to be false
end
end
end
describe '#value' do
context 'with no default' do
describe "#value" do
context "with no default" do
let(:options) do
{}
end
it 'returns nil' do
it "returns nil" do
expect(setting.value).to be(nil)
end
end
context 'with no default and a constructor' do
context "with no default and a constructor" do
let(:options) do
{ constructor: -> value { value + 1 } }
{constructor: -> value { value + 1 }}
end
it 'returns constructed value' do
it "returns constructed value" do
expect(setting.with(input: 1).value).to eql(2)
end
end
context 'with a default value and a constructor' do
context "with a default value and a constructor" do
let(:options) do
{ default: 'hello', constructor: -> value { value.to_sym } }
{default: "hello", constructor: -> value { value.to_sym }}
end
it 'returns default processed by the constructor' do
it "returns default processed by the constructor" do
expect(setting.value).to eql(:hello)
end
end
end
describe '#evaluated?' do
context 'input defined' do
describe "#evaluated?" do
context "input defined" do
let(:options) do
{input: 2}
end
it 'is true' do
it "is true" do
expect(setting).to be_evaluated
end
end
context 'input not defined' do
context "input not defined" do
let(:options) do
{}
end
it 'is false' do
it "is false" do
expect(setting).not_to be_evaluated
end
it 'becomes true after accessing the value' do
it "becomes true after accessing the value" do
expect { setting.value }
.to change { setting.evaluated? }
.from(false)
@ -147,119 +147,119 @@ RSpec.describe Dry::Configurable::Setting do
end
end
context '#with' do
context "#with" do
let(:options) do
{}
end
it 'returns a new instance with a new input' do
expect(setting.with(input: 'hello').value).to eql('hello')
it "returns a new instance with a new input" do
expect(setting.with(input: "hello").value).to eql("hello")
end
it 'returns a new instance with a new default' do
expect(setting.with(default: 'hello').value).to eql('hello')
it "returns a new instance with a new default" do
expect(setting.with(default: "hello").value).to eql("hello")
end
it 'returns a new instance with a preserved value' do
hello = setting.with(input: ['hello'])
it "returns a new instance with a preserved value" do
hello = setting.with(input: ["hello"])
hello.value << 'world'
hello.value << "world"
expect(hello.with(reader: true).value).to eql(['hello', 'world'])
expect(hello.with(reader: true).value).to eql(%w[hello world])
end
end
shared_context 'copying' do
context 'input defined' do
shared_context "copying" do
context "input defined" do
let(:options) do
{ input: 'hello' }
{input: "hello"}
end
before do
setting.value
end
it 'maintains the name' do
it "maintains the name" do
expect(copy.name).to be(setting.name)
end
it 'maintains a copy of the options' do
it "maintains a copy of the options" do
expect(copy.options).to eql(setting.options)
expect(copy.options).to_not be(setting.options)
end
context 'with a naturally cloneable value' do
context "with a naturally cloneable value" do
let(:options) do
{ input: [1, 2, 3] }
{input: [1, 2, 3]}
end
it 'maintains a copy of the value' do
it "maintains a copy of the value" do
expect(copy.value).to eql(setting.value)
expect(copy.value).to_not be(setting.value)
end
end
context 'with cloneable option as true' do
context "with cloneable option as true" do
let(:options) do
{ input: "hello", cloneable: true }
{input: "hello", cloneable: true}
end
it 'maintains a copy of the value' do
it "maintains a copy of the value" do
expect(copy.value).to eql(setting.value)
expect(copy.value).to_not be(setting.value)
end
end
context 'with cloneable option as false' do
context "with cloneable option as false" do
let(:options) do
{ input: [1, 2, 3], cloneable: false }
{input: [1, 2, 3], cloneable: false}
end
it 'maintains the original value' do
it "maintains the original value" do
expect(copy.value).to be(setting.value)
end
end
context 'with a naturally non-cloneable value' do
context "with a naturally non-cloneable value" do
let(:options) do
{ input: :hello }
{input: :hello}
end
it 'maintains the original value' do
it "maintains the original value" do
expect(copy.value).to be(setting.value)
end
end
end
context 'input undefined' do
context "input undefined" do
let(:options) do
# This constructor would crash if setting was evaluated when copied
{constructor: -> value { value + 1 } }
{constructor: -> value { value + 1 }}
end
it 'leaves input undefined' do
it "leaves input undefined" do
expect(copy.input_defined?).to be false
end
it 'does not evaluate the setting' do
it "does not evaluate the setting" do
expect(copy).not_to be_evaluated
end
end
end
describe '#dup' do
describe "#dup" do
let(:copy) do
setting.dup
end
include_context 'copying'
include_context "copying"
end
describe '#clone' do
describe "#clone" do
let(:copy) do
setting.clone
end
include_context 'copying'
include_context "copying"
end
end