Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

+ Refactored n_way_diff into split_and_group, collapse_and_label, and…

… pad_with_empty_strings.

+ n_way_diff now does leading comments separately from the code, to better align diffs.

[git-p4: depot-paths = "//src/flay/dev/": change = 8523]
  • Loading branch information...
commit 0f3a078de69ca9604bd9d516c630ee181f5a0137 1 parent be6f01d
@zenspider zenspider authored
Showing with 184 additions and 19 deletions.
  1. +32 −18 lib/flay.rb
  2. +152 −1 test/test_flay.rb
View
50 lib/flay.rb
@@ -364,34 +364,48 @@ def prune_liberally
# given.
def n_way_diff *data
- data.each_with_index do |s, i|
- c = (?A.ord + i).chr
- s.group = c
+ comments = []
+ codes = []
+
+ split_and_group(data).each do |subdata|
+ n = subdata.find_index { |s| s !~ /^#/ }
+
+ comment, code = subdata[0..n-1], subdata[n..-1]
+
+ comments << comment
+ codes << code
end
- max = data.map { |s| s.scan(/^.*/).size }.max
+ comments = collapse_and_label pad_with_empty_strings comments
+ codes = collapse_and_label pad_with_empty_strings codes
- data.map! { |s| # FIX: this is tarded, but I'm out of brain
- c = s.group
- s = s.scan(/^.*/)
- s.push(*([""] * (max - s.size))) # pad
- s.each do |o|
- o.group = c
- end
- s
+ (comments + codes).flatten.join("\n")
+ end
+
+ def split_and_group ary # :nodoc:
+ ary.enum_with_index.map { |s, i|
+ c = (?A.ord + i).chr
+ s.scan(/^.*/).map { |s2|
+ s2.group = c
+ s2
+ }
}
+ end
+
+ def pad_with_empty_strings ary # :nodoc:
+ max = ary.map { |s| s.size }.max
+
+ ary.map { |a| a + ([""] * (max - a.size)) }
+ end
- groups = data[0].zip(*data[1..-1])
- groups.map! { |lines|
- collapsed = lines.uniq
- if collapsed.size == 1 then
+ def collapse_and_label ary # :nodoc:
+ ary[0].zip(*ary[1..-1]).map { |lines|
+ if lines.uniq.size == 1 then
" #{lines.first}"
else
- # TODO: make r2r have a canonical mode (doesn't make 1-liners)
lines.reject { |l| l.empty? }.map { |l| "#{l.group}: #{l}" }
end
}
- groups.flatten.join("\n")
end
##
View
153 test/test_flay.rb
@@ -65,11 +65,21 @@ def test_all_structural_subhashes
end
DOG_AND_CAT = Ruby18Parser.new.process <<-RUBY
+ ##
+ # I am a dog.
+
class Dog
def x
return "Hello"
end
end
+
+ ##
+ # I
+ # am
+ # a
+ # cat.
+
class Cat
def y
return "Hello"
@@ -200,6 +210,38 @@ def test_process_sexp_no_structure
def test_report
# make sure we run through options parser
$*.clear
+ $* << "--mass=1"
+ $* << "-v"
+
+ opts = nil
+ capture_io do # ignored
+ opts = Flay.parse_options
+ end
+
+ flay = Flay.new opts
+
+ flay.process_sexp DOG_AND_CAT.deep_clone
+ flay.analyze
+
+ out, err = capture_io do
+ flay.report nil
+ end
+
+ exp = <<-END.gsub(/\d+/, "N").gsub(/^ {6}/, "")
+ Total score (lower is better) = 16
+
+ 1) Similar code found in :class (mass = 16)
+ (string):1
+ (string):6
+ END
+
+ assert_equal '', err
+ assert_equal exp, out.gsub(/\d+/, "N")
+ end
+
+ def test_report_diff
+ # make sure we run through options parser
+ $*.clear
$* << "-d"
$* << "--mass=1"
$* << "-v"
@@ -225,6 +267,13 @@ def test_report
A: (string):1
B: (string):6
+ ##
+ A: # I am a dog.
+ B: # I
+ B: # am
+ B: # a
+ B: # cat.
+
A: class Dog
B: class Cat
A: def x
@@ -235,6 +284,108 @@ def test_report
END
assert_equal '', err
- assert_equal exp, out.gsub(/\d+/, "N")
+ assert_equal exp, out.gsub(/\d+/, "N").gsub(/^ {3}$/, "")
+ end
+
+ def test_n_way_diff
+ dog_and_cat = ["##\n# I am a dog.\n\nclass Dog\n def x\n return \"Hello\"\n end\nend",
+ "##\n# I\n#\n# am\n# a\n# cat.\n\nclass Cat\n def y\n return \"Hello\"\n end\nend"]
+
+ flay = Flay.new
+
+ exp = <<-EOM.gsub(/\d+/, "N").gsub(/^ {6}/, "").chomp
+ ##
+ A: # I am a dog.
+ B: # I
+ B: #
+ B: # am
+ B: # a
+ B: # cat.
+
+ A: class Dog
+ B: class Cat
+ A: def x
+ B: def y
+ return \"Hello\"
+ end
+ end
+ EOM
+
+ assert_equal exp, flay.n_way_diff(*dog_and_cat).gsub(/^ {3}$/, "")
+ end
+
+ def test_split_and_group
+ flay = Flay.new
+
+ act = flay.split_and_group ["a\nb\nc", "d\ne\nf"]
+ exp = [%w(a b c), %w(d e f)]
+
+ assert_equal exp, act
+ assert_equal [%w(A A A), %w(B B B)], act.map { |a| a.map { |s| s.group } }
+ end
+
+ def test_pad_with_empty_strings
+ flay = Flay.new
+
+ a = %w(a b c)
+ b = %w(d)
+
+ assert_equal [a, ["d", "", ""]], flay.pad_with_empty_strings([a, b])
+ end
+
+ def test_pad_with_empty_strings_same
+ flay = Flay.new
+
+ a = %w(a b c)
+ b = %w(d e f)
+
+ assert_equal [a, b], flay.pad_with_empty_strings([a, b])
+ end
+
+ def test_collapse_and_label
+ flay = Flay.new
+
+ a = %w(a b c).map { |s| s.group = "A"; s }
+ b = %w(d b f).map { |s| s.group = "B"; s }
+
+ exp = [["A: a", "B: d"], " b", ["A: c", "B: f"]]
+
+ assert_equal exp, flay.collapse_and_label([a, b])
+ end
+
+ def test_collapse_and_label_same
+ flay = Flay.new
+
+ a = %w(a b c).map { |s| s.group = "A"; s }
+ b = %w(a b c).map { |s| s.group = "B"; s }
+
+ exp = [" a", " b", " c"]
+
+ assert_equal exp, flay.collapse_and_label([a, b])
+ end
+
+ def test_n_way_diff_methods
+ dog_and_cat = ["##\n# I am a dog.\n\ndef x\n return \"Hello\"\nend",
+ "##\n# I\n#\n# am\n# a\n# cat.\n\ndef y\n return \"Hello\"\nend"]
+
+ opts = Flay.parse_options
+ flay = Flay.new opts
+
+ exp = <<-EOM.gsub(/\d+/, "N").gsub(/^ {6}/, "").chomp
+ ##
+ A: # I am a dog.
+ B: # I
+ B: #
+ B: # am
+ B: # a
+ B: # cat.
+
+ A: def x
+ B: def y
+ return \"Hello\"
+ end
+ EOM
+
+ assert_equal exp, flay.n_way_diff(*dog_and_cat).gsub(/^ {3}$/, "")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.