summaryrefslogtreecommitdiff
path: root/spec/class_command_spec.rb
blob: 59151d62020700a8d04e472be26e4d6b886415aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# frozen_string_literal: true

RSpec.describe Pry::ClassCommand do
  describe ".inherited" do
    context "when match is defined" do
      subject do
        Class.new(described_class) do
          match('match')
        end
      end

      it "sets match on the subclass" do
        subclass = Class.new(subject)
        expect(subclass.match).to eq('match')
      end
    end

    context "when description is defined" do
      subject do
        Class.new(described_class) do
          description('description')
        end
      end

      it "sets description on the subclass" do
        subclass = Class.new(subject)
        expect(subclass.description).to eq('description')
      end
    end

    context "when command_options is defined" do
      subject do
        Class.new(described_class) do
          command_options(listing: 'listing')
        end
      end

      it "sets command_options on the subclass" do
        subclass = Class.new(subject)
        expect(subclass.command_options)
          .to match(hash_including(listing: 'listing'))
      end
    end
  end

  describe ".source" do
    subject { Class.new(described_class) }

    it "returns source code for the process method" do
      expect(subject.source).to match(/\Adef process\n.+\nend\n\z/)
    end
  end

  describe ".doc" do
    subject do
      Class.new(described_class) { banner('banner') }
    end

    it "returns source code for the process method" do
      expect(subject.doc).to eq("banner\n    -h, --help      Show this message.")
    end
  end

  describe ".source_location" do
    subject { Class.new(described_class) }

    it "returns source location" do
      expect(subject.source_location)
        .to match([/class_command.rb/, be_kind_of(Integer)])
    end
  end

  describe ".source_file" do
    subject { Class.new(described_class) }

    it "returns source file" do
      expect(subject.source_file).to match(/class_command.rb/)
    end
  end

  describe ".source_line" do
    subject { Class.new(described_class) }

    it "returns source file" do
      expect(subject.source_line).to be_kind_of(Integer)
    end
  end

  describe "#call" do
    subject do
      command = Class.new(described_class) do
        def process; end
      end
      command.new
    end

    before { subject.class.banner('banner') }

    it "invokes setup" do
      expect(subject).to receive(:setup)
      expect(subject.call)
    end

    it "sets command's opts" do
      expect { subject.call }.to change { subject.opts }
        .from(nil).to(an_instance_of(Pry::Slop))
    end

    it "sets command's args" do
      expect { subject.call('foo', 'bar') }.to change { subject.args }
        .from(nil).to(%w[foo bar])
    end

    context "when help is invoked" do
      let(:output) { StringIO.new }

      before { subject.output = output }

      it "outputs help info" do
        subject.call('--help')
        expect(subject.output.string)
          .to eq("banner\n    -h, --help      Show this message.\n")
      end

      it "returns void value" do
        expect(subject.call('--help')).to eql(Pry::Command::VOID_VALUE)
      end
    end

    context "when help is not invloved" do
      context "when #process accepts no arguments" do
        subject do
          command = Class.new(described_class) do
            def process; end
          end
          command.new
        end

        it "calls the command despite passed arguments" do
          expect { subject.call('foo') }.not_to raise_error
        end
      end

      context "when #process accepts some arguments" do
        subject do
          command = Class.new(described_class) do
            def process(arg, other); end
          end
          command.new
        end

        it "calls the command even if there's not enough arguments" do
          expect { subject.call('foo') }.not_to raise_error
        end

        it "calls the command even if there are more arguments than needed" do
          expect { subject.call('1', '2', '3') }.not_to raise_error
        end
      end

      context "when passed a variable-length array" do
        subject do
          command = Class.new(described_class) do
            def process(arg, other); end
          end
          command.new
        end

        it "calls the command without arguments" do
          expect { subject.call }.not_to raise_error
        end

        it "calls the command with some arguments" do
          expect { subject.call('1', '2', '3') }.not_to raise_error
        end
      end
    end
  end

  describe "#help" do
    subject { Class.new(described_class).new }

    before { subject.class.banner('banner') }

    it "returns help output" do
      expect(subject.help)
        .to eq("banner\n    -h, --help      Show this message.")
    end
  end

  describe "#slop" do
    subject { Class.new(described_class).new }

    before { subject.class.banner('    banner') }

    it "returns a Slop instance" do
      expect(subject.slop).to be_a(Pry::Slop)
    end

    it "makes Slop's banner unindented" do
      slop = subject.slop
      expect(slop.banner).to eq('banner')
    end

    it "defines the help option" do
      expect(subject.slop.fetch_option(:help)).not_to be_nil
    end

    context "when there are subcommands" do
      subject do
        command = Class.new(described_class) do
          def subcommands(cmd)
            cmd.command(:download)
          end
        end
        command.new
      end

      it "adds subcommands to Slop" do
        expect(subject.slop.fetch_command(:download)).not_to be_nil
      end
    end

    context "when there are options" do
      subject do
        command = Class.new(described_class) do
          def options(opt)
            opt.on(:test)
          end
        end
        command.new
      end

      it "adds subcommands to Slop" do
        expect(subject.slop.fetch_option(:test)).not_to be_nil
      end
    end
  end

  describe "#complete" do
    subject do
      command = Class.new(described_class) do
        def options(opt)
          opt.on(:d, :download)
          opt.on(:u, :upload)
          opt.on(:x)
        end
      end
      command.new
    end

    before { subject.class.banner('') }

    it "generates option completions" do
      expect(subject.complete(''))
        .to match(array_including('--download ', '--upload ', '-x'))
    end
  end

  describe "#process" do
    it "raises CommandError" do
      expect { subject.process }
        .to raise_error(Pry::CommandError, /not implemented/)
    end
  end
end