2014-12-31 15:59:32 -05:00
|
|
|
require 'spec_helper'
|
2016-11-08 00:07:03 -05:00
|
|
|
require 'bigdecimal'
|
|
|
|
require 'rational'
|
|
|
|
require 'set'
|
2010-04-03 00:43:46 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
RSpec.describe 'AwesomePrint' do
|
|
|
|
describe 'Array' do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2016-11-08 21:39:25 -05:00
|
|
|
@arr = [1, :two, 'three', [nil, [true, false]]]
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'empty array' do
|
|
|
|
expect([].ai).to eq('[]')
|
2010-04-05 23:02:07 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true)).to eq <<-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
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline without index' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, index: false)).to eq <<-EOS.strip
|
2010-09-10 12:03:53 -04:00
|
|
|
[
|
|
|
|
1,
|
|
|
|
:two,
|
|
|
|
"three",
|
|
|
|
[
|
|
|
|
nil,
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2010-04-03 00:43:46 -04:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline indented' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, indent: 2)).to eq <<-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
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline indented without index' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, indent: 2, index: false)).to eq <<-EOS.strip
|
2010-09-10 12:03:53 -04:00
|
|
|
[
|
|
|
|
1,
|
|
|
|
:two,
|
|
|
|
"three",
|
|
|
|
[
|
|
|
|
nil,
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain single line' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, multiline: false)).to eq('[ 1, :two, "three", [ nil, [ true, false ] ] ]')
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored multiline (default)' do
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@arr.ai).to eq <<-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
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored multiline indented' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(indent: 8)).to eq <<-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
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored single line' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(multiline: false)).to eq("[ \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
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Nested Array' do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2016-11-08 21:39:25 -05:00
|
|
|
@arr = [1, 2]
|
2010-04-03 00:43:46 -04:00
|
|
|
@arr << @arr
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true)).to eq <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
[
|
|
|
|
[0] 1,
|
|
|
|
[1] 2,
|
|
|
|
[2] [...]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline without index' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, index: false)).to eq <<-EOS.strip
|
2010-09-10 12:03:53 -04:00
|
|
|
[
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
[...]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain single line' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, multiline: false)).to eq('[ 1, 2, [...] ]')
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-30 21:10:45 -04:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Limited Output Array' do
|
2011-05-30 21:10:45 -04:00
|
|
|
before(:each) do
|
|
|
|
@arr = (1..1000).to_a
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain limited output large' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, limit: true)).to eq <<-EOS.strip
|
2011-05-30 21:10:45 -04:00
|
|
|
[
|
|
|
|
[ 0] 1,
|
|
|
|
[ 1] 2,
|
|
|
|
[ 2] 3,
|
|
|
|
[ 3] .. [996],
|
|
|
|
[997] 998,
|
|
|
|
[998] 999,
|
|
|
|
[999] 1000
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain limited output small' do
|
2011-05-30 21:10:45 -04:00
|
|
|
@arr = @arr[0..3]
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, limit: true)).to eq <<-EOS.strip
|
2011-05-30 21:10:45 -04:00
|
|
|
[
|
|
|
|
[0] 1,
|
|
|
|
[1] 2,
|
|
|
|
[2] 3,
|
|
|
|
[3] 4
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain limited output with 10 lines' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, limit: 10)).to eq <<-EOS.strip
|
2011-05-30 21:10:45 -04:00
|
|
|
[
|
|
|
|
[ 0] 1,
|
|
|
|
[ 1] 2,
|
|
|
|
[ 2] 3,
|
|
|
|
[ 3] 4,
|
|
|
|
[ 4] 5,
|
|
|
|
[ 5] .. [995],
|
|
|
|
[996] 997,
|
|
|
|
[997] 998,
|
|
|
|
[998] 999,
|
|
|
|
[999] 1000
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain limited output with 11 lines' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@arr.ai(plain: true, limit: 11)).to eq <<-EOS.strip
|
2011-05-30 21:10:45 -04:00
|
|
|
[
|
|
|
|
[ 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
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Limited Output Hash' do
|
2011-05-30 21:10:45 -04:00
|
|
|
before(:each) do
|
2016-11-08 00:07:03 -05:00
|
|
|
@hash = ('a'..'z').inject({}) { |h, v| h.merge({ v => v.to_sym }) }
|
2011-05-30 21:10:45 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain limited output' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(sort_keys: true, plain: true, limit: true)).to eq <<-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
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Hash' do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2016-11-08 09:53:17 -05:00
|
|
|
@hash = { 1 => { sym: { 'str' => { [1, 2, 3] => { { k: :v } => Hash } } } } }
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
2014-08-07 21:56:23 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'empty hash' do
|
|
|
|
expect({}.ai).to eq('{}')
|
2010-04-05 14:37:22 -04:00
|
|
|
end
|
2014-08-07 21:56:23 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(plain: true)).to eq <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
{
|
|
|
|
1 => {
|
|
|
|
:sym => {
|
|
|
|
"str" => {
|
|
|
|
[ 1, 2, 3 ] => {
|
|
|
|
{ :k => :v } => Hash < Object
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'new hash syntax' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(plain: true, ruby19_syntax: true)).to eq <<-EOS.strip
|
2016-06-07 22:10:24 -04:00
|
|
|
{
|
|
|
|
1 => {
|
|
|
|
sym: {
|
|
|
|
"str" => {
|
|
|
|
[ 1, 2, 3 ] => {
|
|
|
|
{ k: :v } => Hash < Object
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline indented' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(plain: true, indent: 1)).to eq <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
{
|
|
|
|
1 => {
|
|
|
|
:sym => {
|
|
|
|
"str" => {
|
|
|
|
[ 1, 2, 3 ] => {
|
|
|
|
{ :k => :v } => Hash < Object
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain single line' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(plain: true, multiline: false)).to eq('{ 1 => { :sym => { "str" => { [ 1, 2, 3 ] => { { :k => :v } => Hash < Object } } } } }')
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored multiline (default)' do
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@hash.ai).to eq <<-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
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored with new hash syntax' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(ruby19_syntax: true)).to eq <<-EOS.strip
|
2016-06-07 22:10:24 -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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored multiline indented' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(indent: 2)).to eq <<-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
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored single line' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(multiline: false)).to eq("{ 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
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
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
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(plain: true)).to eq <<-EOS.strip
|
2010-04-03 00:43:46 -04:00
|
|
|
{
|
|
|
|
:a => {...}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain single line' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(plain: true, multiline: false)).to eq('{ :a => {...} }')
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Hash with several keys' do
|
2011-02-05 14:31:51 -05:00
|
|
|
before do
|
2016-11-08 21:39:25 -05:00
|
|
|
@hash = { 'b' => 'b', :a => 'a', :z => 'z', 'alpha' => 'alpha' }
|
2010-12-06 12:21:47 -05:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline' do
|
2016-11-08 09:53:17 -05:00
|
|
|
out = @hash.ai(plain: true)
|
2011-01-27 22:14:03 -05:00
|
|
|
if RUBY_VERSION.to_f < 1.9 # Order of @hash keys is not guaranteed.
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to match(/^\{[^\}]+\}/m)
|
|
|
|
expect(out).to match(/ "b" => "b",?/)
|
|
|
|
expect(out).to match(/ :a => "a",?/)
|
|
|
|
expect(out).to match(/ :z => "z",?/)
|
|
|
|
expect(out).to match(/ "alpha" => "alpha",?$/)
|
2011-01-27 22:14:03 -05:00
|
|
|
else
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to eq <<-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
|
2014-08-07 21:56:23 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline with sorted keys' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@hash.ai(plain: true, sort_keys: true)).to eq <<-EOS.strip
|
2010-12-06 12:21:47 -05:00
|
|
|
{
|
|
|
|
:a => "a",
|
|
|
|
"alpha" => "alpha",
|
|
|
|
"b" => "b",
|
|
|
|
:z => "z"
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Negative options[:indent]' do
|
2011-11-09 00:27:02 -05:00
|
|
|
#
|
|
|
|
# With Ruby < 1.9 the order of hash keys is not defined so we can't
|
|
|
|
# reliably compare the output string.
|
|
|
|
#
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'hash keys must be left aligned' do
|
|
|
|
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, indent: -4, sort_keys: true)
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to eq <<-EOS.strip
|
2011-11-24 01:37:25 -05:00
|
|
|
{
|
|
|
|
[ 0, 0, 255 ] => :yellow,
|
|
|
|
"magenta" => "rgb(255, 0, 255)",
|
|
|
|
:red => "rgb(255, 0, 0)"
|
|
|
|
}
|
|
|
|
EOS
|
2010-04-04 23:10:16 -04:00
|
|
|
end
|
2011-11-09 00:27:02 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'nested hash keys should be indented (array of hashes)' do
|
2016-11-08 21:39:25 -05:00
|
|
|
arr = [{ a: 1, bb: 22, ccc: 333 }, { 1 => :a, 22 => :bb, 333 => :ccc }]
|
2016-11-08 09:53:17 -05:00
|
|
|
out = arr.ai(plain: true, indent: -4, sort_keys: true)
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to eq <<-EOS.strip
|
2011-11-24 01:37:25 -05:00
|
|
|
[
|
|
|
|
[0] {
|
|
|
|
:a => 1,
|
|
|
|
:bb => 22,
|
|
|
|
:ccc => 333
|
|
|
|
},
|
|
|
|
[1] {
|
|
|
|
1 => :a,
|
|
|
|
22 => :bb,
|
|
|
|
333 => :ccc
|
|
|
|
}
|
|
|
|
]
|
|
|
|
EOS
|
2011-11-09 00:27:02 -05:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'nested hash keys should be indented (hash of hashes)' do
|
2016-11-08 21:39:25 -05:00
|
|
|
arr = { first: { a: 1, bb: 22, ccc: 333 }, second: { 1 => :a, 22 => :bb, 333 => :ccc } }
|
2016-11-08 09:53:17 -05:00
|
|
|
out = arr.ai(plain: true, indent: -4, sort_keys: true)
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(out).to eq <<-EOS.strip
|
2011-11-24 01:37:25 -05:00
|
|
|
{
|
|
|
|
:first => {
|
|
|
|
:a => 1,
|
|
|
|
:bb => 22,
|
|
|
|
:ccc => 333
|
|
|
|
},
|
|
|
|
:second => {
|
|
|
|
1 => :a,
|
|
|
|
22 => :bb,
|
|
|
|
333 => :ccc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
2011-11-09 00:27:02 -05:00
|
|
|
end
|
2010-04-04 23:10:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Class' do
|
|
|
|
it 'should show superclass (plain)' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(self.class.ai(plain: true)).to eq("#{self.class} < #{self.class.superclass}")
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should show superclass (color)' do
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(self.class.ai).to eq("#{self.class} < #{self.class.superclass}".yellow)
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'File' do
|
|
|
|
it 'should display a file (plain)' do
|
|
|
|
File.open(__FILE__, 'r') do |f|
|
2019-08-25 04:18:19 -04:00
|
|
|
expect(f.ai(plain: true)).to eq("#{f.inspect}\n#{`ls -alF #{f.path}`.chop}")
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Dir' do
|
|
|
|
it 'should display a direcory (plain)' do
|
2010-04-03 00:43:46 -04:00
|
|
|
Dir.open(File.dirname(__FILE__)) do |d|
|
2019-08-25 04:18:19 -04:00
|
|
|
expect(d.ai(plain: true)).to eq("#{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
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'BigDecimal and Rational' do
|
|
|
|
it 'should present BigDecimal object with arbitrary precision' do
|
|
|
|
big = BigDecimal('201020102010201020102010201020102010.4')
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(big.ai(plain: true)).to eq('201020102010201020102010201020102010.4')
|
2010-04-08 23:31:53 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should present Rational object with arbitrary precision' do
|
2012-06-26 12:05:26 -04:00
|
|
|
rat = Rational(201020102010201020102010201020102010, 2)
|
2016-11-08 09:53:17 -05:00
|
|
|
out = rat.ai(plain: true)
|
2012-09-04 00:22:05 -04:00
|
|
|
#
|
|
|
|
# Ruby 1.9 slightly changed the format of Rational#to_s, see
|
|
|
|
# http://techtime.getharvest.com/blog/harvest-is-now-on-ruby-1-dot-9-3 and
|
|
|
|
# http://www.ruby-forum.com/topic/189397
|
|
|
|
#
|
2016-11-08 00:07:03 -05:00
|
|
|
if RUBY_VERSION < '1.9'
|
|
|
|
expect(out).to eq('100510051005100510051005100510051005')
|
2012-09-04 00:22:05 -04:00
|
|
|
else
|
2016-11-08 00:07:03 -05:00
|
|
|
expect(out).to eq('100510051005100510051005100510051005/1')
|
2012-09-04 00:22:05 -04:00
|
|
|
end
|
2010-04-08 23:31:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-08 23:14:47 -04:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Utility methods' do
|
|
|
|
it 'should merge options' do
|
2011-02-05 14:31:51 -05:00
|
|
|
ap = AwesomePrint::Inspector.new
|
2016-11-08 09:53:17 -05:00
|
|
|
ap.send(:merge_options!, { color: { array: :black }, indent: 0 })
|
2016-11-08 00:07:03 -05:00
|
|
|
options = ap.instance_variable_get('@options')
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(options[:color][:array]).to eq(:black)
|
|
|
|
expect(options[:indent]).to eq(0)
|
2010-04-08 23:14:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-19 20:00:28 -04:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Set' do
|
2013-09-19 20:00:28 -04:00
|
|
|
before do
|
2016-11-08 21:39:25 -05:00
|
|
|
@arr = [1, :two, 'three']
|
2013-09-19 20:00:28 -04:00
|
|
|
@set = Set.new(@arr)
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'empty set' do
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(Set.new.ai).to eq([].ai)
|
2013-09-19 20:00:28 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
if RUBY_VERSION > '1.9'
|
|
|
|
it 'plain multiline' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@set.ai(plain: true)).to eq(@arr.ai(plain: true))
|
2013-09-24 15:18:44 -04:00
|
|
|
end
|
2013-09-19 20:00:28 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline indented' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@set.ai(plain: true, indent: 1)).to eq(@arr.ai(plain: true, indent: 1))
|
2013-09-24 15:18:44 -04:00
|
|
|
end
|
2013-09-19 20:00:28 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain single line' do
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@set.ai(plain: true, multiline: false)).to eq(@arr.ai(plain: true, multiline: false))
|
2013-09-24 15:18:44 -04:00
|
|
|
end
|
2013-09-19 20:00:28 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored multiline (default)' do
|
2014-05-15 16:47:57 -04:00
|
|
|
expect(@set.ai).to eq(@arr.ai)
|
2013-09-24 15:18:44 -04:00
|
|
|
end
|
|
|
|
else # Prior to Ruby 1.9 the order of set values is unpredicatble.
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline' do
|
2016-11-08 21:39:25 -05:00
|
|
|
expect(@set.sort_by { |x| x.to_s }.ai(plain: true)).to eq(@arr.sort_by { |x| x.to_s }.ai(plain: true))
|
2013-09-24 15:18:44 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline indented' do
|
2016-11-08 21:39:25 -05:00
|
|
|
expect(@set.sort_by { |x| x.to_s }.ai(plain: true, indent: 1)).to eq(@arr.sort_by { |x| x.to_s }.ai(plain: true, indent: 1))
|
2013-09-24 15:18:44 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain single line' do
|
2016-11-08 21:39:25 -05:00
|
|
|
expect(@set.sort_by { |x| x.to_s }.ai(plain: true, multiline: false)).to eq(@arr.sort_by { |x| x.to_s }.ai(plain: true, multiline: false))
|
2013-09-24 15:18:44 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored multiline (default)' do
|
2016-11-08 21:39:25 -05:00
|
|
|
expect(@set.sort_by { |x| x.to_s }.ai).to eq(@arr.sort_by { |x| x.to_s }.ai)
|
2013-09-24 15:18:44 -04:00
|
|
|
end
|
2013-09-19 20:00:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-10 17:44:13 -04:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05: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)
|
2016-11-08 00:07:03 -05:00
|
|
|
Struct.new('SimpleStruct', :name, :address).new
|
2010-05-13 01:03:33 -04:00
|
|
|
else
|
|
|
|
Struct::SimpleStruct.new
|
|
|
|
end
|
2016-11-08 00:07:03 -05:00
|
|
|
@struct.name = 'Herman Munster'
|
|
|
|
@struct.address = '1313 Mockingbird Lane'
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
2014-08-07 21:56:23 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'empty struct' do
|
|
|
|
expect(Struct.new('EmptyStruct').ai).to eq("\e[1;33mStruct::EmptyStruct < Struct\e[0m")
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
2014-08-07 21:56:23 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline' do
|
2010-05-22 01:53:03 -04:00
|
|
|
s1 = <<-EOS.strip
|
2016-04-14 21:47:53 -04:00
|
|
|
address = \"1313 Mockingbird Lane\",
|
|
|
|
name = \"Herman Munster\"
|
2010-05-10 17:44:13 -04:00
|
|
|
EOS
|
2010-05-22 01:53:03 -04:00
|
|
|
s2 = <<-EOS.strip
|
2016-04-14 21:47:53 -04:00
|
|
|
name = \"Herman Munster\",
|
|
|
|
address = \"1313 Mockingbird Lane\"
|
2010-05-22 01:53:03 -04:00
|
|
|
EOS
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@struct.ai(plain: true)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain multiline indented' do
|
2010-05-22 01:53:03 -04:00
|
|
|
s1 = <<-EOS.strip
|
2016-04-14 21:47:53 -04:00
|
|
|
address = "1313 Mockingbird Lane",
|
|
|
|
name = "Herman Munster"
|
2010-05-10 17:44:13 -04:00
|
|
|
EOS
|
2010-05-22 01:53:03 -04:00
|
|
|
s2 = <<-EOS.strip
|
2016-04-14 21:47:53 -04:00
|
|
|
name = "Herman Munster",
|
|
|
|
address = "1313 Mockingbird Lane"
|
2010-05-22 01:53:03 -04:00
|
|
|
EOS
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@struct.ai(plain: true, indent: 1)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'plain single line' do
|
|
|
|
s1 = 'address = "1313 Mockingbird Lane", name = "Herman Munster"'
|
|
|
|
s2 = 'name = "Herman Munster", address = "1313 Mockingbird Lane"'
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(@struct.ai(plain: true, multiline: false)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'colored multiline (default)' do
|
2010-05-22 01:53:03 -04:00
|
|
|
s1 = <<-EOS.strip
|
2016-04-14 21:47:53 -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
|
2010-05-10 17:44:13 -04:00
|
|
|
EOS
|
2010-05-22 01:53:03 -04:00
|
|
|
s2 = <<-EOS.strip
|
2016-04-14 21:47:53 -04:00
|
|
|
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
|
2010-05-22 01:53:03 -04:00
|
|
|
EOS
|
2016-04-14 21:47:53 -04:00
|
|
|
expect(@struct.ai).to satisfy { |out| out.include?(s1) || out.include?(s2) }
|
2010-05-10 17:44:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-06 18:38:53 -04:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'Inherited from standard Ruby classes' do
|
2010-11-06 18:38:53 -04:00
|
|
|
after do
|
2016-11-08 21:39:25 -05:00
|
|
|
Object.instance_eval { remove_const :My } if defined?(My)
|
2010-11-06 18:38:53 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'inherited from Array should be displayed as Array' do
|
2010-11-06 18:38:53 -04:00
|
|
|
class My < Array; end
|
|
|
|
|
2016-11-08 21:39:25 -05:00
|
|
|
my = My.new([1, :two, 'three', [nil, [true, false]]])
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(my.ai(plain: true)).to eq <<-EOS.strip
|
2010-11-06 18:38:53 -04:00
|
|
|
[
|
|
|
|
[0] 1,
|
|
|
|
[1] :two,
|
|
|
|
[2] "three",
|
|
|
|
[3] [
|
|
|
|
[0] nil,
|
|
|
|
[1] [
|
|
|
|
[0] true,
|
|
|
|
[1] false
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'inherited from Hash should be displayed as Hash' do
|
2010-11-06 18:38:53 -04:00
|
|
|
class My < Hash; end
|
|
|
|
|
2016-11-08 21:39:25 -05:00
|
|
|
my = My[{ 1 => { sym: { 'str' => { [1, 2, 3] => { { k: :v } => Hash } } } } }]
|
2016-11-08 09:53:17 -05:00
|
|
|
expect(my.ai(plain: true)).to eq <<-EOS.strip
|
2010-11-06 18:38:53 -04:00
|
|
|
{
|
|
|
|
1 => {
|
|
|
|
:sym => {
|
|
|
|
"str" => {
|
|
|
|
[ 1, 2, 3 ] => {
|
|
|
|
{ :k => :v } => Hash < Object
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'inherited from File should be displayed as File' do
|
2010-11-06 18:38:53 -04:00
|
|
|
class My < File; end
|
|
|
|
|
2011-03-15 15:07:44 -04:00
|
|
|
my = File.new('/dev/null') rescue File.new('nul')
|
2019-08-25 04:18:19 -04:00
|
|
|
expect(my.ai(plain: true)).to eq("#{my.inspect}\n#{`ls -alF #{my.path}`.chop}")
|
2010-11-06 18:38:53 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'inherited from Dir should be displayed as Dir' do
|
2010-11-06 18:38:53 -04:00
|
|
|
class My < Dir; end
|
|
|
|
|
2011-03-15 14:56:29 -04:00
|
|
|
require 'tmpdir'
|
|
|
|
my = My.new(Dir.tmpdir)
|
2019-08-25 04:18:19 -04:00
|
|
|
expect(my.ai(plain: true)).to eq("#{my.inspect}\n#{`ls -alF #{my.path}`.chop}")
|
2010-11-06 18:38:53 -04:00
|
|
|
end
|
2011-01-27 22:51:17 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should handle a class that defines its own #send method' do
|
2011-01-27 22:51:17 -05:00
|
|
|
class My
|
|
|
|
def send(arg1, arg2, arg3); end
|
|
|
|
end
|
|
|
|
|
|
|
|
my = My.new
|
2016-11-08 09:53:17 -05:00
|
|
|
expect { my.methods.ai(plain: true) }.not_to raise_error
|
2011-01-27 22:51:17 -05:00
|
|
|
end
|
2011-05-13 16:33:16 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should handle a class defines its own #method method (ex. request.method)' do
|
2011-05-13 16:33:16 -04:00
|
|
|
class My
|
|
|
|
def method
|
|
|
|
'POST'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
my = My.new
|
2016-11-08 09:53:17 -05:00
|
|
|
expect { my.methods.ai(plain: true) }.not_to raise_error
|
2011-05-13 16:33:16 -04:00
|
|
|
end
|
2015-02-02 18:19:45 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'should handle a class that defines its own #to_hash method' do
|
|
|
|
it 'that takes arguments' do
|
2015-02-02 18:19:45 -05:00
|
|
|
class My
|
|
|
|
def to_hash(a, b)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
my = My.new
|
2016-11-08 09:53:17 -05:00
|
|
|
expect { my.ai(plain: true) }.not_to raise_error
|
2015-02-02 18:19:45 -05:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'that returns nil' do
|
2015-02-02 18:19:45 -05:00
|
|
|
class My
|
|
|
|
def to_hash()
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
my = My.new
|
2016-11-08 09:53:17 -05:00
|
|
|
expect { my.ai(plain: true) }.not_to raise_error
|
2015-02-02 18:19:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "that returns an object that doesn't support #keys" do
|
|
|
|
class My
|
|
|
|
def to_hash()
|
|
|
|
object = Object.new
|
|
|
|
object.define_singleton_method('[]') { return nil }
|
|
|
|
|
|
|
|
return object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
my = My.new
|
2016-11-08 09:53:17 -05:00
|
|
|
expect { my.ai(plain: true) }.not_to raise_error
|
2015-02-02 18:19:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "that returns an object that doesn't support subscripting" do
|
|
|
|
class My
|
|
|
|
def to_hash()
|
|
|
|
object = Object.new
|
|
|
|
object.define_singleton_method(:keys) { return [:foo] }
|
|
|
|
|
|
|
|
return object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
my = My.new
|
2016-11-08 09:53:17 -05:00
|
|
|
expect { my.ai(plain: true) }.not_to raise_error
|
2015-02-02 18:19:45 -05:00
|
|
|
end
|
|
|
|
end
|
2010-11-06 18:38:53 -04:00
|
|
|
end
|
2010-04-03 00:43:46 -04:00
|
|
|
end
|