spec/task_spec.rb in command_kit-completion-0.1.2 vs spec/task_spec.rb in command_kit-completion-0.2.0

- old
+ new

@@ -217,10 +217,144 @@ expect(subject.completion_rules_for(command_class)).to eq({}) end end end + context "when the command class includes CommandKit::Arguments" do + context "and has at least one argument" do + context "and it's named FILE" do + class TestCommandWithFILEArgument < CommandKit::Command + + command_name 'test' + + option :foo, desc: 'Foo option' + + argument :bar, usage: 'FILE', desc: 'Bar option' + + end + + let(:command_class) { TestCommandWithFILEArgument } + + it "must add '<file>' to the command's completion rule" do + expect(subject.completion_rules_for(command_class)).to eq( + { + "test" => %w[--foo <file>] + } + ) + end + end + + context "and it's named DIR" do + class TestCommandWithDIRArgument < CommandKit::Command + + command_name 'test' + + option :foo, desc: 'Foo option' + + argument :bar, usage: 'DIR', desc: 'Bar option' + + end + + let(:command_class) { TestCommandWithDIRArgument } + + it "must add '<directory>' to the command's completion rule" do + expect(subject.completion_rules_for(command_class)).to eq( + { + "test" => %w[--foo <directory>] + } + ) + end + end + + context "and it's named HOST" do + class TestCommandWithHOSTArgument < CommandKit::Command + + command_name 'test' + + option :foo, desc: 'Foo option' + + argument :bar, usage: 'HOST', desc: 'Bar option' + + end + + let(:command_class) { TestCommandWithHOSTArgument } + + it "must add '<hostname>' to the command's completion rule" do + expect(subject.completion_rules_for(command_class)).to eq( + { + "test" => %w[--foo <hostname>] + } + ) + end + end + + context "and it's named USER" do + class TestCommandWithUSERArgument < CommandKit::Command + + command_name 'test' + + option :foo, desc: 'Foo option' + + argument :bar, usage: 'USER', desc: 'Bar option' + + end + + let(:command_class) { TestCommandWithUSERArgument } + + it "must add '<user>' to the command's completion rule" do + expect(subject.completion_rules_for(command_class)).to eq( + { + "test" => %w[--foo <user>] + } + ) + end + end + + context "but it's named something else" do + class TestCommandWithArgument < CommandKit::Command + + command_name 'test' + + option :foo, desc: 'Foo option' + + argument :bar, usage: 'BAR', desc: 'Bar option' + + end + + let(:command_class) { TestCommandWithArgument } + + it "must not add additional suggestions to the command's completion rule" do + expect(subject.completion_rules_for(command_class)).to eq( + { + "test" => %w[--foo] + } + ) + end + end + end + + context "but has no arguments" do + class TestCommandWithoutArguments < CommandKit::Command + + command_name 'test' + + option :foo, desc: 'Foo option' + + end + + let(:command_class) { TestCommandWithoutArguments } + + it "must not add additional suggestions" do + expect(subject.completion_rules_for(command_class)).to eq( + { + "test" => %w[--foo] + } + ) + end + end + end + context "when the command class includes CommandKit::Commands" do class TestCommandWithSubCommands < CommandKit::Command include CommandKit::Commands option :global_option, short: '-g', @@ -257,11 +391,11 @@ let(:command_class) { TestCommandWithSubCommands } it "must add completion rules for the other commands" do expect(subject.completion_rules_for(command_class)).to eq( { - "test" => %w[--global-option help foo bar], + "test" => %w[--global-option -g help foo bar], "test foo" => %w[--foo-opt1 --foo-opt2], "test bar" => %w[--bar-opt1 --bar-opt2] } ) end @@ -307,11 +441,11 @@ let(:command_class) { TestCommandWithSubCommandsAndCommandAliases } it "must include the command aliases in the completion rules" do expect(subject.completion_rules_for(command_class)).to eq( { - "test" => %w[--global-option help foo bar foo2 bar2], + "test" => %w[--global-option -g help foo bar foo2 bar2], "test foo" => %w[--foo-opt1 --foo-opt2], "test bar" => %w[--bar-opt1 --bar-opt2] } ) end @@ -347,11 +481,11 @@ let(:command_class) { TestCommandWithSubCommandsWithNoOptions } it "must omit the command from the completion rules" do expect(subject.completion_rules_for(command_class)).to eq( { - "test" => %w[--global-option help foo bar], + "test" => %w[--global-option -g help foo bar], "test foo" => %w[--foo-opt1 --foo-opt2] } ) end end @@ -421,11 +555,11 @@ let(:command_class) { TestCommandWithSubSubCommands } it "must recursively include completion rules for the sub-sub-commands" do expect(subject.completion_rules_for(command_class)).to eq( { - "test" => %w[--global-option help foo bar], + "test" => %w[--global-option -g help foo bar], "test foo" => %w[--foo-opt1 --foo-opt2], "test bar" => %w[--bar-opt1 --bar-opt2 help baz qux], "test bar baz" => %w[--baz-opt1 --baz-opt2], "test bar qux" => %w[--qux-opt1 --qux-opt2] } @@ -433,18 +567,69 @@ end end end end + describe "#suggestion_for_argument" do + context "when given 'FILE'" do + it "must return '<file>'" do + expect(subject.suggestion_for_argument('FILE')).to eq('<file>') + end + end + + context "when the string ends with '_FILE'" do + it "must return '<file>'" do + expect(subject.suggestion_for_argument('FOO_FILE')).to eq('<file>') + end + end + + context "when given 'DIR'" do + it "must return '<directory>'" do + expect(subject.suggestion_for_argument('DIR')).to eq('<directory>') + end + end + + context "when the string ends with '_DIR'" do + it "must return '<directory>'" do + expect(subject.suggestion_for_argument('FOO_DIR')).to eq('<directory>') + end + end + + context "when given 'HOST'" do + it "must return '<hostname>'" do + expect(subject.suggestion_for_argument('HOST')).to eq('<hostname>') + end + end + + context "when the string ends with '_HOST'" do + it "must return '<hostname>'" do + expect(subject.suggestion_for_argument('FOO_HOST')).to eq('<hostname>') + end + end + + context "when given 'USER'" do + it "must return '<user>'" do + expect(subject.suggestion_for_argument('USER')).to eq('<user>') + end + end + + context "when the string ends with '_USER'" do + it "must return '<user>'" do + expect(subject.suggestion_for_argument('FOO_USER')).to eq('<user>') + end + end + end + describe "#completion_rules" do it "must load the class from #class_file and return the generated completion rules for it" do expect(subject.completion_rules).to eq( { - "foo" => %w[--config-file help config list update ls up], + "foo" => %w[--config-file -C help config list update ls up], "foo config" => %w[help get set], - "foo update" => %w[--quiet], - "foo*--config-file" => %w[<file>] + "foo update" => %w[--quiet -q], + "foo*--config-file" => %w[<file>], + "foo*-C" => %w[<file>] } ) end context "when #input_file is set" do @@ -460,13 +645,14 @@ end it "must merge the additional completion rules with the generated ones" do expect(subject.completion_rules).to eq( { - "foo" => %w[--config-file help config list update ls up], + "foo" => %w[--config-file -C help config list update ls up], "foo config" => %w[help get set], - "foo update" => ['--quiet', '$(foo list)'], - "foo*--config-file" => %w[<file>] + "foo update" => ['--quiet', '-q', '$(foo list)'], + "foo*--config-file" => %w[<file>], + "foo*-C" => %w[<file>] } ) end end end