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 = {}) def ap(object, options = {})
puts object.ai(options) puts object.ai(options)
object object unless AwesomePrint.console?
end end
alias :awesome_print :ap alias :awesome_print :ap

View file

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

View file

@ -1,12 +1,12 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "AwesomePrint" do describe "AwesomePrint" do
describe "Misc" do
before do before do
stub_dotfile! stub_dotfile!
end end
#------------------------------------------------------------------------------
describe "Misc" do
it "handle weird objects that return nil on inspect" do it "handle weird objects that return nil on inspect" do
weird = Class.new do weird = Class.new do
def inspect def inspect
@ -54,7 +54,12 @@ describe "AwesomePrint" do
end end
end end
#------------------------------------------------------------------------------
describe "HTML output" do describe "HTML output" do
before do
stub_dotfile!
end
it "wraps ap output with plain <pre> tag" do it "wraps ap output with plain <pre> tag" do
markup = rand markup = rand
markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>" markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>"
@ -87,7 +92,16 @@ EOS
end end
end end
#------------------------------------------------------------------------------
describe "AwesomePrint.defaults" do describe "AwesomePrint.defaults" do
before do
stub_dotfile!
end
after do
AwesomePrint.defaults = nil
end
# See https://github.com/michaeldv/awesome_print/issues/98 # See https://github.com/michaeldv/awesome_print/issues/98
it "should properly merge the defaults" do it "should properly merge the defaults" do
AwesomePrint.defaults = { :indent => -2, :sort_keys => true } AwesomePrint.defaults = { :indent => -2, :sort_keys => true }
@ -101,10 +115,17 @@ EOS
} }
EOS EOS
end end
end
#------------------------------------------------------------------------------
describe "Coexistence with the colorize gem" do describe "Coexistence with the colorize gem" do
before do
stub_dotfile!
end
before do # Redefine String#red just like colorize gem does it. before do # Redefine String#red just like colorize gem does it.
@awesome_method = "".method(:red) @awesome_method = "".method(:red)
String.instance_eval do String.instance_eval do
define_method :red do # Method arity is 0. define_method :red do # Method arity is 0.
"[red]#{self}[/red]" "[red]#{self}[/red]"
@ -139,5 +160,50 @@ EOS
out.should == %Q|\e[0;31m"hello"\e[0m| out.should == %Q|\e[0;31m"hello"\e[0m|
end end
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
end end

View file

@ -23,6 +23,13 @@ def stub_dotfile!
File.should_receive(:readable?).at_least(:once).with(dotfile).and_return(false) File.should_receive(:readable?).at_least(:once).with(dotfile).and_return(false)
end 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, # 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. # which sets STDOUT.tty? to false and would otherwise prematurely disallow colors.
### AwesomePrint.force_colors! ### AwesomePrint.force_colors!