Skip to content

Commit

Permalink
Add :collapsed option to the Reporter. Fixes #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Jan 24, 2011
1 parent 59b42d9 commit b13f6a2
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 27 deletions.
37 changes: 21 additions & 16 deletions lib/super_diff/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ def initialize(stdout)
@stdout = stdout
end

def report(data)
_report(data, 0, "*", true)
def report(data, options={})
_report(data, options.merge(:level => 0, :prefix => "*", :root => true))
end

private
def _report(data, level, prefix, root)
def _report(data, args)
return if data[:state] == :equal

formatted_prefix = format_prefix(prefix, level, root)
if !args[:root] && args[:collapsed] && data[:breakdown]
_report_breakdown(data[:breakdown], args)
return
end

formatted_prefix = format_prefix(args)

case data[:common_type]
when nil
Expand All @@ -37,36 +42,36 @@ def _report(data, level, prefix, root)
plural_type = pluralize(data[:common_type])
puts "#{formatted_prefix}: Differing #{plural_type}."
end
puts if root
if data[:state] == :inequal && (root || !data[:common_type] || !data[:breakdown])
maybe_bullet = root ? "" : indented_bullet(level + 1)
puts if args[:root]
if data[:state] == :inequal && (args[:root] || !data[:common_type] || !data[:breakdown])
maybe_bullet = args[:root] ? "" : indented_bullet(args[:level] + 1)
puts "#{maybe_bullet}Expected: #{data[:expected][:value].inspect}"
puts "#{maybe_bullet}Got: #{data[:actual][:value].inspect}"
end
if data[:breakdown]
if root
if args[:root]
puts
puts "Breakdown:"
end
_report_breakdown(data[:breakdown], level, prefix, root)
new_level = args[:root] ? args[:level] : args[:level]+1
_report_breakdown(data[:breakdown], args.merge(:level => new_level))
end
end

def _report_breakdown(breakdown, level, prefix, root)
new_level = root ? level : level+1
def _report_breakdown(breakdown, args)
breakdown.each do |(key, subdata)|
new_prefix = "*[#{key.inspect}]"
formatted_prefix = format_prefix(new_prefix, new_level, false)
_report(subdata, new_level, new_prefix, false)
new_prefix = args[:collapsed] ? "#{args[:prefix]}[#{key.inspect}]" : "*[#{key.inspect}]"
new_args = args.merge(:prefix => new_prefix, :root => false)
_report(subdata, new_args)
end
end

def indented_bullet(level)
(" " * (level * 2)) + "- "
end

def format_prefix(prefix, level, root)
(level == 0 && root) ? "Error" : indented_bullet(level) + prefix
def format_prefix(args)
args[:root] ? "Error" : indented_bullet(args[:level]) + args[:prefix]
end

def pluralize(word)
Expand Down
163 changes: 152 additions & 11 deletions spec/reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1491,19 +1491,160 @@ def out
end

specify "collapsed output" do
pending
@reporter.diff(
{
"foo" => {1 => {"baz" => {"quux" => 2}, "foz" => ["apple", "bananna", "orange"]}},
"biz" => {:fiz => ["bing", "bong", "bam"], 1 => {2 => :sym}},
"bananas" => {:apple => 11}
data = {
:state => :inequal,
:expected => {
:value => {
"foo" => {1 => {"baz" => {"quux" => 2}, "foz" => ["apple", "bananna", "orange"]}},
"biz" => {:fiz => ["bing", "bong", "bam"], 1 => {2 => :sym}},
"bananas" => {:apple => 11}
},
:type => :hash,
:size => 3
},
{
"foo" => {1 => {"foz" => ["apple", "banana", "orange"]}},
"biz" => {42 => {:raz => "matazz"}, :fiz => ["bang", "bong", "bam", "splat"], 1 => 3}
:actual => {
:value => {
"foo" => {1 => {"foz" => ["apple", "banana", "orange"]}},
"biz" => {42 => {:raz => "matazz"}, :fiz => ["bang", "bong", "bam", "splat"], 1 => 3}
},
:type => :hash,
:size => 2
},
:collapsed => true
)
:common_type => :hash,
:breakdown => [
["foo", {
:state => :inequal,
:expected => {
:value => {1 => {"baz" => {"quux" => 2}, "foz" => ["apple", "bananna", "orange"]}},
:type => :hash,
:size => 1
},
:actual => {
:value => {1 => {"foz" => ["apple", "banana", "orange"]}},
:type => :hash,
:size => 1
},
:common_type => :hash,
:breakdown => [
[1, {
:state => :inequal,
:expected => {
:value => {"baz" => {"quux" => 2}, "foz" => ["apple", "bananna", "orange"]},
:type => :hash,
:size => 2
},
:actual => {
:value => {"foz" => ["apple", "banana", "orange"]},
:type => :hash,
:size => 1
},
:common_type => :hash,
:breakdown => [
["baz", {
:state => :missing,
:expected => {:value => {"quux" => 2}, :type => :hash, :size => 1},
:actual => nil,
:common_type => nil,
}],
["foz", {
:state => :inequal,
:expected => {:value => ["apple", "bananna", "orange"], :type => :array, :size => 3},
:actual => {:value => ["apple", "banana", "orange"], :type => :array, :size => 3},
:common_type => :array,
:breakdown => [
[0, {
:state => :equal,
:expected => {:value => "apple", :type => :string},
:actual => {:value => "apple", :type => :string},
:common_type => :string
}],
[1, {
:state => :inequal,
:expected => {:value => "bananna", :type => :string},
:actual => {:value => "banana", :type => :string},
:common_type => :string
}],
[2, {
:state => :equal,
:expected => {:value => "orange", :type => :string},
:actual => {:value => "orange", :type => :string},
:common_type => :string
}]
]
}]
]
}]
]
}],
["biz", {
:state => :inequal,
:expected => {
:value => {:fiz => ["bing", "bong", "bam"], 1 => {2 => :sym}},
:type => :hash,
:size => 2
},
:actual => {
:value => {42 => {:raz => "matazz"}, :fiz => ["bang", "bong", "bam", "splat"], 1 => 3},
:type => :hash,
:size => 3
},
:common_type => :hash,
:breakdown => [
[:fiz, {
:state => :inequal,
:expected => {:value => ["bing", "bong", "bam"], :type => :array, :size => 3},
:actual => {:value => ["bang", "bong", "bam", "splat"], :type => :array, :size => 4},
:common_type => :array,
:breakdown => [
[0, {
:state => :inequal,
:expected => {:value => "bing", :type => :string},
:actual => {:value => "bang", :type => :string},
:common_type => :string
}],
[1, {
:state => :equal,
:expected => {:value => "bong", :type => :string},
:actual => {:value => "bong", :type => :string},
:common_type => :string,
}],
[2, {
:state => :equal,
:expected => {:value => "bam", :type => :string},
:actual => {:value => "bam", :type => :string},
:common_type => :string,
}],
[3, {
:state => :surplus,
:expected => nil,
:actual => {:value => "splat", :type => :string},
:common_type => nil
}]
]
}],
[1, {
:state => :inequal,
:expected => {:value => {2 => :sym}, :type => :hash, :size => 1},
:actual => {:value => 3, :type => :number},
:common_type => nil
}],
[42, {
:state => :surplus,
:expected => nil,
:actual => {:value => {:raz => "matazz"}, :type => :hash, :size => 1},
:common_type => nil
}]
]
}],
["bananas", {
:state => :missing,
:expected => {:value => {:apple => 11}, :type => :hash, :size => 1},
:actual => nil,
:common_type => nil
}]
]
}
@reporter.report(data, :collapsed => true)
msg = <<EOT
Error: Hashes of differing size and elements.
Expand Down

0 comments on commit b13f6a2

Please sign in to comment.