mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Fixed nested left alighment; fixed ActiveRecord and MongoMapper object specs
This commit is contained in:
parent
782e38268b
commit
d2c0a398e2
4 changed files with 97 additions and 36 deletions
|
@ -271,10 +271,12 @@ module AwesomePrint
|
|||
#------------------------------------------------------------------------------
|
||||
def align(value, width)
|
||||
if @options[:multiline]
|
||||
if @options[:indent] >= 0
|
||||
if @options[:indent] > 0
|
||||
value.rjust(width)
|
||||
elsif @options[:indent] == 0
|
||||
indent + value.ljust(width)
|
||||
else
|
||||
indent[0, -@options[:indent]] + value.ljust(width)
|
||||
indent[0, @indentation + @options[:indent]] + value.ljust(width)
|
||||
end
|
||||
else
|
||||
value
|
||||
|
@ -289,7 +291,7 @@ module AwesomePrint
|
|||
end
|
||||
|
||||
def left_aligned
|
||||
current, @options[:indent] = @options[:indent], @options[:indent].abs * -1
|
||||
current, @options[:indent] = @options[:indent], 0
|
||||
yield
|
||||
ensure
|
||||
@options[:indent] = current
|
||||
|
|
|
@ -50,13 +50,13 @@ begin
|
|||
out = @ap.send(:awesome, @diana)
|
||||
str = <<-EOS.strip
|
||||
#<User:0x01234567
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
@marked_for_destruction = false,
|
||||
@new_record = true,
|
||||
@previously_changed = {},
|
||||
@readonly = false,
|
||||
attr_accessor :attributes = {
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
@marked_for_destruction = false,
|
||||
@new_record = true,
|
||||
@previously_changed = {},
|
||||
@readonly = false,
|
||||
attr_accessor :attributes = {
|
||||
"admin" => false,
|
||||
"created_at" => "?",
|
||||
"name" => "Diana",
|
||||
|
@ -79,13 +79,13 @@ EOS
|
|||
str = <<-EOS.strip
|
||||
[
|
||||
[0] #<User:0x01234567
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
@marked_for_destruction = false,
|
||||
@new_record = true,
|
||||
@previously_changed = {},
|
||||
@readonly = false,
|
||||
attr_accessor :attributes = {
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
@marked_for_destruction = false,
|
||||
@new_record = true,
|
||||
@previously_changed = {},
|
||||
@readonly = false,
|
||||
attr_accessor :attributes = {
|
||||
"admin" => false,
|
||||
"created_at" => "?",
|
||||
"name" => "Diana",
|
||||
|
@ -99,13 +99,13 @@ EOS
|
|||
}
|
||||
>,
|
||||
[1] #<User:0x01234567
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
@marked_for_destruction = false,
|
||||
@new_record = true,
|
||||
@previously_changed = {},
|
||||
@readonly = false,
|
||||
attr_accessor :attributes = {
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
@marked_for_destruction = false,
|
||||
@new_record = true,
|
||||
@previously_changed = {},
|
||||
@readonly = false,
|
||||
attr_accessor :attributes = {
|
||||
"admin" => true,
|
||||
"created_at" => "?",
|
||||
"name" => "Laura",
|
||||
|
|
|
@ -27,8 +27,8 @@ begin
|
|||
out = @ap.send(:awesome, user)
|
||||
str = <<-EOS.strip
|
||||
#<MongoUser:0x01234567
|
||||
@_new = true,
|
||||
attr_accessor :_id = #<BSON::ObjectId:0x01234567
|
||||
@_new = true,
|
||||
attr_accessor :_id = #<BSON::ObjectId:0x01234567
|
||||
attr_accessor :data = [
|
||||
[ 0] 42,
|
||||
[ 1] 42,
|
||||
|
@ -44,9 +44,9 @@ begin
|
|||
[11] 42
|
||||
]
|
||||
>,
|
||||
attr_accessor :first_name = "Al",
|
||||
attr_accessor :last_name = "Capone",
|
||||
attr_reader :_id_before_type_cast = #<BSON::ObjectId:0x01234567
|
||||
attr_accessor :first_name = "Al",
|
||||
attr_accessor :last_name = "Capone",
|
||||
attr_reader :_id_before_type_cast = #<BSON::ObjectId:0x01234567
|
||||
attr_accessor :data = [
|
||||
[ 0] 42,
|
||||
[ 1] 42,
|
||||
|
@ -62,12 +62,12 @@ begin
|
|||
[11] 42
|
||||
]
|
||||
>,
|
||||
attr_reader :changed_attributes = {
|
||||
attr_reader :changed_attributes = {
|
||||
"first_name" => nil,
|
||||
"last_name" => nil
|
||||
},
|
||||
attr_reader :first_name_before_type_cast = "Al",
|
||||
attr_reader :last_name_before_type_cast = "Capone"
|
||||
attr_reader :last_name_before_type_cast = "Capone"
|
||||
>
|
||||
EOS
|
||||
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
|
||||
|
|
|
@ -393,18 +393,77 @@ EOS
|
|||
|
||||
#------------------------------------------------------------------------------
|
||||
describe "Negative options[:indent]" do
|
||||
before do
|
||||
@hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
|
||||
end
|
||||
|
||||
#
|
||||
# With Ruby < 1.9 the order of hash keys is not defined so we can't
|
||||
# reliably compare the output string.
|
||||
#
|
||||
it "hash keys must be left aligned" do
|
||||
out = @hash.ai(:plain => true, :indent => -4)
|
||||
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
|
||||
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
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue