require 'spec_helper' module Flydata describe Cli do subject { Cli.new(args) } describe '#run' do let(:command_obj) { double('command_obj') } let(:args) { options.unshift command } let(:empty_slop) { double('empty_slop') } let (:sub_command) { "subcomm" } context 'target commands supporting no options' do let(:main_command) { "maincomm" } let(:target_class) { module Command; class Maincomm; end; end; Command::Maincomm } context 'without subcommand' do let(:command) { main_command } context 'without options' do let(:options) { [] } it 'calls #run of the target class object' do expect(target_class).to receive(:new){|a| expect(a.to_hash).to be_empty }.and_return(command_obj) expect(command_obj).to receive(:run) subject.run end end context 'with options' do let(:options) { %w(-a --host-name localhost) } it 'returns with an error' do expect(subject).to receive(:puts).with('! Unknown options -a, --host-name').once allow(subject).to receive(:puts) subject.run end end context 'with arguments' do let(:options) { %w(a b c ) } it 'calls #run of the target class instance with arguments' do expect(target_class).to receive(:new){|a| expect(a.to_hash).to be_empty }.and_return(command_obj) expect(command_obj).to receive(:run).with(*options) subject.run end end end context 'with subcommand' do let (:command) { "#{main_command}:#{sub_command}" } context 'without options' do let(:options) { [] } it 'calls subcommand method of the target class object' do expect(target_class).to receive(:new){|a| expect(a.to_hash).to be_empty }.and_return(command_obj) expect(command_obj).to receive(sub_command.to_sym) subject.run end end context 'with optionss' do let(:options) { %w(-a --host-name localhost) } it 'returns an error' do expect(subject).to receive(:puts).with('! Unknown options -a, --host-name').once allow(subject).to receive(:puts) subject.run end end end end context 'target commands supporting options' do let(:main_command) { "maincommop" } let(:target_class) do module Command class Maincommop < Base # Options for the main command def self.slop Slop.new(strict: true) do on 'a', 'short option' on 'host-name=', 'long option taking an argument' end end # Options for the sub command 'subcomm' def self.slop_subcomm Slop.new(strict: true) do on 'b', 'short option' on 'port=', 'Port', as: Integer end end end end Command::Maincommop end context 'without subcommand' do let(:command) { main_command } context 'without options' do let(:options) { [] } it 'calls #run of the target class object' do expect(target_class).to receive(:new) { |a| expect(a.a?).to be false expect(a[:"host-name"]).to be_nil }.and_return(command_obj) expect(command_obj).to receive(:run) subject.run end end context 'with options' do let(:options) { %w(-a --host-name localhost) } it 'calls #run of the target class object' do expect(target_class).to receive(:new) { |a| expect(a.a?).to be true expect(a[:'host-name']).to eq "localhost" }.and_return(command_obj) expect(command_obj).to receive(:run) subject.run end end context 'with options and arguments' do let(:arguments) { %w(foo bar bal) } let(:options) { %w(foo -a bar --host-name localhost bal) } it 'calls #run of the target class object' do expect(target_class).to receive(:new) { |a| expect(a.a?).to be true expect(a[:'host-name']).to eq "localhost" }.and_return(command_obj) expect(command_obj).to receive(:run).with(*arguments) subject.run end end end context 'with subcommand' do let (:command) { "#{main_command}:#{sub_command}" } context 'without options' do let(:options) { [] } it 'calls the subcommand of the target class object' do expect(target_class).to receive(:new) { |a| expect(a.b?).to be false expect(a[:port]).to be_nil }.and_return(command_obj) expect(command_obj).to receive(sub_command.to_sym) subject.run end end context 'with options' do let(:options) { %w(-b --port 5439) } it 'calls the subcommand of the target class object' do expect(target_class).to receive(:new) { |a| expect(a.b?).to be true expect(a[:port]).to eq 5439 }.and_return(command_obj) expect(command_obj).to receive(sub_command.to_sym) subject.run end end end end end end end