Permalink
Browse files

+ Refactored calculations down to 2 methods: total_score and totals. …

…(makaroni4)

[git-p4: depot-paths = "//src/flog/dev/": change = 8404]
  • Loading branch information...
1 parent 87efac1 commit f3339b9ef24e5daafd56cef87b101469ed3a8d89 @zenspider zenspider committed Apr 15, 2013
Showing with 47 additions and 48 deletions.
  1. +17 −26 lib/flog.rb
  2. +2 −2 lib/flog_cli.rb
  3. +3 −3 lib/gauntlet_flog.rb
  4. +22 −8 test/test_flog.rb
  5. +3 −9 test/test_flog_cli.rb
View
@@ -91,6 +91,7 @@ class Flog < SexpProcessor
attr_accessor :multiplier
attr_reader :calls, :option, :class_stack, :method_stack, :mass, :sclass
attr_reader :method_locations, :method_scores, :scores
+ attr_reader :total_score, :totals
# :startdoc:
@@ -108,7 +109,7 @@ def add_to_score name, score = OTHER_SCORES[name]
def average
return 0 if calls.size == 0
- total / calls.size
+ total_score / calls.size
end
##
@@ -142,11 +143,10 @@ def dsl_name? args
# Iterate over the calls sorted (descending) by score.
def each_by_score max = nil
- my_totals = totals
current = 0
- calls.sort_by { |k,v| -my_totals[k] }.each do |class_method, call_list|
- score = my_totals[class_method]
+ calls.sort_by { |k,v| -totals[k] }.each do |class_method, call_list|
+ score = totals[class_method]
yield class_method, score, call_list
@@ -169,6 +169,8 @@ def flog(*files)
flog_ruby ruby, file
end
+
+ calculate_total_scores
end
##
@@ -361,36 +363,25 @@ def signature
# Final threshold that is used for report
def threshold
- option[:all] ? nil : total * THRESHOLD
+ option[:all] ? nil : total_score * THRESHOLD
end
##
- # Calculates and returns the score (and total score on the side).
-
- def total # FIX: I hate this indirectness
- totals unless @total_score # calculates total_score as well
+ # Calculates the total score and populates @totals.
- @total_score
- end
+ def calculate_total_scores
+ return if @totals
- ##
- # Return the total score and populates @totals.
+ @total_score = 0
+ @totals = Hash.new(0)
- def totals
- unless @totals then
- @total_score = 0
- @totals = Hash.new(0)
+ calls.each do |meth, tally|
+ next if option[:methods] and meth =~ /##{@@no_method}$/
+ score = score_method(tally)
- calls.each do |meth, tally|
- next if option[:methods] and meth =~ /##{@@no_method}$/
- score = score_method(tally)
-
- @totals[meth] = score
- @total_score += score
- end
+ @totals[meth] = score
+ @total_score += score
end
-
- @totals
end
def no_method # :nodoc:
View
@@ -9,7 +9,7 @@ class FlogCLI
def_delegators :@flog, :average, :calculate, :each_by_score, :option
def_delegators :@flog, :method_locations, :method_scores, :reset, :scores
- def_delegators :@flog, :threshold, :total, :no_method
+ def_delegators :@flog, :threshold, :total_score, :no_method, :calculate_total_scores
##
# Expands +*dirs+ to all files within that match ruby and rake extensions.
@@ -221,7 +221,7 @@ def print_score io, name, score
# Report results to #io, STDOUT by default.
def report(io = $stdout)
- io.puts "%8.1f: %s" % [total, "flog total"]
+ io.puts "%8.1f: %s" % [total_score, "flog total"]
io.puts "%8.1f: %s" % [average, "flog/method average"]
return if option[:score]
View
@@ -18,7 +18,7 @@ class FlogGauntlet < Gauntlet
$syntax_error = {:total => -2, :average => -2, :methods => {}}
$no_gem = {:total => -4, :average => -4, :methods => {}}
- # copied straight from hoedown.rb
+ # copied straight from hoedown.rb
my_projects = %w[InlineFortran ParseTree RubyInline RubyToC
ZenHacks ZenTest bfts box_layout
change_class flay flog gauntlet heckle
@@ -56,7 +56,7 @@ def display_report max = 10
puts "avg methods / gem : %8.2f (%8.2f stddev)" % [method_counts.average, method_counts.stddev]
puts "avg flog / gem : %8.2f (%8.2f stddev)" % [flog_numbers.average, flog_numbers.stddev]
end
-
+
worst = scores.sort_by { |k,v| -v[:total] }.first(max)
report_worst "Worst Projects EVAR", worst do |project, score|
owner = $owners[project].join(', ') rescue nil
@@ -120,7 +120,7 @@ def score_for dir
topN = Hash[*methods.sort_by { |k,v| -v }.first(n).flatten]
{
:max => methods.values.max,
- :total => flogger.total,
+ :total => flogger.total_score,
:size => methods.size,
:average => flogger.average,
:stddev => flogger.stddev,
View
@@ -12,6 +12,9 @@ def setup_flog
$stdin.rewind
@flog.flog "-"
+ # REFACTOR
+ # @flog here is not Flog, but FlogCLI in some tests
+ @flog.calculate_total_scores
ensure
$stdin = old_stdin
end
@@ -47,7 +50,7 @@ def test_flog
exp = { "main#none" => { :+ => 1.0, :lit_fixnum => 0.6 } }
assert_equal exp, @flog.calls
- assert_equal 1.6, @flog.total unless @flog.option[:methods]
+ assert_equal 1.6, @flog.total_score unless @flog.option[:methods]
assert_equal 3, @flog.mass["-"]
end
@@ -56,11 +59,12 @@ def test_flog_ruby
file = "sample.rb"
@flog.flog_ruby ruby, file
+ @flog.calculate_total_scores
exp = { "main#none" => { :+ => 1.0, :lit_fixnum => 0.6 } }
assert_equal exp, @flog.calls
- assert_equal 1.6, @flog.total unless @flog.option[:methods]
+ assert_equal 1.6, @flog.total_score unless @flog.option[:methods]
assert_equal 3, @flog.mass[file]
end
@@ -296,11 +300,12 @@ def test_process_defn_in_self
setup
@flog.process sexp
+ @flog.calculate_total_scores
exp = {'main::x' => {:lit_fixnum => 0.375}, 'main#none' => {:sclass => 5.0}}
assert_equal exp, @flog.calls
- assert_in_delta 5.375, @flog.total
+ assert_in_delta 5.375, @flog.total_score
end
def test_process_defn_in_self_after_self
@@ -312,11 +317,12 @@ def test_process_defn_in_self_after_self
setup
@flog.process sexp
+ @flog.calculate_total_scores
exp = {'main::x' => {:lit_fixnum => 0.375}, 'main#none' => {:sclass => 12.5}}
assert_equal exp, @flog.calls
- assert_in_delta 12.875, @flog.total
+ assert_in_delta 12.875, @flog.total_score
end
def test_process_defs
@@ -428,9 +434,10 @@ def test_process_iter_dsl_namespaced
setup
@flog.process sexp
+ @flog.calculate_total_scores
assert_equal hash, @flog.calls
- assert_in_delta score, @flog.total
+ assert_in_delta score, @flog.total_score
end
def test_process_lit
@@ -526,9 +533,11 @@ def test_signature
assert_equal "main#y", @flog.signature
end
- def test_total
+ def test_total_score
@flog.add_to_score "blah", 2
- assert_equal 2.0, @flog.total
+ @flog.calculate_total_scores
+
+ assert_equal 2.0, @flog.total_score
end
def test_max_method
@@ -538,6 +547,7 @@ def test_max_method
"main#meth_two" => {"foo" => 2.0, "bar" => 14.0},
}
+ @flog.calculate_total_scores
assert_equal ["main#meth_two", 16.0], @flog.max_method
end
@@ -547,6 +557,7 @@ def test_max_score
"main#meth_one" => {"foo" => 1.0, "bar" => 1.0},
"main#meth_two" => {"foo" => 2.0, "bar" => 14.0},
}
+ @flog.calculate_total_scores
assert_equal 16.0, @flog.max_score
end
@@ -563,7 +574,9 @@ def util_process sexp, score = -1, hash = {}
assert_equal exp, @flog.calls
end
- assert_in_delta score, @flog.total
+ @flog.calculate_total_scores
+
+ assert_in_delta score, @flog.total_score
end
def test_threshold
@@ -579,6 +592,7 @@ def test_no_threshold
def test_calculate
setup_my_klass
+ @flog.calculate_total_scores
@flog.calculate
assert_equal({ 'MyKlass' => 42.0 }, @flog.scores)
View
@@ -84,8 +84,6 @@ def test_output_details
@flog.option[:all] = true
setup_flog
- @flog.totals["main#something"] = 42.0
-
o = StringIO.new
@flog.output_details o
@@ -99,6 +97,7 @@ def test_output_details_grouped
setup_flog
o = StringIO.new
+ @flog.calculate_total_scores
@flog.output_details_grouped o
expected = "\n 1.6: main total\n 1.6: main#none\n"
@@ -111,8 +110,6 @@ def test_output_details_methods
setup_flog
- @flog.totals["main#something"] = 42.0 # TODO: no sense... why no output?
-
o = StringIO.new
@flog.output_details o
@@ -125,8 +122,6 @@ def test_output_details_detailed
setup_flog
- @flog.totals["main#something"] = 42.0
-
o = StringIO.new
@flog.output_details o, nil
@@ -161,14 +156,14 @@ def test_report_all
$stdin.rewind
@flog.flog "-"
- @flog.totals["main#something"] = 42.0
exp = { "main#none" => { :+ => 1.0, :lit_fixnum => 0.6 } }
assert_equal exp, @flog.calls
@flog.option[:all] = true
+ @flog.calculate_total_scores
- assert_equal 1.6, @flog.total unless @flog.option[:methods]
+ assert_equal 1.6, @flog.total_score unless @flog.option[:methods]
assert_equal 3, @flog.mass["-"]
o = StringIO.new
@@ -205,4 +200,3 @@ def test_report_group
assert_equal expected, o.string
end
end
-

0 comments on commit f3339b9

Please sign in to comment.