|
| 1 | +require 'pact/mock_service/cli/custom_thor' |
| 2 | + |
| 3 | +class Pact::MockService::CLI |
| 4 | + |
| 5 | + class Delegate |
| 6 | + def self.call options; end |
| 7 | + end |
| 8 | + |
| 9 | + class TestThor < CustomThor |
| 10 | + desc 'ARGUMENT', 'This is the description' |
| 11 | + def test_default(argument) |
| 12 | + Delegate.call(argument: argument) |
| 13 | + end |
| 14 | + |
| 15 | + desc '', '' |
| 16 | + method_option :multi, type: :array |
| 17 | + def test_multiple_options |
| 18 | + Delegate.call(options) |
| 19 | + end |
| 20 | + |
| 21 | + default_command :test_default |
| 22 | + end |
| 23 | + |
| 24 | + describe CustomThor do |
| 25 | + subject { TestThor.new } |
| 26 | + |
| 27 | + it "invokes the default task when aguments are given without specifying a task" do |
| 28 | + expect(Delegate).to receive(:call).with(argument: 'foo') |
| 29 | + TestThor.start(%w{foo}) |
| 30 | + end |
| 31 | + |
| 32 | + it "converts options that are specified multiple times into a single array" do |
| 33 | + expect(Delegate).to receive(:call).with({'multi' => ['one', 'two']}) |
| 34 | + TestThor.start(%w{test_multiple_options --multi one --multi two}) |
| 35 | + end |
| 36 | + |
| 37 | + describe ".prepend_default_task_name" do |
| 38 | + let(:argv_with) { [TestThor.default_command, 'foo'] } |
| 39 | + |
| 40 | + context "when the default task name is given" do |
| 41 | + it "does not prepend the default task name" do |
| 42 | + expect(TestThor.prepend_default_task_name(argv_with)).to eq(argv_with) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context "when the first argument is --help" do |
| 47 | + let(:argv) { ['--help', 'foo'] } |
| 48 | + |
| 49 | + it "does not prepend the default task name" do |
| 50 | + expect(TestThor.prepend_default_task_name(argv)).to eq(argv) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context "when the default task name is not given" do |
| 55 | + let(:argv) { ['foo'] } |
| 56 | + |
| 57 | + it "prepends the default task name" do |
| 58 | + expect(TestThor.prepend_default_task_name(argv)).to eq(argv_with) |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe ".turn_muliple_tag_options_into_array" do |
| 64 | + it "turns '--tag foo --tag bar' into '--tag foo bar'" do |
| 65 | + input = %w{--ignore this --tag foo --tag bar --wiffle --that} |
| 66 | + output = %w{--ignore this --tag foo bar --wiffle --that } |
| 67 | + expect(TestThor.turn_muliple_tag_options_into_array(input)).to eq output |
| 68 | + end |
| 69 | + |
| 70 | + it "turns '--tag foo bar --tag meep' into '--tag foo meep bar'" do |
| 71 | + input = %w{--ignore this --tag foo bar --tag meep --wiffle --that} |
| 72 | + output = %w{--ignore this --tag foo meep bar --wiffle --that} |
| 73 | + expect(TestThor.turn_muliple_tag_options_into_array(input)).to eq output |
| 74 | + end |
| 75 | + |
| 76 | + it "turns '--tag foo --tag bar wiffle' into '--tag foo bar wiffle' which is silly" do |
| 77 | + input = %w{--ignore this --tag foo --tag bar wiffle} |
| 78 | + output = %w{--ignore this --tag foo bar wiffle} |
| 79 | + expect(TestThor.turn_muliple_tag_options_into_array(input)).to eq output |
| 80 | + end |
| 81 | + |
| 82 | + it "maintains '--tag foo bar wiffle'" do |
| 83 | + input = %w{--ignore this --tag foo bar wiffle --meep} |
| 84 | + output = %w{--ignore this --tag foo bar wiffle --meep} |
| 85 | + expect(TestThor.turn_muliple_tag_options_into_array(input)).to eq output |
| 86 | + end |
| 87 | + |
| 88 | + it "turns '-t foo -t bar' into '-t foo bar'" do |
| 89 | + input = %w{--ignore this -t foo -t bar --meep --that 1 2 3} |
| 90 | + output = %w{--ignore this -t foo bar --meep --that 1 2 3} |
| 91 | + expect(TestThor.turn_muliple_tag_options_into_array(input)).to eq output |
| 92 | + end |
| 93 | + |
| 94 | + it "doesn't change anything when there are no duplicate options" do |
| 95 | + input = %w{--ignore this --taggy foo --blah bar --wiffle --that} |
| 96 | + expect(TestThor.turn_muliple_tag_options_into_array(input)).to eq input |
| 97 | + end |
| 98 | + |
| 99 | + it "return an empty array when given an empty array" do |
| 100 | + input = [] |
| 101 | + expect(TestThor.turn_muliple_tag_options_into_array(input)).to eq input |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | +end |
0 commit comments