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:
parent
f9ff436030
commit
6e4117d495
2 changed files with 11 additions and 6 deletions
|
@ -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] ]
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue