From edee37c5c79bbeeb75bbad92bb93c4590ad891cf Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Wed, 5 Sep 2012 21:55:04 -0700 Subject: [PATCH] ap(object) now returns nil when running under IRB or Pry --- lib/awesome_print/core_ext/kernel.rb | 2 +- lib/awesome_print/inspector.rb | 4 +- spec/{misc_specs.rb => misc_spec.rb} | 130 ++++++++++++++++++++------- spec/spec_helper.rb | 7 ++ 4 files changed, 108 insertions(+), 35 deletions(-) rename spec/{misc_specs.rb => misc_spec.rb} (51%) diff --git a/lib/awesome_print/core_ext/kernel.rb b/lib/awesome_print/core_ext/kernel.rb index 9fc72ee..5e187ee 100644 --- a/lib/awesome_print/core_ext/kernel.rb +++ b/lib/awesome_print/core_ext/kernel.rb @@ -13,7 +13,7 @@ module Kernel def ap(object, options = {}) puts object.ai(options) - object + object unless AwesomePrint.console? end alias :awesome_print :ap diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 1c17d7e..9cd99a0 100755 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -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 diff --git a/spec/misc_specs.rb b/spec/misc_spec.rb similarity index 51% rename from spec/misc_specs.rb rename to spec/misc_spec.rb index f438276..4e81933 100644 --- a/spec/misc_specs.rb +++ b/spec/misc_spec.rb @@ -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
 tag" do
       markup = rand
       markup.ai(:html => true, :plain => true).should == "
#{markup}
" @@ -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|
[red]"hello"[/red]
| - end + it "shoud not raise ArgumentError when formatting HTML" do + out = "hello".ai(:color => { :string => :red }, :html => true) + out.should == %Q|
[red]"hello"[/red]
| + end - it "shoud not raise ArgumentError when formatting HTML (shade color)" do - out = "hello".ai(:color => { :string => :redish }, :html => true) - out.should == %Q|
"hello"
| - end + it "shoud not raise ArgumentError when formatting HTML (shade color)" do + out = "hello".ai(:color => { :string => :redish }, :html => true) + out.should == %Q|
"hello"
| + 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5523993..446ab71 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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!