awesome_print 0.3.0 → 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.
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010 Michael Dvorkin
1
+ # Copyright (c) 2010-2011 Michael Dvorkin
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -14,6 +14,8 @@ module AwesomePrintActiveSupport
14
14
  #------------------------------------------------------------------------------
15
15
  def printable_with_active_support(object)
16
16
  printable = printable_without_active_support(object)
17
+ return printable if !defined?(ActiveSupport::TimeWithZone) || !defined?(HashWithIndifferentAccess)
18
+
17
19
  if printable == :self
18
20
  if object.is_a?(ActiveSupport::TimeWithZone)
19
21
  printable = :active_support_time
@@ -0,0 +1,54 @@
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
+ module AwesomePrintMongoMapper
7
+
8
+ def self.included(base)
9
+ base.send :alias_method, :printable_without_mongo_mapper, :printable
10
+ base.send :alias_method, :printable, :printable_with_mongo_mapper
11
+ end
12
+
13
+ # Add MongoMapper class names to the dispatcher pipeline.
14
+ #------------------------------------------------------------------------------
15
+ def printable_with_mongo_mapper(object)
16
+ printable = printable_without_mongo_mapper(object)
17
+ return printable if !defined?(MongoMapper::Document)
18
+
19
+ if printable == :self
20
+ if object.is_a?(MongoMapper::Document) || object.is_a?(MongoMapper::EmbeddedDocument)
21
+ printable = :mongo_mapper_instance
22
+ end
23
+ elsif printable == :class && (object.ancestors & [MongoMapper::Document, MongoMapper::EmbeddedDocument]).size > 0
24
+ printable = :mongo_mapper_class
25
+ end
26
+ printable
27
+ end
28
+
29
+ # Format MongoMapper instance object.
30
+ #------------------------------------------------------------------------------
31
+ def awesome_mongo_mapper_instance(object)
32
+ return object.inspect if !defined?(ActiveSupport::OrderedHash)
33
+
34
+ data = object.keys.keys.sort_by{|k| k}.inject(ActiveSupport::OrderedHash.new) do |hash, name|
35
+ hash[name] = object[name]
36
+ hash
37
+ end
38
+ "#{object} " + awesome_hash(data)
39
+ end
40
+
41
+ # Format MongoMapper class object.
42
+ #------------------------------------------------------------------------------
43
+ def awesome_mongo_mapper_class(object)
44
+ return object.inspect if !defined?(ActiveSupport::OrderedHash) || !object.respond_to?(:keys)
45
+
46
+ data = object.keys.sort_by{|k| k}.inject(ActiveSupport::OrderedHash.new) do |hash, c|
47
+ hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern
48
+ hash
49
+ end
50
+ "class #{object} < #{object.superclass} " << awesome_hash(data)
51
+ end
52
+ end
53
+
54
+ AwesomePrint.send(:include, AwesomePrintMongoMapper)
data/lib/ap.rb CHANGED
@@ -1,18 +1,24 @@
1
- # Copyright (c) 2010 Michael Dvorkin
1
+ # Copyright (c) 2010-2011 Michael Dvorkin
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
- require File.dirname(__FILE__) + "/ap/core_ext/array"
7
- require File.dirname(__FILE__) + "/ap/core_ext/string"
8
- require File.dirname(__FILE__) + "/ap/core_ext/object"
9
- require File.dirname(__FILE__) + "/ap/core_ext/class"
10
- require File.dirname(__FILE__) + "/ap/core_ext/kernel"
11
- require File.dirname(__FILE__) + "/ap/awesome_print"
12
-
13
- require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(::Logger) or defined?(::ActiveSupport::BufferedLogger)
6
+ #
7
+ # AwesomePrint might be loaded implicitly through ~/.irbrc so do nothing
8
+ # for subsequent requires.
9
+ #
10
+ unless defined?(AwesomePrint)
11
+ %w(array string method object class kernel).each do |file|
12
+ require File.dirname(__FILE__) + "/ap/core_ext/#{file}"
13
+ end
14
14
 
15
- require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(::ActionView)
16
- require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(::ActiveRecord)
17
- require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(::ActiveSupport)
15
+ require File.dirname(__FILE__) + "/ap/awesome_print"
16
+ require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger)
17
+ require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView)
18
18
 
19
+ # Load the following under normal circumstatnces as well as in Rails
20
+ # console when required from ~/.irbrc.
21
+ require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV'])
22
+ require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV'])
23
+ require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper)
24
+ end
data/lib/awesome_print.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010 Michael Dvorkin
1
+ # Copyright (c) 2010-2011 Michael Dvorkin
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -7,19 +7,20 @@
7
7
  # This is the copy of original 'ap.rb' file that matches the gem name. It makes
8
8
  # it possible to omit the :require part in bundler's Gemfile:
9
9
  #
10
- # gem 'awesome_print', '>= 0.2.1', :require => 'ap'
11
- # gem 'awesome_print', '>= 3.0.0'
10
+ # gem 'awesome_print', '>= 0.4.0'
12
11
  #
13
- require File.dirname(__FILE__) + "/ap/core_ext/array"
14
- require File.dirname(__FILE__) + "/ap/core_ext/string"
15
- require File.dirname(__FILE__) + "/ap/core_ext/object"
16
- require File.dirname(__FILE__) + "/ap/core_ext/class"
17
- require File.dirname(__FILE__) + "/ap/core_ext/kernel"
18
- require File.dirname(__FILE__) + "/ap/awesome_print"
12
+ unless defined?(AwesomePrint)
13
+ %w(array string method object class kernel).each do |file|
14
+ require File.dirname(__FILE__) + "/ap/core_ext/#{file}"
15
+ end
19
16
 
20
- require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(::Logger) or defined?(::ActiveSupport::BufferedLogger)
21
-
22
- require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(::ActionView)
23
- require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(::ActiveRecord)
24
- require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(::ActiveSupport)
17
+ require File.dirname(__FILE__) + "/ap/awesome_print"
18
+ require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger)
19
+ require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView)
25
20
 
21
+ # Load the following under normal circumstatnces as well as in Rails
22
+ # console when required from ~/.irbrc.
23
+ require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV'])
24
+ require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV'])
25
+ require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper)
26
+ end
data/rails/init.rb CHANGED
@@ -1 +1,9 @@
1
- require File.join(File.dirname(__FILE__), "..", "init")
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
+ # Load awesome_print when installed as Rails 2.3.x plugin.
8
+ #
9
+ require File.join(File.dirname(__FILE__), "..", "init") unless defined?(AwesomePrint)
@@ -1,35 +1,25 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- require 'action_view'
4
- require 'ap/mixin/action_view'
3
+ begin
4
+ require 'action_view'
5
+ require 'ap/mixin/action_view'
5
6
 
6
- describe "AwesomePrint ActionView extensions" do
7
- before(:each) do
8
- @view = ActionView::Base.new
9
- end
7
+ describe "AwesomePrint ActionView extension" do
8
+ before(:each) do
9
+ @view = ActionView::Base.new
10
+ end
10
11
 
11
- it "should wrap ap output with <pre> tag" do
12
- obj = 42
13
- @view.ap(obj, :plain => true).should == '<pre class="debug_dump">42</pre>'
14
- end
15
-
16
- it "should encode HTML entities" do
17
- obj = " &<hello>"
18
- @view.ap(obj, :plain => true).should == '<pre class="debug_dump">&quot; &amp;&lt;hello&gt;&quot;</pre>'
19
- end
20
-
21
- it "should convert primary ANSI colors to HTML" do
22
- obj = 42
23
- [ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each do |color|
24
- @view.ap(obj, :color => { :fixnum => color }).should == %Q|<pre class="debug_dump"><font color="#{color}">42</font></pre>|
12
+ it "uses HTML and adds 'debug_dump' class to plain <pre> tag" do
13
+ markup = rand
14
+ @view.ap(markup, :plain => true).should == %Q|<pre class="debug_dump">#{markup}</pre>|
25
15
  end
26
- end
27
16
 
28
- it "should convert mixed ANSI colors to HTML" do
29
- obj = 42
30
- [ :grayish, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :whiteish, :black, :pale ].zip(
31
- [ :black, :darkred, :darkgreen, :brown, :navy, :darkmagenta, :darkcyan, :slategray, :black, :slategray ]) do |ansi, html|
32
- @view.ap(obj, :color => { :fixnum => ansi }).should == %Q|<pre class="debug_dump"><font color="#{html}">42</font></pre>|
17
+ it "uses HTML and adds 'debug_dump' class to colorized <pre> tag" do
18
+ markup = ' &<hello>'
19
+ @view.ap(markup).should == '<pre class="debug_dump" style="color:brown">&quot; &amp;&lt;hello&gt;&quot;</pre>'
33
20
  end
34
21
  end
22
+
23
+ rescue LoadError
24
+ puts "Skipping ActionView specs..."
35
25
  end
@@ -1,56 +1,56 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- require 'active_record'
4
- require 'ap/mixin/active_record'
3
+ begin
4
+ require 'active_record'
5
+ require 'ap/mixin/active_record'
5
6
 
7
+ if defined?(::ActiveRecord)
6
8
 
7
- if defined?(::ActiveRecord)
8
-
9
- # Create tableless ActiveRecord model.
10
- #------------------------------------------------------------------------------
11
- class User < ActiveRecord::Base
12
- def self.columns()
13
- @columns ||= []
14
- end
9
+ # Create tableless ActiveRecord model.
10
+ #------------------------------------------------------------------------------
11
+ class User < ActiveRecord::Base
12
+ def self.columns()
13
+ @columns ||= []
14
+ end
15
15
 
16
- def self.column(name, sql_type = nil, default = nil, null = true)
17
- columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
18
- end
16
+ def self.column(name, sql_type = nil, default = nil, null = true)
17
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
18
+ end
19
19
 
20
- column :id, :integer
21
- column :name, :string
22
- column :rank, :integer
23
- column :admin, :boolean
24
- column :created_at, :datetime
20
+ column :id, :integer
21
+ column :name, :string
22
+ column :rank, :integer
23
+ column :admin, :boolean
24
+ column :created_at, :datetime
25
25
 
26
- def self.table_exists?
27
- true
26
+ def self.table_exists?
27
+ true
28
+ end
28
29
  end
29
- end
30
30
 
31
- class SubUser < User
32
- def self.columns
33
- User.columns
31
+ class SubUser < User
32
+ def self.columns
33
+ User.columns
34
+ end
34
35
  end
35
- end
36
36
 
37
- describe "AwesomePrint/ActiveRecord" do
38
- before(:each) do
39
- stub_dotfile!
40
- end
41
-
42
- #------------------------------------------------------------------------------
43
- describe "ActiveRecord instance" do
37
+ describe "AwesomePrint/ActiveRecord" do
44
38
  before(:each) do
45
- ActiveRecord::Base.default_timezone = :utc
46
- @diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
47
- @laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
48
- @ap = AwesomePrint.new(:plain => true)
39
+ stub_dotfile!
49
40
  end
50
41
 
51
- it "display single record" do
52
- out = @ap.send(:awesome, @diana)
53
- str = <<-EOS.strip
42
+ #------------------------------------------------------------------------------
43
+ describe "ActiveRecord instance" do
44
+ before(:each) do
45
+ ActiveRecord::Base.default_timezone = :utc
46
+ @diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
47
+ @laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
48
+ @ap = AwesomePrint.new(:plain => true)
49
+ end
50
+
51
+ it "display single record" do
52
+ out = @ap.send(:awesome, @diana)
53
+ str = <<-EOS.strip
54
54
  #<User:0x01234567> {
55
55
  :id => nil,
56
56
  :name => "Diana",
@@ -59,18 +59,18 @@ if defined?(::ActiveRecord)
59
59
  :created_at => ?
60
60
  }
61
61
  EOS
62
- if RUBY_VERSION.to_f < 1.9
63
- str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
64
- else
65
- str.sub!('?', '1992-10-10 12:30:00 UTC')
66
- end
62
+ if RUBY_VERSION.to_f < 1.9
63
+ str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
64
+ else
65
+ str.sub!('?', '1992-10-10 12:30:00 UTC')
66
+ end
67
67
 
68
- out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
69
- end
68
+ out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
69
+ end
70
70
 
71
- it "display multiple records" do
72
- out = @ap.send(:awesome, [ @diana, @laura ])
73
- str = <<-EOS.strip
71
+ it "display multiple records" do
72
+ out = @ap.send(:awesome, [ @diana, @laura ])
73
+ str = <<-EOS.strip
74
74
  [
75
75
  [0] #<User:0x01234567> {
76
76
  :id => nil,
@@ -88,23 +88,23 @@ EOS
88
88
  }
89
89
  ]
90
90
  EOS
91
- if RUBY_VERSION.to_f < 1.9
92
- str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
93
- str.sub!('!', 'Mon May 26 14:15:00 UTC 2003')
94
- else
95
- str.sub!('?', '1992-10-10 12:30:00 UTC')
96
- str.sub!('!', '2003-05-26 14:15:00 UTC')
97
- end
91
+ if RUBY_VERSION.to_f < 1.9
92
+ str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
93
+ str.sub!('!', 'Mon May 26 14:15:00 UTC 2003')
94
+ else
95
+ str.sub!('?', '1992-10-10 12:30:00 UTC')
96
+ str.sub!('!', '2003-05-26 14:15:00 UTC')
97
+ end
98
98
 
99
- out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
99
+ out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
100
+ end
100
101
  end
101
- end
102
102
 
103
- #------------------------------------------------------------------------------
104
- describe "ActiveRecord class" do
105
- it "should print the class" do
106
- @ap = AwesomePrint.new(:plain => true)
107
- @ap.send(:awesome, User).should == <<-EOS.strip
103
+ #------------------------------------------------------------------------------
104
+ describe "ActiveRecord class" do
105
+ it "should print the class" do
106
+ @ap = AwesomePrint.new(:plain => true)
107
+ @ap.send(:awesome, User).should == <<-EOS.strip
108
108
  class User < ActiveRecord::Base {
109
109
  :id => :integer,
110
110
  :name => :string,
@@ -112,13 +112,12 @@ class User < ActiveRecord::Base {
112
112
  :admin => :boolean,
113
113
  :created_at => :datetime
114
114
  }
115
- EOS
116
-
117
- end
115
+ EOS
116
+ end
118
117
 
119
- it "should print the class for non-direct subclasses of AR::Base" do
120
- @ap = AwesomePrint.new(:plain => true)
121
- @ap.send(:awesome, SubUser).should == <<-EOS.strip
118
+ it "should print the class for non-direct subclasses of AR::Base" do
119
+ @ap = AwesomePrint.new(:plain => true)
120
+ @ap.send(:awesome, SubUser).should == <<-EOS.strip
122
121
  class SubUser < User {
123
122
  :id => :integer,
124
123
  :name => :string,
@@ -126,9 +125,12 @@ class SubUser < User {
126
125
  :admin => :boolean,
127
126
  :created_at => :datetime
128
127
  }
129
- EOS
130
-
128
+ EOS
129
+ end
131
130
  end
132
131
  end
133
132
  end
133
+
134
+ rescue LoadError
135
+ puts "Skipping ActiveRecord specs..."
134
136
  end
@@ -263,6 +263,45 @@ EOS
263
263
  end
264
264
  end
265
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
+
266
305
  #------------------------------------------------------------------------------
267
306
  describe "Negative options[:indent]" do
268
307
  before(:each) do
@@ -415,6 +454,47 @@ EOS
415
454
  end
416
455
  weird.new.ai(:plain => true).should == ''
417
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
418
498
  end
419
499
 
420
500
  #------------------------------------------------------------------------------
@@ -465,15 +545,36 @@ EOS
465
545
  it "inherited from File should be displayed as File" do
466
546
  class My < File; end
467
547
 
468
- my = File.new('/dev/null')
548
+ my = File.new('/dev/null') rescue File.new('nul')
469
549
  my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
470
550
  end
471
551
 
472
552
  it "inherited from Dir should be displayed as Dir" do
473
553
  class My < Dir; end
474
554
 
475
- my = My.new('/tmp')
555
+ require 'tmpdir'
556
+ my = My.new(Dir.tmpdir)
476
557
  my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
477
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
478
579
  end
479
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
data/spec/methods_spec.rb CHANGED
@@ -406,4 +406,23 @@ describe "Methods arrays" do
406
406
  out = Hello.methods.grep(/^m(\d)$/) { %w(none one)[$1.to_i] }.ai(:plain => true)
407
407
  out.should == "[\n [0] none() Hello\n [1] one() Hello\n]"
408
408
  end
409
+
410
+ # See https://github.com/michaeldv/awesome_print/issues/30 for details.
411
+ it "grepping methods and converting them to_sym should work as expected" do
412
+ class Hello
413
+ private
414
+ def him; end
415
+
416
+ def his
417
+ private_methods.grep(/^h..$/) { |n| n.to_sym }
418
+ end
419
+
420
+ def her
421
+ private_methods.grep(/^.e.$/) { |n| n.to_sym }
422
+ end
423
+ end
424
+
425
+ hello = Hello.new
426
+ (hello.send(:his) - hello.send(:her)).sort_by { |x| x.to_s }.should == [ :him, :his ]
427
+ end
409
428
  end