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:
parent
67cde1be8d
commit
edee37c5c7
4 changed files with 108 additions and 35 deletions
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
before do
|
|
||||||
stub_dotfile!
|
|
||||||
end
|
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
|
||||||
describe "Misc" do
|
describe "Misc" do
|
||||||
|
before do
|
||||||
|
stub_dotfile!
|
||||||
|
end
|
||||||
|
|
||||||
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,43 +115,95 @@ EOS
|
||||||
}
|
}
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "Coexistence with the colorize gem" do
|
#------------------------------------------------------------------------------
|
||||||
before do # Redefine String#red just like colorize gem does it.
|
describe "Coexistence with the colorize gem" do
|
||||||
@awesome_method = "".method(:red)
|
before do
|
||||||
String.instance_eval do
|
stub_dotfile!
|
||||||
define_method :red do # Method arity is 0.
|
end
|
||||||
"[red]#{self}[/red]"
|
|
||||||
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
|
end
|
||||||
|
end
|
||||||
|
|
||||||
after do # Restore String#red method.
|
after do # Restore String#red method.
|
||||||
awesome_method = @awesome_method
|
awesome_method = @awesome_method
|
||||||
String.instance_eval do
|
String.instance_eval do
|
||||||
define_method :red, awesome_method
|
define_method :red, awesome_method
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "shoud not raise ArgumentError when formatting HTML" do
|
it "shoud not raise ArgumentError when formatting HTML" do
|
||||||
out = "hello".ai(:color => { :string => :red }, :html => true)
|
out = "hello".ai(:color => { :string => :red }, :html => true)
|
||||||
out.should == %Q|<pre>[red]<kbd style="color:red">"hello"</kbd>[/red]</pre>|
|
out.should == %Q|<pre>[red]<kbd style="color:red">"hello"</kbd>[/red]</pre>|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "shoud not raise ArgumentError when formatting HTML (shade color)" do
|
it "shoud not raise ArgumentError when formatting HTML (shade color)" do
|
||||||
out = "hello".ai(:color => { :string => :redish }, :html => true)
|
out = "hello".ai(:color => { :string => :redish }, :html => true)
|
||||||
out.should == %Q|<pre><kbd style="color:darkred">"hello"</kbd></pre>|
|
out.should == %Q|<pre><kbd style="color:darkred">"hello"</kbd></pre>|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "shoud not raise ArgumentError when formatting non-HTML" do
|
it "shoud not raise ArgumentError when formatting non-HTML" do
|
||||||
out = "hello".ai(:color => { :string => :red }, :html => false)
|
out = "hello".ai(:color => { :string => :red }, :html => false)
|
||||||
out.should == %Q|[red]"hello"[/red]|
|
out.should == %Q|[red]"hello"[/red]|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "shoud not raise ArgumentError when formatting non-HTML (shade color)" do
|
it "shoud not raise ArgumentError when formatting non-HTML (shade color)" do
|
||||||
out = "hello".ai(:color => { :string => :redish }, :html => false)
|
out = "hello".ai(:color => { :string => :redish }, :html => false)
|
||||||
out.should == %Q|\e[0;31m"hello"\e[0m|
|
out.should == %Q|\e[0;31m"hello"\e[0m|
|
||||||
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
|
end
|
|
@ -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!
|
||||||
|
|
Loading…
Reference in a new issue