mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Make specs pass with Ruby 1.8.6
This commit is contained in:
parent
84dc4295e5
commit
b6a02f200b
3 changed files with 48 additions and 59 deletions
|
@ -151,10 +151,17 @@ module AwesomePrint
|
|||
end
|
||||
end
|
||||
|
||||
# Format a Struct. If @options[:indent] if negative left align hash keys.
|
||||
# Format a Struct.
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_struct(s)
|
||||
awesome_hash(Hash[s.members.zip(s.values)])
|
||||
#
|
||||
# The code is slightly uglier because of Ruby 1.8.6 quirks:
|
||||
# awesome_hash(Hash[s.members.zip(s.values)]) <-- ArgumentError: odd number of arguments for Hash)
|
||||
# awesome_hash(Hash[*s.members.zip(s.values).flatten]) <-- s.members returns strings, not symbols.
|
||||
#
|
||||
hash = {}
|
||||
s.each_pair { |key, value| hash[key] = value }
|
||||
awesome_hash(hash)
|
||||
end
|
||||
|
||||
# Format Class object.
|
||||
|
|
|
@ -56,7 +56,7 @@ begin
|
|||
EOS
|
||||
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
|
||||
out.gsub!(/(\[\s?\d+\])\s\d+/, "\\1 42")
|
||||
out.should eq str
|
||||
out.should == str
|
||||
end
|
||||
|
||||
it "should print the class" do
|
||||
|
|
|
@ -399,70 +399,52 @@ EOS
|
|||
#
|
||||
it "hash keys must be left aligned" do
|
||||
hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
|
||||
out = hash.ai(:plain => true, :indent => -4)
|
||||
# {
|
||||
# [ 0, 0, 255 ] => :yellow,
|
||||
# :red => "rgb(255, 0, 0)",
|
||||
# "magenta" => "rgb(255, 0, 255)"
|
||||
# }
|
||||
out.start_with?("{\n").should == true
|
||||
out.match(/^\s{4}:red => "rgb\(255, 0, 0\)"/m).should_not == nil
|
||||
out.match(/^\s{4}"magenta" => "rgb\(255, 0, 255\)"/m).should_not == nil
|
||||
out.match(/^\s{4}\[ 0, 0, 255 \] => :yellow/m).should_not == nil
|
||||
out.end_with?("\n}").should == true
|
||||
out = hash.ai(:plain => true, :indent => -4, :sort_keys => true)
|
||||
out.should == <<-EOS.strip
|
||||
{
|
||||
[ 0, 0, 255 ] => :yellow,
|
||||
"magenta" => "rgb(255, 0, 255)",
|
||||
:red => "rgb(255, 0, 0)"
|
||||
}
|
||||
EOS
|
||||
end
|
||||
|
||||
it "nested hash keys should be indented (array of hashes)" do
|
||||
arr = [ { :a => 1, :bb => 22, :ccc => 333}, { 1 => :a, 22 => :bb, 333 => :ccc} ]
|
||||
out = arr.ai(:plain => true, :indent => -4)
|
||||
# [
|
||||
# [0] {
|
||||
# :a => 1,
|
||||
# :bb => 22,
|
||||
# :ccc => 333
|
||||
# },
|
||||
# [1] {
|
||||
# 1 => :a,
|
||||
# 22 => :bb,
|
||||
# 333 => :ccc
|
||||
# }
|
||||
# ]
|
||||
out.start_with?("[\n").should == true
|
||||
out.match(/^\s{4}\[0|1\] {/m).should_not == nil
|
||||
out.match(/^\s{8}:a => 1,/m).should_not == nil
|
||||
out.match(/^\s{8}:bb => 22,/m).should_not == nil
|
||||
out.match(/^\s{8}:ccc => 333/m).should_not == nil
|
||||
out.match(/^\s{8}1 => :a,/m).should_not == nil
|
||||
out.match(/^\s{8}22 => :bb,/m).should_not == nil
|
||||
out.match(/^\s{8}333 => :ccc/m).should_not == nil
|
||||
out.end_with?("\n]").should == true
|
||||
out = arr.ai(:plain => true, :indent => -4, :sort_keys => true)
|
||||
out.should == <<-EOS.strip
|
||||
[
|
||||
[0] {
|
||||
:a => 1,
|
||||
:bb => 22,
|
||||
:ccc => 333
|
||||
},
|
||||
[1] {
|
||||
1 => :a,
|
||||
22 => :bb,
|
||||
333 => :ccc
|
||||
}
|
||||
]
|
||||
EOS
|
||||
end
|
||||
|
||||
it "nested hash keys should be indented (hash of hashes)" do
|
||||
arr = { :first => { :a => 1, :bb => 22, :ccc => 333}, :second => { 1 => :a, 22 => :bb, 333 => :ccc} }
|
||||
out = arr.ai(:plain => true, :indent => -4)
|
||||
# {
|
||||
# :first => {
|
||||
# :a => 1,
|
||||
# :bb => 22,
|
||||
# :ccc => 333
|
||||
# },
|
||||
# :second => {
|
||||
# 1 => :a,
|
||||
# 22 => :bb,
|
||||
# 333 => :ccc
|
||||
# }
|
||||
# }
|
||||
out.start_with?("{\n").should == true
|
||||
out.match(/^\s{4}:first => {/m).should_not == nil
|
||||
out.match(/^\s{8}:a => 1,/m).should_not == nil
|
||||
out.match(/^\s{8}:bb => 22,/m).should_not == nil
|
||||
out.match(/^\s{8}:ccc => 333/m).should_not == nil
|
||||
out.match(/^\s{4}:second => {/m).should_not == nil
|
||||
out.match(/^\s{8}1 => :a,/m).should_not == nil
|
||||
out.match(/^\s{8}22 => :bb,/m).should_not == nil
|
||||
out.match(/^\s{8}333 => :ccc/m).should_not == nil
|
||||
out.end_with?("\n}").should == true
|
||||
out = arr.ai(:plain => true, :indent => -4, :sort_keys => true)
|
||||
out.should == <<-EOS.strip
|
||||
{
|
||||
:first => {
|
||||
:a => 1,
|
||||
:bb => 22,
|
||||
:ccc => 333
|
||||
},
|
||||
:second => {
|
||||
1 => :a,
|
||||
22 => :bb,
|
||||
333 => :ccc
|
||||
}
|
||||
}
|
||||
EOS
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue