ryansch-awesome_print 1.0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,458 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ 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
+ method.ai(:plain => true).should == 'String#upcase()'
15
+ end
16
+
17
+ it "color: should handle a method with no arguments" do
18
+ method = ''.method(:upcase)
19
+ method.ai.should == "\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
+ method.ai(:plain => true).should == 'String#include?(arg1)'
25
+ end
26
+
27
+ it "color: should handle a method with one argument" do
28
+ method = ''.method(:include?)
29
+ method.ai.should == "\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
+ method.ai(:plain => true).should == 'String#tr(arg1, arg2)'
35
+ end
36
+
37
+ it "color: should handle a method with two arguments" do
38
+ method = ''.method(:tr)
39
+ method.ai.should == "\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
+ method.ai(:plain => true).should == 'String#split(*arg1)'
45
+ end
46
+
47
+ it "color: should handle a method with multiple arguments" do
48
+ method = ''.method(:split)
49
+ method.ai.should == "\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
+ method.ai(:plain => true).should == '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
+ method.ai.should == "\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
+ method.ai(:plain => true).should == '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
+ method.ai.should == "\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(arg1, arg2)\e[0m"
77
+ else
78
+ method.ai.should == "\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
+ 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
+ out.should =~ /^\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
+ out.should =~ /^\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
101
+ end
102
+ end
103
+
104
+ describe "object.public_methods" do
105
+ it "index: should handle object.public_methods" do
106
+ out = nil.public_methods.ai(:plain => true).split("\n").grep(/is_a\?/).first
107
+ out.should =~ /^\s+\[\s*\d+\]\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
108
+ end
109
+
110
+ it "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
+ out.should =~ /^\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
+ out.should =~ /^\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
+ out.should =~ /^\s+sleep\(\*arg1\)\s+NilClass \(Kernel\)$/
125
+ end
126
+ end
127
+
128
+ describe "object.protected_methods" do
129
+ it "index: should handle object.protected_methods" do
130
+ class Hello
131
+ protected
132
+ def m1; end
133
+ def m2; end
134
+ end
135
+ Hello.new.protected_methods.ai(:plain => true).should == "[\n [0] m1() Hello\n [1] m2() Hello\n]"
136
+ end
137
+
138
+ it "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
+ Hello.new.protected_methods.ai(:plain => true, :index => false).should == "[\n m3(arg1, arg2) Hello\n]"
145
+ else
146
+ Hello.new.protected_methods.ai(:plain => true, :index => false).should == "[\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
+ out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/)
159
+ out.first.should =~ /^\s+\[\d+\]\s+m1\(\)\s+Hello$/
160
+ out.last.should =~ /^\s+\[\d+\]\s+m2\(\)\s+Hello$/
161
+ end
162
+
163
+ it "no index: should handle object.private_methods" do
164
+ class Hello
165
+ private
166
+ def m3(a,b); end
167
+ end
168
+ out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/)
169
+ if RUBY_VERSION < '1.9.2'
170
+ out.first.should =~ /^\s+\[\d+\]\s+m3\(arg1, arg2\)\s+Hello$/
171
+ else
172
+ out.first.should =~ /^\s+\[\d+\]\s+m3\(a, b\)\s+Hello$/
173
+ end
174
+ end
175
+ end
176
+
177
+ describe "object.singleton_methods" do
178
+ it "index: should handle object.singleton_methods" do
179
+ class Hello
180
+ class << self
181
+ def m1; end
182
+ def m2; end
183
+ end
184
+ end
185
+ out = Hello.singleton_methods.ai(:plain => true).split("\n").grep(/m\d/)
186
+ out.first.should =~ /^\s+\[\d+\]\s+m1\(\)\s+Hello$/
187
+ out.last.should =~ /^\s+\[\d+\]\s+m2\(\)\s+Hello$/
188
+ end
189
+
190
+ it "no index: should handle object.singleton_methods" do
191
+ class Hello
192
+ def self.m3(a,b); end
193
+ end
194
+ out = Hello.singleton_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
195
+ if RUBY_VERSION < '1.9.2'
196
+ out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello$/
197
+ else
198
+ out.first.should =~ /^\s+m3\(a, b\)\s+Hello$/
199
+ end
200
+ end
201
+ end
202
+ end
203
+
204
+ describe "Class methods" do
205
+ before do
206
+ stub_dotfile!
207
+ end
208
+
209
+ after do
210
+ Object.instance_eval{ remove_const :Hello } if defined?(Hello)
211
+ end
212
+
213
+ describe "class.instance_methods" do
214
+ it "index: should handle unbound class.instance_methods" do
215
+ class Hello
216
+ def m1; end
217
+ def m2; end
218
+ end
219
+ out = Hello.instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
220
+ out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
221
+ out.last.should =~ /^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
222
+ end
223
+
224
+ it "no index: should handle unbound class.instance_methods" do
225
+ class Hello
226
+ def m3(a,b); end
227
+ end
228
+ out = Hello.instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
229
+ if RUBY_VERSION < '1.9.2'
230
+ out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/
231
+ else
232
+ out.first.should =~ /^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/
233
+ end
234
+ end
235
+ end
236
+
237
+ describe "class.public_instance_methods" do
238
+ it "index: should handle class.public_instance_methods" do
239
+ class Hello
240
+ def m1; end
241
+ def m2; end
242
+ end
243
+ out = Hello.public_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
244
+ out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
245
+ out.last.should =~ /^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
246
+ end
247
+
248
+ it "no index: should handle class.public_instance_methods" do
249
+ class Hello
250
+ def m3(a,b); end
251
+ end
252
+ out = Hello.public_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
253
+ if RUBY_VERSION < '1.9.2'
254
+ out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/
255
+ else
256
+ out.first.should =~ /^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/
257
+ end
258
+ end
259
+ end
260
+
261
+ describe "class.protected_instance_methods" do
262
+ it "index: should handle class.protected_instance_methods" do
263
+ class Hello
264
+ protected
265
+ def m1; end
266
+ def m2; end
267
+ end
268
+ out = Hello.protected_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
269
+ out.first.should =~ /^\s+\[\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
270
+ out.last.should =~ /^\s+\[\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
271
+ end
272
+
273
+ it "no index: should handle class.protected_instance_methods" do
274
+ class Hello
275
+ protected
276
+ def m3(a,b); end
277
+ end
278
+ out = Hello.protected_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
279
+ if RUBY_VERSION < '1.9.2'
280
+ out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/
281
+ else
282
+ out.first.should =~ /^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/
283
+ end
284
+ end
285
+ end
286
+
287
+ describe "class.private_instance_methods" do
288
+ it "index: should handle class.private_instance_methods" do
289
+ class Hello
290
+ private
291
+ def m1; end
292
+ def m2; end
293
+ end
294
+ out = Hello.private_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
295
+ out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
296
+ out.last.should =~ /^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
297
+ end
298
+
299
+ it "no index: should handle class.private_instance_methods" do
300
+ class Hello
301
+ private
302
+ def m3(a,b); end
303
+ end
304
+ out = Hello.private_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
305
+ if RUBY_VERSION < '1.9.2'
306
+ out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/
307
+ else
308
+ out.first.should =~ /^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/
309
+ end
310
+ end
311
+ end
312
+ end
313
+
314
+ if RUBY_VERSION >= '1.9.2'
315
+ describe "Ruby 1.9.2+ Method#parameters" do
316
+ before do
317
+ stub_dotfile!
318
+ end
319
+
320
+ after do
321
+ Object.instance_eval{ remove_const :Hello } if defined?(Hello)
322
+ end
323
+
324
+ it "()" do
325
+ class Hello
326
+ def m1; end
327
+ end
328
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
329
+ out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/
330
+ end
331
+
332
+ it ":req" do
333
+ class Hello
334
+ def m1(a, b, c); end
335
+ end
336
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
337
+ out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(a, b, c\)\s+Hello$/
338
+ end
339
+
340
+ it ":opt" do
341
+ class Hello
342
+ def m1(a, b = 1, c = 2); end # m1(a, *b, *c)
343
+ end
344
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
345
+ out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(a, \*b, \*c\)\s+Hello$/
346
+ end
347
+
348
+ it ":rest" do
349
+ class Hello
350
+ def m1(*a); end # m1(*a)
351
+ end
352
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
353
+ out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\*a\)\s+Hello$/
354
+ end
355
+
356
+ it ":block" do
357
+ class Hello
358
+ def m1(a, b = nil, &blk); end # m1(a, *b, &blk)
359
+ end
360
+ out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
361
+ out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(a, \*b, &blk\)\s+Hello$/
362
+ end
363
+ end
364
+ end
365
+
366
+ describe "Methods arrays" do
367
+ after do
368
+ Object.instance_eval{ remove_const :Hello } if defined?(Hello)
369
+ Object.instance_eval{ remove_const :World } if defined?(World)
370
+ end
371
+
372
+ it "obj1.methods - obj2.methods should be awesome printed" do
373
+ stub_dotfile!
374
+ class Hello
375
+ def self.m1; end
376
+ end
377
+ out = (Hello.methods - Class.methods).ai(:plain => true)
378
+ out.should == "[\n [0] m1() Hello\n]"
379
+ end
380
+
381
+ it "obj1.methods & obj2.methods should be awesome printed" do
382
+ stub_dotfile!
383
+ class Hello
384
+ def self.m1; end
385
+ def self.m2; end
386
+ end
387
+ class World
388
+ def self.m1; end
389
+ end
390
+ out = (Hello.methods & World.methods - Class.methods).ai(:plain => true)
391
+ out.should == "[\n [0] m1() Hello\n]"
392
+ end
393
+
394
+ it "obj1.methods.grep(pattern) should be awesome printed" do
395
+ stub_dotfile!
396
+ class Hello
397
+ def self.m1; end
398
+ def self.m2; end
399
+ def self.m3; end
400
+ end
401
+ out = Hello.methods.grep(/^m1$/).ai(:plain => true)
402
+ out.should == "[\n [0] m1() Hello\n]"
403
+ out = Hello.methods.grep(/^m\d$/).ai(:plain => true)
404
+ out.should == "[\n [0] m1() Hello\n [1] m2() Hello\n [2] m3() Hello\n]"
405
+ end
406
+
407
+ it "obj1.methods.grep(pattern, &block) should pass the matching string within the block" do
408
+ class Hello
409
+ def self.m_one; end
410
+ def self.m_two; end
411
+ end
412
+
413
+ out = Hello.methods.grep(/^m_(.+)$/) { $1.to_sym }
414
+ out.should =~ [:one, :two]
415
+ end
416
+
417
+ it "obj1.methods.grep(pattern, &block) should be awesome printed" do
418
+ stub_dotfile!
419
+ class Hello
420
+ def self.m0; end
421
+ def self.none; end
422
+ def self.m1; end
423
+ def self.one; end
424
+ end
425
+
426
+ out = Hello.methods.grep(/^m(\d)$/) { %w(none one)[$1.to_i] }.ai(:plain => true)
427
+ out.should == "[\n [0] none() Hello\n [1] one() Hello\n]"
428
+ end
429
+
430
+ # See https://github.com/michaeldv/awesome_print/issues/30 for details.
431
+ it "grepping methods and converting them to_sym should work as expected" do
432
+ class Hello
433
+ private
434
+ def him; end
435
+
436
+ def his
437
+ private_methods.grep(/^h..$/) { |n| n.to_sym }
438
+ end
439
+
440
+ def her
441
+ private_methods.grep(/^.e.$/) { |n| n.to_sym }
442
+ end
443
+ end
444
+
445
+ hello = Hello.new
446
+ (hello.send(:his) - hello.send(:her)).sort_by { |x| x.to_s }.should == [ :him, :his ]
447
+ end
448
+
449
+ it "appending garbage to methods array should not raise error" do
450
+ arr = 42.methods << [ :wtf ]
451
+ arr.ai(:plain => true).should_not raise_error(TypeError)
452
+ if RUBY_VERSION < '1.9.2'
453
+ arr.ai(:plain => true).should =~ /\s+wtf\(\?\)\s+\?/ # [ :wtf ].to_s => "wtf"
454
+ else
455
+ arr.ai(:plain => true).should =~ /\s+\[:wtf\]\(\?\)\s+\?/ # [ :wtf ].to_s => [:wtf]
456
+ end
457
+ end
458
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ 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
+ describe "object" do
13
+ it "attributes" do
14
+ class Hello
15
+ attr_reader :abra
16
+ attr_writer :ca
17
+ attr_accessor :dabra
18
+
19
+ def initialize
20
+ @abra, @ca, @dabra = 1, 2, 3
21
+ end
22
+ end
23
+
24
+ out = Hello.new.ai(:plain => true)
25
+ str = <<-EOS.strip
26
+ #<Hello:0x01234567
27
+ attr_accessor :dabra = 3,
28
+ attr_reader :abra = 1,
29
+ attr_writer :ca = 2
30
+ >
31
+ EOS
32
+ out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
33
+ end
34
+
35
+ it "instance variables" do
36
+ class Hello
37
+ def initialize
38
+ @abra, @ca, @dabra = 1, 2, 3
39
+ end
40
+ end
41
+
42
+ out = Hello.new.ai(:plain => true)
43
+ str = <<-EOS.strip
44
+ #<Hello:0x01234567
45
+ @abra = 1,
46
+ @ca = 2,
47
+ @dabra = 3
48
+ >
49
+ EOS
50
+ out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
51
+ end
52
+
53
+ it "attributes and instance variables" do
54
+ class Hello
55
+ attr_reader :abra
56
+ attr_writer :ca
57
+ attr_accessor :dabra
58
+
59
+ def initialize
60
+ @abra, @ca, @dabra = 1, 2, 3
61
+ @scooby, @dooby, @doo = 3, 2, 1
62
+ end
63
+ end
64
+
65
+ out = Hello.new.ai(:plain => true)
66
+ str = <<-EOS.strip
67
+ #<Hello:0x01234567
68
+ @doo = 1,
69
+ @dooby = 2,
70
+ @scooby = 3,
71
+ attr_accessor :dabra = 3,
72
+ attr_reader :abra = 1,
73
+ attr_writer :ca = 2
74
+ >
75
+ EOS
76
+ out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,51 @@
1
+ # Copyright (c) 2010-2011 Michael Dvorkin
2
+ #
3
+ # Awesome Print is freely distributable under the terms of MIT license.
4
+ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
+ #------------------------------------------------------------------------------
6
+ #
7
+ # Running specs from the command line:
8
+ # $ rake spec # Entire spec suite.
9
+ # $ rspec spec/logger_spec.rb # Individual spec file.
10
+ #
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ require 'awesome_print'
14
+
15
+ def stub_dotfile!
16
+ dotfile = File.join(ENV["HOME"], ".aprc")
17
+ File.should_receive(:readable?).at_least(:once).with(dotfile).and_return(false)
18
+ end
19
+
20
+ # The following is needed for the Infinity Test. It runs tests as subprocesses,
21
+ # which sets STDOUT.tty? to false and would otherwise prematurely disallow colors.
22
+ ### AwesomePrint.force_colors!
23
+
24
+ # Ruby 1.8.6 only: define missing String methods that are needed for the specs to pass.
25
+ if RUBY_VERSION < '1.8.7'
26
+ class String
27
+ def shellescape # Taken from Ruby 1.9.2 standard library, see lib/shellwords.rb.
28
+ return "''" if self.empty?
29
+ str = self.dup
30
+ str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
31
+ str.gsub!(/\n/, "'\n'")
32
+ str
33
+ end
34
+
35
+ def start_with?(*prefixes)
36
+ prefixes.each do |prefix|
37
+ prefix = prefix.to_s
38
+ return true if prefix == self[0, prefix.size]
39
+ end
40
+ false
41
+ end
42
+
43
+ def end_with?(*suffixes)
44
+ suffixes.each do |suffix|
45
+ suffix = suffix.to_s
46
+ return true if suffix == self[-suffix.size, suffix.size]
47
+ end
48
+ false
49
+ end
50
+ end
51
+ end