-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow developers to define #call with arguments for convenience
If the "call" instance method accepts arguments, those arguments will be automatically assigned from the provided context, matching on name. This works for both positional and keyword arguments. If an argument is specified but no matching value is provided in the context, an ArgumentError is raised.
- Loading branch information
1 parent
31efe66
commit 3607df5
Showing
2 changed files
with
239 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,213 @@ | ||
describe Interactor do | ||
include_examples :lint | ||
|
||
describe "#call" do | ||
let(:interactor) { Class.new.send(:include, described_class) } | ||
|
||
context "positional arguments" do | ||
it "accepts required positional arguments" do | ||
interactor.class_eval do | ||
def call(foo) | ||
context.output = foo | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz", hello: "world") | ||
|
||
expect(result.output).to eq("baz") | ||
end | ||
|
||
it "accepts optional positional arguments" do | ||
interactor.class_eval do | ||
def call(foo = "bar") | ||
context.output = foo | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz", hello: "world") | ||
|
||
expect(result.output).to eq("baz") | ||
end | ||
|
||
it "assigns absent positional arguments" do | ||
interactor.class_eval do | ||
def call(foo = "bar") | ||
context.output = foo | ||
end | ||
end | ||
|
||
result = interactor.call(hello: "world") | ||
|
||
expect(result.output).to eq("bar") | ||
end | ||
|
||
it "raises an error for missing positional arguments" do | ||
interactor.class_eval do | ||
def call(foo) | ||
context.output = foo | ||
end | ||
end | ||
|
||
expect { interactor.call(hello: "world") }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context "keyword arguments" do | ||
it "accepts required keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo:) | ||
context.output = foo | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz", hello: "world") | ||
|
||
expect(result.output).to eq("baz") | ||
end | ||
|
||
it "accepts optional keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo: "bar") | ||
context.output = foo | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz", hello: "world") | ||
|
||
expect(result.output).to eq("baz") | ||
end | ||
|
||
it "assigns absent keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo: "bar") | ||
context.output = foo | ||
end | ||
end | ||
|
||
result = interactor.call(hello: "world") | ||
|
||
expect(result.output).to eq("bar") | ||
end | ||
|
||
it "raises an error for missing keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo:) | ||
context.output = foo | ||
end | ||
end | ||
|
||
expect { interactor.call(hello: "world") }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context "combination arguments" do | ||
it "accepts required positional with required keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo, hello:) | ||
context.output = [foo, hello] | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz", hello: "world") | ||
|
||
expect(result.output).to eq(["baz", "world"]) | ||
end | ||
|
||
it "accepts required positional with optional keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo, hello: "there") | ||
context.output = [foo, hello] | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz", hello: "world") | ||
|
||
expect(result.output).to eq(["baz", "world"]) | ||
end | ||
|
||
it "accepts required positional and assigns absent keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo, hello: "there") | ||
context.output = [foo, hello] | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz") | ||
|
||
expect(result.output).to eq(["baz", "there"]) | ||
end | ||
|
||
it "accepts optional positional with required keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo = "bar", hello:) | ||
context.output = [foo, hello] | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz", hello: "world") | ||
|
||
expect(result.output).to eq(["baz", "world"]) | ||
end | ||
|
||
it "accepts optional positional with optional keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo = "bar", hello: "there") | ||
context.output = [foo, hello] | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz", hello: "world") | ||
|
||
expect(result.output).to eq(["baz", "world"]) | ||
end | ||
|
||
it "accepts optional positional and assigns absent keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo = "bar", hello: "there") | ||
context.output = [foo, hello] | ||
end | ||
end | ||
|
||
result = interactor.call(foo: "baz") | ||
|
||
expect(result.output).to eq(["baz", "there"]) | ||
end | ||
|
||
it "assigns absent positional and accepts required keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo = "bar", hello:) | ||
context.output = [foo, hello] | ||
end | ||
end | ||
|
||
result = interactor.call(hello: "world") | ||
|
||
expect(result.output).to eq(["bar", "world"]) | ||
end | ||
|
||
it "assigns absent positional and accepts optional keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo = "bar", hello: "there") | ||
context.output = [foo, hello] | ||
end | ||
end | ||
|
||
result = interactor.call(hello: "world") | ||
|
||
expect(result.output).to eq(["bar", "world"]) | ||
end | ||
|
||
it "assigns absent positional and absent keyword arguments" do | ||
interactor.class_eval do | ||
def call(foo = "bar", hello: "there") | ||
context.output = [foo, hello] | ||
end | ||
end | ||
|
||
result = interactor.call | ||
|
||
expect(result.output).to eq(["bar", "there"]) | ||
end | ||
end | ||
end | ||
end |