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

added a flag to sort hash keys

This commit is contained in:
Ed Ruder 2010-12-06 09:21:47 -08:00
parent 73b83af69e
commit 0132bccb83
3 changed files with 33 additions and 1 deletions

1
.gitignore vendored
View file

@ -19,3 +19,4 @@ rdoc
pkg
## PROJECT::SPECIFIC
.rvmrc

View file

@ -75,7 +75,8 @@ class AwesomePrint
def awesome_hash(h)
return "{}" if h == {}
data = h.keys.map do |key|
keys = @options[:sorted_hash_keys] ? h.keys.sort { |a, b| a.to_s <=> b.to_s } : h.keys
data = keys.map do |key|
plain_single_line do
[ awesome(key), h[key] ]
end

View file

@ -263,6 +263,36 @@ EOS
end
end
#------------------------------------------------------------------------------
describe "Hash with several keys" do
before(:each) do
@hash = {"b" => "b", :a => "a", :z => "z", "alpha" => "alpha"}
end
it "plain multiline" do
@hash.ai(:plain => true).should == <<-EOS.strip
{
"b" => "b",
:a => "a",
:z => "z",
"alpha" => "alpha"
}
EOS
end
it "plain multiline with sorted keys" do
@hash.ai(:plain => true, :sorted_hash_keys => true).should == <<-EOS.strip
{
:a => "a",
"alpha" => "alpha",
"b" => "b",
:z => "z"
}
EOS
end
end
#------------------------------------------------------------------------------
describe "Negative options[:indent]" do
before(:each) do