awesome_print 1.0.2 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -1
- data/Appraisals +52 -0
- data/CHANGELOG.md +152 -0
- data/CONTRIBUTING.md +81 -0
- data/Gemfile +3 -2
- data/Gemfile.lock +44 -13
- data/LICENSE +3 -3
- data/README.md +272 -264
- data/Rakefile +23 -1
- data/lib/ap.rb +1 -1
- data/lib/awesome_print/colorize.rb +24 -0
- data/lib/awesome_print/core_ext/array.rb +12 -2
- data/lib/awesome_print/core_ext/class.rb +1 -1
- data/lib/awesome_print/core_ext/kernel.rb +8 -3
- data/lib/awesome_print/core_ext/logger.rb +1 -1
- data/lib/awesome_print/core_ext/method.rb +1 -1
- data/lib/awesome_print/core_ext/object.rb +1 -1
- data/lib/awesome_print/core_ext/string.rb +1 -1
- data/lib/awesome_print/ext/action_view.rb +1 -1
- data/lib/awesome_print/ext/active_record.rb +8 -2
- data/lib/awesome_print/ext/active_support.rb +2 -2
- data/lib/awesome_print/ext/mongo_mapper.rb +86 -3
- data/lib/awesome_print/ext/mongoid.rb +3 -3
- data/lib/awesome_print/ext/nobrainer.rb +49 -0
- data/lib/awesome_print/ext/nokogiri.rb +1 -1
- data/lib/awesome_print/ext/ostruct.rb +27 -0
- data/lib/awesome_print/ext/ripple.rb +72 -0
- data/lib/awesome_print/ext/sequel.rb +57 -0
- data/lib/awesome_print/formatter.rb +59 -309
- data/lib/awesome_print/formatters/array_formatter.rb +73 -0
- data/lib/awesome_print/formatters/base_formatter.rb +138 -0
- data/lib/awesome_print/formatters/class_formatter.rb +24 -0
- data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
- data/lib/awesome_print/formatters/file_formatter.rb +22 -0
- data/lib/awesome_print/formatters/hash_formatter.rb +54 -0
- data/lib/awesome_print/formatters/method_formatter.rb +22 -0
- data/lib/awesome_print/formatters/object_formatter.rb +80 -0
- data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
- data/lib/awesome_print/indentator.rb +18 -0
- data/lib/awesome_print/inspector.rb +48 -6
- data/lib/awesome_print/version.rb +2 -2
- data/lib/awesome_print.rb +20 -10
- data/spec/active_record_helper.rb +24 -0
- data/spec/colors_spec.rb +10 -10
- data/spec/formats_spec.rb +191 -170
- data/spec/methods_spec.rb +69 -68
- data/spec/misc_spec.rb +250 -0
- data/spec/objects_spec.rb +62 -12
- data/spec/spec_helper.rb +66 -34
- metadata +125 -49
- data/CHANGELOG +0 -77
data/spec/formats_spec.rb
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'spec_helper'
|
|
2
2
|
require "bigdecimal"
|
|
3
3
|
require "rational"
|
|
4
|
+
require "set"
|
|
4
5
|
|
|
5
|
-
describe "AwesomePrint" do
|
|
6
|
+
RSpec.describe "AwesomePrint" do
|
|
6
7
|
before do
|
|
7
8
|
stub_dotfile!
|
|
8
9
|
end
|
|
@@ -13,11 +14,11 @@ describe "AwesomePrint" do
|
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
it "empty array" do
|
|
16
|
-
[].ai.
|
|
17
|
+
expect([].ai).to eq("[]")
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
it "plain multiline" do
|
|
20
|
-
@arr.ai(:plain => true).
|
|
21
|
+
expect(@arr.ai(:plain => true)).to eq <<-EOS.strip
|
|
21
22
|
[
|
|
22
23
|
[0] 1,
|
|
23
24
|
[1] :two,
|
|
@@ -34,7 +35,7 @@ EOS
|
|
|
34
35
|
end
|
|
35
36
|
|
|
36
37
|
it "plain multiline without index" do
|
|
37
|
-
@arr.ai(:plain => true, :index => false).
|
|
38
|
+
expect(@arr.ai(:plain => true, :index => false)).to eq <<-EOS.strip
|
|
38
39
|
[
|
|
39
40
|
1,
|
|
40
41
|
:two,
|
|
@@ -51,7 +52,7 @@ EOS
|
|
|
51
52
|
end
|
|
52
53
|
|
|
53
54
|
it "plain multiline indented" do
|
|
54
|
-
@arr.ai(:plain => true, :indent => 2).
|
|
55
|
+
expect(@arr.ai(:plain => true, :indent => 2)).to eq <<-EOS.strip
|
|
55
56
|
[
|
|
56
57
|
[0] 1,
|
|
57
58
|
[1] :two,
|
|
@@ -68,7 +69,7 @@ EOS
|
|
|
68
69
|
end
|
|
69
70
|
|
|
70
71
|
it "plain multiline indented without index" do
|
|
71
|
-
@arr.ai(:plain => true, :indent => 2, :index => false).
|
|
72
|
+
expect(@arr.ai(:plain => true, :indent => 2, :index => false)).to eq <<-EOS.strip
|
|
72
73
|
[
|
|
73
74
|
1,
|
|
74
75
|
:two,
|
|
@@ -85,11 +86,11 @@ EOS
|
|
|
85
86
|
end
|
|
86
87
|
|
|
87
88
|
it "plain single line" do
|
|
88
|
-
@arr.ai(:plain => true, :multiline => false).
|
|
89
|
+
expect(@arr.ai(:plain => true, :multiline => false)).to eq('[ 1, :two, "three", [ nil, [ true, false ] ] ]')
|
|
89
90
|
end
|
|
90
91
|
|
|
91
92
|
it "colored multiline (default)" do
|
|
92
|
-
@arr.ai.
|
|
93
|
+
expect(@arr.ai).to eq <<-EOS.strip
|
|
93
94
|
[
|
|
94
95
|
\e[1;37m[0] \e[0m\e[1;34m1\e[0m,
|
|
95
96
|
\e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
|
|
@@ -106,7 +107,7 @@ EOS
|
|
|
106
107
|
end
|
|
107
108
|
|
|
108
109
|
it "colored multiline indented" do
|
|
109
|
-
@arr.ai(:indent => 8).
|
|
110
|
+
expect(@arr.ai(:indent => 8)).to eq <<-EOS.strip
|
|
110
111
|
[
|
|
111
112
|
\e[1;37m[0] \e[0m\e[1;34m1\e[0m,
|
|
112
113
|
\e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
|
|
@@ -123,7 +124,7 @@ EOS
|
|
|
123
124
|
end
|
|
124
125
|
|
|
125
126
|
it "colored single line" do
|
|
126
|
-
@arr.ai(:multiline => false).
|
|
127
|
+
expect(@arr.ai(:multiline => false)).to eq("[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]")
|
|
127
128
|
end
|
|
128
129
|
end
|
|
129
130
|
|
|
@@ -135,7 +136,7 @@ EOS
|
|
|
135
136
|
end
|
|
136
137
|
|
|
137
138
|
it "plain multiline" do
|
|
138
|
-
@arr.ai(:plain => true).
|
|
139
|
+
expect(@arr.ai(:plain => true)).to eq <<-EOS.strip
|
|
139
140
|
[
|
|
140
141
|
[0] 1,
|
|
141
142
|
[1] 2,
|
|
@@ -145,7 +146,7 @@ EOS
|
|
|
145
146
|
end
|
|
146
147
|
|
|
147
148
|
it "plain multiline without index" do
|
|
148
|
-
@arr.ai(:plain => true, :index => false).
|
|
149
|
+
expect(@arr.ai(:plain => true, :index => false)).to eq <<-EOS.strip
|
|
149
150
|
[
|
|
150
151
|
1,
|
|
151
152
|
2,
|
|
@@ -155,7 +156,7 @@ EOS
|
|
|
155
156
|
end
|
|
156
157
|
|
|
157
158
|
it "plain single line" do
|
|
158
|
-
@arr.ai(:plain => true, :multiline => false).
|
|
159
|
+
expect(@arr.ai(:plain => true, :multiline => false)).to eq("[ 1, 2, [...] ]")
|
|
159
160
|
end
|
|
160
161
|
end
|
|
161
162
|
|
|
@@ -166,7 +167,7 @@ EOS
|
|
|
166
167
|
end
|
|
167
168
|
|
|
168
169
|
it "plain limited output large" do
|
|
169
|
-
@arr.ai(:plain => true, :limit => true).
|
|
170
|
+
expect(@arr.ai(:plain => true, :limit => true)).to eq <<-EOS.strip
|
|
170
171
|
[
|
|
171
172
|
[ 0] 1,
|
|
172
173
|
[ 1] 2,
|
|
@@ -181,7 +182,7 @@ EOS
|
|
|
181
182
|
|
|
182
183
|
it "plain limited output small" do
|
|
183
184
|
@arr = @arr[0..3]
|
|
184
|
-
@arr.ai(:plain => true, :limit => true).
|
|
185
|
+
expect(@arr.ai(:plain => true, :limit => true)).to eq <<-EOS.strip
|
|
185
186
|
[
|
|
186
187
|
[0] 1,
|
|
187
188
|
[1] 2,
|
|
@@ -192,7 +193,7 @@ EOS
|
|
|
192
193
|
end
|
|
193
194
|
|
|
194
195
|
it "plain limited output with 10 lines" do
|
|
195
|
-
@arr.ai(:plain => true, :limit => 10).
|
|
196
|
+
expect(@arr.ai(:plain => true, :limit => 10)).to eq <<-EOS.strip
|
|
196
197
|
[
|
|
197
198
|
[ 0] 1,
|
|
198
199
|
[ 1] 2,
|
|
@@ -209,7 +210,7 @@ EOS
|
|
|
209
210
|
end
|
|
210
211
|
|
|
211
212
|
it "plain limited output with 11 lines" do
|
|
212
|
-
@arr.ai(:plain => true, :limit => 11).
|
|
213
|
+
expect(@arr.ai(:plain => true, :limit => 11)).to eq <<-EOS.strip
|
|
213
214
|
[
|
|
214
215
|
[ 0] 1,
|
|
215
216
|
[ 1] 2,
|
|
@@ -234,7 +235,7 @@ EOS
|
|
|
234
235
|
end
|
|
235
236
|
|
|
236
237
|
it "plain limited output" do
|
|
237
|
-
@hash.ai(:sort_keys => true, :plain => true, :limit => true).
|
|
238
|
+
expect(@hash.ai(:sort_keys => true, :plain => true, :limit => true)).to eq <<-EOS.strip
|
|
238
239
|
{
|
|
239
240
|
"a" => :a,
|
|
240
241
|
"b" => :b,
|
|
@@ -253,13 +254,13 @@ EOS
|
|
|
253
254
|
before do
|
|
254
255
|
@hash = { 1 => { :sym => { "str" => { [1, 2, 3] => { { :k => :v } => Hash } } } } }
|
|
255
256
|
end
|
|
256
|
-
|
|
257
|
+
|
|
257
258
|
it "empty hash" do
|
|
258
|
-
{}.ai.
|
|
259
|
+
expect({}.ai).to eq("{}")
|
|
259
260
|
end
|
|
260
|
-
|
|
261
|
+
|
|
261
262
|
it "plain multiline" do
|
|
262
|
-
@hash.ai(:plain => true).
|
|
263
|
+
expect(@hash.ai(:plain => true)).to eq <<-EOS.strip
|
|
263
264
|
{
|
|
264
265
|
1 => {
|
|
265
266
|
:sym => {
|
|
@@ -275,7 +276,7 @@ EOS
|
|
|
275
276
|
end
|
|
276
277
|
|
|
277
278
|
it "plain multiline indented" do
|
|
278
|
-
@hash.ai(:plain => true, :indent => 1).
|
|
279
|
+
expect(@hash.ai(:plain => true, :indent => 1)).to eq <<-EOS.strip
|
|
279
280
|
{
|
|
280
281
|
1 => {
|
|
281
282
|
:sym => {
|
|
@@ -291,11 +292,11 @@ EOS
|
|
|
291
292
|
end
|
|
292
293
|
|
|
293
294
|
it "plain single line" do
|
|
294
|
-
@hash.ai(:plain => true, :multiline => false).
|
|
295
|
+
expect(@hash.ai(:plain => true, :multiline => false)).to eq('{ 1 => { :sym => { "str" => { [ 1, 2, 3 ] => { { :k => :v } => Hash < Object } } } } }')
|
|
295
296
|
end
|
|
296
297
|
|
|
297
298
|
it "colored multiline (default)" do
|
|
298
|
-
@hash.ai.
|
|
299
|
+
expect(@hash.ai).to eq <<-EOS.strip
|
|
299
300
|
{
|
|
300
301
|
1\e[0;37m => \e[0m{
|
|
301
302
|
:sym\e[0;37m => \e[0m{
|
|
@@ -311,7 +312,7 @@ EOS
|
|
|
311
312
|
end
|
|
312
313
|
|
|
313
314
|
it "colored multiline indented" do
|
|
314
|
-
@hash.ai(:indent => 2).
|
|
315
|
+
expect(@hash.ai(:indent => 2)).to eq <<-EOS.strip
|
|
315
316
|
{
|
|
316
317
|
1\e[0;37m => \e[0m{
|
|
317
318
|
:sym\e[0;37m => \e[0m{
|
|
@@ -327,7 +328,7 @@ EOS
|
|
|
327
328
|
end
|
|
328
329
|
|
|
329
330
|
it "colored single line" do
|
|
330
|
-
@hash.ai(:multiline => false).
|
|
331
|
+
expect(@hash.ai(:multiline => false)).to eq("{ 1\e[0;37m => \e[0m{ :sym\e[0;37m => \e[0m{ \"str\"\e[0;37m => \e[0m{ [ 1, 2, 3 ]\e[0;37m => \e[0m{ { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m } } } } }")
|
|
331
332
|
end
|
|
332
333
|
|
|
333
334
|
end
|
|
@@ -340,7 +341,7 @@ EOS
|
|
|
340
341
|
end
|
|
341
342
|
|
|
342
343
|
it "plain multiline" do
|
|
343
|
-
@hash.ai(:plain => true).
|
|
344
|
+
expect(@hash.ai(:plain => true)).to eq <<-EOS.strip
|
|
344
345
|
{
|
|
345
346
|
:a => {...}
|
|
346
347
|
}
|
|
@@ -348,7 +349,7 @@ EOS
|
|
|
348
349
|
end
|
|
349
350
|
|
|
350
351
|
it "plain single line" do
|
|
351
|
-
@hash.ai(:plain => true, :multiline => false).
|
|
352
|
+
expect(@hash.ai(:plain => true, :multiline => false)).to eq('{ :a => {...} }')
|
|
352
353
|
end
|
|
353
354
|
end
|
|
354
355
|
|
|
@@ -361,13 +362,13 @@ EOS
|
|
|
361
362
|
it "plain multiline" do
|
|
362
363
|
out = @hash.ai(:plain => true)
|
|
363
364
|
if RUBY_VERSION.to_f < 1.9 # Order of @hash keys is not guaranteed.
|
|
364
|
-
out.
|
|
365
|
-
out.
|
|
366
|
-
out.
|
|
367
|
-
out.
|
|
368
|
-
out.
|
|
365
|
+
expect(out).to match(/^\{[^\}]+\}/m)
|
|
366
|
+
expect(out).to match(/ "b" => "b",?/)
|
|
367
|
+
expect(out).to match(/ :a => "a",?/)
|
|
368
|
+
expect(out).to match(/ :z => "z",?/)
|
|
369
|
+
expect(out).to match(/ "alpha" => "alpha",?$/)
|
|
369
370
|
else
|
|
370
|
-
out.
|
|
371
|
+
expect(out).to eq <<-EOS.strip
|
|
371
372
|
{
|
|
372
373
|
"b" => "b",
|
|
373
374
|
:a => "a",
|
|
@@ -377,9 +378,9 @@ EOS
|
|
|
377
378
|
EOS
|
|
378
379
|
end
|
|
379
380
|
end
|
|
380
|
-
|
|
381
|
+
|
|
381
382
|
it "plain multiline with sorted keys" do
|
|
382
|
-
@hash.ai(:plain => true, :sort_keys => true).
|
|
383
|
+
expect(@hash.ai(:plain => true, :sort_keys => true)).to eq <<-EOS.strip
|
|
383
384
|
{
|
|
384
385
|
:a => "a",
|
|
385
386
|
"alpha" => "alpha",
|
|
@@ -400,7 +401,7 @@ EOS
|
|
|
400
401
|
it "hash keys must be left aligned" do
|
|
401
402
|
hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
|
|
402
403
|
out = hash.ai(:plain => true, :indent => -4, :sort_keys => true)
|
|
403
|
-
out.
|
|
404
|
+
expect(out).to eq <<-EOS.strip
|
|
404
405
|
{
|
|
405
406
|
[ 0, 0, 255 ] => :yellow,
|
|
406
407
|
"magenta" => "rgb(255, 0, 255)",
|
|
@@ -412,7 +413,7 @@ EOS
|
|
|
412
413
|
it "nested hash keys should be indented (array of hashes)" do
|
|
413
414
|
arr = [ { :a => 1, :bb => 22, :ccc => 333}, { 1 => :a, 22 => :bb, 333 => :ccc} ]
|
|
414
415
|
out = arr.ai(:plain => true, :indent => -4, :sort_keys => true)
|
|
415
|
-
out.
|
|
416
|
+
expect(out).to eq <<-EOS.strip
|
|
416
417
|
[
|
|
417
418
|
[0] {
|
|
418
419
|
:a => 1,
|
|
@@ -431,7 +432,7 @@ EOS
|
|
|
431
432
|
it "nested hash keys should be indented (hash of hashes)" do
|
|
432
433
|
arr = { :first => { :a => 1, :bb => 22, :ccc => 333}, :second => { 1 => :a, 22 => :bb, 333 => :ccc} }
|
|
433
434
|
out = arr.ai(:plain => true, :indent => -4, :sort_keys => true)
|
|
434
|
-
out.
|
|
435
|
+
expect(out).to eq <<-EOS.strip
|
|
435
436
|
{
|
|
436
437
|
:first => {
|
|
437
438
|
:a => 1,
|
|
@@ -450,12 +451,12 @@ EOS
|
|
|
450
451
|
|
|
451
452
|
#------------------------------------------------------------------------------
|
|
452
453
|
describe "Class" do
|
|
453
|
-
it "
|
|
454
|
-
self.class.ai(:plain => true).
|
|
454
|
+
it "should show superclass (plain)" do
|
|
455
|
+
expect(self.class.ai(:plain => true)).to eq("#{self.class} < #{self.class.superclass}")
|
|
455
456
|
end
|
|
456
457
|
|
|
457
|
-
it "
|
|
458
|
-
self.class.ai.
|
|
458
|
+
it "should show superclass (color)" do
|
|
459
|
+
expect(self.class.ai).to eq("#{self.class} < #{self.class.superclass}".yellow)
|
|
459
460
|
end
|
|
460
461
|
end
|
|
461
462
|
|
|
@@ -463,7 +464,7 @@ EOS
|
|
|
463
464
|
describe "File" do
|
|
464
465
|
it "should display a file (plain)" do
|
|
465
466
|
File.open(__FILE__, "r") do |f|
|
|
466
|
-
f.ai(:plain => true).
|
|
467
|
+
expect(f.ai(:plain => true)).to eq("#{f.inspect}\n" << `ls -alF #{f.path}`.chop)
|
|
467
468
|
end
|
|
468
469
|
end
|
|
469
470
|
end
|
|
@@ -472,21 +473,31 @@ EOS
|
|
|
472
473
|
describe "Dir" do
|
|
473
474
|
it "should display a direcory (plain)" do
|
|
474
475
|
Dir.open(File.dirname(__FILE__)) do |d|
|
|
475
|
-
d.ai(:plain => true).
|
|
476
|
+
expect(d.ai(:plain => true)).to eq("#{d.inspect}\n" << `ls -alF #{d.path}`.chop)
|
|
476
477
|
end
|
|
477
478
|
end
|
|
478
479
|
end
|
|
479
480
|
|
|
480
481
|
#------------------------------------------------------------------------------
|
|
481
482
|
describe "BigDecimal and Rational" do
|
|
482
|
-
it "should present BigDecimal object
|
|
483
|
-
big = BigDecimal("
|
|
484
|
-
big.ai(:plain => true).
|
|
485
|
-
end
|
|
486
|
-
|
|
487
|
-
it "should present Rational object
|
|
488
|
-
rat = Rational(
|
|
489
|
-
rat.ai(:plain => true)
|
|
483
|
+
it "should present BigDecimal object with arbitrary precision" do
|
|
484
|
+
big = BigDecimal("201020102010201020102010201020102010.4")
|
|
485
|
+
expect(big.ai(:plain => true)).to eq("201020102010201020102010201020102010.4")
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
it "should present Rational object with arbitrary precision" do
|
|
489
|
+
rat = Rational(201020102010201020102010201020102010, 2)
|
|
490
|
+
out = rat.ai(:plain => true)
|
|
491
|
+
#
|
|
492
|
+
# Ruby 1.9 slightly changed the format of Rational#to_s, see
|
|
493
|
+
# http://techtime.getharvest.com/blog/harvest-is-now-on-ruby-1-dot-9-3 and
|
|
494
|
+
# http://www.ruby-forum.com/topic/189397
|
|
495
|
+
#
|
|
496
|
+
if RUBY_VERSION < "1.9"
|
|
497
|
+
expect(out).to eq("100510051005100510051005100510051005")
|
|
498
|
+
else
|
|
499
|
+
expect(out).to eq("100510051005100510051005100510051005/1")
|
|
500
|
+
end
|
|
490
501
|
end
|
|
491
502
|
end
|
|
492
503
|
|
|
@@ -496,11 +507,56 @@ EOS
|
|
|
496
507
|
ap = AwesomePrint::Inspector.new
|
|
497
508
|
ap.send(:merge_options!, { :color => { :array => :black }, :indent => 0 })
|
|
498
509
|
options = ap.instance_variable_get("@options")
|
|
499
|
-
options[:color][:array].
|
|
500
|
-
options[:indent].
|
|
510
|
+
expect(options[:color][:array]).to eq(:black)
|
|
511
|
+
expect(options[:indent]).to eq(0)
|
|
501
512
|
end
|
|
502
513
|
end
|
|
503
514
|
|
|
515
|
+
#------------------------------------------------------------------------------
|
|
516
|
+
describe "Set" do
|
|
517
|
+
before do
|
|
518
|
+
@arr = [1, :two, "three" ]
|
|
519
|
+
@set = Set.new(@arr)
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
it "empty set" do
|
|
523
|
+
expect(Set.new.ai).to eq([].ai)
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
if RUBY_VERSION > "1.9"
|
|
527
|
+
it "plain multiline" do
|
|
528
|
+
expect(@set.ai(:plain => true)).to eq(@arr.ai(:plain => true))
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
it "plain multiline indented" do
|
|
532
|
+
expect(@set.ai(:plain => true, :indent => 1)).to eq(@arr.ai(:plain => true, :indent => 1))
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
it "plain single line" do
|
|
536
|
+
expect(@set.ai(:plain => true, :multiline => false)).to eq(@arr.ai(:plain => true, :multiline => false))
|
|
537
|
+
end
|
|
538
|
+
|
|
539
|
+
it "colored multiline (default)" do
|
|
540
|
+
expect(@set.ai).to eq(@arr.ai)
|
|
541
|
+
end
|
|
542
|
+
else # Prior to Ruby 1.9 the order of set values is unpredicatble.
|
|
543
|
+
it "plain multiline" do
|
|
544
|
+
expect(@set.sort_by{ |x| x.to_s }.ai(:plain => true)).to eq(@arr.sort_by{ |x| x.to_s }.ai(:plain => true))
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
it "plain multiline indented" do
|
|
548
|
+
expect(@set.sort_by{ |x| x.to_s }.ai(:plain => true, :indent => 1)).to eq(@arr.sort_by{ |x| x.to_s }.ai(:plain => true, :indent => 1))
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
it "plain single line" do
|
|
552
|
+
expect(@set.sort_by{ |x| x.to_s }.ai(:plain => true, :multiline => false)).to eq(@arr.sort_by{ |x| x.to_s }.ai(:plain => true, :multiline => false))
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
it "colored multiline (default)" do
|
|
556
|
+
expect(@set.sort_by{ |x| x.to_s }.ai).to eq(@arr.sort_by{ |x| x.to_s }.ai)
|
|
557
|
+
end
|
|
558
|
+
end
|
|
559
|
+
end
|
|
504
560
|
|
|
505
561
|
#------------------------------------------------------------------------------
|
|
506
562
|
describe "Struct" do
|
|
@@ -513,137 +569,51 @@ EOS
|
|
|
513
569
|
@struct.name = "Herman Munster"
|
|
514
570
|
@struct.address = "1313 Mockingbird Lane"
|
|
515
571
|
end
|
|
516
|
-
|
|
572
|
+
|
|
517
573
|
it "empty struct" do
|
|
518
|
-
Struct.new("EmptyStruct").ai.
|
|
574
|
+
expect(Struct.new("EmptyStruct").ai).to eq("\e[1;33mStruct::EmptyStruct < Struct\e[0m")
|
|
519
575
|
end
|
|
520
|
-
|
|
576
|
+
|
|
521
577
|
it "plain multiline" do
|
|
522
578
|
s1 = <<-EOS.strip
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
:name => "Herman Munster"
|
|
526
|
-
}
|
|
579
|
+
address = \"1313 Mockingbird Lane\",
|
|
580
|
+
name = \"Herman Munster\"
|
|
527
581
|
EOS
|
|
528
582
|
s2 = <<-EOS.strip
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
:address => "1313 Mockingbird Lane"
|
|
532
|
-
}
|
|
583
|
+
name = \"Herman Munster\",
|
|
584
|
+
address = \"1313 Mockingbird Lane\"
|
|
533
585
|
EOS
|
|
534
|
-
@struct.ai(:plain => true).
|
|
586
|
+
expect(@struct.ai(:plain => true)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
|
535
587
|
end
|
|
536
588
|
|
|
537
589
|
it "plain multiline indented" do
|
|
538
590
|
s1 = <<-EOS.strip
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
:name => "Herman Munster"
|
|
542
|
-
}
|
|
591
|
+
address = "1313 Mockingbird Lane",
|
|
592
|
+
name = "Herman Munster"
|
|
543
593
|
EOS
|
|
544
594
|
s2 = <<-EOS.strip
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
:address => "1313 Mockingbird Lane"
|
|
548
|
-
}
|
|
595
|
+
name = "Herman Munster",
|
|
596
|
+
address = "1313 Mockingbird Lane"
|
|
549
597
|
EOS
|
|
550
|
-
@struct.ai(:plain => true, :indent => 1).
|
|
598
|
+
expect(@struct.ai(:plain => true, :indent => 1)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
|
551
599
|
end
|
|
552
600
|
|
|
553
601
|
it "plain single line" do
|
|
554
|
-
s1 = "
|
|
555
|
-
s2 = "
|
|
556
|
-
@struct.ai(:plain => true, :multiline => false).
|
|
602
|
+
s1 = "address = \"1313 Mockingbird Lane\", name = \"Herman Munster\""
|
|
603
|
+
s2 = "name = \"Herman Munster\", address = \"1313 Mockingbird Lane\""
|
|
604
|
+
expect(@struct.ai(:plain => true, :multiline => false)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
|
557
605
|
end
|
|
558
606
|
|
|
559
607
|
it "colored multiline (default)" do
|
|
560
608
|
s1 = <<-EOS.strip
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
:name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m
|
|
564
|
-
}
|
|
609
|
+
address\e[0;37m = \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m,
|
|
610
|
+
name\e[0;37m = \e[0m\e[0;33m\"Herman Munster\"\e[0m
|
|
565
611
|
EOS
|
|
566
612
|
s2 = <<-EOS.strip
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
:address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
|
|
570
|
-
}
|
|
613
|
+
name\e[0;37m = \e[0m\e[0;33m\"Herman Munster\"\e[0m,
|
|
614
|
+
address\e[0;37m = \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
|
|
571
615
|
EOS
|
|
572
|
-
@struct.ai.
|
|
573
|
-
end
|
|
574
|
-
end
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
#------------------------------------------------------------------------------
|
|
578
|
-
describe "Misc" do
|
|
579
|
-
it "handle weird objects that return nil on inspect" do
|
|
580
|
-
weird = Class.new do
|
|
581
|
-
def inspect
|
|
582
|
-
nil
|
|
583
|
-
end
|
|
584
|
-
end
|
|
585
|
-
weird.new.ai(:plain => true).should == ''
|
|
586
|
-
end
|
|
587
|
-
|
|
588
|
-
it "handle frozen object.inspect" do
|
|
589
|
-
weird = Class.new do
|
|
590
|
-
def inspect
|
|
591
|
-
"ice".freeze
|
|
592
|
-
end
|
|
593
|
-
end
|
|
594
|
-
weird.new.ai(:plain => false).should == "ice"
|
|
595
|
-
end
|
|
596
|
-
|
|
597
|
-
# See https://github.com/michaeldv/awesome_print/issues/35
|
|
598
|
-
it "handle array grep when pattern contains / chapacter" do
|
|
599
|
-
hash = { "1/x" => 1, "2//x" => :"2" }
|
|
600
|
-
grepped = hash.keys.grep(/^(\d+)\//) { $1 }
|
|
601
|
-
grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]'
|
|
602
|
-
end
|
|
603
|
-
|
|
604
|
-
it "returns value passed as a parameter" do
|
|
605
|
-
object = rand
|
|
606
|
-
self.stub!(:puts)
|
|
607
|
-
(ap object).should == object
|
|
608
|
-
end
|
|
609
|
-
|
|
610
|
-
# Require different file name this time (lib/ap.rb vs. lib/awesome_print).
|
|
611
|
-
it "several require 'awesome_print' should do no harm" do
|
|
612
|
-
require File.expand_path(File.dirname(__FILE__) + '/../lib/ap')
|
|
613
|
-
lambda { rand.ai }.should_not raise_error
|
|
614
|
-
end
|
|
615
|
-
end
|
|
616
|
-
|
|
617
|
-
describe "HTML output" do
|
|
618
|
-
it "wraps ap output with plain <pre> tag" do
|
|
619
|
-
markup = rand
|
|
620
|
-
markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>"
|
|
621
|
-
end
|
|
622
|
-
|
|
623
|
-
it "wraps ap output with <pre> tag with colorized <kbd>" do
|
|
624
|
-
markup = rand
|
|
625
|
-
markup.ai(:html => true).should == %Q|<pre><kbd style="color:blue">#{markup}</kbd></pre>|
|
|
626
|
-
end
|
|
627
|
-
|
|
628
|
-
it "wraps multiline ap output with <pre> tag with colorized <kbd>" do
|
|
629
|
-
markup = [ 1, :two, "three" ]
|
|
630
|
-
markup.ai(:html => true).should == <<-EOS.strip
|
|
631
|
-
<pre>[
|
|
632
|
-
<kbd style="color:white">[0] </kbd><pre><kbd style="color:blue">1</kbd></pre>,
|
|
633
|
-
<kbd style="color:white">[1] </kbd><pre><kbd style="color:darkcyan">:two</kbd></pre>,
|
|
634
|
-
<kbd style="color:white">[2] </kbd><pre><kbd style="color:brown">"three"</kbd></pre>
|
|
635
|
-
]</pre>
|
|
636
|
-
EOS
|
|
637
|
-
end
|
|
638
|
-
|
|
639
|
-
it "encodes HTML entities (plain)" do
|
|
640
|
-
markup = ' &<hello>'
|
|
641
|
-
markup.ai(:html => true, :plain => true).should == '<pre>" &<hello>"</pre>'
|
|
642
|
-
end
|
|
643
|
-
|
|
644
|
-
it "encodes HTML entities (color)" do
|
|
645
|
-
markup = ' &<hello>'
|
|
646
|
-
markup.ai(:html => true).should == '<pre><kbd style="color:brown">" &<hello>"</kbd></pre>'
|
|
616
|
+
expect(@struct.ai).to satisfy { |out| out.include?(s1) || out.include?(s2) }
|
|
647
617
|
end
|
|
648
618
|
end
|
|
649
619
|
|
|
@@ -657,7 +627,7 @@ EOS
|
|
|
657
627
|
class My < Array; end
|
|
658
628
|
|
|
659
629
|
my = My.new([ 1, :two, "three", [ nil, [ true, false ] ] ])
|
|
660
|
-
my.ai(:plain => true).
|
|
630
|
+
expect(my.ai(:plain => true)).to eq <<-EOS.strip
|
|
661
631
|
[
|
|
662
632
|
[0] 1,
|
|
663
633
|
[1] :two,
|
|
@@ -677,7 +647,7 @@ EOS
|
|
|
677
647
|
class My < Hash; end
|
|
678
648
|
|
|
679
649
|
my = My[ { 1 => { :sym => { "str" => { [1, 2, 3] => { { :k => :v } => Hash } } } } } ]
|
|
680
|
-
my.ai(:plain => true).
|
|
650
|
+
expect(my.ai(:plain => true)).to eq <<-EOS.strip
|
|
681
651
|
{
|
|
682
652
|
1 => {
|
|
683
653
|
:sym => {
|
|
@@ -696,7 +666,7 @@ EOS
|
|
|
696
666
|
class My < File; end
|
|
697
667
|
|
|
698
668
|
my = File.new('/dev/null') rescue File.new('nul')
|
|
699
|
-
my.ai(:plain => true).
|
|
669
|
+
expect(my.ai(:plain => true)).to eq("#{my.inspect}\n" << `ls -alF #{my.path}`.chop)
|
|
700
670
|
end
|
|
701
671
|
|
|
702
672
|
it "inherited from Dir should be displayed as Dir" do
|
|
@@ -704,7 +674,7 @@ EOS
|
|
|
704
674
|
|
|
705
675
|
require 'tmpdir'
|
|
706
676
|
my = My.new(Dir.tmpdir)
|
|
707
|
-
my.ai(:plain => true).
|
|
677
|
+
expect(my.ai(:plain => true)).to eq("#{my.inspect}\n" << `ls -alF #{my.path}`.chop)
|
|
708
678
|
end
|
|
709
679
|
|
|
710
680
|
it "should handle a class that defines its own #send method" do
|
|
@@ -713,7 +683,7 @@ EOS
|
|
|
713
683
|
end
|
|
714
684
|
|
|
715
685
|
my = My.new
|
|
716
|
-
my.methods.ai(:plain => true).
|
|
686
|
+
expect { my.methods.ai(:plain => true) }.not_to raise_error
|
|
717
687
|
end
|
|
718
688
|
|
|
719
689
|
it "should handle a class defines its own #method method (ex. request.method)" do
|
|
@@ -724,7 +694,58 @@ EOS
|
|
|
724
694
|
end
|
|
725
695
|
|
|
726
696
|
my = My.new
|
|
727
|
-
my.methods.ai(:plain => true).
|
|
697
|
+
expect { my.methods.ai(:plain => true) }.not_to raise_error
|
|
698
|
+
end
|
|
699
|
+
|
|
700
|
+
describe "should handle a class that defines its own #to_hash method" do
|
|
701
|
+
it "that takes arguments" do
|
|
702
|
+
class My
|
|
703
|
+
def to_hash(a, b)
|
|
704
|
+
end
|
|
705
|
+
end
|
|
706
|
+
|
|
707
|
+
my = My.new
|
|
708
|
+
expect { my.ai(:plain => true) }.not_to raise_error
|
|
709
|
+
end
|
|
710
|
+
|
|
711
|
+
it "that returns nil" do
|
|
712
|
+
class My
|
|
713
|
+
def to_hash()
|
|
714
|
+
return nil
|
|
715
|
+
end
|
|
716
|
+
end
|
|
717
|
+
|
|
718
|
+
my = My.new
|
|
719
|
+
expect { my.ai(:plain => true) }.not_to raise_error
|
|
720
|
+
end
|
|
721
|
+
|
|
722
|
+
it "that returns an object that doesn't support #keys" do
|
|
723
|
+
class My
|
|
724
|
+
def to_hash()
|
|
725
|
+
object = Object.new
|
|
726
|
+
object.define_singleton_method('[]') { return nil }
|
|
727
|
+
|
|
728
|
+
return object
|
|
729
|
+
end
|
|
730
|
+
end
|
|
731
|
+
|
|
732
|
+
my = My.new
|
|
733
|
+
expect { my.ai(:plain => true) }.not_to raise_error
|
|
734
|
+
end
|
|
735
|
+
|
|
736
|
+
it "that returns an object that doesn't support subscripting" do
|
|
737
|
+
class My
|
|
738
|
+
def to_hash()
|
|
739
|
+
object = Object.new
|
|
740
|
+
object.define_singleton_method(:keys) { return [:foo] }
|
|
741
|
+
|
|
742
|
+
return object
|
|
743
|
+
end
|
|
744
|
+
end
|
|
745
|
+
|
|
746
|
+
my = My.new
|
|
747
|
+
expect { my.ai(:plain => true) }.not_to raise_error
|
|
748
|
+
end
|
|
728
749
|
end
|
|
729
750
|
end
|
|
730
751
|
end
|