-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from dry-rb/respect-previously-defined-ivars-d…
…uring-initialize Only assign nil values in initialize if ivar not previously defined
- Loading branch information
Showing
4 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
spec/integration/hash/inheritance/existing_ivars_before_initialize_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "kwargs / inheritance / instance variables set before #initialize" do | ||
before do | ||
module Test | ||
AutoInject = Dry::AutoInject(configuration: "configuration", another_dep: "another_dep") | ||
end | ||
end | ||
|
||
let(:framework_class) { | ||
Class.new do | ||
def self.new(configuration:, **args) | ||
allocate.tap do |obj| | ||
obj.instance_variable_set :@configuration, configuration | ||
obj.send :initialize, **args | ||
end | ||
end | ||
end | ||
} | ||
|
||
let(:parent_class) { | ||
Class.new(framework_class) do | ||
include Test::AutoInject.hash[:configuration] | ||
end | ||
} | ||
|
||
let(:child_class) { | ||
Class.new(parent_class) do | ||
include Test::AutoInject.hash[:another_dep] | ||
end | ||
|
||
} | ||
|
||
it "does not assign nil value from missing dependency arg to its instance variable if it is already defined" do | ||
child = child_class.new | ||
expect(child.configuration).to eq "configuration" | ||
expect(child.another_dep).to eq "another_dep" | ||
end | ||
|
||
it "does assign an explicitly provided non-nil dependency to iits instance variable, even if it is already defined" do | ||
child = child_class.new(configuration: "child configuration") | ||
expect(child.configuration).to eq "child configuration" | ||
expect(child.another_dep).to eq "another_dep" | ||
end | ||
end |
45 changes: 45 additions & 0 deletions
45
spec/integration/kwargs/inheritance/existing_ivars_before_initialize_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "kwargs / inheritance / instance variables set before #initialize" do | ||
before do | ||
module Test | ||
AutoInject = Dry::AutoInject(configuration: "configuration", another_dep: "another_dep") | ||
end | ||
end | ||
|
||
let(:framework_class) { | ||
Class.new do | ||
def self.new(configuration:, **args) | ||
allocate.tap do |obj| | ||
obj.instance_variable_set :@configuration, configuration | ||
obj.send :initialize, **args | ||
end | ||
end | ||
end | ||
} | ||
|
||
let(:parent_class) { | ||
Class.new(framework_class) do | ||
include Test::AutoInject[:configuration] | ||
end | ||
} | ||
|
||
let(:child_class) { | ||
Class.new(parent_class) do | ||
include Test::AutoInject[:another_dep] | ||
end | ||
|
||
} | ||
|
||
it "does not assign nil value from missing dependency arg to its instance variable if it is already defined" do | ||
child = child_class.new | ||
expect(child.configuration).to eq "configuration" | ||
expect(child.another_dep).to eq "another_dep" | ||
end | ||
|
||
it "does assign an explicitly provided non-nil dependency to iits instance variable, even if it is already defined" do | ||
child = child_class.new(configuration: "child configuration") | ||
expect(child.configuration).to eq "child configuration" | ||
expect(child.another_dep).to eq "another_dep" | ||
end | ||
end |