-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability preserve parameters definitions when subclassing
- Loading branch information
1 parent
ef58080
commit cb497a7
Showing
2 changed files
with
68 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe TTY::Option do | ||
it "inherits parameters from parent class" do | ||
parent_class = command("ParentCmd") do | ||
option :foo do | ||
long "--foo string" | ||
end | ||
end | ||
|
||
child_class = command("ChildCmd", parent_class) | ||
|
||
expect(parent_class.parameters.map(&:name)) | ||
.to eq(child_class.parameters.map(&:name)) | ||
|
||
parent_cmd = parent_class.new | ||
child_cmd = child_class.new | ||
|
||
child_cmd.parse(%w[--foo b]) | ||
parent_cmd.parse(%w[--foo a]) | ||
|
||
expect(parent_cmd.params[:foo]).to eq("a") | ||
expect(parent_cmd.remaining).to eq([]) | ||
expect(child_cmd.params[:foo]).to eq("b") | ||
expect(child_cmd.remaining).to eq([]) | ||
end | ||
|
||
it "adds new parameters in child class" do | ||
parent_class = command("ParentCmd") do | ||
option :foo do | ||
long "--foo string" | ||
end | ||
end | ||
|
||
child_class = command("ChildCmd", parent_class) do | ||
option :bar do | ||
long "--bar string" | ||
end | ||
end | ||
|
||
parent_cmd = parent_class.new | ||
child_cmd = child_class.new | ||
|
||
child_cmd.parse(%w[--foo a --bar b]) | ||
parent_cmd.parse(%w[--foo a --bar b], check_invalid_params: false) | ||
|
||
expect(parent_class.parameters.map(&:name)).to eq([:foo]) | ||
expect(parent_cmd.params[:foo]).to eq("a") | ||
expect(parent_cmd.params[:bar]).to eq(nil) | ||
expect(parent_cmd.remaining).to eq(%w[--bar b]) | ||
|
||
expect(child_class.parameters.map(&:name)).to eq([:foo, :bar]) | ||
expect(child_cmd.params[:foo]).to eq("a") | ||
expect(child_cmd.params[:bar]).to eq("b") | ||
expect(child_cmd.remaining).to eq([]) | ||
end | ||
end |