Permalink
Browse files

+ Allow plugins to provide sexp to source converters for --diff. (Unc…

…leGene)

[git-p4: depot-paths = "//src/flay/dev/": change = 8827]
  • Loading branch information...
1 parent ee313d0 commit bf188494f401d929a7612eec958e6a5dfe0da2ad @zenspider zenspider committed Jul 23, 2013
Showing with 58 additions and 4 deletions.
  1. +17 −4 lib/flay.rb
  2. +41 −0 test/test_flay.rb
View
21 lib/flay.rb
@@ -165,8 +165,6 @@ def initialize option = nil
self.masses = {}
self.total = 0
self.mass_threshold = @option[:mass]
-
- require 'ruby2ruby' if @option[:diff]
end
##
@@ -488,11 +486,26 @@ def report prune = nil
if option[:diff] then
puts
- r2r = Ruby2Ruby.new
- puts n_way_diff(*nodes.map { |s| r2r.process(s.deep_clone) })
+ sources = nodes.map do |s|
+ msg = "sexp_to_#{File.extname(s.file).sub(/./, '')}"
+ self.respond_to?(msg) ? self.send(msg, s) : sexp_to_rb(s)
+ end
+ puts n_way_diff(*sources)
end
end
end
+
+ private
+
+ def sexp_to_rb sexp
+ begin
+ require 'ruby2ruby'
+ rescue LoadError
+ return 'ruby2ruby is required for diff'
+ end
+ @r2r ||= Ruby2Ruby.new
+ @r2r.process sexp.deep_clone
+ end
end
class String
View
41 test/test_flay.rb
@@ -287,6 +287,47 @@ def test_report_diff
assert_equal exp, out.gsub(/\d+/, "N").gsub(/^ {3}$/, "")
end
+ def test_report_diff_plugin_converter
+ # make sure we run through options parser
+ $*.clear
+ $* << "-d"
+ $* << "--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
+
+ # (string) does not have extension, maps to :sexp_to_
+ Flay.send(:define_method, :sexp_to_){|s| "source code #{s.line}"}
+
+ out, err = capture_io do
+ flay.report nil
+ end
+
+ Flay.send(:remove_method, :sexp_to_)
+
+ exp = <<-END.gsub(/\d+/, "N").gsub(/^ {6}/, "")
+ Total score (lower is better) = 16
+
+ 1) Similar code found in :class (mass = 16)
+ A: (string):1
+ B: (string):6
+
+ A: source code 1
+ B: source code 6
+ END
+
+ assert_equal '', err
+ 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"]

0 comments on commit bf18849

Please sign in to comment.