summaryrefslogtreecommitdiff
path: root/spec/output_spec.rb
blob: f84bf3644688b144acb79f9c1eae48094a9130c7 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# frozen_string_literal: true

RSpec.describe Pry::Output do
  let(:output) { StringIO.new }
  let(:pry_instance) { Pry.new(output: output) }

  subject { described_class.new(pry_instance) }

  describe "#puts" do
    it "returns nil" do
      expect(subject.puts).to be_nil
    end

    context "when given an empty array" do
      it "prints a newline" do
        subject.puts([])
        expect(output.string).to eq("\n")
      end
    end

    context "when given multiple empty arrays" do
      it "prints multiple newline" do
        subject.puts([], [], [])
        expect(output.string).to eq("\n\n\n")
      end
    end

    context "when given convertible to array objects" do
      let(:obj) do
        Object.new.tap { |o| o.define_singleton_method(:to_ary) { [1] } }
      end

      let(:other_obj) do
        Object.new.tap { |o| o.define_singleton_method(:to_ary) { [2] } }
      end

      it "prints the converted objects" do
        subject.puts([obj, other_obj])
        expect(output.string).to eq("1\n2\n")
      end
    end

    context "when given non-convertible to array objects" do
      let(:obj) { Object.new }
      let(:other_obj) { Object.new }

      it "prints the non-converted objects in its string form" do
        subject.puts([obj, other_obj])
        expect(output.string).to match(/\A#<Object:.+>\n#<Object:.+>\n\z/)
      end

      context "and when the object's #to_s has a newline" do
        let(:obj) do
          Object.new.tap { |o| o.define_singleton_method(:to_s) { "abc\n" } }
        end

        it "doesn't print a double newline" do
          subject.puts(obj)
          expect(output.string).to eq("abc\n")
        end
      end
    end

    context "when the given pry instance has 'color' enabled" do
      let(:pry_instance) { Pry.new(output: output, color: true) }

      it "doesn't decolorize output" do
        subject.puts("\e[30mhi\e[0m")
        expect(output.string).to eq("\e[30mhi\e[0m\n")
      end
    end

    context "when the given pry instance has 'color' disabled" do
      let(:pry_instance) { Pry.new(output: output, color: false) }

      it "decolorizes output" do
        subject.puts("\e[30mhi\e[0m")
        expect(output.string).to eq("hi\n")
      end
    end
  end

  describe "#print" do
    it "returns nil" do
      expect(subject.print).to be_nil
    end

    context "when the given pry instance has 'color' enabled" do
      let(:pry_instance) { Pry.new(output: output, color: true) }

      it "doesn't decolorize output" do
        subject.print("\e[30mhi\e[0m")
        expect(output.string).to eq("\e[30mhi\e[0m")
      end
    end

    context "when the given pry instance has 'color' disabled" do
      let(:pry_instance) { Pry.new(output: output, color: false) }

      it "decolorizes output" do
        subject.print("\e[30mhi\e[0m")
        expect(output.string).to eq('hi')
      end
    end
  end

  describe "#<<" do
    specify { expect(subject.method(:<<)).to eq(subject.method(:print)) }
  end

  describe "#write" do
    specify { expect(subject.method(:write)).to eq(subject.method(:print)) }
  end

  describe "#tty?" do
    context "when the output responds to #tty? and is a TTY" do
      before { expect(output).to receive(:tty?).and_return(true) }

      it "returns true" do
        expect(subject).to be_tty
      end
    end

    context "when the output responds to #tty? and is not a TTY" do
      before do
        expect(output).to receive(:respond_to?).with(:tty?).and_return(true)
        expect(output).to receive(:tty?).and_return(false)
      end

      it "returns false" do
        expect(subject).not_to be_tty
      end
    end

    context "when the output doesn't respond to #tty?" do
      before do
        expect(output).to receive(:respond_to?).with(:tty?).and_return(false)
      end

      it "returns false" do
        expect(subject).not_to be_tty
      end
    end
  end

  describe "#method_missing" do
    context "when the output responds to the given method name" do
      it "forwards the method to the output" do
        expect(output).to receive(:abcd)
        subject.abcd
      end
    end

    context "when the output doesn't respond to the given method name" do
      it "raises NoMethodError" do
        expect { subject.abcd }.to raise_error(NoMethodError)
      end
    end
  end

  describe "#respond_to_missing?" do
    context "when the output responds to the given method name" do
      before { output.define_singleton_method(:test_method) {} }

      it "finds the method that is not defined on self" do
        expect(subject).to respond_to(:test_method)
        expect(subject.method(:test_method)).to be_a(Method)
      end
    end

    context "when the output doesn't respond to the given method name" do
      it "doesn't find the method" do
        expect(subject).not_to respond_to(:test_method)
        expect { subject.method(:test_method) }.to raise_error(NameError)
      end
    end
  end

  describe "#decolorize_maybe" do
    context "when the given pry instance has 'color' enabled" do
      let(:pry_instance) { Pry.new(output: output, color: true) }

      it "returns the given string without modifications" do
        str = "\e[30mhi\e[0m"
        expect(subject.decolorize_maybe(str)).to eql(str)
      end
    end

    context "when the given pry instance has 'color' disabled" do
      let(:pry_instance) { Pry.new(output: output, color: false) }

      it "returns decolorized string" do
        expect(subject.decolorize_maybe("\e[30mhi\e[0m")).to eq('hi')
      end
    end
  end

  describe "#size" do
    context "when the output is a tty and responds to winsize" do
      before do
        skip("io/console doesn't support JRuby") if Pry::Helpers::Platform.jruby?
        expect(output).to receive(:tty?).and_return(true)
        expect(output).to receive(:winsize).and_return([1, 1])
      end

      it "returns the io/console winsize" do
        expect(subject.size).to eq([1, 1])
      end
    end

    context "when the output is not a tty" do
      before do
        skip("io/console doesn't support JRuby") if Pry::Helpers::Platform.jruby?
        expect(output).to receive(:tty?).and_return(false)
        allow(Pry::Env).to receive(:[])
      end

      context "and ENV has size info in ROWS and COLUMNS" do
        before do
          expect(Pry::Env).to receive(:[]).with('ROWS').and_return(2)
          expect(Pry::Env).to receive(:[]).with('COLUMNS').and_return(2)
        end

        it "returns the ENV variable winsize" do
          expect(subject.size).to eq([2, 2])
        end
      end

      context "and ENV has size info in LINES and COLUMNS" do
        before do
          expect(Pry::Env).to receive(:[]).with('LINES').and_return(3)
          expect(Pry::Env).to receive(:[]).with('COLUMNS').and_return(2)
        end

        it "returns ENV variable winsize" do
          expect(subject.size).to eq([3, 2])
        end
      end
    end

    context "when the output is not a tty and no info in ENV" do
      let(:readline) { Object.new }

      before do
        unless Pry::Helpers::Platform.jruby?
          expect(output).to receive(:tty?).and_return(false)
        end

        allow(Pry::Env).to receive(:[])

        stub_const('Readline', readline)
      end

      context "when Readline's size has no zeroes" do
        before do
          expect(readline).to receive(:get_screen_size).and_return([1, 1])
        end

        it "returns the Readline winsize" do
          expect(subject.size).to eq([1, 1])
        end
      end

      context "when Readline's size has zero column" do
        before do
          expect(readline).to receive(:get_screen_size).and_return([1, 0])
        end

        it "returns the default size" do
          expect(subject.size).to eq([27, 80])
        end
      end
    end

    context "when the output is not a tty, and no info in ENV and no Readline info" do
      let(:readline) { Object.new }

      before do
        unless Pry::Helpers::Platform.jruby?
          expect(output).to receive(:tty?).and_return(false)
        end

        allow(Pry::Env).to receive(:[])
        stub_const('Readline', readline)
        expect(readline).to receive(:respond_to?)
          .with(:get_screen_size).and_return(false)
      end

      context "and when there's ANSICON ENV variable" do
        context "and when it can be matched" do
          context "and when the size consists of positive integers" do
            before do
              expect(Pry::Env).to receive(:[]).with('ANSICON').and_return('(5x5)')
            end

            it "returns the ansicon winsize" do
              expect(subject.size).to eq([5, 5])
            end
          end

          context "and when the size has a zero column" do
            before do
              expect(Pry::Env).to receive(:[]).with('ANSICON').and_return('(0x0)')
            end

            it "returns the default winsize" do
              expect(subject.size).to eq([27, 80])
            end
          end
        end

        context "and when it cannot be matched" do
          before do
            expect(Pry::Env).to receive(:[]).with('ANSICON').and_return('5x5')
          end

          it "returns the default winsize" do
            expect(subject.size).to eq([27, 80])
          end
        end
      end

      context "and when there's no ANSICON ENV variable" do
        it "returns the default winsize" do
          expect(subject.size).to eq([27, 80])
        end
      end
    end
  end

  describe "#width" do
    let(:readline) { Object.new }

    before do
      unless Pry::Helpers::Platform.jruby?
        expect(output).to receive(:tty?).and_return(false)
      end

      allow(Pry::Env).to receive(:[])
      stub_const('Readline', readline)
      expect(readline).to receive(:respond_to?)
        .with(:get_screen_size).and_return(false)
    end

    it "returns the number of columns" do
      expect(subject.width).to eq(80)
    end
  end

  describe "#height" do
    let(:readline) { Object.new }

    before do
      unless Pry::Helpers::Platform.jruby?
        expect(output).to receive(:tty?).and_return(false)
      end

      allow(Pry::Env).to receive(:[])
      stub_const('Readline', readline)
      expect(readline).to receive(:respond_to?)
        .with(:get_screen_size).and_return(false)
    end

    it "returns the number of rows" do
      expect(subject.height).to eq(27)
    end
  end
end