File: methods_spec.rb

package info (click to toggle)
ruby-awesome-print 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 652 kB
  • sloc: ruby: 3,827; makefile: 6
file content (455 lines) | stat: -rw-r--r-- 14,620 bytes parent folder | download | duplicates (3)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
require 'spec_helper'

RSpec.describe 'Single method' do
  after do
    Object.instance_eval { remove_const :Hello } if defined?(Hello)
  end

  it 'plain: should handle a method with no arguments' do
    method = ''.method(:upcase)
    if RUBY_VERSION >= '2.4.0'
      expect(method.ai(plain: true)).to eq('String#upcase(*arg1)')
    else
      expect(method.ai(plain: true)).to eq('String#upcase()')
    end
  end

  it 'color: should handle a method with no arguments' do
    method = ''.method(:upcase)
    if RUBY_VERSION >= '2.4.0'
      expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35mupcase\e[0m\e[0;37m(*arg1)\e[0m")
    else
      expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35mupcase\e[0m\e[0;37m()\e[0m")
    end
  end

  it 'plain: should handle a method with one argument' do
    method = ''.method(:include?)
    expect(method.ai(plain: true)).to eq('String#include?(arg1)')
  end

  it 'color: should handle a method with one argument' do
    method = ''.method(:include?)
    expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35minclude?\e[0m\e[0;37m(arg1)\e[0m")
  end

  it 'plain: should handle a method with two arguments' do
    method = ''.method(:tr)
    expect(method.ai(plain: true)).to eq('String#tr(arg1, arg2)')
  end

  it 'color: should handle a method with two arguments' do
    method = ''.method(:tr)
    expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35mtr\e[0m\e[0;37m(arg1, arg2)\e[0m")
  end

  it 'plain: should handle a method with multiple arguments' do
    method = ''.method(:split)
    expect(method.ai(plain: true)).to eq('String#split(*arg1)')
  end

  it 'color: should handle a method with multiple arguments' do
    method = ''.method(:split)
    expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35msplit\e[0m\e[0;37m(*arg1)\e[0m")
  end

  it 'plain: should handle a method defined in mixin' do
    method = ''.method(:is_a?)
    expect(method.ai(plain: true)).to eq('String (Kernel)#is_a?(arg1)')
  end

  it 'color: should handle a method defined in mixin' do
    method = ''.method(:is_a?)
    expect(method.ai).to eq("\e[1;33mString (Kernel)\e[0m#\e[0;35mis_a?\e[0m\e[0;37m(arg1)\e[0m")
  end

  it 'plain: should handle an unbound method' do
    class Hello
      def world; end
    end
    method = Hello.instance_method(:world)
    expect(method.ai(plain: true)).to eq('Hello (unbound)#world()')
  end

  it 'color: should handle an unbound method' do
    class Hello
      def world(a, b); end
    end
    method = Hello.instance_method(:world)
    if RUBY_VERSION < '1.9.2'
      expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(arg1, arg2)\e[0m")
    else
      expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(a, b)\e[0m")
    end
  end
end

RSpec.describe 'Object methods' do
  after do
    Object.instance_eval { remove_const :Hello } if defined?(Hello)
  end

  describe 'object.methods' do
    it 'index: should handle object.methods' do
      out = nil.methods.ai(plain: true).split("\n").grep(/is_a\?/).first
      expect(out).to match(/^\s+\[\s*\d+\]\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/)
    end

    it 'no index: should handle object.methods' do
      out = nil.methods.ai(plain: true, index: false).split("\n").grep(/is_a\?/).first
      expect(out).to match(/^\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/)
    end
  end

  describe 'object.public_methods' do
    it 'index: should handle object.public_methods' do
      out = nil.public_methods.ai(plain: true).split("\n").grep(/is_a\?/).first
      expect(out).to match(/^\s+\[\s*\d+\]\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/)
    end

    it 'no index: should handle object.public_methods' do
      out = nil.public_methods.ai(plain: true, index: false).split("\n").grep(/is_a\?/).first
      expect(out).to match(/^\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/)
    end
  end

  describe 'object.private_methods' do
    it 'index: should handle object.private_methods' do
      out = nil.private_methods.ai(plain: true).split("\n").grep(/sleep/).first
      expect(out).to match(/^\s+\[\s*\d+\]\s+sleep\(\*arg1\)\s+NilClass \(Kernel\)$/)
    end

    it 'no index: should handle object.private_methods' do
      out = nil.private_methods.ai(plain: true, index: false).split("\n").grep(/sleep/).first
      expect(out).to match(/^\s+sleep\(\*arg1\)\s+NilClass \(Kernel\)$/)
    end
  end

  describe 'object.protected_methods' do
    it 'index: should handle object.protected_methods' do
      class Hello
        protected
        def m1; end
        def m2; end
      end
      expect(Hello.new.protected_methods.ai(plain: true)).to eq("[\n    [0] m1() Hello\n    [1] m2() Hello\n]")
    end

    it 'no index: should handle object.protected_methods' do
      class Hello
        protected
        def m3(a, b); end
      end
      if RUBY_VERSION < '1.9.2'
        expect(Hello.new.protected_methods.ai(plain: true, index: false)).to eq("[\n     m3(arg1, arg2) Hello\n]")
      else
        expect(Hello.new.protected_methods.ai(plain: true, index: false)).to eq("[\n     m3(a, b) Hello\n]")
      end
    end
  end

  describe 'object.private_methods' do
    it 'index: should handle object.private_methods' do
      class Hello
        private
        def m1; end
        def m2; end
      end

      out = Hello.new.private_methods.ai(plain: true).split("\n").grep(/m\d/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/)
      expect(out.last).to  match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello$/)
    end

    it 'no index: should handle object.private_methods' do
      class Hello
        private
        def m3(a, b); end
      end
      out = Hello.new.private_methods.ai(plain: true).split("\n").grep(/m\d/)
      if RUBY_VERSION < '1.9.2'
        expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(arg1, arg2\)\s+Hello$/)
      else
        expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(a, b\)\s+Hello$/)
      end
    end
  end

  describe 'object.singleton_methods' do
    it 'index: should handle object.singleton_methods' do
      class Hello
        class << self
          def m1; end
          def m2; end
        end
      end
      out = Hello.singleton_methods.ai(plain: true).split("\n").grep(/m\d/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/)
      expect(out.last).to  match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello$/)
    end

    it 'no index: should handle object.singleton_methods' do
      class Hello
        def self.m3(a, b); end
      end
      out = Hello.singleton_methods.ai(plain: true, index: false).split("\n").grep(/m\d/)
      if RUBY_VERSION < '1.9.2'
        expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello$/)
      else
        expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello$/)
      end
    end
  end
end

RSpec.describe 'Class methods' do
  after do
    Object.instance_eval { remove_const :Hello } if defined?(Hello)
  end

  describe 'class.instance_methods' do
    it 'index: should handle unbound class.instance_methods' do
      class Hello
        def m1; end
        def m2; end
      end
      out = Hello.instance_methods.ai(plain: true).split("\n").grep(/m\d/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/)
      expect(out.last).to  match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/)
    end

    it 'no index: should handle unbound class.instance_methods' do
      class Hello
        def m3(a, b); end
      end
      out = Hello.instance_methods.ai(plain: true, index: false).split("\n").grep(/m\d/)
      if RUBY_VERSION < '1.9.2'
        expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
      else
        expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
      end
    end
  end

  describe 'class.public_instance_methods' do
    it 'index: should handle class.public_instance_methods' do
      class Hello
        def m1; end
        def m2; end
      end
      out = Hello.public_instance_methods.ai(plain: true).split("\n").grep(/m\d/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/)
      expect(out.last).to  match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/)
    end

    it 'no index: should handle class.public_instance_methods' do
      class Hello
        def m3(a, b); end
      end
      out = Hello.public_instance_methods.ai(plain: true, index: false).split("\n").grep(/m\d/)
      if RUBY_VERSION < '1.9.2'
        expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
      else
        expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
      end
    end
  end

  describe 'class.protected_instance_methods' do
    it 'index: should handle class.protected_instance_methods' do
      class Hello
        protected
        def m1; end
        def m2; end
      end
      out = Hello.protected_instance_methods.ai(plain: true).split("\n").grep(/m\d/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/)
      expect(out.last).to  match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/)
    end

    it 'no index: should handle class.protected_instance_methods' do
      class Hello
        protected
        def m3(a, b); end
      end
      out = Hello.protected_instance_methods.ai(plain: true, index: false).split("\n").grep(/m\d/)
      if RUBY_VERSION < '1.9.2'
        expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
      else
        expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
      end
    end
  end

  describe 'class.private_instance_methods' do
    it 'index: should handle class.private_instance_methods' do
      class Hello
        private
        def m1; end
        def m2; end
      end
      out = Hello.private_instance_methods.ai(plain: true).split("\n").grep(/m\d/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/)
      expect(out.last).to  match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/)
    end

    it 'no index: should handle class.private_instance_methods' do
      class Hello
        private
        def m3(a, b); end
      end
      out = Hello.private_instance_methods.ai(plain: true, index: false).split("\n").grep(/m\d/)
      if RUBY_VERSION < '1.9.2'
        expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
      else
        expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
      end
    end
  end
end

if RUBY_VERSION >= '1.9.2'
  RSpec.describe 'Ruby 1.9.2+ Method#parameters' do
    before do
      stub_dotfile!
    end

    after do
      Object.instance_eval { remove_const :Hello } if defined?(Hello)
    end

    it '()' do
      class Hello
        def m1; end
      end
      out = Hello.new.methods.ai(plain: true).split("\n").grep(/m1/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/)
    end

    it ':req' do
      class Hello
        def m1(a, b, c); end
      end
      out = Hello.new.methods.ai(plain: true).split("\n").grep(/m1/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, b, c\)\s+Hello$/)
    end

    it ':opt' do
      class Hello
        def m1(a, b = 1, c = 2); end # m1(a, *b, *c)
      end
      out = Hello.new.methods.ai(plain: true).split("\n").grep(/m1/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, \*c\)\s+Hello$/)
    end

    it ':rest' do
      class Hello
        def m1(*a); end # m1(*a)
      end
      out = Hello.new.methods.ai(plain: true).split("\n").grep(/m1/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\*a\)\s+Hello$/)
    end

    it ':block' do
      class Hello
        def m1(a, b = nil, &blk); end # m1(a, *b, &blk)
      end
      out = Hello.new.methods.ai(plain: true).split("\n").grep(/m1/)
      expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, &blk\)\s+Hello$/)
    end
  end
end

RSpec.describe 'Methods arrays' do
  after do
    Object.instance_eval { remove_const :Hello } if defined?(Hello)
    Object.instance_eval { remove_const :World } if defined?(World)
  end

  it 'obj1.methods - obj2.methods should be awesome printed' do
    stub_dotfile!
    class Hello
      def self.m1; end
    end
    out = (Hello.methods - Class.methods).ai(plain: true)
    expect(out).to eq("[\n    [0] m1() Hello\n]")
  end

  it 'obj1.methods & obj2.methods should be awesome printed' do
    stub_dotfile!
    class Hello
      def self.m1; end
      def self.m2; end
    end
    class World
      def self.m1; end
    end
    out = (Hello.methods & World.methods - Class.methods).ai(plain: true)
    expect(out).to eq("[\n    [0] m1() Hello\n]")
  end

  it 'obj1.methods.grep(pattern) should be awesome printed' do
    stub_dotfile!
    class Hello
      def self.m1; end
      def self.m2; end
      def self.m3; end
    end
    out = Hello.methods.grep(/^m1$/).ai(plain: true)
    expect(out).to eq("[\n    [0] m1() Hello\n]")
    out = Hello.methods.grep(/^m\d$/).ai(plain: true)
    expect(out).to eq("[\n    [0] m1() Hello\n    [1] m2() Hello\n    [2] m3() Hello\n]")
  end

  it 'obj1.methods.grep(pattern, &block) should pass the matching string within the block' do
    class Hello
      def self.m_one; end
      def self.m_two; end
    end

    out = Hello.methods.sort.grep(/^m_(.+)$/) { $1.to_sym }
    expect(out).to eq([:one, :two])
  end

  it 'obj1.methods.grep(pattern, &block) should be awesome printed' do
    stub_dotfile!
    class Hello
      def self.m0; end
      def self.none; end
      def self.m1; end
      def self.one; end
    end

    out = Hello.methods.grep(/^m(\d)$/) { %w(none one)[$1.to_i] }.ai(plain: true)
    expect(out).to eq("[\n    [0] none() Hello\n    [1]  one() Hello\n]")
  end

  # See https://github.com/awesome-print/awesome_print/issues/30 for details.
  it 'grepping methods and converting them to_sym should work as expected' do
    class Hello
      private
      def him; end

      def his
        private_methods.grep(/^h..$/) { |n| n.to_sym }
      end

      def her
        private_methods.grep(/^.e.$/) { |n| n.to_sym }
      end
    end

    hello = Hello.new
    expect((hello.send(:his) - hello.send(:her)).sort_by { |x| x.to_s }).to eq([:him, :his])
  end

  it 'appending garbage to methods array should not raise error' do
    arr = 42.methods << [:wtf]
    expect { arr.ai(plain: true) }.not_to raise_error
    if RUBY_VERSION < '1.9.2'
      expect(arr.ai(plain: true)).to match(/\s+wtf\(\?\)\s+\?/)      # [ :wtf ].to_s => "wtf"
    else
      expect(arr.ai(plain: true)).to match(/\s+\[:wtf\]\(\?\)\s+\?/) # [ :wtf ].to_s => [:wtf]
    end
  end
end