Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

+ Refactored to #calculate, #threshold. (makaroni4)

+ Added accessors for methods, scores. Now available for CIs! (makaroni4)

[git-p4: depot-paths = "//src/flog/dev/": change = 8390]
  • Loading branch information...
commit 40e4982937f1024aea151b7e9843c6abac378b16 1 parent 360ca6d
@zenspider zenspider authored
Showing with 48 additions and 15 deletions.
  1. +22 −12 lib/flog.rb
  2. +26 −3 test/test_flog.rb
View
34 lib/flog.rb
@@ -93,6 +93,7 @@ class Flog < SexpProcessor
attr_accessor :multiplier
attr_reader :calls, :option, :class_stack, :method_stack, :mass, :sclass
attr_reader :method_locations
+ attr_reader :methods, :scores
# :startdoc:
##
@@ -423,23 +424,25 @@ def output_details io, max = nil
io.puts
end
end
- # io.puts
end
##
- # Output the report, grouped by class/module, up to a given max or
- # report everything, if nil.
-
- def output_details_grouped io, max = nil
- scores = Hash.new 0
- methods = Hash.new { |h,k| h[k] = [] }
-
- each_by_score max do |class_method, score, call_list|
+ # Calculates classes and methods scores.
+ def calculate
+ each_by_score threshold do |class_method, score, call_list|
klass = class_method.split(/#|::/).first
methods[klass] << [class_method, score]
scores[klass] += score
end
+ end
+
+ ##
+ # Output the report, grouped by class/module, up to a given max or
+ # report everything, if nil.
+
+ def output_details_grouped io, threshold = nil
+ calculate
scores.sort_by { |_, n| -n }.each do |klass, total|
io.puts
@@ -484,6 +487,12 @@ def process_until_empty exp
end
##
+ # Final threshold that is used for report
+ def threshold
+ option[:all] ? nil : total * THRESHOLD
+ end
+
+ ##
# Report results to #io, STDOUT by default.
def report(io = $stdout)
@@ -492,11 +501,10 @@ def report(io = $stdout)
return if option[:score]
- max = option[:all] ? nil : total * THRESHOLD
if option[:group] then
- output_details_grouped io, max
+ output_details_grouped io, threshold
else
- output_details io, max
+ output_details io, threshold
end
ensure
self.reset
@@ -509,6 +517,8 @@ def reset
@totals = @total_score = nil
@multiplier = 1.0
@calls = Hash.new { |h,k| h[k] = Hash.new 0 }
+ @methods = Hash.new { |h,k| h[k] = [] }
+ @scores = Hash.new 0
end
##
View
29 test/test_flog.rb
@@ -12,9 +12,7 @@ def setup
def test_add_to_score
assert_empty @flog.calls
- @flog.class_stack << "Base" << "MyKlass"
- @flog.method_stack << "mymethod"
- @flog.add_to_score "blah", 42
+ setup_my_klass
expected = {"MyKlass::Base#mymethod" => {"blah" => 42.0}}
assert_equal expected, @flog.calls
@@ -738,4 +736,29 @@ def util_process sexp, score = -1, hash = {}
assert_in_delta score, @flog.total
end
+
+ def test_threshold
+ test_flog
+ assert_equal Flog::THRESHOLD * 1.6, @flog.threshold
+ end
+
+ def test_no_threshold
+ @flog.option[:all] = true
+ assert_equal nil, @flog.threshold
+ end
+
+ def test_calculate
+ setup_my_klass
+
+ @flog.calculate
+
+ assert_equal({ 'MyKlass' => 42.0 }, @flog.scores)
+ assert_equal({ 'MyKlass' => [["MyKlass::Base#mymethod", 42.0]] }, @flog.methods)
+ end
+
+ def setup_my_klass
+ @flog.class_stack << "Base" << "MyKlass"
+ @flog.method_stack << "mymethod"
+ @flog.add_to_score "blah", 42
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.