Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support custom decorators via mode config.

  • Loading branch information...
commit 3f22685d53653153548838941dbe13e9f728d865 2 parents 9a65cca + 4bac3af
@trans trans authored
View
2  README.md
@@ -46,7 +46,7 @@ application.
## INSTRUCTION:
-Turn can be using from the command-line or via require. The command-line tool
+Turn can be used from the command-line or via require. The command-line tool
offers additional options for how one runs tests.
### Command Line
View
8 lib/turn/command.rb
@@ -24,10 +24,10 @@ module Turn
# --cross run each pair of test files in a separate process
#
# OUTPUT MODES
- # -O, --outline turn's original case/test outline mode [default]
+ # -O, --outline turn's original case/test outline mode
# -P, --progress indicates progress with progress bar
# -D, --dotted test-unit's traditonal dot-progress mode
- # -R, -T, --pretty new pretty output mode
+ # -R, -T, --pretty new pretty output mode [default]
# -C, --cue cue for action on each failure/error
# -M, --marshal dump output as YAML (normal run mode only)
#
@@ -193,7 +193,7 @@ def option_parser
opts.separator " "
opts.separator "OUTPUT MODES"
- opts.on('--outline', '-O', "turn's original case/test outline mode [default]") do
+ opts.on('--outline', '-O', "turn's original case/test outline mode") do
@outmode = :outline
end
@@ -205,7 +205,7 @@ def option_parser
@outmode = :dotted
end
- opts.on('--pretty', '-R', '-T', "new pretty output mode") do
+ opts.on('--pretty', '-R', '-T', "new pretty output mode [default]") do
@outmode = :pretty
end
View
71 lib/turn/configuration.rb
@@ -36,7 +36,11 @@ class Configuration
# Reporter type.
attr_accessor :format
- # Run mode.
+ # Report modifier.
+ attr_accessor :mode
+
+ # Run mode, which defaults to `nil`, but can also be `:solo`,
+ # `:cross` or `:marshal`.
attr_accessor :runmode
# Test against live install (i.e. Don't use loadpath option)
@@ -66,20 +70,24 @@ def verbose?
@verbose
end
+ #
def live?
@live
end
+ #
def natural?
@natural
end
+ #
def ansi?
@ansi
end
private
+ #
def initialize
yield(self) if block_given?
initialize_defaults
@@ -99,6 +107,7 @@ def initialize_defaults
@natural ||= false
@verbose ||= false
@format ||= environment_format
+ @mode ||= environment_mode
@trace ||= environment_trace
@ansi ||= environment_ansi
@@ -177,22 +186,51 @@ def suite_name
files.map{ |path| File.dirname(path).sub(Dir.pwd+'/','') }.uniq.join(',')
end
- # Select reporter based on output mode.
+ # Get selected reporter with any mode decorator.
def reporter
- @reporter ||= (
- rpt_format = format || :pretty
- class_name = rpt_format.to_s.capitalize + "Reporter"
-
- path = "turn/reporters/#{rpt_format}_reporter"
- [$HOME, DIR.pwd].each do |dir|
- file = File.join(dir, ".turn", "reporters", "#{rpt_format}_reporter.rb")
- path = file if File.exist?(file)
- end
+ @reporter ||= decorate_reporter(choose_reporter_class.new($stdout, reporter_options))
+ end
+
+ # Load reporter based on output mode and return its class.
+ def reporter_class
+ rpt_format = format || :pretty
+ class_name = rpt_format.to_s.capitalize + "Reporter"
+
+ path = "turn/reporters/#{rpt_format}_reporter"
+ [$HOME, DIR.pwd].each do |dir|
+ file = File.join(dir, ".turn", "reporters", "#{rpt_format}_reporter.rb")
+ path = file if File.exist?(file)
+ end
+
+ require path
+
+ Turn.const_get(class_name)
+ end
- require path
+ #
+ def decorate_reporter(reporter)
+ if mode
+ decorator_class.new(reporter)
+ else
+ reporter
+ end
+ end
+
+ #
+ def decorator_class
+ return nil unless mode
+
+ class_name = mode.to_s.capitalize + "Decorator"
- Turn.const_get(class_name).new($stdout, reporter_opts)
- )
+ path = "turn/decorators/#{mode}_decorator"
+ [$HOME, DIR.pwd].each do |dir|
+ file = File.join(dir, ".turn", "decorators", "#{mode}_reporter.rb")
+ path = file if File.exist?(file)
+ end
+
+ require path
+
+ Turn.const_get(class_name)
end
#
@@ -206,6 +244,11 @@ def environment_format
end
#
+ def environment_mode
+ ENV['mode']
+ end
+
+ #
def environment_trace
(ENV['backtrace'] ? ENV['backtrace'].to_i : nil)
end
View
67 lib/turn/decorators/topten_decorator.rb
@@ -0,0 +1,67 @@
+module Turn
+
+ class ToptenDecorator
+
+ def initialize(reporter)
+ @reporter = reporter
+ end
+
+ def method_missing(m,*args,&block)
+ @reporter.send(m,*args,&block)
+ end
+
+ def start_case(kase)
+ @reporter.start_case(kase)
+ @top_ten_current_case = kase
+ end
+
+ def start_test(test)
+ @reporter.start_test(test)
+ test_time_data[test_key(test)] = {:start => Time.now}
+ end
+
+ def finish_test(test)
+ @reporter.finish_test(test)
+ test_time_data[test_key(test)][:end] = Time.now
+ end
+
+ def finish_suite(suite)
+ @reporter.finish_suite(suite)
+ io.puts
+ io.puts Colorize.bold("Top 10 Longest Running Tests")
+ top_ten_times.each do |(test_name, time)|
+ io.print format_time(time)
+ io.puts format_test_name(test_name, time)
+ end
+ io.puts
+ end
+
+ private
+
+ def test_key(test)
+ "#{@top_ten_current_case.name} #{test}"
+ end
+
+ def top_ten_times
+ test_times.sort_by {|(_, time)| -time}.take(10)
+ end
+
+ def test_times
+ test_time_data.map {|(test, times)| [test, times[:end] - times[:start]]}
+ end
+
+ def test_time_data
+ @test_time_data ||= {}
+ end
+
+ def format_time(time)
+ Colorize.blue(time)
+ end
+
+ def format_test_name(test_name, time)
+ test_name.tabto(11-time.to_s.length)
+ end
+
+ end
+
+end
View
2  lib/turn/reporter.rb
@@ -119,7 +119,7 @@ def naturalized_name(test)
#
def ticktock
t = Time.now - @time
- h, t = t.divmod(60)
+ h, t = t.divmod(3600)
m, t = t.divmod(60)
s = t.truncate
f = ((t - s) * 1000).to_i
Please sign in to comment.
Something went wrong with that request. Please try again.