awesome_print 0.2.1 → 0.4.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.
@@ -30,6 +30,23 @@ describe "AwesomePrint" do
30
30
  ]
31
31
  ]
32
32
  ]
33
+ EOS
34
+ end
35
+
36
+ it "plain multiline without index" do
37
+ @arr.ai(:plain => true, :index => false).should == <<-EOS.strip
38
+ [
39
+ 1,
40
+ :two,
41
+ "three",
42
+ [
43
+ nil,
44
+ [
45
+ true,
46
+ false
47
+ ]
48
+ ]
49
+ ]
33
50
  EOS
34
51
  end
35
52
 
@@ -50,6 +67,23 @@ EOS
50
67
  EOS
51
68
  end
52
69
 
70
+ it "plain multiline indented without index" do
71
+ @arr.ai(:plain => true, :indent => 2, :index => false).should == <<-EOS.strip
72
+ [
73
+ 1,
74
+ :two,
75
+ "three",
76
+ [
77
+ nil,
78
+ [
79
+ true,
80
+ false
81
+ ]
82
+ ]
83
+ ]
84
+ EOS
85
+ end
86
+
53
87
  it "plain single line" do
54
88
  @arr.ai(:plain => true, :multiline => false).should == '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
55
89
  end
@@ -110,6 +144,16 @@ EOS
110
144
  EOS
111
145
  end
112
146
 
147
+ it "plain multiline without index" do
148
+ @arr.ai(:plain => true, :index => false).should == <<-EOS.strip
149
+ [
150
+ 1,
151
+ 2,
152
+ [...]
153
+ ]
154
+ EOS
155
+ end
156
+
113
157
  it "plain single line" do
114
158
  @arr.ai(:plain => true, :multiline => false).should == "[ 1, 2, [...] ]"
115
159
  end
@@ -219,6 +263,45 @@ EOS
219
263
  end
220
264
  end
221
265
 
266
+ #------------------------------------------------------------------------------
267
+ describe "Hash with several keys" do
268
+ before(:each) do
269
+ @hash = {"b" => "b", :a => "a", :z => "z", "alpha" => "alpha"}
270
+ end
271
+
272
+ it "plain multiline" do
273
+ out = @hash.ai(:plain => true)
274
+ if RUBY_VERSION.to_f < 1.9 # Order of @hash keys is not guaranteed.
275
+ out.should =~ /^\{[^\}]+\}/m
276
+ out.should =~ / "b" => "b",?/
277
+ out.should =~ / :a => "a",?/
278
+ out.should =~ / :z => "z",?/
279
+ out.should =~ / "alpha" => "alpha",?$/
280
+ else
281
+ out.should == <<-EOS.strip
282
+ {
283
+ "b" => "b",
284
+ :a => "a",
285
+ :z => "z",
286
+ "alpha" => "alpha"
287
+ }
288
+ EOS
289
+ end
290
+ end
291
+
292
+ it "plain multiline with sorted keys" do
293
+ @hash.ai(:plain => true, :sorted_hash_keys => true).should == <<-EOS.strip
294
+ {
295
+ :a => "a",
296
+ "alpha" => "alpha",
297
+ "b" => "b",
298
+ :z => "z"
299
+ }
300
+ EOS
301
+ end
302
+
303
+ end
304
+
222
305
  #------------------------------------------------------------------------------
223
306
  describe "Negative options[:indent]" do
224
307
  before(:each) do
@@ -361,4 +444,137 @@ EOS
361
444
  end
362
445
 
363
446
 
447
+ #------------------------------------------------------------------------------
448
+ describe "Misc" do
449
+ it "handle weird objects that return nil on inspect" do
450
+ weird = Class.new do
451
+ def inspect
452
+ nil
453
+ end
454
+ end
455
+ weird.new.ai(:plain => true).should == ''
456
+ end
457
+
458
+ # See https://github.com/michaeldv/awesome_print/issues/35
459
+ it "handle array grep when pattern contains / chapacter" do
460
+ hash = { "1/x" => 1, "2//x" => :"2" }
461
+ grepped = hash.keys.grep(/^(\d+)\//) { $1 }
462
+ grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]'
463
+ end
464
+
465
+ it "returns value passed as a parameter" do
466
+ object = rand
467
+ self.stub!(:puts)
468
+ (ap object).should == object
469
+ end
470
+
471
+ # Require different file name this time (lib/ap.rb vs. lib/awesome_print).
472
+ it "several require 'awesome_print' should do no harm" do
473
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/ap')
474
+ lambda { rand.ai }.should_not raise_error
475
+ end
476
+ end
477
+
478
+ describe "HTML output" do
479
+ it "wraps ap output with plain <pre> tag" do
480
+ markup = rand
481
+ markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>"
482
+ end
483
+
484
+ it "wraps ap output with colorized <pre> tag" do
485
+ markup = rand
486
+ markup.ai(:html => true).should == %Q|<pre style="color:blue">#{markup}</pre>|
487
+ end
488
+
489
+ it "encodes HTML entities (plain)" do
490
+ markup = ' &<hello>'
491
+ markup.ai(:html => true, :plain => true).should == '<pre>&quot; &amp;&lt;hello&gt;&quot;</pre>'
492
+ end
493
+
494
+ it "encodes HTML entities (color)" do
495
+ markup = ' &<hello>'
496
+ markup.ai(:html => true).should == '<pre style="color:brown">&quot; &amp;&lt;hello&gt;&quot;</pre>'
497
+ end
498
+ end
499
+
500
+ #------------------------------------------------------------------------------
501
+ describe "Inherited from standard Ruby classes" do
502
+ after do
503
+ Object.instance_eval{ remove_const :My } if defined?(My)
504
+ end
505
+
506
+ it "inherited from Array should be displayed as Array" do
507
+ class My < Array; end
508
+
509
+ my = My.new([ 1, :two, "three", [ nil, [ true, false ] ] ])
510
+ my.ai(:plain => true).should == <<-EOS.strip
511
+ [
512
+ [0] 1,
513
+ [1] :two,
514
+ [2] "three",
515
+ [3] [
516
+ [0] nil,
517
+ [1] [
518
+ [0] true,
519
+ [1] false
520
+ ]
521
+ ]
522
+ ]
523
+ EOS
524
+ end
525
+
526
+ it "inherited from Hash should be displayed as Hash" do
527
+ class My < Hash; end
528
+
529
+ my = My[ { 1 => { :sym => { "str" => { [1, 2, 3] => { { :k => :v } => Hash } } } } } ]
530
+ my.ai(:plain => true).should == <<-EOS.strip
531
+ {
532
+ 1 => {
533
+ :sym => {
534
+ "str" => {
535
+ [ 1, 2, 3 ] => {
536
+ { :k => :v } => Hash < Object
537
+ }
538
+ }
539
+ }
540
+ }
541
+ }
542
+ EOS
543
+ end
544
+
545
+ it "inherited from File should be displayed as File" do
546
+ class My < File; end
547
+
548
+ my = File.new('/dev/null') rescue File.new('nul')
549
+ my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
550
+ end
551
+
552
+ it "inherited from Dir should be displayed as Dir" do
553
+ class My < Dir; end
554
+
555
+ require 'tmpdir'
556
+ my = My.new(Dir.tmpdir)
557
+ my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
558
+ end
559
+
560
+ it "should handle a class that defines its own #send method" do
561
+ class My
562
+ def send(arg1, arg2, arg3); end
563
+ end
564
+
565
+ my = My.new
566
+ my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
567
+ end
568
+
569
+ it "should handle a class defines its own #method method (ex. request.method)" do
570
+ class My
571
+ def method
572
+ 'POST'
573
+ end
574
+ end
575
+
576
+ my = My.new
577
+ my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
578
+ end
579
+ end
364
580
  end
@@ -0,0 +1,84 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AwesomePrint" do
4
+ before(:each) do
5
+ stub_dotfile!
6
+ end
7
+
8
+ describe "colorization" do
9
+ PLAIN = '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
10
+ COLORIZED = "[ \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 ] ] ]"
11
+
12
+ before(:each) do
13
+ AwesomePrint.force_colors!(false)
14
+ ENV['TERM'] = "xterm-colors"
15
+ ENV.delete('ANSICON')
16
+ @arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
17
+ end
18
+
19
+ it "colorizes tty processes by default" do
20
+ stub_tty!(STDOUT, true)
21
+
22
+ @arr.ai(:multiline => false).should == COLORIZED
23
+ end
24
+
25
+ it "colorizes tty processes by default" do
26
+ stub_tty!(STDOUT, true)
27
+
28
+ @arr.ai(:multiline => false).should == COLORIZED
29
+ end
30
+
31
+
32
+ it "colorizes processes with ENV['ANSICON'] by default" do
33
+ stub_tty!(STDOUT, true)
34
+ ENV['ANSICON'] = "1"
35
+
36
+ @arr.ai(:multiline => false).should == COLORIZED
37
+ end
38
+
39
+ it "does not colorize tty processes running in dumb terminals by default" do
40
+ stub_tty!(STDOUT, true)
41
+ ENV['TERM'] = "dumb"
42
+
43
+ @arr.ai(:multiline => false).should == PLAIN
44
+ end
45
+
46
+ it "does not colorize subprocesses by default" do
47
+ stub_tty!(STDOUT, false)
48
+
49
+ @arr.ai(:multiline => false).should == PLAIN
50
+ end
51
+
52
+ describe "forced" do
53
+ before(:each) do
54
+ AwesomePrint.force_colors!
55
+ end
56
+
57
+ it "still colorizes tty processes" do
58
+ stub_tty!(STDOUT, true)
59
+
60
+ @arr.ai(:multiline => false).should == COLORIZED
61
+ end
62
+
63
+ it "colorizes dumb terminals" do
64
+ stub_tty!(STDOUT, true)
65
+ ENV["TERM"] = "dumb"
66
+
67
+ @arr.ai(:multiline => false).should == COLORIZED
68
+ end
69
+
70
+ it "colorizes subprocess" do
71
+ stub_tty!(STDOUT, true)
72
+ @arr.ai(:multiline => false).should == COLORIZED
73
+ end
74
+ end
75
+ end
76
+
77
+ def stub_tty!(stream, value)
78
+ eval(%{class << stream
79
+ def tty?
80
+ #{value}
81
+ end
82
+ end})
83
+ end
84
+ end
data/spec/logger_spec.rb CHANGED
@@ -6,7 +6,7 @@ require 'ap/core_ext/logger'
6
6
 
7
7
  describe "AwesomePrint logging extensions" do
8
8
  before(:all) do
9
- @logger = Logger.new('/dev/null')
9
+ @logger = Logger.new('/dev/null') rescue Logger.new('nul')
10
10
  end
11
11
 
12
12
  describe "ap method" do