Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored all plugin, option, output, and cmdline stuff to FlogCLI.

Resorted methods in Flog as they got messy.

[git-p4: depot-paths = "//src/flog/dev/": change = 8393]
  • Loading branch information...
commit 6357978a44bdf4041ab963e49b9dc6c7bb684391 1 parent 9ec60a5
@zenspider zenspider authored
View
2  Manifest.txt
@@ -5,6 +5,8 @@ README.txt
Rakefile
bin/flog
lib/flog.rb
+lib/flog_cli.rb
lib/flog_task.rb
lib/gauntlet_flog.rb
test/test_flog.rb
+test/test_flog_cli.rb
View
9 bin/flog
@@ -1,15 +1,14 @@
#!/usr/local/bin/ruby -w
-require 'optparse'
-require 'flog'
+require "flog_cli"
-Flog.load_plugins
+FlogCLI.load_plugins
-options = Flog.parse_options ARGV
+options = FlogCLI.parse_options ARGV
ARGV << "-" if ARGV.empty?
-flogger = Flog.new options
+flogger = FlogCLI.new options
flogger.flog ARGV
flogger.report
View
312 lib/flog.rb
@@ -1,8 +1,6 @@
-require 'rubygems'
-require 'sexp_processor'
-require 'ruby_parser'
-require 'optparse'
-require 'timeout'
+require "sexp_processor"
+require "ruby_parser"
+require "timeout"
class File
RUBY19 = "<3".respond_to? :encoding unless defined? RUBY19 # :nodoc:
@@ -98,160 +96,47 @@ class Flog < SexpProcessor
# :startdoc:
##
- # The known plugins for Flog. See Flog.load_plugins.
+ # Add a score to the tally. Score can be predetermined or looked up
+ # automatically. Uses multiplier for additional spankings.
+ # Spankings!
- def self.plugins
- @plugins ||= {}
+ def add_to_score name, score = OTHER_SCORES[name]
+ @calls[signature][name] += score * @multiplier
end
##
- # Loads all flog plugins. Files must be named "flog/*.rb".
-
- def self.load_plugins
- # TODO: I think I want to do this more like hoe's plugin system. Generalize?
- loaded, found = {}, {}
-
- Gem.find_files("flog/*.rb").reverse.each do |path|
- found[File.basename(path, ".rb").intern] = path
- end
-
- found.each do |name, plugin|
- next if loaded[name]
- begin
- warn "loading #{plugin}" # if $DEBUG
- loaded[name] = load plugin
- rescue LoadError => e
- warn "error loading #{plugin.inspect}: #{e.message}. skipping..."
- end
- end
-
- self.plugins.merge loaded
-
- names = Flog.constants.map {|s| s.to_s}.reject {|n| n =~ /^[A-Z_]+$/}
-
- names.each do |name|
- # next unless Hoe.plugins.include? name.downcase.intern
- mod = Flog.const_get(name)
- next if Class === mod
- warn "extend #{mod}" if $DEBUG
- # self.extend mod
- end
- end
+ # really?
- ##
- # Expands +*dirs+ to all files within that match ruby and rake extensions.
- # --
- # REFACTOR: from flay
-
- def self.expand_dirs_to_files *dirs
- extensions = %w[rb rake]
-
- dirs.flatten.map { |p|
- if File.directory? p then
- Dir[File.join(p, '**', "*.{#{extensions.join(',')}}")]
- else
- p
- end
- }.flatten.sort
+ def average
+ return 0 if calls.size == 0
+ total / calls.size
end
##
- # Parse options in +args+ (defaults to ARGV).
-
- def self.parse_options args = ARGV
- option = {
- :quiet => false,
- :continue => false,
- :parser => RubyParser,
- }
-
- OptionParser.new do |opts|
- opts.separator "Standard options:"
-
- opts.on("-a", "--all", "Display all flog results, not top 60%.") do
- option[:all] = true
- end
-
- opts.on("-b", "--blame", "Include blame information for methods.") do
- option[:blame] = true
- end
-
- opts.on("-c", "--continue", "Continue despite syntax errors.") do
- option[:continue] = true
- end
-
- opts.on("-d", "--details", "Show method details.") do
- option[:details] = true
- end
-
- opts.on("-g", "--group", "Group and sort by class.") do
- option[:group] = true
- end
-
- opts.on("-h", "--help", "Show this message.") do
- puts opts
- exit
- end
-
- opts.on("-I dir1,dir2,dir3", Array, "Add to LOAD_PATH.") do |dirs|
- dirs.each do |dir|
- $: << dir
- end
- end
-
- opts.on("-m", "--methods-only", "Skip code outside of methods.") do
- option[:methods] = true
- end
-
- opts.on("-q", "--quiet", "Don't show parse errors.") do
- option[:quiet] = true
- end
-
- opts.on("-s", "--score", "Display total score only.") do
- option[:score] = true
- end
-
- opts.on("-v", "--verbose", "Display progress during processing.") do
- option[:verbose] = true
- end
-
- opts.on("--18", "Use a ruby 1.8 parser.") do
- option[:parser] = Ruby18Parser
- end
-
- opts.on("--19", "Use a ruby 1.9 parser.") do
- option[:parser] = Ruby19Parser
- end
-
- next if self.plugins.empty?
- opts.separator "Plugin options:"
-
- extra = self.methods.grep(/parse_options/) - %w(parse_options)
-
- extra.sort.each do |msg|
- self.send msg, opts, option
- end
+ # Calculates classes and methods scores.
- end.parse! Array(args)
+ def calculate
+ each_by_score threshold do |class_method, score, call_list|
+ klass = class_method.split(/#|::/).first
- option
+ methods[klass] << [class_method, score]
+ scores[klass] += score
+ end
end
##
- # Add a score to the tally. Score can be predetermined or looked up
- # automatically. Uses multiplier for additional spankings.
- # Spankings!
+ # Returns true if the form looks like a "DSL" construct.
+ #
+ # task :blah do ... end
+ # => s(:iter, s(:call, nil, :task, s(:lit, :blah)), ...)
- def add_to_score name, score = OTHER_SCORES[name]
- @calls[signature][name] += score * @multiplier
- end
+ def dsl_name? args
+ return false unless args and not args.empty?
- ##
- # really?
+ first_arg = args.first
+ first_arg = first_arg[1] if first_arg[0] == :hash
- def average
- return 0 if calls.size == 0
- total / calls.size
+ [:lit, :str].include? first_arg[0] and first_arg[1]
end
##
@@ -272,12 +157,12 @@ def each_by_score max = nil
end
##
- # Flog the given files or directories. Smart. Deals with "-", syntax
- # errors, and traversing subdirectories intelligently.
-
- def flog(*files_or_dirs)
- files = Flog.expand_dirs_to_files(*files_or_dirs)
+ # Flog the given files. Deals with "-", and syntax errors.
+ #
+ # Not as smart as FlogCLI's #flog method as it doesn't traverse
+ # dirs. Use FlogCLI.expand_dirs_to_files or see FlogCLI#flog.
+ def flog(*files)
files.each do |file|
next unless file == '-' or File.readable? file
@@ -398,63 +283,27 @@ def klass_name
end
##
- # Returns the first method in the list, or "#none" if there are
- # none.
-
- def method_name
- m = @method_stack.first || @@no_method
- m = "##{m}" unless m =~ /::/
- m
- end
-
- ##
- # Output the report up to a given max or report everything, if nil.
-
- def output_details io, max = nil
- io.puts
-
- each_by_score max do |class_method, score, call_list|
- return 0 if option[:methods] and class_method =~ /##{@@no_method}/
-
- self.print_score io, class_method, score
+ # Returns the method/score pair of the maximum score.
- if option[:details] then
- call_list.sort_by { |k,v| -v }.each do |call, count|
- io.puts " %6.1f: %s" % [count, call]
- end
- io.puts
- end
- end
+ def max_method
+ totals.max_by { |_, score| score }
end
##
- # Calculates classes and methods scores.
-
- def calculate
- each_by_score threshold do |class_method, score, call_list|
- klass = class_method.split(/#|::/).first
+ # Returns the maximum score for a single method. Used for FlogTask.
- methods[klass] << [class_method, score]
- scores[klass] += score
- end
+ def max_score
+ max_method.last
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
-
- io.puts "%8.1f: %s" % [total, "#{klass} total"]
+ # Returns the first method in the list, or "#none" if there are
+ # none.
- methods[klass].each do |name, score|
- self.print_score io, name, score
- end
- end
+ def method_name
+ m = @method_stack.first || @@no_method
+ m = "##{m}" unless m =~ /::/
+ m
end
##
@@ -470,18 +319,6 @@ def penalize_by bonus
end
##
- # Print out one formatted score.
-
- def print_score io, name, score
- location = @method_locations[name]
- if location then
- io.puts "%8.1f: %-32s %s" % [score, name, location]
- else
- io.puts "%8.1f: %s" % [score, name]
- end
- end
-
- ##
# Process each element of #exp in turn.
def process_until_empty exp
@@ -489,31 +326,6 @@ 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)
- io.puts "%8.1f: %s" % [total, "flog total"]
- io.puts "%8.1f: %s" % [average, "flog/method average"]
-
- return if option[:score]
-
- if option[:group] then
- output_details_grouped io, threshold
- else
- output_details io, threshold
- end
- ensure
- self.reset
- end
-
- ##
# Reset score data
def reset
@@ -547,26 +359,19 @@ def signature
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
+ # Final threshold that is used for report
- @total_score
+ def threshold
+ option[:all] ? nil : total * THRESHOLD
end
##
- # Returns the maximum score for a single method. Used for FlogTask.
-
- def max_score
- max_method.last
- end
+ # Calculates and returns the score (and total score on the side).
- ##
- # Returns the method/score pair of the maximum score.
+ def total # FIX: I hate this indirectness
+ totals unless @total_score # calculates total_score as well
- def max_method
- totals.max_by { |_, score| score }
+ @total_score
end
##
@@ -589,6 +394,10 @@ def totals
@totals
end
+ def no_method # :nodoc:
+ @@no_method
+ end
+
############################################################
# Process Methods:
@@ -733,15 +542,6 @@ def process_if(exp)
s()
end
- def dsl_name? args
- return false unless args and not args.empty?
-
- first_arg = args.first
- first_arg = first_arg[1] if first_arg[0] == :hash
-
- [:lit, :str].include? first_arg[0] and first_arg[1]
- end
-
def process_iter(exp)
context = (self.context - [:class, :module, :scope])
context = context.uniq.sort_by { |s| s.to_s }
View
237 lib/flog_cli.rb
@@ -0,0 +1,237 @@
+require "rubygems"
+require "optparse"
+require "forwardable"
+
+require "flog"
+
+class FlogCLI
+ extend Forwardable
+
+ def_delegators :@flog, :average, :calculate, :each_by_score
+ def_delegators :@flog, :method_locations, :methods, :reset, :scores
+ def_delegators :@flog, :threshold, :total, :no_method
+
+ ##
+ # Expands +*dirs+ to all files within that match ruby and rake extensions.
+ # --
+ # REFACTOR: from flay
+
+ def self.expand_dirs_to_files *dirs
+ extensions = %w[rb rake]
+
+ dirs.flatten.map { |p|
+ if File.directory? p then
+ Dir[File.join(p, '**', "*.{#{extensions.join(',')}}")]
+ else
+ p
+ end
+ }.flatten.sort
+ end
+
+ ##
+ # Loads all flog plugins. Files must be named "flog/*.rb".
+
+ def self.load_plugins
+ # TODO: I think I want to do this more like hoe's plugin system. Generalize?
+ loaded, found = {}, {}
+
+ Gem.find_files("flog/*.rb").reverse.each do |path|
+ found[File.basename(path, ".rb").intern] = path
+ end
+
+ found.each do |name, plugin|
+ next if loaded[name]
+ begin
+ warn "loading #{plugin}" # if $DEBUG
+ loaded[name] = load plugin
+ rescue LoadError => e
+ warn "error loading #{plugin.inspect}: #{e.message}. skipping..."
+ end
+ end
+
+ self.plugins.merge loaded
+
+ names = Flog.constants.map {|s| s.to_s}.reject {|n| n =~ /^[A-Z_]+$/}
+
+ names.each do |name|
+ # next unless Hoe.plugins.include? name.downcase.intern
+ mod = Flog.const_get(name)
+ next if Class === mod
+ warn "extend #{mod}" if $DEBUG
+ # self.extend mod
+ end
+ end
+
+ ##
+ # Parse options in +args+ (defaults to ARGV).
+
+ def self.parse_options args = ARGV
+ option = {
+ :quiet => false,
+ :continue => false,
+ :parser => RubyParser,
+ }
+
+ OptionParser.new do |opts|
+ opts.separator "Standard options:"
+
+ opts.on("-a", "--all", "Display all flog results, not top 60%.") do
+ option[:all] = true
+ end
+
+ opts.on("-b", "--blame", "Include blame information for methods.") do
+ option[:blame] = true
+ end
+
+ opts.on("-c", "--continue", "Continue despite syntax errors.") do
+ option[:continue] = true
+ end
+
+ opts.on("-d", "--details", "Show method details.") do
+ option[:details] = true
+ end
+
+ opts.on("-g", "--group", "Group and sort by class.") do
+ option[:group] = true
+ end
+
+ opts.on("-h", "--help", "Show this message.") do
+ puts opts
+ exit
+ end
+
+ opts.on("-I dir1,dir2,dir3", Array, "Add to LOAD_PATH.") do |dirs|
+ dirs.each do |dir|
+ $: << dir
+ end
+ end
+
+ opts.on("-m", "--methods-only", "Skip code outside of methods.") do
+ option[:methods] = true
+ end
+
+ opts.on("-q", "--quiet", "Don't show parse errors.") do
+ option[:quiet] = true
+ end
+
+ opts.on("-s", "--score", "Display total score only.") do
+ option[:score] = true
+ end
+
+ opts.on("-v", "--verbose", "Display progress during processing.") do
+ option[:verbose] = true
+ end
+
+ opts.on("--18", "Use a ruby 1.8 parser.") do
+ option[:parser] = Ruby18Parser
+ end
+
+ opts.on("--19", "Use a ruby 1.9 parser.") do
+ option[:parser] = Ruby19Parser
+ end
+
+ next if self.plugins.empty?
+ opts.separator "Plugin options:"
+
+ extra = self.methods.grep(/parse_options/) - %w(parse_options)
+
+ extra.sort.each do |msg|
+ self.send msg, opts, option
+ end
+
+ end.parse! Array(args)
+
+ option
+ end
+
+ ##
+ # The known plugins for Flog. See Flog.load_plugins.
+
+ def self.plugins
+ @plugins ||= {}
+ end
+
+ ##
+ # Flog the given files or directories. Smart. Deals with "-", syntax
+ # errors, and traversing subdirectories intelligently.
+
+ def flog(*files_or_dirs)
+ files = FlogCLI.expand_dirs_to_files(*files_or_dirs)
+ @flog.flog(*files)
+ end
+
+ ##
+ # Creates a new Flog instance with +options+.
+
+ def initialize options = {}
+ @flog = Flog.new options
+ end
+
+ ##
+ # Output the report up to a given max or report everything, if nil.
+
+ def output_details io, max = nil
+ io.puts
+
+ each_by_score max do |class_method, score, call_list|
+ return 0 if option[:methods] and class_method =~ /##{no_method}/
+
+ self.print_score io, class_method, score
+
+ if option[:details] then
+ call_list.sort_by { |k,v| -v }.each do |call, count|
+ io.puts " %6.1f: %s" % [count, call]
+ end
+ io.puts
+ end
+ 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
+
+ io.puts "%8.1f: %s" % [total, "#{klass} total"]
+
+ methods[klass].each do |name, score|
+ self.print_score io, name, score
+ end
+ end
+ end
+
+ ##
+ # Print out one formatted score.
+
+ def print_score io, name, score
+ location = method_locations[name]
+ if location then
+ io.puts "%8.1f: %-32s %s" % [score, name, location]
+ else
+ io.puts "%8.1f: %s" % [score, name]
+ end
+ end
+
+ ##
+ # Report results to #io, STDOUT by default.
+
+ def report(io = $stdout)
+ io.puts "%8.1f: %s" % [total, "flog total"]
+ io.puts "%8.1f: %s" % [average, "flog/method average"]
+
+ return if option[:score]
+
+ if option[:group] then
+ output_details_grouped io, threshold
+ else
+ output_details io, threshold
+ end
+ ensure
+ self.reset
+ end
+end
View
219 test/test_flog.rb
@@ -1,11 +1,23 @@
-require 'minitest/autorun'
-require 'flog'
+require "minitest/autorun"
+require "flog"
class Flog
attr_writer :calls
end
-class TestFlog < MiniTest::Unit::TestCase
+class FlogTest < MiniTest::Unit::TestCase
+ def setup_flog
+ old_stdin = $stdin
+ $stdin = StringIO.new "2 + 3"
+ $stdin.rewind
+
+ @flog.flog "-"
+ ensure
+ $stdin = old_stdin
+ end
+end
+
+class TestFlog < FlogTest
def setup
@flog = Flog.new :parser => RubyParser
end
@@ -29,88 +41,14 @@ def test_average
assert_equal 1.0, @flog.average
end
- def test_cls_expand_dirs_to_files
- expected = %w(lib/flog.rb lib/flog_task.rb lib/gauntlet_flog.rb)
- assert_equal expected, Flog.expand_dirs_to_files('lib')
- expected = %w(Rakefile)
- assert_equal expected, Flog.expand_dirs_to_files('Rakefile')
- end
-
- def test_cls_parse_options
- # defaults
- opts = Flog.parse_options
- assert_equal false, opts[:quiet]
- assert_equal false, opts[:continue]
-
- {
- "-a" => :all,
- "--all" => :all,
- "-b" => :blame,
- "--blame" => :blame,
- "-c" => :continue,
- "--continue" => :continue,
- "-d" => :details,
- "--details" => :details,
- "-g" => :group,
- "--group" => :group,
- "-m" => :methods,
- "--methods-only" => :methods,
- "-q" => :quiet,
- "--quiet" => :quiet,
- "-s" => :score,
- "--score" => :score,
- "-v" => :verbose,
- "--verbose" => :verbose,
- }.each do |key, val|
- assert_equal true, Flog.parse_options(key)[val]
- end
- end
-
- def test_cls_parse_options_path
- old_path = $:.dup
- Flog.parse_options("-Ia,b,c")
- assert_equal old_path + %w(a b c), $:
-
- Flog.parse_options(["-I", "d,e,f"])
- assert_equal old_path + %w(a b c d e f), $:
-
- Flog.parse_options(["-I", "g", "-Ih"])
- assert_equal old_path + %w(a b c d e f g h), $:
- ensure
- $:.replace old_path
- end
-
- def test_cls_parse_options_help
- def Flog.exit
- raise "happy"
- end
-
- ex = nil
- o, e = capture_io do
- ex = assert_raises RuntimeError do
- Flog.parse_options "-h"
- end
- end
-
- assert_equal "happy", ex.message
- assert_match(/methods-only/, o)
- assert_equal "", e
- end
-
def test_flog
- old_stdin = $stdin
- $stdin = StringIO.new "2 + 3"
- $stdin.rewind
-
- @flog.flog "-"
+ setup_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 3, @flog.mass["-"]
- ensure
- $stdin = old_stdin
end
def test_flog_ruby
@@ -195,66 +133,6 @@ def test_method_name_cls
assert_equal "::whatevs", @flog.method_name
end
- def test_output_details
- @flog.option[:all] = true
- test_flog
-
- @flog.totals["main#something"] = 42.0
-
- o = StringIO.new
- @flog.output_details o
-
- expected = "\n 1.6: main#none\n"
-
- assert_equal expected, o.string
- assert_equal 1.6, @flog.totals["main#none"]
- end
-
- def test_output_details_grouped
- test_flog
-
- o = StringIO.new
- @flog.output_details_grouped o
-
- expected = "\n 1.6: main total\n 1.6: main#none\n"
-
- assert_equal expected, o.string
- end
-
- def test_output_details_methods
- @flog.option[:methods] = true
-
- test_flog
-
- @flog.totals["main#something"] = 42.0 # TODO: no sense... why no output?
-
- o = StringIO.new
- @flog.output_details o
-
- # HACK assert_equal "", o.string
- assert_equal 0, @flog.totals["main#none"]
- end
-
- def test_output_details_detailed
- @flog.option[:details] = true
-
- test_flog
-
- @flog.totals["main#something"] = 42.0
-
- o = StringIO.new
- @flog.output_details o, nil
-
- expected = "\n 1.6: main#none
- 1.0: +
- 0.6: lit_fixnum
-
-"
-
- assert_equal expected, o.string
- assert_equal 1.6, @flog.totals["main#none"]
- end
-
# def test_process_until_empty
# flunk "no"
# end
@@ -627,71 +505,6 @@ def test_process_yield
util_process sexp, 1.50, :yield => 1.0, :lit_fixnum => 0.50
end
- def test_report
- test_flog
-
- o = StringIO.new
- @flog.report o
-
- expected = " 1.6: flog total
- 1.6: flog/method average
-
- 1.6: main#none
-"
-
- assert_equal expected, o.string
- end
-
- def test_report_all
- old_stdin = $stdin
- $stdin = StringIO.new "2 + 3"
- $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
-
- assert_equal 1.6, @flog.total unless @flog.option[:methods]
- assert_equal 3, @flog.mass["-"]
-
- o = StringIO.new
- @flog.report o
-
- expected = " 1.6: flog total
- 1.6: flog/method average
-
- 1.6: main#none
-"
-
- assert_equal expected, o.string
- # FIX: add thresholded output
- ensure
- $stdin = old_stdin
- end
-
- def test_report_group
- # TODO: add second group to ensure proper output
- @flog.option[:group] = true
-
- test_flog
-
- o = StringIO.new
- @flog.report o
-
- expected = " 1.6: flog total
- 1.6: flog/method average
-
- 1.6: main total
- 1.6: main#none
-"
-
- assert_equal expected, o.string
- end
-
def test_score_method
assert_equal 3.0, @flog.score_method(:blah => 3.0)
assert_equal 4.0, @flog.score_method(:assignment => 4.0)
View
209 test/test_flog_cli.rb
@@ -0,0 +1,209 @@
+require "test/test_flog"
+require "flog_cli"
+
+class FlogCLI
+ def_delegators :@flog, :option
+ def_delegators :@flog, :totals # FIX: test_report_all is overreaching?
+ def_delegators :@flog, :calls # FIX: refactor?
+ def_delegators :@flog, :mass # FIX: refactor?
+end
+
+class TestFlogCLI < FlogTest
+ def setup
+ @flog = FlogCLI.new :parser => RubyParser
+ end
+
+ def test_cls_expand_dirs_to_files
+ expected = %w(lib/flog.rb lib/flog_cli.rb lib/flog_task.rb lib/gauntlet_flog.rb)
+ assert_equal expected, FlogCLI.expand_dirs_to_files('lib')
+ expected = %w(Rakefile)
+ assert_equal expected, FlogCLI.expand_dirs_to_files('Rakefile')
+ end
+
+ def test_cls_parse_options
+ # defaults
+ opts = FlogCLI.parse_options
+ assert_equal false, opts[:quiet]
+ assert_equal false, opts[:continue]
+
+ {
+ "-a" => :all,
+ "--all" => :all,
+ "-b" => :blame,
+ "--blame" => :blame,
+ "-c" => :continue,
+ "--continue" => :continue,
+ "-d" => :details,
+ "--details" => :details,
+ "-g" => :group,
+ "--group" => :group,
+ "-m" => :methods,
+ "--methods-only" => :methods,
+ "-q" => :quiet,
+ "--quiet" => :quiet,
+ "-s" => :score,
+ "--score" => :score,
+ "-v" => :verbose,
+ "--verbose" => :verbose,
+ }.each do |key, val|
+ assert_equal true, FlogCLI.parse_options(key)[val]
+ end
+ end
+
+ def test_cls_parse_options_path
+ old_path = $:.dup
+ FlogCLI.parse_options("-Ia,b,c")
+ assert_equal old_path + %w(a b c), $:
+
+ FlogCLI.parse_options(["-I", "d,e,f"])
+ assert_equal old_path + %w(a b c d e f), $:
+
+ FlogCLI.parse_options(["-I", "g", "-Ih"])
+ assert_equal old_path + %w(a b c d e f g h), $:
+ ensure
+ $:.replace old_path
+ end
+
+ def test_cls_parse_options_help
+ def FlogCLI.exit
+ raise "happy"
+ end
+
+ ex = nil
+ o, e = capture_io do
+ ex = assert_raises RuntimeError do
+ FlogCLI.parse_options "-h"
+ end
+ end
+
+ assert_equal "happy", ex.message
+ assert_match(/methods-only/, o)
+ assert_equal "", e
+ end
+
+ def test_output_details
+ @flog.option[:all] = true
+ setup_flog
+
+ @flog.totals["main#something"] = 42.0
+
+ o = StringIO.new
+ @flog.output_details o
+
+ expected = "\n 1.6: main#none\n"
+
+ assert_equal expected, o.string
+ assert_equal 1.6, @flog.totals["main#none"]
+ end
+
+ def test_output_details_grouped
+ setup_flog
+
+ o = StringIO.new
+ @flog.output_details_grouped o
+
+ expected = "\n 1.6: main total\n 1.6: main#none\n"
+
+ assert_equal expected, o.string
+ end
+
+ def test_output_details_methods
+ @flog.option[:methods] = true
+
+ setup_flog
+
+ @flog.totals["main#something"] = 42.0 # TODO: no sense... why no output?
+
+ o = StringIO.new
+ @flog.output_details o
+
+ # HACK assert_equal "", o.string
+ assert_equal 0, @flog.totals["main#none"]
+ end
+
+ def test_output_details_detailed
+ @flog.option[:details] = true
+
+ setup_flog
+
+ @flog.totals["main#something"] = 42.0
+
+ o = StringIO.new
+ @flog.output_details o, nil
+
+ expected = "\n 1.6: main#none
+ 1.0: +
+ 0.6: lit_fixnum
+
+"
+
+ assert_equal expected, o.string
+ assert_equal 1.6, @flog.totals["main#none"]
+ end
+
+ def test_report
+ setup_flog
+
+ o = StringIO.new
+ @flog.report o
+
+ expected = " 1.6: flog total
+ 1.6: flog/method average
+
+ 1.6: main#none
+"
+
+ assert_equal expected, o.string
+ end
+
+ def test_report_all
+ old_stdin = $stdin
+ $stdin = StringIO.new "2 + 3"
+ $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
+
+ assert_equal 1.6, @flog.total unless @flog.option[:methods]
+ assert_equal 3, @flog.mass["-"]
+
+ o = StringIO.new
+ @flog.report o
+
+ expected = " 1.6: flog total
+ 1.6: flog/method average
+
+ 1.6: main#none
+"
+
+ assert_equal expected, o.string
+ # FIX: add thresholded output
+ ensure
+ $stdin = old_stdin
+ end
+
+ def test_report_group
+ # TODO: add second group to ensure proper output
+ @flog.option[:group] = true
+
+ setup_flog
+
+ o = StringIO.new
+ @flog.report o
+
+ expected = " 1.6: flog total
+ 1.6: flog/method average
+
+ 1.6: main total
+ 1.6: main#none
+"
+
+ assert_equal expected, o.string
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.