rubocop -a --only Style/StringLiterals

This commit is contained in:
Piotr Solnica 2020-12-12 07:43:25 +01:00
parent 1782094b94
commit 78f36e8f17
No known key found for this signature in database
GPG Key ID: 66BF2FDA7BA0F29C
33 changed files with 343 additions and 343 deletions

42
Gemfile
View File

@ -1,36 +1,36 @@
source 'https://rubygems.org'
source "https://rubygems.org"
eval_gemfile 'Gemfile.devtools'
eval_gemfile "Gemfile.devtools"
gemspec
if ENV['DRY_TYPES_FROM_MASTER'].eql?('true')
gem 'dry-types', github: 'dry-rb/dry-types', branch: 'master'
if ENV["DRY_TYPES_FROM_MASTER"].eql?("true")
gem "dry-types", github: "dry-rb/dry-types", branch: "master"
else
gem 'dry-types'
gem "dry-types"
end
group :benchmarks do
if RUBY_VERSION < '2.4'
gem 'activesupport', '< 5'
if RUBY_VERSION < "2.4"
gem "activesupport", "< 5"
else
gem 'activesupport'
gem "activesupport"
end
gem 'active_attr'
gem 'anima'
gem 'attr_extras'
gem 'benchmark-ips', '~> 2.5'
gem 'concord'
gem 'fast_attributes'
gem 'kwattr'
gem 'ruby-prof', platform: :mri
gem 'value_struct'
gem 'values'
gem 'virtus'
gem "active_attr"
gem "anima"
gem "attr_extras"
gem "benchmark-ips", "~> 2.5"
gem "concord"
gem "fast_attributes"
gem "kwattr"
gem "ruby-prof", platform: :mri
gem "value_struct"
gem "values"
gem "virtus"
end
group :development, :test do
gem 'pry', platform: :mri
gem 'pry-byebug', platform: :mri
gem "pry", platform: :mri
gem "pry-byebug", platform: :mri
end

View File

@ -1,8 +1,8 @@
require 'bundler/setup'
require "bundler/setup"
Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new :default
load 'lib/tasks/benchmark.rake'
load 'lib/tasks/profile.rake'
load "lib/tasks/benchmark.rake"
load "lib/tasks/profile.rake"

View File

@ -1,6 +1,6 @@
Bundler.require(:benchmarks)
require 'dry-initializer'
require "dry-initializer"
class WithoutDefaults
extend Dry::Initializer
@ -14,67 +14,67 @@ class WithOneDefault
param :foo
param :bar
param :baz, default: proc { 'BAZ' }
param :baz, default: proc { "BAZ" }
end
class WithTwoDefaults
extend Dry::Initializer
param :foo
param :bar, default: proc { 'BAR' }
param :baz, default: proc { 'BAZ' }
param :bar, default: proc { "BAR" }
param :baz, default: proc { "BAZ" }
end
class WithThreeDefaults
extend Dry::Initializer
param :foo, default: proc { 'FOO' }
param :bar, default: proc { 'BAR' }
param :baz, default: proc { 'BAZ' }
param :foo, default: proc { "FOO" }
param :bar, default: proc { "BAR" }
param :baz, default: proc { "BAZ" }
end
puts 'Benchmark for various options'
puts "Benchmark for various options"
Benchmark.ips do |x|
x.config time: 15, warmup: 10
x.report('without defaults') do
WithoutDefaults.new 'FOO', 'BAR', 'BAZ'
x.report("without defaults") do
WithoutDefaults.new "FOO", "BAR", "BAZ"
end
x.report('with 0 of 1 default used') do
WithOneDefault.new 'FOO', 'BAR', 'BAZ'
x.report("with 0 of 1 default used") do
WithOneDefault.new "FOO", "BAR", "BAZ"
end
x.report('with 1 of 1 default used') do
WithOneDefault.new 'FOO', 'BAR'
x.report("with 1 of 1 default used") do
WithOneDefault.new "FOO", "BAR"
end
x.report('with 0 of 2 defaults used') do
WithTwoDefaults.new 'FOO', 'BAR', 'BAZ'
x.report("with 0 of 2 defaults used") do
WithTwoDefaults.new "FOO", "BAR", "BAZ"
end
x.report('with 1 of 2 defaults used') do
WithTwoDefaults.new 'FOO', 'BAR'
x.report("with 1 of 2 defaults used") do
WithTwoDefaults.new "FOO", "BAR"
end
x.report('with 2 of 2 defaults used') do
WithTwoDefaults.new 'FOO'
x.report("with 2 of 2 defaults used") do
WithTwoDefaults.new "FOO"
end
x.report('with 0 of 3 defaults used') do
WithThreeDefaults.new 'FOO', 'BAR', 'BAZ'
x.report("with 0 of 3 defaults used") do
WithThreeDefaults.new "FOO", "BAR", "BAZ"
end
x.report('with 1 of 3 defaults used') do
WithThreeDefaults.new 'FOO', 'BAR'
x.report("with 1 of 3 defaults used") do
WithThreeDefaults.new "FOO", "BAR"
end
x.report('with 2 of 3 defaults used') do
WithThreeDefaults.new 'FOO'
x.report("with 2 of 3 defaults used") do
WithThreeDefaults.new "FOO"
end
x.report('with 3 of 3 defaults used') do
x.report("with 3 of 3 defaults used") do
WithThreeDefaults.new
end

View File

@ -1,6 +1,6 @@
Bundler.require(:benchmarks)
require 'dry-initializer'
require "dry-initializer"
class DryTest
extend Dry::Initializer[undefined: false]
@ -24,39 +24,39 @@ class PlainRubyTest
end
end
require 'anima'
require "anima"
class AnimaTest
include Anima.new(:foo, :bar)
end
require 'kwattr'
require "kwattr"
class KwattrTest
kwattr :foo, :bar
end
puts 'Benchmark for instantiation with plain options'
puts "Benchmark for instantiation with plain options"
Benchmark.ips do |x|
x.config time: 15, warmup: 10
x.report('plain Ruby') do
PlainRubyTest.new foo: 'FOO', bar: 'BAR'
x.report("plain Ruby") do
PlainRubyTest.new foo: "FOO", bar: "BAR"
end
x.report('dry-initializer') do
DryTest.new foo: 'FOO', bar: 'BAR'
x.report("dry-initializer") do
DryTest.new foo: "FOO", bar: "BAR"
end
x.report('dry-initializer (with UNDEFINED)') do
DryTestUndefined.new foo: 'FOO', bar: 'BAR'
x.report("dry-initializer (with UNDEFINED)") do
DryTestUndefined.new foo: "FOO", bar: "BAR"
end
x.report('anima') do
AnimaTest.new foo: 'FOO', bar: 'BAR'
x.report("anima") do
AnimaTest.new foo: "FOO", bar: "BAR"
end
x.report('kwattr') do
KwattrTest.new foo: 'FOO', bar: 'BAR'
x.report("kwattr") do
KwattrTest.new foo: "FOO", bar: "BAR"
end
x.compare!

View File

@ -1,6 +1,6 @@
Bundler.require(:benchmarks)
require 'dry-initializer'
require "dry-initializer"
class DryTest
extend Dry::Initializer[undefined: false]
@ -26,58 +26,58 @@ end
StructTest = Struct.new(:foo, :bar)
require 'concord'
require "concord"
class ConcordTest
include Concord.new(:foo, :bar)
end
require 'values'
require "values"
ValueTest = Value.new(:foo, :bar)
require 'value_struct'
require "value_struct"
ValueStructTest = ValueStruct.new(:foo, :bar)
require 'attr_extras'
require "attr_extras"
class AttrExtrasText
attr_initialize :foo, :bar
attr_reader :foo, :bar
end
puts 'Benchmark for instantiation with plain params'
puts "Benchmark for instantiation with plain params"
Benchmark.ips do |x|
x.config time: 15, warmup: 10
x.report('plain Ruby') do
PlainRubyTest.new 'FOO', 'BAR'
x.report("plain Ruby") do
PlainRubyTest.new "FOO", "BAR"
end
x.report('Core Struct') do
StructTest.new 'FOO', 'BAR'
x.report("Core Struct") do
StructTest.new "FOO", "BAR"
end
x.report('values') do
ValueTest.new 'FOO', 'BAR'
x.report("values") do
ValueTest.new "FOO", "BAR"
end
x.report('value_struct') do
ValueStructTest.new 'FOO', 'BAR'
x.report("value_struct") do
ValueStructTest.new "FOO", "BAR"
end
x.report('dry-initializer') do
DryTest.new 'FOO', 'BAR'
x.report("dry-initializer") do
DryTest.new "FOO", "BAR"
end
x.report('dry-initializer (with UNDEFINED)') do
DryTestUndefined.new 'FOO', 'BAR'
x.report("dry-initializer (with UNDEFINED)") do
DryTestUndefined.new "FOO", "BAR"
end
x.report('concord') do
ConcordTest.new 'FOO', 'BAR'
x.report("concord") do
ConcordTest.new "FOO", "BAR"
end
x.report('attr_extras') do
AttrExtrasText.new 'FOO', 'BAR'
x.report("attr_extras") do
AttrExtrasText.new "FOO", "BAR"
end
x.compare!

View File

@ -1,6 +1,6 @@
Bundler.require(:benchmarks)
require 'dry-initializer'
require "dry-initializer"
class DryTest
extend Dry::Initializer[undefined: false]
@ -24,7 +24,7 @@ class PlainRubyTest
end
end
require 'virtus'
require "virtus"
class VirtusTest
include Virtus.model
@ -32,7 +32,7 @@ class VirtusTest
attribute :bar, String
end
require 'fast_attributes'
require "fast_attributes"
class FastAttributesTest
extend FastAttributes
@ -42,29 +42,29 @@ class FastAttributesTest
end
end
puts 'Benchmark for instantiation with coercion'
puts "Benchmark for instantiation with coercion"
Benchmark.ips do |x|
x.config time: 15, warmup: 10
x.report('plain Ruby') do
PlainRubyTest.new foo: 'FOO', bar: 'BAR'
x.report("plain Ruby") do
PlainRubyTest.new foo: "FOO", bar: "BAR"
end
x.report('dry-initializer') do
DryTest.new foo: 'FOO', bar: 'BAR'
x.report("dry-initializer") do
DryTest.new foo: "FOO", bar: "BAR"
end
x.report('dry-initializer (with UNDEFINED)') do
DryTestUndefined.new foo: 'FOO', bar: 'BAR'
x.report("dry-initializer (with UNDEFINED)") do
DryTestUndefined.new foo: "FOO", bar: "BAR"
end
x.report('virtus') do
VirtusTest.new foo: 'FOO', bar: 'BAR'
x.report("virtus") do
VirtusTest.new foo: "FOO", bar: "BAR"
end
x.report('fast_attributes') do
FastAttributesTest.new foo: 'FOO', bar: 'BAR'
x.report("fast_attributes") do
FastAttributesTest.new foo: "FOO", bar: "BAR"
end
x.compare!

View File

@ -1,64 +1,64 @@
Bundler.require(:benchmarks)
require 'dry-initializer'
require "dry-initializer"
class DryTest
extend Dry::Initializer[undefined: false]
option :foo, default: -> { 'FOO' }
option :bar, default: -> { 'BAR' }
option :foo, default: -> { "FOO" }
option :bar, default: -> { "BAR" }
end
class DryTestUndefined
extend Dry::Initializer
option :foo, default: -> { 'FOO' }
option :bar, default: -> { 'BAR' }
option :foo, default: -> { "FOO" }
option :bar, default: -> { "BAR" }
end
class PlainRubyTest
attr_reader :foo, :bar
def initialize(foo: 'FOO', bar: 'BAR')
def initialize(foo: "FOO", bar: "BAR")
@foo = foo
@bar = bar
end
end
require 'kwattr'
require "kwattr"
class KwattrTest
kwattr foo: 'FOO', bar: 'BAR'
kwattr foo: "FOO", bar: "BAR"
end
require 'active_attr'
require "active_attr"
class ActiveAttrTest
include ActiveAttr::AttributeDefaults
attribute :foo, default: 'FOO'
attribute :bar, default: 'BAR'
attribute :foo, default: "FOO"
attribute :bar, default: "BAR"
end
puts 'Benchmark for instantiation with default values'
puts "Benchmark for instantiation with default values"
Benchmark.ips do |x|
x.config time: 15, warmup: 10
x.report('plain Ruby') do
x.report("plain Ruby") do
PlainRubyTest.new
end
x.report('dry-initializer') do
x.report("dry-initializer") do
DryTest.new
end
x.report('dry-initializer (with UNDEFINED)') do
x.report("dry-initializer (with UNDEFINED)") do
DryTestUndefined.new
end
x.report('kwattr') do
x.report("kwattr") do
KwattrTest.new
end
x.report('active_attr') do
x.report("active_attr") do
ActiveAttrTest.new
end

View File

@ -1,24 +1,24 @@
Bundler.require(:benchmarks)
require 'dry-initializer'
require "dry-initializer"
class DryTest
extend Dry::Initializer[undefined: false]
option :foo, proc(&:to_s), default: -> { 'FOO' }
option :bar, proc(&:to_s), default: -> { 'BAR' }
option :foo, proc(&:to_s), default: -> { "FOO" }
option :bar, proc(&:to_s), default: -> { "BAR" }
end
class DryTestUndefined
extend Dry::Initializer
option :foo, proc(&:to_s), default: -> { 'FOO' }
option :bar, proc(&:to_s), default: -> { 'BAR' }
option :foo, proc(&:to_s), default: -> { "FOO" }
option :bar, proc(&:to_s), default: -> { "BAR" }
end
class PlainRubyTest
attr_reader :foo, :bar
def initialize(foo: 'FOO', bar: 'BAR')
def initialize(foo: "FOO", bar: "BAR")
@foo = foo
@bar = bar
raise TypeError unless String === @foo
@ -26,32 +26,32 @@ class PlainRubyTest
end
end
require 'virtus'
require "virtus"
class VirtusTest
include Virtus.model
attribute :foo, String, default: 'FOO'
attribute :bar, String, default: 'BAR'
attribute :foo, String, default: "FOO"
attribute :bar, String, default: "BAR"
end
puts 'Benchmark for instantiation with type constraints and default values'
puts "Benchmark for instantiation with type constraints and default values"
Benchmark.ips do |x|
x.config time: 15, warmup: 10
x.report('plain Ruby') do
x.report("plain Ruby") do
PlainRubyTest.new
end
x.report('dry-initializer') do
x.report("dry-initializer") do
DryTest.new
end
x.report('dry-initializer (with UNDEFINED)') do
x.report("dry-initializer (with UNDEFINED)") do
DryTest.new
end
x.report('virtus') do
x.report("virtus") do
VirtusTest.new
end

View File

@ -1 +1 @@
require_relative 'dry/initializer'
require_relative "dry/initializer"

View File

@ -1,4 +1,4 @@
require 'set'
require "set"
# Namespace for gems in a dry-rb community
module Dry
@ -6,13 +6,13 @@ module Dry
# DSL for declaring params and options of class initializers
#
module Initializer
require_relative 'initializer/undefined'
require_relative 'initializer/dsl'
require_relative 'initializer/definition'
require_relative 'initializer/builders'
require_relative 'initializer/config'
require_relative 'initializer/mixin'
require_relative 'initializer/dispatchers'
require_relative "initializer/undefined"
require_relative "initializer/dsl"
require_relative "initializer/definition"
require_relative "initializer/builders"
require_relative "initializer/config"
require_relative "initializer/mixin"
require_relative "initializer/dispatchers"
# Adds methods [.[]] and [.define]
extend DSL
@ -56,6 +56,6 @@ module Dry
dry_initializer.children << config
end
require_relative 'initializer/struct'
require_relative "initializer/struct"
end
end

View File

@ -1,36 +1,36 @@
namespace :benchmark do
desc 'Runs benchmarks for plain params'
desc "Runs benchmarks for plain params"
task :plain_params do
system 'ruby benchmarks/plain_params.rb'
system "ruby benchmarks/plain_params.rb"
end
desc 'Runs benchmarks for plain options'
desc "Runs benchmarks for plain options"
task :plain_options do
system 'ruby benchmarks/plain_options.rb'
system "ruby benchmarks/plain_options.rb"
end
desc 'Runs benchmarks for value coercion'
desc "Runs benchmarks for value coercion"
task :with_coercion do
system 'ruby benchmarks/with_coercion.rb'
system "ruby benchmarks/with_coercion.rb"
end
desc 'Runs benchmarks with defaults'
desc "Runs benchmarks with defaults"
task :with_defaults do
system 'ruby benchmarks/with_defaults.rb'
system "ruby benchmarks/with_defaults.rb"
end
desc 'Runs benchmarks with defaults and coercion'
desc "Runs benchmarks with defaults and coercion"
task :with_defaults_and_coercion do
system 'ruby benchmarks/with_defaults_and_coercion.rb'
system "ruby benchmarks/with_defaults_and_coercion.rb"
end
desc 'Runs benchmarks for several defaults'
desc "Runs benchmarks for several defaults"
task :compare_several_defaults do
system 'ruby benchmarks/with_several_defaults.rb'
system "ruby benchmarks/with_several_defaults.rb"
end
end
desc 'Runs all benchmarks'
desc "Runs all benchmarks"
task benchmark: %i[
benchmark:plain_params
benchmark:plain_options

View File

@ -1,32 +1,32 @@
namespace :profile do
def profile(name, execution, &definition)
require 'dry-initializer'
require 'ruby-prof'
require 'fileutils'
require "dry-initializer"
require "ruby-prof"
require "fileutils"
definition.call
result = RubyProf.profile do
1_000.times { execution.call }
end
FileUtils.mkdir_p './tmp'
FileUtils.mkdir_p "./tmp"
FileUtils.touch "./tmp/#{name}.dot"
File.open("./tmp/#{name}.dot", 'w+') do |output|
File.open("./tmp/#{name}.dot", "w+") do |output|
RubyProf::DotPrinter.new(result).print(output, min_percent: 0)
end
FileUtils.touch "./tmp/#{name}.html"
File.open("./tmp/#{name}.html", 'w+') do |output|
File.open("./tmp/#{name}.html", "w+") do |output|
RubyProf::CallStackPrinter.new(result).print(output, min_percent: 0)
end
system "dot -Tpng ./tmp/#{name}.dot > ./tmp/#{name}.png"
end
desc 'Profiles initialization with required param and option'
desc "Profiles initialization with required param and option"
task :required do
profile('required', -> { User.new :Andy, email: 'andy@example.com' }) do
profile("required", -> { User.new :Andy, email: "andy@example.com" }) do
class User
extend Dry::Initializer
param :name
@ -35,20 +35,20 @@ namespace :profile do
end
end
desc 'Profiles initialization with default param and option'
desc "Profiles initialization with default param and option"
task :defaults do
profile('defaults', -> { User.new }) do
profile("defaults", -> { User.new }) do
class User
extend Dry::Initializer
param :name, default: -> { :Andy }
option :email, default: -> { 'andy@example.com' }
option :email, default: -> { "andy@example.com" }
end
end
end
desc 'Profiles initialization with coerced param and option'
desc "Profiles initialization with coerced param and option"
task :coercion do
profile('coercion', -> { User.new :Andy, email: :"andy@example.com" }) do
profile("coercion", -> { User.new :Andy, email: :"andy@example.com" }) do
class User
extend Dry::Initializer
param :name, proc(&:to_s)
@ -57,9 +57,9 @@ namespace :profile do
end
end
desc 'Profiles initialization with coerced defaults of param and option'
desc "Profiles initialization with coerced defaults of param and option"
task :default_coercion do
profile('default_coercion', -> { User.new }) do
profile("default_coercion", -> { User.new }) do
class User
extend Dry::Initializer
param :name, proc(&:to_s), default: -> { :Andy }
@ -69,7 +69,7 @@ namespace :profile do
end
end
desc 'Makes all profiling at once'
desc "Makes all profiling at once"
task profile: %i[
profile:required
profile:defaults

View File

@ -1,7 +1,7 @@
describe Dry::Initializer, 'dry_initializer.attributes' do
describe Dry::Initializer, "dry_initializer.attributes" do
subject { instance.class.dry_initializer.attributes(instance) }
context 'when class has params' do
context "when class has params" do
before do
class Test::Foo
extend Dry::Initializer
@ -13,12 +13,12 @@ describe Dry::Initializer, 'dry_initializer.attributes' do
let(:instance) { Test::Foo.new(:FOO) }
it 'collects coerced params with default values' do
expect(subject).to eq({ foo: 'FOO', bar: 1 })
it "collects coerced params with default values" do
expect(subject).to eq({ foo: "FOO", bar: 1 })
end
end
context 'when class has options' do
context "when class has options" do
before do
class Test::Foo
extend Dry::Initializer
@ -31,8 +31,8 @@ describe Dry::Initializer, 'dry_initializer.attributes' do
let(:instance) { Test::Foo.new(foo: :FOO, qux: :QUX) }
it 'collects coerced and renamed options with default values' do
expect(subject).to eq({ foo: :FOO, bar: 1, quxx: 'QUX' })
it "collects coerced and renamed options with default values" do
expect(subject).to eq({ foo: :FOO, bar: 1, quxx: "QUX" })
end
end
end

View File

@ -1,4 +1,4 @@
describe 'coercion of nil' do
describe "coercion of nil" do
before do
class Test::Foo
extend Dry::Initializer
@ -15,11 +15,11 @@ describe 'coercion of nil' do
let(:foo) { Test::Foo.new(nil) }
let(:baz) { Test::Baz.new(nil) }
it 'works with extend syntax' do
it "works with extend syntax" do
expect(foo.bar).to eq 0
end
it 'works with include syntax' do
it "works with include syntax" do
expect(baz.qux).to eq 0
end
end

View File

@ -1,12 +1,12 @@
describe 'custom dispatchers' do
subject { Test::Foo.new '123' }
describe "custom dispatchers" do
subject { Test::Foo.new "123" }
before do
dispatcher = ->(op) { op[:integer] ? op.merge(type: proc(&:to_i)) : op }
Dry::Initializer::Dispatchers << dispatcher
end
context 'with extend syntax' do
context "with extend syntax" do
before do
class Test::Foo
extend Dry::Initializer
@ -14,12 +14,12 @@ describe 'custom dispatchers' do
end
end
it 'adds syntax sugar' do
it "adds syntax sugar" do
expect(subject.id).to eq 123
end
end
context 'with include syntax' do
context "with include syntax" do
before do
class Test::Foo
include Dry::Initializer.define -> do
@ -28,7 +28,7 @@ describe 'custom dispatchers' do
end
end
it 'adds syntax sugar' do
it "adds syntax sugar" do
expect(subject.id).to eq 123
end
end

View File

@ -1,4 +1,4 @@
describe 'custom initializer' do
describe "custom initializer" do
before do
class Test::Foo
extend Dry::Initializer
@ -21,7 +21,7 @@ describe 'custom initializer' do
end
end
it 'reloads the initializer' do
it "reloads the initializer" do
baz = Test::Baz.new(5, 5)
expect(baz.bar).to eq 15 # 5 * 3

View File

@ -1,4 +1,4 @@
describe 'default values' do
describe "default values" do
before do
class Test::Foo
extend Dry::Initializer
@ -17,7 +17,7 @@ describe 'default values' do
end
end
it 'instantiate arguments' do
it "instantiate arguments" do
subject = Test::Foo.new(1, 2, baz: 3, qux: 4)
expect(subject.foo).to eql 1
@ -26,7 +26,7 @@ describe 'default values' do
expect(subject.qux).to eql 4
end
it 'applies default values' do
it "applies default values" do
subject = Test::Foo.new
expect(subject.foo).to eql :FOO
@ -35,7 +35,7 @@ describe 'default values' do
expect(subject.qux).to eql :FOO
end
it 'applies default values partially' do
it "applies default values partially" do
subject = Test::Foo.new 1, baz: 3
expect(subject.foo).to eql 1
@ -44,12 +44,12 @@ describe 'default values' do
expect(subject.qux).to eql 1
end
it 'applies default values from private methods' do
it "applies default values from private methods" do
subject = Test::Foo.new
expect(subject.mox).to eql :MOX
end
describe 'when the last param has a default and there are no options' do
describe "when the last param has a default and there are no options" do
before do
class Test::Bar
extend Dry::Initializer
@ -59,21 +59,21 @@ describe 'default values' do
end
end
it 'instantiates arguments' do
it "instantiates arguments" do
subject = Test::Bar.new(1, 2)
expect(subject.foo).to eql 1
expect(subject.bar).to eql 2
end
it 'applies default values' do
it "applies default values" do
subject = Test::Bar.new(1)
expect(subject.foo).to eql 1
expect(subject.bar).to eql({})
end
it 'instantiates arguments also if the last is an hash' do
it "instantiates arguments also if the last is an hash" do
subject = Test::Bar.new(1, { baz: 2, qux: 3 })
expect(subject.foo).to eql 1

View File

@ -1,4 +1,4 @@
describe 'definition' do
describe "definition" do
shared_examples :initializer do |in_context|
subject { Test::Foo.new(1, bar: 2) }
@ -8,7 +8,7 @@ describe 'definition' do
end
end
it_behaves_like :initializer, 'extend Dry::Initializer' do
it_behaves_like :initializer, "extend Dry::Initializer" do
before do
class Test::Foo
extend Dry::Initializer
@ -17,7 +17,7 @@ describe 'definition' do
end
end
it 'preservers definition params' do
it "preservers definition params" do
params = Test::Foo.dry_initializer.params.map do |definition|
[definition.source, definition.options]
end
@ -27,7 +27,7 @@ describe 'definition' do
]
end
it 'preservers definition options' do
it "preservers definition options" do
options = Test::Foo.dry_initializer.options.map do |definition|
[definition.source, definition.options]
end
@ -38,7 +38,7 @@ describe 'definition' do
end
end
it_behaves_like :initializer, 'extend Dry::Initializer' do
it_behaves_like :initializer, "extend Dry::Initializer" do
before do
class Test::Foo
extend Dry::Initializer
@ -48,7 +48,7 @@ describe 'definition' do
end
end
it_behaves_like :initializer, 'extend Dry::Initializer[undefined: false]' do
it_behaves_like :initializer, "extend Dry::Initializer[undefined: false]" do
before do
class Test::Foo
extend Dry::Initializer[undefined: false]
@ -58,7 +58,7 @@ describe 'definition' do
end
end
it_behaves_like :initializer, 'include Dry::Initializer with block' do
it_behaves_like :initializer, "include Dry::Initializer with block" do
before do
class Test::Foo
include(
@ -71,7 +71,7 @@ describe 'definition' do
end
end
it_behaves_like :initializer, 'include Dry::Initializer with lambda' do
it_behaves_like :initializer, "include Dry::Initializer with lambda" do
before do
class Test::Foo
include Dry::Initializer.define -> do
@ -82,7 +82,7 @@ describe 'definition' do
end
end
it_behaves_like :initializer, 'include Dry::Initializer[undefined: false]' do
it_behaves_like :initializer, "include Dry::Initializer[undefined: false]" do
before do
class Test::Foo
include(
@ -96,7 +96,7 @@ describe 'definition' do
end
# @deprecated
it_behaves_like :initializer, 'include Dry::Initializer::Mixin' do
it_behaves_like :initializer, "include Dry::Initializer::Mixin" do
before do
class Test::Foo
include(

View File

@ -1,4 +1,4 @@
describe 'invalid default value assignment' do
describe "invalid default value assignment" do
subject do
class Test::Foo
extend Dry::Initializer
@ -7,7 +7,7 @@ describe 'invalid default value assignment' do
end
end
it 'raises TypeError' do
it "raises TypeError" do
expect { subject }.to raise_error TypeError
end
end

View File

@ -1,29 +1,29 @@
require 'dry-types'
require "dry-types"
describe 'list type argument' do
describe "list type argument" do
before do
class Test::Foo
extend Dry::Initializer
param :foo, [proc(&:to_s)]
option :bar, [Dry::Types['strict.string']]
option :bar, [Dry::Types["strict.string"]]
option :baz, []
end
end
context 'with single items' do
subject { Test::Foo.new(1, bar: '2', baz: { qux: :QUX }) }
context "with single items" do
subject { Test::Foo.new(1, bar: "2", baz: { qux: :QUX }) }
it 'coerces and wraps them to arrays' do
it "coerces and wraps them to arrays" do
expect(subject.foo).to eq %w[1]
expect(subject.bar).to eq %w[2]
expect(subject.baz).to eq [{ qux: :QUX }]
end
end
context 'with arrays' do
context "with arrays" do
subject { Test::Foo.new([1], bar: %w[2], baz: [{ qux: :QUX }]) }
it 'coerces elements' do
it "coerces elements" do
expect(subject.foo).to eq %w[1]
expect(subject.bar).to eq %w[2]
expect(subject.baz).to eq [{ qux: :QUX }]

View File

@ -1,4 +1,4 @@
describe 'missed default values' do
describe "missed default values" do
subject do
class Test::Foo
extend Dry::Initializer
@ -8,7 +8,7 @@ describe 'missed default values' do
end
end
it 'raises SyntaxError' do
it "raises SyntaxError" do
expect { subject }.to raise_error SyntaxError, /bar/
end
end

View File

@ -1,7 +1,7 @@
describe 'nested type argument' do
subject { Test::Xyz.new('bar' => { 'baz' => 42 }) }
describe "nested type argument" do
subject { Test::Xyz.new("bar" => { "baz" => 42 }) }
context 'with nested definition only' do
context "with nested definition only" do
before do
class Test::Xyz
extend Dry::Initializer
@ -15,16 +15,16 @@ describe 'nested type argument' do
end
end
it 'builds the type' do
expect(subject.x.y.z).to eq '42'
it "builds the type" do
expect(subject.x.y.z).to eq "42"
end
it 'converts the nested type to hash' do
expect(subject.x.to_h).to eq('y' => { 'z' => '42' })
it "converts the nested type to hash" do
expect(subject.x.to_h).to eq("y" => { "z" => "42" })
end
end
context 'with nested and wrapped definitions' do
context "with nested and wrapped definitions" do
before do
class Test::Xyz
extend Dry::Initializer
@ -38,11 +38,11 @@ describe 'nested type argument' do
end
end
it 'builds the type' do
it "builds the type" do
x = subject.x
expect(x).to be_instance_of Array
expect(x.first.y.z).to eq '42'
expect(x.first.y.z).to eq "42"
end
end
end

View File

@ -1,5 +1,5 @@
describe 'optional value' do
context 'when has no default value' do
describe "optional value" do
context "when has no default value" do
before do
class Test::Foo
extend Dry::Initializer
@ -9,27 +9,27 @@ describe 'optional value' do
end
end
it 'quacks like nil' do
it "quacks like nil" do
subject = Test::Foo.new(1)
expect(subject.bar).to eq nil
end
it 'keeps info about been UNDEFINED' do
it "keeps info about been UNDEFINED" do
subject = Test::Foo.new(1)
expect(subject.instance_variable_get(:@bar))
.to eq Dry::Initializer::UNDEFINED
end
it 'can be set explicitly' do
subject = Test::Foo.new(1, 'qux')
it "can be set explicitly" do
subject = Test::Foo.new(1, "qux")
expect(subject.bar).to eq 'qux'
expect(subject.bar).to eq "qux"
end
end
context 'with undefined: false' do
context "with undefined: false" do
before do
class Test::Foo
extend Dry::Initializer[undefined: false]
@ -39,33 +39,33 @@ describe 'optional value' do
end
end
it 'sets undefined values to nil' do
it "sets undefined values to nil" do
subject = Test::Foo.new(1)
expect(subject.instance_variable_get(:@bar)).to be_nil
end
end
context 'when has a default value' do
context "when has a default value" do
before do
class Test::Foo
extend Dry::Initializer
param :foo
param :bar, optional: true, default: proc { 'baz' }
param :bar, optional: true, default: proc { "baz" }
end
end
it 'is takes default value' do
it "is takes default value" do
subject = Test::Foo.new(1)
expect(subject.bar).to eq 'baz'
expect(subject.bar).to eq "baz"
end
it 'can be set explicitly' do
subject = Test::Foo.new(1, 'qux')
it "can be set explicitly" do
subject = Test::Foo.new(1, "qux")
expect(subject.bar).to eq 'qux'
expect(subject.bar).to eq "qux"
end
end
end

View File

@ -1,11 +1,11 @@
describe 'options tolerance' do
describe "options tolerance" do
before do
class Test::Foo
extend Dry::Initializer
end
end
it 'allows options before any definition' do
it "allows options before any definition" do
expect { Test::Foo.new bar: :baz }.not_to raise_error
end
end

View File

@ -1,11 +1,11 @@
describe Dry::Initializer, '.dry_initializer.public_attributes' do
describe Dry::Initializer, ".dry_initializer.public_attributes" do
subject { instance.class.dry_initializer.public_attributes(instance) }
context 'when class has params' do
context "when class has params" do
before do
class Test::Foo
extend Dry::Initializer
param :foo, proc(&:to_s), desc: 'a weird parameter'
param :foo, proc(&:to_s), desc: "a weird parameter"
option :moo, optional: true
option :bar, default: proc { 1 }, reader: false
option :baz, optional: true, reader: :protected
@ -15,8 +15,8 @@ describe Dry::Initializer, '.dry_initializer.public_attributes' do
let(:instance) { Test::Foo.new(:FOO, bar: :BAR, baz: :BAZ, qux: :QUX) }
it 'collects public options only' do
expect(subject).to eq({ foo: 'FOO', moo: nil })
it "collects public options only" do
expect(subject).to eq({ foo: "FOO", moo: nil })
end
end
end

View File

@ -1,12 +1,12 @@
describe 'reader' do
shared_examples 'it has no public attr_reader' do
it 'does not define a public attr_reader' do
describe "reader" do
shared_examples "it has no public attr_reader" do
it "does not define a public attr_reader" do
expect(subject).not_to respond_to :foo
expect(subject).not_to respond_to :bar
end
end
context 'with reader: :public or no reader: option' do
context "with reader: :public or no reader: option" do
subject do
class Test::Foo
extend Dry::Initializer
@ -20,14 +20,14 @@ describe 'reader' do
Test::Foo.new 1, 2, bar: 3, bar2: 4
end
it 'defines a public attr_reader by default' do
it "defines a public attr_reader by default" do
expect(subject).to respond_to(:foo, :foo2)
expect(subject).to respond_to :bar
expect(subject).to respond_to :bar2
end
end
context 'with reader: false' do
context "with reader: false" do
before do
class Test::Foo
extend Dry::Initializer
@ -39,15 +39,15 @@ describe 'reader' do
subject { Test::Foo.new 1, bar: 2 }
it_behaves_like 'it has no public attr_reader'
it_behaves_like "it has no public attr_reader"
it 'keeps assigning variables' do
it "keeps assigning variables" do
expect(subject.instance_variable_get(:@foo)).to eql 1
expect(subject.instance_variable_get(:@bar)).to eql 2
end
end
context 'with reader: :private' do
context "with reader: :private" do
before do
class Test::Foo
extend Dry::Initializer
@ -59,15 +59,15 @@ describe 'reader' do
subject { Test::Foo.new 1, bar: 2 }
it_behaves_like 'it has no public attr_reader'
it_behaves_like "it has no public attr_reader"
it 'adds a private attr_reader' do
it "adds a private attr_reader" do
expect(subject.send(:foo)).to eql 1
expect(subject.send(:bar)).to eql 2
end
end
context 'with reader: :protected' do
context "with reader: :protected" do
subject do
class Test::Foo
extend Dry::Initializer
@ -79,7 +79,7 @@ describe 'reader' do
Test::Foo.new 1, bar: 2
end
it 'adds a protected attr_reader' do
it "adds a protected attr_reader" do
protected_instance_methods = subject.class.protected_instance_methods
expect(protected_instance_methods).to match_array(%i[foo bar])
end

View File

@ -1,7 +1,7 @@
describe 'repetitive definitions' do
describe "repetitive definitions" do
subject { Test::Foo.new }
context 'of params' do
context "of params" do
before do
class Test::Foo
extend Dry::Initializer
@ -12,12 +12,12 @@ describe 'repetitive definitions' do
end
end
it 'reloads the attribute' do
it "reloads the attribute" do
expect(subject.foo).to eq 2
end
end
context 'of options' do
context "of options" do
before do
class Test::Foo
extend Dry::Initializer
@ -28,12 +28,12 @@ describe 'repetitive definitions' do
end
end
it 'reloads the attribute' do
it "reloads the attribute" do
expect(subject.foo).to eq 2
end
end
context 'of param and option' do
context "of param and option" do
before do
class Test::Foo
extend Dry::Initializer
@ -44,12 +44,12 @@ describe 'repetitive definitions' do
end
end
it 'reloads the attribute' do
it "reloads the attribute" do
expect(subject.foo).to eq 2
end
end
context 'of optional param and option' do
context "of optional param and option" do
before do
class Test::Foo
extend Dry::Initializer
@ -60,7 +60,7 @@ describe 'repetitive definitions' do
end
end
it 'allows various assignments' do
it "allows various assignments" do
expect(Test::Foo.new(1).foo).to eq 1
expect(Test::Foo.new(foo: 2).foo).to eq 2
expect(Test::Foo.new(1, foo: 2).foo).to eq 2

View File

@ -1,4 +1,4 @@
describe 'attribute with several assignments' do
describe "attribute with several assignments" do
before do
class Test::Foo
extend Dry::Initializer
@ -8,33 +8,33 @@ describe 'attribute with several assignments' do
end
end
context 'when not defined' do
context "when not defined" do
subject { Test::Foo.new }
it 'is left undefined' do
it "is left undefined" do
expect(subject.bar).to be_nil
expect(subject.instance_variable_get :@bar)
.to eq Dry::Initializer::UNDEFINED
end
end
context 'when set directly' do
context "when set directly" do
subject { Test::Foo.new bar: :BAZ }
it 'sets the attribute' do
expect(subject.bar).to eq 'BAZ'
it "sets the attribute" do
expect(subject.bar).to eq "BAZ"
end
end
context 'when renamed' do
context "when renamed" do
subject { Test::Foo.new "some foo": :BAZ }
it 'renames the attribute' do
it "renames the attribute" do
expect(subject.bar).to eq :BAZ
expect(subject).not_to respond_to :foo
end
it 'renames the variable' do
it "renames the variable" do
expect(subject.instance_variable_get(:@bar)).to eq :BAZ
end
end

View File

@ -1,9 +1,9 @@
require_relative 'support/coverage'
require_relative "support/coverage"
require 'dry/initializer'
require "dry/initializer"
begin
require 'pry'
require "pry"
rescue LoadError
nil
end

View File

@ -1,4 +1,4 @@
describe 'subclassing' do
describe "subclassing" do
before do
class Test::Foo
extend Dry::Initializer[undefined: false]
@ -20,24 +20,24 @@ describe 'subclassing' do
Test::Bar.new 1, 2, bar: 3, qux: 4
end
it 'preserves null definition' do
it "preserves null definition" do
expect(Test::Foo.dry_initializer.null).to be_nil
expect(Test::Bar.dry_initializer.null).to be_nil
end
it 'preserves definitions made in the superclass' do
it "preserves definitions made in the superclass" do
expect(instance_of_subclass.foo).to eql 1
expect(instance_of_subclass.baz).to eql 2
expect(instance_of_subclass.bar).to eql 3
expect(instance_of_subclass.qux).to eql 4
end
it 'does not pollute superclass with definitions from subclass' do
it "does not pollute superclass with definitions from subclass" do
expect(instance_of_superclass).not_to respond_to :baz
expect(instance_of_superclass).not_to respond_to :qux
end
it 'calls .inherited hook added by other mixin' do
it "calls .inherited hook added by other mixin" do
called = false
mixin = Module.new { define_method(:inherited) { |_| called = true } }

View File

@ -1,34 +1,34 @@
require 'dry-types'
require "dry-types"
describe 'type argument' do
describe "type argument" do
before do
class Test::Foo
extend Dry::Initializer
param :foo, Dry::Types['strict.string']
option :bar, Dry::Types['strict.string']
param :foo, Dry::Types["strict.string"]
option :bar, Dry::Types["strict.string"]
end
end
context 'in case of param mismatch' do
subject { Test::Foo.new 1, bar: '2' }
context "in case of param mismatch" do
subject { Test::Foo.new 1, bar: "2" }
it 'raises TypeError' do
it "raises TypeError" do
expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
end
end
context 'in case of option mismatch' do
subject { Test::Foo.new '1', bar: 2 }
context "in case of option mismatch" do
subject { Test::Foo.new "1", bar: 2 }
it 'raises TypeError' do
it "raises TypeError" do
expect { subject }.to raise_error Dry::Types::ConstraintError, /2/
end
end
context 'in case of match' do
subject { Test::Foo.new '1', bar: '2' }
context "in case of match" do
subject { Test::Foo.new "1", bar: "2" }
it 'completes the initialization' do
it "completes the initialization" do
expect { subject }.not_to raise_error
end
end

View File

@ -1,7 +1,7 @@
require 'dry-types'
require "dry-types"
describe 'type constraint' do
context 'by a proc with 1 argument' do
describe "type constraint" do
context "by a proc with 1 argument" do
before do
class Test::Foo
extend Dry::Initializer
@ -11,12 +11,12 @@ describe 'type constraint' do
subject { Test::Foo.new :foo }
it 'coerces a value' do
expect(subject.__foo__).to eq 'foo'
it "coerces a value" do
expect(subject.__foo__).to eq "foo"
end
end
context 'by a proc with 2 arguments' do
context "by a proc with 2 arguments" do
before do
class Test::Foo
extend Dry::Initializer
@ -26,12 +26,12 @@ describe 'type constraint' do
subject { Test::Foo.new :foo }
it 'coerces a value with self as a second argument' do
it "coerces a value with self as a second argument" do
expect(subject.foo).to eq "#{subject.hash}:foo"
end
end
context 'by dry-type' do
context "by dry-type" do
before do
constraint = self.constraint
@ -41,66 +41,66 @@ describe 'type constraint' do
end
end
context 'with a strict string' do
let(:constraint) { Dry::Types['strict.string'] }
context "with a strict string" do
let(:constraint) { Dry::Types["strict.string"] }
context 'in case of mismatch' do
context "in case of mismatch" do
subject { Test::Foo.new 1 }
it 'raises ArgumentError' do
it "raises ArgumentError" do
expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
end
end
context 'in case of match' do
subject { Test::Foo.new 'foo' }
context "in case of match" do
subject { Test::Foo.new "foo" }
it 'completes the initialization' do
it "completes the initialization" do
expect { subject }.not_to raise_error
end
end
context 'if optional value not set' do
context "if optional value not set" do
subject { Test::Foo.new }
it 'not applicable to Dry::Initializer::UNDEFINED' do
it "not applicable to Dry::Initializer::UNDEFINED" do
expect(subject.instance_variable_get(:@foo))
.to eq Dry::Initializer::UNDEFINED
end
end
end
context 'with a integer member array' do
let(:constraint) { Dry::Types['array'].of(Dry::Types['coercible.integer']) }
context "with a integer member array" do
let(:constraint) { Dry::Types["array"].of(Dry::Types["coercible.integer"]) }
context 'with arity other than 1' do
subject { Test::Foo.new ['1'] }
context "with arity other than 1" do
subject { Test::Foo.new ["1"] }
it 'completes the initialization' do
it "completes the initialization" do
expect(subject.foo).to eql([1])
end
end
context 'when value is not valid' do
subject { Test::Foo.new 'foo' }
context "when value is not valid" do
subject { Test::Foo.new "foo" }
it 'raises constraint error' do
it "raises constraint error" do
expect { subject }.to raise_error(Dry::Types::ConstraintError, /foo/)
end
end
context 'when member value is not valid' do
subject { Test::Foo.new ['foo'] }
context "when member value is not valid" do
subject { Test::Foo.new ["foo"] }
it 'raises constraint error' do
it "raises constraint error" do
expect { subject }.to raise_error(Dry::Types::CoercionError, /foo/)
end
end
end
end
context 'by invalid constraint' do
it 'raises ArgumentError' do
context "by invalid constraint" do
it "raises ArgumentError" do
expect do
class Test::Foo
extend Dry::Initializer

View File

@ -1,6 +1,6 @@
require 'dry-types'
require "dry-types"
describe 'value coercion via dry-types' do
describe "value coercion via dry-types" do
before do
module Test::Types
include Dry.Types
@ -10,18 +10,18 @@ describe 'value coercion via dry-types' do
extend Dry::Initializer
param :foo, type: Test::Types::Coercible::String
option :bar, proc(&:to_i), default: proc { '16' }
option :bar, proc(&:to_i), default: proc { "16" }
end
end
it 'coerces assigned values' do
subject = Test::Foo.new :foo, bar: '13'
it "coerces assigned values" do
subject = Test::Foo.new :foo, bar: "13"
expect(subject.foo).to eql 'foo'
expect(subject.foo).to eql "foo"
expect(subject.bar).to eql 13
end
it 'coerces defaults as well' do
it "coerces defaults as well" do
subject = Test::Foo.new :foo
expect(subject.bar).to eql 16