1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

ap(object) now returns nil when running under IRB or Pry

This commit is contained in:
Mike Dvorkin 2012-09-05 21:55:04 -07:00
parent 67cde1be8d
commit edee37c5c7
4 changed files with 108 additions and 35 deletions

View file

@ -13,7 +13,7 @@ module Kernel
def ap(object, options = {})
puts object.ai(options)
object
object unless AwesomePrint.console?
end
alias :awesome_print :ap

View file

@ -16,11 +16,11 @@ module AwesomePrint
end
def console?
defined?(IRB) || defined?(Pry)
!!(defined?(IRB) || defined?(Pry))
end
def rails_console?
console? && (defined?(Rails::Console) || ENV["RAILS_ENV"])
console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"])
end
end

View file

@ -1,12 +1,12 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "AwesomePrint" do
before do
stub_dotfile!
end
#------------------------------------------------------------------------------
describe "Misc" do
before do
stub_dotfile!
end
it "handle weird objects that return nil on inspect" do
weird = Class.new do
def inspect
@ -54,7 +54,12 @@ describe "AwesomePrint" do
end
end
#------------------------------------------------------------------------------
describe "HTML output" do
before do
stub_dotfile!
end
it "wraps ap output with plain <pre> tag" do
markup = rand
markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>"
@ -87,7 +92,16 @@ EOS
end
end
#------------------------------------------------------------------------------
describe "AwesomePrint.defaults" do
before do
stub_dotfile!
end
after do
AwesomePrint.defaults = nil
end
# See https://github.com/michaeldv/awesome_print/issues/98
it "should properly merge the defaults" do
AwesomePrint.defaults = { :indent => -2, :sort_keys => true }
@ -101,43 +115,95 @@ EOS
}
EOS
end
end
describe "Coexistence with the colorize gem" do
before do # Redefine String#red just like colorize gem does it.
@awesome_method = "".method(:red)
String.instance_eval do
define_method :red do # Method arity is 0.
"[red]#{self}[/red]"
end
#------------------------------------------------------------------------------
describe "Coexistence with the colorize gem" do
before do
stub_dotfile!
end
before do # Redefine String#red just like colorize gem does it.
@awesome_method = "".method(:red)
String.instance_eval do
define_method :red do # Method arity is 0.
"[red]#{self}[/red]"
end
end
end
after do # Restore String#red method.
awesome_method = @awesome_method
String.instance_eval do
define_method :red, awesome_method
end
after do # Restore String#red method.
awesome_method = @awesome_method
String.instance_eval do
define_method :red, awesome_method
end
end
it "shoud not raise ArgumentError when formatting HTML" do
out = "hello".ai(:color => { :string => :red }, :html => true)
out.should == %Q|<pre>[red]<kbd style="color:red">&quot;hello&quot;</kbd>[/red]</pre>|
end
it "shoud not raise ArgumentError when formatting HTML" do
out = "hello".ai(:color => { :string => :red }, :html => true)
out.should == %Q|<pre>[red]<kbd style="color:red">&quot;hello&quot;</kbd>[/red]</pre>|
end
it "shoud not raise ArgumentError when formatting HTML (shade color)" do
out = "hello".ai(:color => { :string => :redish }, :html => true)
out.should == %Q|<pre><kbd style="color:darkred">&quot;hello&quot;</kbd></pre>|
end
it "shoud not raise ArgumentError when formatting HTML (shade color)" do
out = "hello".ai(:color => { :string => :redish }, :html => true)
out.should == %Q|<pre><kbd style="color:darkred">&quot;hello&quot;</kbd></pre>|
end
it "shoud not raise ArgumentError when formatting non-HTML" do
out = "hello".ai(:color => { :string => :red }, :html => false)
out.should == %Q|[red]"hello"[/red]|
end
it "shoud not raise ArgumentError when formatting non-HTML" do
out = "hello".ai(:color => { :string => :red }, :html => false)
out.should == %Q|[red]"hello"[/red]|
end
it "shoud not raise ArgumentError when formatting non-HTML (shade color)" do
out = "hello".ai(:color => { :string => :redish }, :html => false)
out.should == %Q|\e[0;31m"hello"\e[0m|
end
it "shoud not raise ArgumentError when formatting non-HTML (shade color)" do
out = "hello".ai(:color => { :string => :redish }, :html => false)
out.should == %Q|\e[0;31m"hello"\e[0m|
end
end
#------------------------------------------------------------------------------
describe "Console" do
it "should detect IRB" do
class IRB; end
AwesomePrint.console?.should == true
AwesomePrint.rails_console?.should == false
Object.instance_eval{ remove_const :IRB }
end
it "should detect Pry" do
class Pry; end
AwesomePrint.console?.should == true
AwesomePrint.rails_console?.should == false
Object.instance_eval{ remove_const :Pry }
end
it "should detect Rails::Console" do
class IRB; end
class Rails; class Console; end; end
AwesomePrint.console?.should == true
AwesomePrint.rails_console?.should == true
Object.instance_eval{ remove_const :IRB }
Object.instance_eval{ remove_const :Rails }
end
it "should detect ENV['RAILS_ENV']" do
class Pry; end
ENV["RAILS_ENV"] = "development"
AwesomePrint.console?.should == true
AwesomePrint.rails_console?.should == true
Object.instance_eval{ remove_const :Pry }
end
it "should return the actual object when *not* running under console" do
capture! { ap([ 1, 2, 3 ]) }.should == [ 1, 2, 3 ]
capture! { ap({ :a => 1 }) }.should == { :a => 1 }
end
it "should return nil when running under console" do
class IRB; end
capture! { ap([ 1, 2, 3 ]) }.should == nil
capture! { ap({ :a => 1 }) }.should == nil
Object.instance_eval{ remove_const :IRB }
end
end
end

View file

@ -23,6 +23,13 @@ def stub_dotfile!
File.should_receive(:readable?).at_least(:once).with(dotfile).and_return(false)
end
def capture!
standard, $stdout = $stdout, StringIO.new
yield
ensure
$stdout = standard
end
# The following is needed for the Infinity Test. It runs tests as subprocesses,
# which sets STDOUT.tty? to false and would otherwise prematurely disallow colors.
### AwesomePrint.force_colors!