2014-12-31 15:59:32 -05:00
|
|
|
require 'spec_helper'
|
2012-09-03 16:57:10 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
RSpec.describe 'AwesomePrint' do
|
2012-09-03 16:57:10 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Misc' do
|
|
|
|
it 'handle weird objects that return nil on inspect' do
|
2012-09-03 16:57:10 -04:00
|
|
|
weird = Class.new do
|
|
|
|
def inspect
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(weird.new.ai(plain: true)).to eq('')
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'handle frozen object.inspect' do
|
2012-09-03 16:57:10 -04:00
|
|
|
weird = Class.new do
|
|
|
|
def inspect
|
2016-11-08 00:07:03 -05:00
|
|
|
'ice'.freeze
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
end
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(weird.new.ai(plain: false)).to eq('ice')
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
|
2016-06-21 09:07:59 -04:00
|
|
|
# See https://github.com/awesome-print/awesome_print/issues/35
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'handle array grep when pattern contains / chapacter' do
|
|
|
|
hash = { '1/x' => 1, '2//x' => :"2" }
|
2012-09-03 16:57:10 -04:00
|
|
|
grepped = hash.keys.sort.grep(/^(\d+)\//) { $1 }
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(grepped.ai(plain: true, multiline: false)).to eq('[ "1", "2" ]')
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
|
2016-06-21 09:07:59 -04:00
|
|
|
# See https://github.com/awesome-print/awesome_print/issues/85
|
2016-11-08 00:07:03 -05:00
|
|
|
if RUBY_VERSION >= '1.8.7'
|
2012-09-03 16:57:10 -04:00
|
|
|
it "handle array grep when a method is defined in C and thus doesn't have a binding" do
|
|
|
|
arr = (0..6).to_a
|
|
|
|
grepped = arr.grep(1..4, &:succ)
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(grepped.ai(plain: true, multiline: false)).to eq('[ 2, 3, 4, 5 ]')
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'returns value passed as a parameter' do
|
2012-09-03 16:57:10 -04:00
|
|
|
object = rand
|
2014-05-15 16:47:57 -04:00
|
|
|
allow(self).to receive(:puts)
|
|
|
|
expect(ap object).to eq(object)
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Require different file name this time (lib/ap.rb vs. lib/awesome_print).
|
|
|
|
it "several require 'awesome_print' should do no harm" do
|
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/../lib/ap')
|
2014-05-15 16:47:57 -04:00
|
|
|
expect { rand.ai }.not_to raise_error
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
2013-09-21 03:29:18 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'format ENV as hash' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(ENV.ai(plain: true)).to eq(ENV.to_hash.ai(plain: true))
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(ENV.ai).to eq(ENV.to_hash.ai)
|
2013-09-21 03:29:18 -04:00
|
|
|
end
|
2013-10-08 18:36:26 -04:00
|
|
|
|
2016-06-21 09:07:59 -04:00
|
|
|
# See https://github.com/awesome-print/awesome_print/issues/134
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'IPAddr workaround' do
|
|
|
|
require 'ipaddr'
|
|
|
|
ipaddr = IPAddr.new('3ffe:505:2::1')
|
|
|
|
expect(ipaddr.ai).to eq('#<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>')
|
2013-10-08 18:36:26 -04:00
|
|
|
end
|
2013-11-05 13:58:45 -05:00
|
|
|
|
2016-06-21 09:07:59 -04:00
|
|
|
# See https://github.com/awesome-print/awesome_print/issues/139
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'Object that overrides == and expects the :id method' do
|
2013-11-05 13:58:45 -05:00
|
|
|
weird = Class.new do
|
|
|
|
# Raises NoMethodError: undefined method `id' when "other" is nil or ENV.
|
|
|
|
def ==(other)
|
|
|
|
self.id == other.id
|
|
|
|
end
|
|
|
|
alias :eql? :==
|
|
|
|
end
|
2014-05-15 16:47:57 -04:00
|
|
|
expect { weird.new.ai }.not_to raise_error
|
2013-11-05 13:58:45 -05:00
|
|
|
end
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
|
2012-09-06 00:55:04 -04:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'HTML output' do
|
|
|
|
it 'wraps ap output with plain <pre> tag' do
|
2012-09-03 16:57:10 -04:00
|
|
|
markup = rand
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(markup.ai(html: true, plain: true)).to eq("<pre>#{markup}</pre>")
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'wraps ap output with <pre> tag with colorized <kbd>' do
|
2012-09-03 16:57:10 -04:00
|
|
|
markup = rand
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(markup.ai(html: true)).to eq(%Q|<pre><kbd style="color:blue">#{markup}</kbd></pre>|)
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'wraps multiline ap output with <pre> tag with colorized <kbd>' do
|
2016-11-08 21:39:25 -05:00
|
|
|
markup = [1, :two, 'three']
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(markup.ai(html: true)).to eq <<-EOS.strip_heredoc.strip
|
2016-07-04 05:57:10 -04:00
|
|
|
<pre>[
|
|
|
|
<kbd style="color:white">[0] </kbd><kbd style="color:blue">1</kbd>,
|
|
|
|
<kbd style="color:white">[1] </kbd><kbd style="color:darkcyan">:two</kbd>,
|
|
|
|
<kbd style="color:white">[2] </kbd><kbd style="color:brown">"three"</kbd>
|
|
|
|
]</pre>
|
|
|
|
EOS
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'wraps hash ap output with only an outer <pre> tag' do
|
2016-11-08 21:39:25 -05:00
|
|
|
markup = [{ 'hello' => 'world' }]
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(markup.ai(html: true)).to eq <<-EOS.strip_heredoc.strip
|
2016-07-04 05:57:10 -04:00
|
|
|
<pre>[
|
|
|
|
<kbd style="color:white">[0] </kbd>{
|
|
|
|
"hello"<kbd style="color:slategray"> => </kbd><kbd style="color:brown">"world"</kbd>
|
|
|
|
}
|
|
|
|
]</pre>
|
|
|
|
EOS
|
2013-04-27 06:23:38 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'encodes HTML entities (plain)' do
|
2012-09-03 16:57:10 -04:00
|
|
|
markup = ' &<hello>'
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(markup.ai(html: true, plain: true)).to eq('<pre>" &<hello>"</pre>')
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'encodes HTML entities (color)' do
|
2012-09-03 16:57:10 -04:00
|
|
|
markup = ' &<hello>'
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(markup.ai(html: true)).to eq('<pre><kbd style="color:brown">" &<hello>"</kbd></pre>')
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-03 17:05:14 -04:00
|
|
|
|
2012-09-06 00:55:04 -04:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'AwesomePrint.defaults' do
|
2012-09-06 00:55:04 -04:00
|
|
|
after do
|
|
|
|
AwesomePrint.defaults = nil
|
|
|
|
end
|
|
|
|
|
2016-06-21 09:07:59 -04:00
|
|
|
# See https://github.com/awesome-print/awesome_print/issues/98
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should properly merge the defaults' do
|
2016-11-08 09:53:17 -05:00
|
|
|
AwesomePrint.defaults = { indent: -2, sort_keys: true }
|
2016-11-08 00:07:03 -05:00
|
|
|
hash = { [0, 0, 255] => :yellow, :red => 'rgb(255, 0, 0)', 'magenta' => 'rgb(255, 0, 255)' }
|
2016-11-08 09:53:17 -05:00
|
|
|
out = hash.ai(plain: true)
|
2016-07-04 05:57:10 -04:00
|
|
|
expect(out).to eq <<-EOS.strip_heredoc.strip
|
|
|
|
{
|
|
|
|
[ 0, 0, 255 ] => :yellow,
|
|
|
|
"magenta" => "rgb(255, 0, 255)",
|
|
|
|
:red => "rgb(255, 0, 0)"
|
|
|
|
}
|
|
|
|
EOS
|
2012-09-03 17:05:14 -04:00
|
|
|
end
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
2012-09-03 20:26:27 -04:00
|
|
|
|
2012-09-06 00:55:04 -04:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Coexistence with the colorize gem' do
|
2012-09-06 00:55:04 -04:00
|
|
|
before do # Redefine String#red just like colorize gem does it.
|
2016-11-08 00:07:03 -05:00
|
|
|
@awesome_method = ''.method(:red)
|
2012-09-03 20:26:27 -04:00
|
|
|
|
2012-09-06 00:55:04 -04:00
|
|
|
String.instance_eval do
|
2012-09-06 01:25:52 -04:00
|
|
|
define_method :red do # Method arity is now 0 in Ruby 1.9+.
|
2012-09-06 00:55:04 -04:00
|
|
|
"[red]#{self}[/red]"
|
2012-09-03 20:26:27 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
2012-09-03 20:26:27 -04:00
|
|
|
|
2012-09-06 00:55:04 -04:00
|
|
|
after do # Restore String#red method.
|
|
|
|
awesome_method = @awesome_method
|
|
|
|
String.instance_eval do
|
|
|
|
define_method :red, awesome_method
|
2012-09-03 20:26:27 -04:00
|
|
|
end
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
2012-09-03 20:26:27 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'shoud not raise ArgumentError when formatting HTML' do
|
2016-11-08 09:53:17 -05:00
|
|
|
out = 'hello'.ai(color: { string: :red }, html: true)
|
2016-11-08 00:07:03 -05:00
|
|
|
if RUBY_VERSION >= '1.9'
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to eq(%Q|<pre>[red]<kbd style="color:red">"hello"</kbd>[/red]</pre>|)
|
2012-09-06 01:25:52 -04:00
|
|
|
else
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to eq(%Q|<pre>[red]"hello"[/red]</pre>|)
|
2012-09-06 01:25:52 -04:00
|
|
|
end
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
2012-09-03 20:26:27 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'shoud not raise ArgumentError when formatting HTML (shade color)' do
|
2016-11-08 09:53:17 -05:00
|
|
|
out = 'hello'.ai(color: { string: :redish }, html: true)
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to eq(%Q|<pre><kbd style="color:darkred">"hello"</kbd></pre>|)
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
2012-09-03 20:26:27 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'shoud not raise ArgumentError when formatting non-HTML' do
|
2016-11-08 09:53:17 -05:00
|
|
|
out = 'hello'.ai(color: { string: :red }, html: false)
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to eq(%Q|[red]"hello"[/red]|)
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'shoud not raise ArgumentError when formatting non-HTML (shade color)' do
|
2016-11-08 09:53:17 -05:00
|
|
|
out = 'hello'.ai(color: { string: :redish }, html: false)
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to eq(%Q|\e[0;31m"hello"\e[0m|)
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Console' do
|
|
|
|
it 'should detect IRB' do
|
2012-09-06 00:55:04 -04:00
|
|
|
class IRB; end
|
2015-01-12 16:02:35 -05:00
|
|
|
ENV.delete('RAILS_ENV')
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(AwesomePrint.console?).to eq(true)
|
|
|
|
expect(AwesomePrint.rails_console?).to eq(false)
|
2016-11-08 21:39:25 -05:00
|
|
|
Object.instance_eval { remove_const :IRB }
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should detect Pry' do
|
2012-09-06 00:55:04 -04:00
|
|
|
class Pry; end
|
2015-01-12 16:02:35 -05:00
|
|
|
ENV.delete('RAILS_ENV')
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(AwesomePrint.console?).to eq(true)
|
|
|
|
expect(AwesomePrint.rails_console?).to eq(false)
|
2016-11-08 21:39:25 -05:00
|
|
|
Object.instance_eval { remove_const :Pry }
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should detect Rails::Console' do
|
2012-09-06 00:55:04 -04:00
|
|
|
class IRB; end
|
2014-12-29 11:38:13 -05:00
|
|
|
module Rails; class Console; end; end
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(AwesomePrint.console?).to eq(true)
|
|
|
|
expect(AwesomePrint.rails_console?).to eq(true)
|
2016-11-08 21:39:25 -05:00
|
|
|
Object.instance_eval { remove_const :IRB }
|
|
|
|
Object.instance_eval { remove_const :Rails }
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should detect ENV['RAILS_ENV']" do
|
|
|
|
class Pry; end
|
2016-11-08 00:07:03 -05:00
|
|
|
ENV['RAILS_ENV'] = 'development'
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(AwesomePrint.console?).to eq(true)
|
|
|
|
expect(AwesomePrint.rails_console?).to eq(true)
|
2016-11-08 21:39:25 -05:00
|
|
|
Object.instance_eval { remove_const :Pry }
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should return the actual object when *not* running under console' do
|
2016-11-08 21:39:25 -05:00
|
|
|
expect(capture! { ap([1, 2, 3]) }).to eq([1, 2, 3])
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(capture! { ap({ a: 1 }) }).to eq({ a: 1 })
|
2012-09-06 00:55:04 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should return nil when running under console' do
|
2012-09-06 00:55:04 -04:00
|
|
|
class IRB; end
|
2016-11-08 21:39:25 -05:00
|
|
|
expect(capture! { ap([1, 2, 3]) }).to eq(nil)
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(capture! { ap({ a: 1 }) }).to eq(nil)
|
2016-11-08 21:39:25 -05:00
|
|
|
Object.instance_eval { remove_const :IRB }
|
2012-09-03 20:26:27 -04:00
|
|
|
end
|
2015-12-18 19:12:14 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'handles NoMethodError on IRB implicit #ai' do
|
2015-12-18 19:12:14 -05:00
|
|
|
module IRB; class Irb; end; end
|
|
|
|
irb_context = double('irb_context', last_value: BasicObject.new)
|
|
|
|
IRB.define_singleton_method :version, -> { 'test_version' }
|
|
|
|
irb = IRB::Irb.new
|
|
|
|
irb.instance_eval { @context = irb_context }
|
|
|
|
AwesomePrint.irb!
|
|
|
|
expect(irb).to receive(:puts).with("(Object doesn't support #ai)")
|
|
|
|
expect { irb.output_value }.to_not raise_error
|
|
|
|
Object.instance_eval { remove_const :IRB }
|
|
|
|
end
|
2012-09-03 17:05:14 -04:00
|
|
|
end
|
2012-09-03 16:57:10 -04:00
|
|
|
end
|