1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Correctly handle empty arrays and hashes

This commit is contained in:
Mike Dvorkin 2010-04-05 20:02:07 -07:00
parent f9ff436030
commit 6e4117d495
2 changed files with 11 additions and 6 deletions

View file

@ -43,6 +43,8 @@ class AwesomePrint
# Format an array.
#------------------------------------------------------------------------------
def awesome_array(a)
return "[]" if a == []
if @options[:multiline]
width = (a.size - 1).to_s.size
data = a.inject([]) do |arr, item|
@ -61,6 +63,8 @@ class AwesomePrint
# Format a hash. If @options[:indent] if negative left align hash keys.
#------------------------------------------------------------------------------
def awesome_hash(h)
return "{}" if h == {}
data = h.keys.inject([]) do |arr, key|
plain_single_line do
arr << [ awesome(key), h[key] ]

View file

@ -12,6 +12,11 @@ describe "AwesomePrint" do
@arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
end
it "empty array" do
ap = AwesomePrint.new
ap.send(:awesome, []).should == "[]"
end
it "plain multiline" do
ap = AwesomePrint.new(:plain => true)
ap.send(:awesome, @arr).should == <<-EOS.strip
@ -126,12 +131,8 @@ EOS
end
it "empty hash" do
ap = AwesomePrint.new(:plain => true)
ap.send(:awesome, {}).should == <<-EOS.strip
{
}
EOS
ap = AwesomePrint.new
ap.send(:awesome, {}).should == "{}"
end
it "plain multiline" do