Skip to content

Commit

Permalink
Fixed nested left alighment; fixed ActiveRecord and MongoMapper objec…
Browse files Browse the repository at this point in the history
…t specs
  • Loading branch information
Mike Dvorkin committed Nov 9, 2011
1 parent 782e382 commit d2c0a39
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 36 deletions.
8 changes: 5 additions & 3 deletions lib/awesome_print/formatter.rb
Expand Up @@ -271,10 +271,12 @@ def plain_single_line
#------------------------------------------------------------------------------
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
Expand All @@ -289,7 +291,7 @@ def indented
end

def left_aligned
current, @options[:indent] = @options[:indent], @options[:indent].abs * -1
current, @options[:indent] = @options[:indent], 0
yield
ensure
@options[:indent] = current
Expand Down
42 changes: 21 additions & 21 deletions spec/ext/active_record_spec.rb
Expand Up @@ -50,13 +50,13 @@ def self.columns
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",
Expand All @@ -79,13 +79,13 @@ def self.columns
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",
Expand All @@ -99,13 +99,13 @@ def self.columns
}
>,
[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",
Expand Down
14 changes: 7 additions & 7 deletions spec/ext/mongo_mapper_spec.rb
Expand Up @@ -27,8 +27,8 @@ class MongoUser
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,
Expand All @@ -44,9 +44,9 @@ class MongoUser
[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,
Expand All @@ -62,12 +62,12 @@ class MongoUser
[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")
Expand Down
69 changes: 64 additions & 5 deletions spec/formats_spec.rb
Expand Up @@ -393,18 +393,77 @@

#------------------------------------------------------------------------------
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

#------------------------------------------------------------------------------
Expand Down

0 comments on commit d2c0a39

Please sign in to comment.