2010-04-03 00:43:46 -04:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2010-04-08 23:31:53 -04:00
|
|
|
require "bigdecimal"
|
|
|
|
require "rational"
|
2010-04-03 00:43:46 -04:00
|
|
|
|
|
|
|
describe "AwesomePrint" do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2010-04-08 22:57:58 -04:00
|
|
|
stub_dotfile!
|
|
|
|
end
|
2010-04-03 00:43:46 -04:00
|
|
|
|
|
|
|
describe "Array" do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2010-04-03 00:43:46 -04:00
|
|
|
@arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
|
|
|
|
end
|
|
|
|
|
2010-04-05 23:02:07 -04:00
|
|
|
it "empty array" do
|
2010-04-05 23:24:46 -04:00
|
|
|
[].ai.should == "[]"
|
2010-04-05 23:02:07 -04:00
|
|
|
end
|
|
|
|
|
2010-04-03 00:43:46 -04:00
|
|
|
it "plain multiline" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@arr.ai(:plain => true).should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
[
|
|
|
|
[0] 1,
|
|
|
|
[1] :two,
|
|
|
|
[2] "three",
|
|
|
|
[3] [
|
|
|
|
[0] nil,
|
|
|
|
[1] [
|
|
|
|
[0] true,
|
|
|
|
[1] false
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2010-09-10 12:03:53 -04:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain multiline without index" do
|
|
|
|
@arr.ai(:plain => true, :index => false).should == <<-EOS.strip
|
|
|
|
[
|
|
|
|
1,
|
|
|
|
:two,
|
|
|
|
"three",
|
|
|
|
[
|
|
|
|
nil,
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2010-04-03 00:43:46 -04:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain multiline indented" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@arr.ai(:plain => true, :indent => 2).should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
[
|
|
|
|
[0] 1,
|
|
|
|
[1] :two,
|
|
|
|
[2] "three",
|
|
|
|
[3] [
|
|
|
|
[0] nil,
|
|
|
|
[1] [
|
|
|
|
[0] true,
|
|
|
|
[1] false
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2010-09-10 12:03:53 -04:00
|
|
|
it "plain multiline indented without index" do
|
|
|
|
@arr.ai(:plain => true, :indent => 2, :index => false).should == <<-EOS.strip
|
|
|
|
[
|
|
|
|
1,
|
|
|
|
:two,
|
|
|
|
"three",
|
|
|
|
[
|
|
|
|
nil,
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2010-04-03 00:43:46 -04:00
|
|
|
it "plain single line" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@arr.ai(:plain => true, :multiline => false).should == '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
|
2010-04-05 23:24:46 -04:00
|
|
|
it "colored multiline (default)" do
|
|
|
|
@arr.ai.should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
[
|
2011-02-09 22:20:56 -05:00
|
|
|
\e[1;37m[0] \e[0m\e[1;34m1\e[0m,
|
|
|
|
\e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
|
|
|
|
\e[1;37m[2] \e[0m\e[0;33m\"three\"\e[0m,
|
|
|
|
\e[1;37m[3] \e[0m[
|
|
|
|
\e[1;37m[0] \e[0m\e[1;31mnil\e[0m,
|
|
|
|
\e[1;37m[1] \e[0m[
|
|
|
|
\e[1;37m[0] \e[0m\e[1;32mtrue\e[0m,
|
|
|
|
\e[1;37m[1] \e[0m\e[1;31mfalse\e[0m
|
2010-04-03 00:43:46 -04:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "colored multiline indented" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@arr.ai(:indent => 8).should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
[
|
2011-02-09 22:20:56 -05:00
|
|
|
\e[1;37m[0] \e[0m\e[1;34m1\e[0m,
|
|
|
|
\e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
|
|
|
|
\e[1;37m[2] \e[0m\e[0;33m\"three\"\e[0m,
|
|
|
|
\e[1;37m[3] \e[0m[
|
|
|
|
\e[1;37m[0] \e[0m\e[1;31mnil\e[0m,
|
|
|
|
\e[1;37m[1] \e[0m[
|
|
|
|
\e[1;37m[0] \e[0m\e[1;32mtrue\e[0m,
|
|
|
|
\e[1;37m[1] \e[0m\e[1;31mfalse\e[0m
|
2010-04-03 00:43:46 -04:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "colored single line" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@arr.ai(:multiline => false).should == "[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]"
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Nested Array" do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2010-04-03 00:43:46 -04:00
|
|
|
@arr = [ 1, 2 ]
|
|
|
|
@arr << @arr
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain multiline" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@arr.ai(:plain => true).should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
[
|
|
|
|
[0] 1,
|
|
|
|
[1] 2,
|
|
|
|
[2] [...]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2010-09-10 12:03:53 -04:00
|
|
|
it "plain multiline without index" do
|
|
|
|
@arr.ai(:plain => true, :index => false).should == <<-EOS.strip
|
|
|
|
[
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
[...]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2010-04-03 00:43:46 -04:00
|
|
|
it "plain single line" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@arr.ai(:plain => true, :multiline => false).should == "[ 1, 2, [...] ]"
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-30 21:10:45 -04:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Limited Output Array" do
|
|
|
|
before(:each) do
|
|
|
|
@arr = (1..1000).to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain limited output large" do
|
|
|
|
@arr.ai(:plain => true, :limited => true).should == <<-EOS.strip
|
|
|
|
[
|
|
|
|
[ 0] 1,
|
|
|
|
[ 1] 2,
|
|
|
|
[ 2] 3,
|
|
|
|
[ 3] .. [996],
|
|
|
|
[997] 998,
|
|
|
|
[998] 999,
|
|
|
|
[999] 1000
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain limited output small" do
|
|
|
|
@arr = @arr[0..3]
|
|
|
|
@arr.ai(:plain => true, :limited => true).should == <<-EOS.strip
|
|
|
|
[
|
|
|
|
[0] 1,
|
|
|
|
[1] 2,
|
|
|
|
[2] 3,
|
|
|
|
[3] 4
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain limited output with 10 lines" do
|
|
|
|
@arr.ai(:plain => true, :limit_size => 10).should == <<-EOS.strip
|
|
|
|
[
|
|
|
|
[ 0] 1,
|
|
|
|
[ 1] 2,
|
|
|
|
[ 2] 3,
|
|
|
|
[ 3] 4,
|
|
|
|
[ 4] 5,
|
|
|
|
[ 5] .. [995],
|
|
|
|
[996] 997,
|
|
|
|
[997] 998,
|
|
|
|
[998] 999,
|
|
|
|
[999] 1000
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain limited output with 11 lines" do
|
|
|
|
@arr.ai(:plain => true, :limit_size => 11).should == <<-EOS.strip
|
|
|
|
[
|
|
|
|
[ 0] 1,
|
|
|
|
[ 1] 2,
|
|
|
|
[ 2] 3,
|
|
|
|
[ 3] 4,
|
|
|
|
[ 4] 5,
|
|
|
|
[ 5] .. [994],
|
|
|
|
[995] 996,
|
|
|
|
[996] 997,
|
|
|
|
[997] 998,
|
|
|
|
[998] 999,
|
|
|
|
[999] 1000
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Limited Output Hash" do
|
|
|
|
before(:each) do
|
|
|
|
@hash = ("a".."z").inject({}) { |h, v| h.merge({ v => v.to_sym }) }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain limited output" do
|
2011-05-30 21:21:30 -04:00
|
|
|
@hash.ai(:sort_keys => true, :plain => true, :limited => true).should == <<-EOS.strip
|
2011-05-30 21:10:45 -04:00
|
|
|
{
|
|
|
|
"a" => :a,
|
|
|
|
"b" => :b,
|
|
|
|
"c" => :c,
|
|
|
|
"d" => :d .. "w" => :w,
|
|
|
|
"x" => :x,
|
|
|
|
"y" => :y,
|
|
|
|
"z" => :z
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-03 00:43:46 -04:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Hash" do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2010-04-03 00:43:46 -04:00
|
|
|
@hash = { 1 => { :sym => { "str" => { [1, 2, 3] => { { :k => :v } => Hash } } } } }
|
|
|
|
end
|
2010-04-05 14:37:22 -04:00
|
|
|
|
|
|
|
it "empty hash" do
|
2010-04-05 23:24:46 -04:00
|
|
|
{}.ai.should == "{}"
|
2010-04-05 14:37:22 -04:00
|
|
|
end
|
|
|
|
|
2010-04-03 00:43:46 -04:00
|
|
|
it "plain multiline" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@hash.ai(:plain => true).should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
{
|
|
|
|
1 => {
|
|
|
|
:sym => {
|
|
|
|
"str" => {
|
|
|
|
[ 1, 2, 3 ] => {
|
|
|
|
{ :k => :v } => Hash < Object
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain multiline indented" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@hash.ai(:plain => true, :indent => 1).should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
{
|
|
|
|
1 => {
|
|
|
|
:sym => {
|
|
|
|
"str" => {
|
|
|
|
[ 1, 2, 3 ] => {
|
|
|
|
{ :k => :v } => Hash < Object
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain single line" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@hash.ai(:plain => true, :multiline => false).should == '{ 1 => { :sym => { "str" => { [ 1, 2, 3 ] => { { :k => :v } => Hash < Object } } } } }'
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
|
2010-04-05 23:24:46 -04:00
|
|
|
it "colored multiline (default)" do
|
|
|
|
@hash.ai.should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
{
|
2010-05-11 12:53:19 -04:00
|
|
|
1\e[0;37m => \e[0m{
|
|
|
|
:sym\e[0;37m => \e[0m{
|
|
|
|
\"str\"\e[0;37m => \e[0m{
|
|
|
|
[ 1, 2, 3 ]\e[0;37m => \e[0m{
|
|
|
|
{ :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
|
2010-04-03 00:43:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "colored multiline indented" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@hash.ai(:indent => 2).should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
{
|
2010-05-11 12:53:19 -04:00
|
|
|
1\e[0;37m => \e[0m{
|
|
|
|
:sym\e[0;37m => \e[0m{
|
|
|
|
\"str\"\e[0;37m => \e[0m{
|
|
|
|
[ 1, 2, 3 ]\e[0;37m => \e[0m{
|
|
|
|
{ :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
|
2010-04-03 00:43:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "colored single line" do
|
2010-05-11 12:53:19 -04:00
|
|
|
@hash.ai(:multiline => false).should == "{ 1\e[0;37m => \e[0m{ :sym\e[0;37m => \e[0m{ \"str\"\e[0;37m => \e[0m{ [ 1, 2, 3 ]\e[0;37m => \e[0m{ { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m } } } } }"
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Nested Hash" do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2010-04-03 00:43:46 -04:00
|
|
|
@hash = {}
|
|
|
|
@hash[:a] = @hash
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain multiline" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@hash.ai(:plain => true).should == <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
{
|
|
|
|
:a => {...}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain single line" do
|
2010-04-05 23:24:46 -04:00
|
|
|
@hash.ai(:plain => true, :multiline => false).should == '{ :a => {...} }'
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2010-12-06 12:21:47 -05:00
|
|
|
describe "Hash with several keys" do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2010-12-06 12:21:47 -05:00
|
|
|
@hash = {"b" => "b", :a => "a", :z => "z", "alpha" => "alpha"}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain multiline" do
|
2011-01-27 22:14:03 -05:00
|
|
|
out = @hash.ai(:plain => true)
|
|
|
|
if RUBY_VERSION.to_f < 1.9 # Order of @hash keys is not guaranteed.
|
|
|
|
out.should =~ /^\{[^\}]+\}/m
|
|
|
|
out.should =~ / "b" => "b",?/
|
|
|
|
out.should =~ / :a => "a",?/
|
|
|
|
out.should =~ / :z => "z",?/
|
|
|
|
out.should =~ / "alpha" => "alpha",?$/
|
|
|
|
else
|
|
|
|
out.should == <<-EOS.strip
|
2010-12-06 12:21:47 -05:00
|
|
|
{
|
|
|
|
"b" => "b",
|
|
|
|
:a => "a",
|
|
|
|
:z => "z",
|
|
|
|
"alpha" => "alpha"
|
|
|
|
}
|
|
|
|
EOS
|
2011-01-27 22:14:03 -05:00
|
|
|
end
|
2010-12-06 12:21:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "plain multiline with sorted keys" do
|
2011-02-05 14:31:51 -05:00
|
|
|
@hash.ai(:plain => true, :sort_keys => true).should == <<-EOS.strip
|
2010-12-06 12:21:47 -05:00
|
|
|
{
|
|
|
|
:a => "a",
|
|
|
|
"alpha" => "alpha",
|
|
|
|
"b" => "b",
|
|
|
|
:z => "z"
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2010-04-04 23:10:16 -04:00
|
|
|
describe "Negative options[:indent]" do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2010-04-04 23:10:16 -04:00
|
|
|
@hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "hash keys must be left aligned" do
|
2010-04-05 23:24:46 -04:00
|
|
|
out = @hash.ai(:plain => true, :indent => -4)
|
2010-04-04 23:10:16 -04:00
|
|
|
out.start_with?("{\n").should == true
|
|
|
|
out.include?(' :red => "rgb(255, 0, 0)"').should == true
|
|
|
|
out.include?(' "magenta" => "rgb(255, 0, 255)"').should == true
|
|
|
|
out.include?(' [ 0, 0, 255 ] => :yellow').should == true
|
|
|
|
out.end_with?("\n}").should == true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2010-04-03 00:43:46 -04:00
|
|
|
describe "Class" do
|
|
|
|
it "shoud show superclass (plain)" do
|
2010-04-05 23:24:46 -04:00
|
|
|
self.class.ai(:plain => true).should == "#{self.class} < #{self.class.superclass}"
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "shoud show superclass (color)" do
|
2010-04-05 23:24:46 -04:00
|
|
|
self.class.ai.should == "#{self.class} < #{self.class.superclass}".yellow
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "File" do
|
|
|
|
it "should display a file (plain)" do
|
|
|
|
File.open(__FILE__, "r") do |f|
|
2010-04-05 23:24:46 -04:00
|
|
|
f.ai(:plain => true).should == "#{f.inspect}\n" << `ls -alF #{f.path}`.chop
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Dir" do
|
|
|
|
it "should display a direcory (plain)" do
|
|
|
|
Dir.open(File.dirname(__FILE__)) do |d|
|
2010-04-05 23:24:46 -04:00
|
|
|
d.ai(:plain => true).should == "#{d.inspect}\n" << `ls -alF #{d.path}`.chop
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-08 23:31:53 -04:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "BigDecimal and Rational" do
|
|
|
|
it "should present BigDecimal object as Float scalar" do
|
2010-04-21 21:27:26 -04:00
|
|
|
big = BigDecimal("2010.4")
|
2010-04-08 23:31:53 -04:00
|
|
|
big.ai(:plain => true).should == "2010.4"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should present Rational object as Float scalar" do
|
|
|
|
rat = Rational(2010, 2)
|
|
|
|
rat.ai(:plain => true).should == "1005.0"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-08 23:14:47 -04:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Utility methods" do
|
|
|
|
it "should merge options" do
|
2011-02-05 14:31:51 -05:00
|
|
|
ap = AwesomePrint::Inspector.new
|
2010-04-08 23:14:47 -04:00
|
|
|
ap.send(:merge_options!, { :color => { :array => :black }, :indent => 0 })
|
|
|
|
options = ap.instance_variable_get("@options")
|
|
|
|
options[:color][:array].should == :black
|
|
|
|
options[:indent].should == 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-10 17:44:13 -04:00
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Struct" do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2010-05-13 01:03:33 -04:00
|
|
|
@struct = unless defined?(Struct::SimpleStruct)
|
|
|
|
Struct.new("SimpleStruct", :name, :address).new
|
|
|
|
else
|
|
|
|
Struct::SimpleStruct.new
|
|
|
|
end
|
2010-05-10 17:44:13 -04:00
|
|
|
@struct.name = "Herman Munster"
|
|
|
|
@struct.address = "1313 Mockingbird Lane"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "empty struct" do
|
|
|
|
Struct.new("EmptyStruct").ai.should == "\e[1;33mStruct::EmptyStruct < Struct\e[0m"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "plain multiline" do
|
2010-05-22 01:53:03 -04:00
|
|
|
s1 = <<-EOS.strip
|
2010-05-10 17:44:13 -04:00
|
|
|
{
|
|
|
|
:address => "1313 Mockingbird Lane",
|
|
|
|
:name => "Herman Munster"
|
|
|
|
}
|
|
|
|
EOS
|
2010-05-22 01:53:03 -04:00
|
|
|
s2 = <<-EOS.strip
|
|
|
|
{
|
|
|
|
:name => "Herman Munster",
|
|
|
|
:address => "1313 Mockingbird Lane"
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
@struct.ai(:plain => true).should satisfy { |match| match == s1 || match == s2 }
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "plain multiline indented" do
|
2010-05-22 01:53:03 -04:00
|
|
|
s1 = <<-EOS.strip
|
2010-05-10 17:44:13 -04:00
|
|
|
{
|
|
|
|
:address => "1313 Mockingbird Lane",
|
|
|
|
:name => "Herman Munster"
|
|
|
|
}
|
|
|
|
EOS
|
2010-05-22 01:53:03 -04:00
|
|
|
s2 = <<-EOS.strip
|
|
|
|
{
|
|
|
|
:name => "Herman Munster",
|
|
|
|
:address => "1313 Mockingbird Lane"
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
@struct.ai(:plain => true, :indent => 1).should satisfy { |match| match == s1 || match == s2 }
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "plain single line" do
|
2010-05-22 01:53:03 -04:00
|
|
|
s1 = "{ :address => \"1313 Mockingbird Lane\", :name => \"Herman Munster\" }"
|
|
|
|
s2 = "{ :name => \"Herman Munster\", :address => \"1313 Mockingbird Lane\" }"
|
|
|
|
@struct.ai(:plain => true, :multiline => false).should satisfy { |match| match == s1 || match == s2 }
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "colored multiline (default)" do
|
2010-05-22 01:53:03 -04:00
|
|
|
s1 = <<-EOS.strip
|
2010-05-10 17:44:13 -04:00
|
|
|
{
|
|
|
|
:address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m,
|
|
|
|
:name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m
|
|
|
|
}
|
|
|
|
EOS
|
2010-05-22 01:53:03 -04:00
|
|
|
s2 = <<-EOS.strip
|
|
|
|
{
|
|
|
|
:name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m,
|
|
|
|
:address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
@struct.ai.should satisfy { |match| match == s1 || match == s2 }
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-10-13 01:52:53 -04:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Misc" do
|
|
|
|
it "handle weird objects that return nil on inspect" do
|
|
|
|
weird = Class.new do
|
|
|
|
def inspect
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
weird.new.ai(:plain => true).should == ''
|
|
|
|
end
|
2011-05-06 17:49:51 -04:00
|
|
|
|
|
|
|
# See https://github.com/michaeldv/awesome_print/issues/35
|
|
|
|
it "handle array grep when pattern contains / chapacter" do
|
|
|
|
hash = { "1/x" => 1, "2//x" => :"2" }
|
|
|
|
grepped = hash.keys.grep(/^(\d+)\//) { $1 }
|
|
|
|
grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]'
|
|
|
|
end
|
2011-05-07 14:50:04 -04:00
|
|
|
|
|
|
|
it "returns value passed as a parameter" do
|
|
|
|
object = rand
|
|
|
|
self.stub!(:puts)
|
|
|
|
(ap object).should == object
|
|
|
|
end
|
2011-05-13 17:42:04 -04:00
|
|
|
|
|
|
|
# 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')
|
|
|
|
lambda { rand.ai }.should_not raise_error
|
|
|
|
end
|
2011-05-07 14:50:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "HTML output" do
|
|
|
|
it "wraps ap output with plain <pre> tag" do
|
|
|
|
markup = rand
|
|
|
|
markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "wraps ap output with colorized <pre> tag" do
|
|
|
|
markup = rand
|
|
|
|
markup.ai(:html => true).should == %Q|<pre style="color:blue">#{markup}</pre>|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "encodes HTML entities (plain)" do
|
|
|
|
markup = ' &<hello>'
|
|
|
|
markup.ai(:html => true, :plain => true).should == '<pre>" &<hello>"</pre>'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "encodes HTML entities (color)" do
|
|
|
|
markup = ' &<hello>'
|
|
|
|
markup.ai(:html => true).should == '<pre style="color:brown">" &<hello>"</pre>'
|
|
|
|
end
|
2010-10-13 01:52:53 -04:00
|
|
|
end
|
|
|
|
|
2010-11-06 18:38:53 -04:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
describe "Inherited from standard Ruby classes" do
|
|
|
|
after do
|
|
|
|
Object.instance_eval{ remove_const :My } if defined?(My)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "inherited from Array should be displayed as Array" do
|
|
|
|
class My < Array; end
|
|
|
|
|
|
|
|
my = My.new([ 1, :two, "three", [ nil, [ true, false ] ] ])
|
|
|
|
my.ai(:plain => true).should == <<-EOS.strip
|
|
|
|
[
|
|
|
|
[0] 1,
|
|
|
|
[1] :two,
|
|
|
|
[2] "three",
|
|
|
|
[3] [
|
|
|
|
[0] nil,
|
|
|
|
[1] [
|
|
|
|
[0] true,
|
|
|
|
[1] false
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "inherited from Hash should be displayed as Hash" do
|
|
|
|
class My < Hash; end
|
|
|
|
|
|
|
|
my = My[ { 1 => { :sym => { "str" => { [1, 2, 3] => { { :k => :v } => Hash } } } } } ]
|
|
|
|
my.ai(:plain => true).should == <<-EOS.strip
|
|
|
|
{
|
|
|
|
1 => {
|
|
|
|
:sym => {
|
|
|
|
"str" => {
|
|
|
|
[ 1, 2, 3 ] => {
|
|
|
|
{ :k => :v } => Hash < Object
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
it "inherited from File should be displayed as File" do
|
|
|
|
class My < File; end
|
|
|
|
|
2011-03-15 15:07:44 -04:00
|
|
|
my = File.new('/dev/null') rescue File.new('nul')
|
2010-11-06 18:38:53 -04:00
|
|
|
my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
|
|
|
|
end
|
|
|
|
|
|
|
|
it "inherited from Dir should be displayed as Dir" do
|
|
|
|
class My < Dir; end
|
|
|
|
|
2011-03-15 14:56:29 -04:00
|
|
|
require 'tmpdir'
|
|
|
|
my = My.new(Dir.tmpdir)
|
2010-11-06 18:38:53 -04:00
|
|
|
my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
|
|
|
|
end
|
2011-01-27 22:51:17 -05:00
|
|
|
|
|
|
|
it "should handle a class that defines its own #send method" do
|
|
|
|
class My
|
|
|
|
def send(arg1, arg2, arg3); end
|
|
|
|
end
|
|
|
|
|
|
|
|
my = My.new
|
|
|
|
my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
|
|
|
|
end
|
2011-05-13 16:33:16 -04:00
|
|
|
|
|
|
|
it "should handle a class defines its own #method method (ex. request.method)" do
|
|
|
|
class My
|
|
|
|
def method
|
|
|
|
'POST'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
my = My.new
|
|
|
|
my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
|
|
|
|
end
|
2010-11-06 18:38:53 -04:00
|
|
|
end
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|