awesome_print_lite 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,459 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe "Single method" do
4
+ before do
5
+ stub_dotfile!
6
+ end
7
+
8
+ after do
9
+ Object.instance_eval{ remove_const :Hello } if defined?(Hello)
10
+ end
11
+
12
+ it "plain: should handle a method with no arguments" do
13
+ method = ''.method(:upcase)
14
+ expect(method.ai(:plain => true)).to eq('String#upcase()')
15
+ end
16
+
17
+ it "color: should handle a method with no arguments" do
18
+ method = ''.method(:upcase)
19
+ expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35mupcase\e[0m\e[0;37m()\e[0m")
20
+ end
21
+
22
+ it "plain: should handle a method with one argument" do
23
+ method = ''.method(:include?)
24
+ expect(method.ai(:plain => true)).to eq('String#include?(arg1)')
25
+ end
26
+
27
+ it "color: should handle a method with one argument" do
28
+ method = ''.method(:include?)
29
+ expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35minclude?\e[0m\e[0;37m(arg1)\e[0m")
30
+ end
31
+
32
+ it "plain: should handle a method with two arguments" do
33
+ method = ''.method(:tr)
34
+ expect(method.ai(:plain => true)).to eq('String#tr(arg1, arg2)')
35
+ end
36
+
37
+ it "color: should handle a method with two arguments" do
38
+ method = ''.method(:tr)
39
+ expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35mtr\e[0m\e[0;37m(arg1, arg2)\e[0m")
40
+ end
41
+
42
+ it "plain: should handle a method with multiple arguments" do
43
+ method = ''.method(:split)
44
+ expect(method.ai(:plain => true)).to eq('String#split(*arg1)')
45
+ end
46
+
47
+ it "color: should handle a method with multiple arguments" do
48
+ method = ''.method(:split)
49
+ expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35msplit\e[0m\e[0;37m(*arg1)\e[0m")
50
+ end
51
+
52
+ it "plain: should handle a method defined in mixin" do
53
+ method = ''.method(:is_a?)
54
+ expect(method.ai(:plain => true)).to eq('String (Kernel)#is_a?(arg1)')
55
+ end
56
+
57
+ it "color: should handle a method defined in mixin" do
58
+ method = ''.method(:is_a?)
59
+ expect(method.ai).to eq("\e[1;33mString (Kernel)\e[0m#\e[0;35mis_a?\e[0m\e[0;37m(arg1)\e[0m")
60
+ end
61
+
62
+ it "plain: should handle an unbound method" do
63
+ class Hello
64
+ def world; end
65
+ end
66
+ method = Hello.instance_method(:world)
67
+ expect(method.ai(:plain => true)).to eq('Hello (unbound)#world()')
68
+ end
69
+
70
+ it "color: should handle an unbound method" do
71
+ class Hello
72
+ def world(a,b); end
73
+ end
74
+ method = Hello.instance_method(:world)
75
+ if RUBY_VERSION < '1.9.2'
76
+ expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(arg1, arg2)\e[0m")
77
+ else
78
+ expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(a, b)\e[0m")
79
+ end
80
+ end
81
+ end
82
+
83
+ RSpec.describe "Object methods" do
84
+ before do
85
+ stub_dotfile!
86
+ end
87
+
88
+ after do
89
+ Object.instance_eval{ remove_const :Hello } if defined?(Hello)
90
+ end
91
+
92
+ describe "object.methods" do
93
+ it "index: should handle object.methods" do
94
+ out = nil.methods.ai(:plain => true).split("\n").grep(/is_a\?/).first
95
+ expect(out).to match(/^\s+\[\s*\d+\]\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/)
96
+ end
97
+
98
+ it "no index: should handle object.methods" do
99
+ out = nil.methods.ai(:plain => true, :index => false).split("\n").grep(/is_a\?/).first
100
+ expect(out).to match(/^\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/)
101
+ end
102
+ end
103
+
104
+ describe "object.public_methods" do
105
+ xit "index: should handle object.public_methods" do
106
+ out = nil.public_methods.ai(:plain => true).split("\n").grep(/is_a\?/).first
107
+ expect(out).to match(/^\s+\[\s*\d+\]\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/)
108
+ end
109
+
110
+ xit "no index: should handle object.public_methods" do
111
+ out = nil.public_methods.ai(:plain => true, :index => false).split("\n").grep(/is_a\?/).first
112
+ expect(out).to match(/^\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/)
113
+ end
114
+ end
115
+
116
+ describe "object.private_methods" do
117
+ it "index: should handle object.private_methods" do
118
+ out = nil.private_methods.ai(:plain => true).split("\n").grep(/sleep/).first
119
+ expect(out).to match(/^\s+\[\s*\d+\]\s+sleep\(\*arg1\)\s+NilClass \(Kernel\)$/)
120
+ end
121
+
122
+ it "no index: should handle object.private_methods" do
123
+ out = nil.private_methods.ai(:plain => true, :index => false).split("\n").grep(/sleep/).first
124
+ expect(out).to match(/^\s+sleep\(\*arg1\)\s+NilClass \(Kernel\)$/)
125
+ end
126
+ end
127
+
128
+ describe "object.protected_methods" do
129
+ xit "index: should handle object.protected_methods" do
130
+ class Hello
131
+ protected
132
+ def m1; end
133
+ def m2; end
134
+ end
135
+ expect(Hello.new.protected_methods.ai(:plain => true)).to eq("[\n [0] m1() Hello\n [1] m2() Hello\n]")
136
+ end
137
+
138
+ xit "no index: should handle object.protected_methods" do
139
+ class Hello
140
+ protected
141
+ def m3(a,b); end
142
+ end
143
+ if RUBY_VERSION < '1.9.2'
144
+ expect(Hello.new.protected_methods.ai(:plain => true, :index => false)).to eq("[\n m3(arg1, arg2) Hello\n]")
145
+ else
146
+ expect(Hello.new.protected_methods.ai(:plain => true, :index => false)).to eq("[\n m3(a, b) Hello\n]")
147
+ end
148
+ end
149
+ end
150
+
151
+ describe "object.private_methods" do
152
+ it "index: should handle object.private_methods" do
153
+ class Hello
154
+ private
155
+ def m1; end
156
+ def m2; end
157
+ end
158
+
159
+ out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/)
160
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/)
161
+ expect(out.last).to match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello$/)
162
+ end
163
+
164
+ it "no index: should handle object.private_methods" do
165
+ class Hello
166
+ private
167
+ def m3(a,b); end
168
+ end
169
+ out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/)
170
+ if RUBY_VERSION < '1.9.2'
171
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(arg1, arg2\)\s+Hello$/)
172
+ else
173
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(a, b\)\s+Hello$/)
174
+ end
175
+ end
176
+ end
177
+
178
+ describe "object.singleton_methods" do
179
+ xit "index: should handle object.singleton_methods" do
180
+ class Hello
181
+ class << self
182
+ def m1; end
183
+ def m2; end
184
+ end
185
+ end
186
+ out = Hello.singleton_methods.ai(:plain => true).split("\n").grep(/m\d/)
187
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/)
188
+ expect(out.last).to match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello$/)
189
+ end
190
+
191
+ xit "no index: should handle object.singleton_methods" do
192
+ class Hello
193
+ def self.m3(a,b); end
194
+ end
195
+ out = Hello.singleton_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
196
+ if RUBY_VERSION < '1.9.2'
197
+ expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello$/)
198
+ else
199
+ expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello$/)
200
+ end
201
+ end
202
+ end
203
+ end
204
+
205
+ RSpec.describe "Class methods" do
206
+ before do
207
+ stub_dotfile!
208
+ end
209
+
210
+ after do
211
+ Object.instance_eval{ remove_const :Hello } if defined?(Hello)
212
+ end
213
+
214
+ describe "class.instance_methods" do
215
+ it "index: should handle unbound class.instance_methods" do
216
+ class Hello
217
+ def m1; end
218
+ def m2; end
219
+ end
220
+ out = Hello.instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
221
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/)
222
+ expect(out.last).to match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/)
223
+ end
224
+
225
+ it "no index: should handle unbound class.instance_methods" do
226
+ class Hello
227
+ def m3(a,b); end
228
+ end
229
+ out = Hello.instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
230
+ if RUBY_VERSION < '1.9.2'
231
+ expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
232
+ else
233
+ expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
234
+ end
235
+ end
236
+ end
237
+
238
+ describe "class.public_instance_methods" do
239
+ it "index: should handle class.public_instance_methods" do
240
+ class Hello
241
+ def m1; end
242
+ def m2; end
243
+ end
244
+ out = Hello.public_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
245
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/)
246
+ expect(out.last).to match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/)
247
+ end
248
+
249
+ it "no index: should handle class.public_instance_methods" do
250
+ class Hello
251
+ def m3(a,b); end
252
+ end
253
+ out = Hello.public_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
254
+ if RUBY_VERSION < '1.9.2'
255
+ expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
256
+ else
257
+ expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
258
+ end
259
+ end
260
+ end
261
+
262
+ describe "class.protected_instance_methods" do
263
+ xit "index: should handle class.protected_instance_methods" do
264
+ class Hello
265
+ protected
266
+ def m1; end
267
+ def m2; end
268
+ end
269
+ out = Hello.protected_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
270
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/)
271
+ expect(out.last).to match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/)
272
+ end
273
+
274
+ xit "no index: should handle class.protected_instance_methods" do
275
+ class Hello
276
+ protected
277
+ def m3(a,b); end
278
+ end
279
+ out = Hello.protected_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
280
+ if RUBY_VERSION < '1.9.2'
281
+ expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
282
+ else
283
+ expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
284
+ end
285
+ end
286
+ end
287
+
288
+ describe "class.private_instance_methods" do
289
+ it "index: should handle class.private_instance_methods" do
290
+ class Hello
291
+ private
292
+ def m1; end
293
+ def m2; end
294
+ end
295
+ out = Hello.private_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
296
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/)
297
+ expect(out.last).to match(/^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/)
298
+ end
299
+
300
+ it "no index: should handle class.private_instance_methods" do
301
+ class Hello
302
+ private
303
+ def m3(a,b); end
304
+ end
305
+ out = Hello.private_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
306
+ if RUBY_VERSION < '1.9.2'
307
+ expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
308
+ else
309
+ expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
310
+ end
311
+ end
312
+ end
313
+ end
314
+
315
+ if RUBY_VERSION >= '1.9.2'
316
+ RSpec.describe "Ruby 1.9.2+ Method#parameters" do
317
+ before do
318
+ stub_dotfile!
319
+ end
320
+
321
+ after do
322
+ Object.instance_eval{ remove_const :Hello } if defined?(Hello)
323
+ end
324
+
325
+ it "()" do
326
+ class Hello
327
+ def m1; end
328
+ end
329
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
330
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/)
331
+ end
332
+
333
+ it ":req" do
334
+ class Hello
335
+ def m1(a, b, c); end
336
+ end
337
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
338
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, b, c\)\s+Hello$/)
339
+ end
340
+
341
+ it ":opt" do
342
+ class Hello
343
+ def m1(a, b = 1, c = 2); end # m1(a, *b, *c)
344
+ end
345
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
346
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, \*c\)\s+Hello$/)
347
+ end
348
+
349
+ it ":rest" do
350
+ class Hello
351
+ def m1(*a); end # m1(*a)
352
+ end
353
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
354
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\*a\)\s+Hello$/)
355
+ end
356
+
357
+ it ":block" do
358
+ class Hello
359
+ def m1(a, b = nil, &blk); end # m1(a, *b, &blk)
360
+ end
361
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
362
+ expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, &blk\)\s+Hello$/)
363
+ end
364
+ end
365
+ end
366
+
367
+ RSpec.describe "Methods arrays" do
368
+ after do
369
+ Object.instance_eval{ remove_const :Hello } if defined?(Hello)
370
+ Object.instance_eval{ remove_const :World } if defined?(World)
371
+ end
372
+
373
+ it "obj1.methods - obj2.methods should be awesome printed" do
374
+ stub_dotfile!
375
+ class Hello
376
+ def self.m1; end
377
+ end
378
+ out = (Hello.methods - Class.methods).ai(:plain => true)
379
+ expect(out).to eq("[\n [0] m1() Hello\n]")
380
+ end
381
+
382
+ it "obj1.methods & obj2.methods should be awesome printed" do
383
+ stub_dotfile!
384
+ class Hello
385
+ def self.m1; end
386
+ def self.m2; end
387
+ end
388
+ class World
389
+ def self.m1; end
390
+ end
391
+ out = (Hello.methods & World.methods - Class.methods).ai(:plain => true)
392
+ expect(out).to eq("[\n [0] m1() Hello\n]")
393
+ end
394
+
395
+ it "obj1.methods.grep(pattern) should be awesome printed" do
396
+ stub_dotfile!
397
+ class Hello
398
+ def self.m1; end
399
+ def self.m2; end
400
+ def self.m3; end
401
+ end
402
+ out = Hello.methods.grep(/^m1$/).ai(:plain => true)
403
+ expect(out).to eq("[\n [0] m1() Hello\n]")
404
+ out = Hello.methods.grep(/^m\d$/).ai(:plain => true)
405
+ expect(out).to eq("[\n [0] m1() Hello\n [1] m2() Hello\n [2] m3() Hello\n]")
406
+ end
407
+
408
+ it "obj1.methods.grep(pattern, &block) should pass the matching string within the block" do
409
+ class Hello
410
+ def self.m_one; end
411
+ def self.m_two; end
412
+ end
413
+
414
+ out = Hello.methods.sort.grep(/^m_(.+)$/) { $1.to_sym }
415
+ expect(out).to eq([:one, :two])
416
+ end
417
+
418
+ it "obj1.methods.grep(pattern, &block) should be awesome printed" do
419
+ stub_dotfile!
420
+ class Hello
421
+ def self.m0; end
422
+ def self.none; end
423
+ def self.m1; end
424
+ def self.one; end
425
+ end
426
+
427
+ out = Hello.methods.grep(/^m(\d)$/) { %w(none one)[$1.to_i] }.ai(:plain => true)
428
+ expect(out).to eq("[\n [0] none() Hello\n [1] one() Hello\n]")
429
+ end
430
+
431
+ # See https://github.com/michaeldv/awesome_print/issues/30 for details.
432
+ it "grepping methods and converting them to_sym should work as expected" do
433
+ class Hello
434
+ private
435
+ def him; end
436
+
437
+ def his
438
+ private_methods.grep(/^h..$/) { |n| n.to_sym }
439
+ end
440
+
441
+ def her
442
+ private_methods.grep(/^.e.$/) { |n| n.to_sym }
443
+ end
444
+ end
445
+
446
+ hello = Hello.new
447
+ expect((hello.send(:his) - hello.send(:her)).sort_by { |x| x.to_s }).to eq([ :him, :his ])
448
+ end
449
+
450
+ it "appending garbage to methods array should not raise error" do
451
+ arr = 42.methods << [ :wtf ]
452
+ expect { arr.ai(:plain => true) }.not_to raise_error
453
+ if RUBY_VERSION < '1.9.2'
454
+ expect(arr.ai(:plain => true)).to match(/\s+wtf\(\?\)\s+\?/) # [ :wtf ].to_s => "wtf"
455
+ else
456
+ expect(arr.ai(:plain => true)).to match(/\s+\[:wtf\]\(\?\)\s+\?/) # [ :wtf ].to_s => [:wtf]
457
+ end
458
+ end
459
+ end