turn 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,163 @@
1
+ require 'minitest/unit'
2
+ require 'minitest/spec'
3
+ #require 'rubygems'
4
+ require 'turn/colorize'
5
+
6
+ class MiniTest::Unit
7
+ PADDING_SIZE = 4
8
+
9
+ @@use_natural_language_case_names = false
10
+ def self.use_natural_language_case_names=(boolean)
11
+ @use_natural_language_case_names = boolean
12
+ end
13
+
14
+ def self.use_natural_language_case_names?
15
+ @use_natural_language_case_names
16
+ end
17
+
18
+
19
+ def run(args = [])
20
+ @verbose = true
21
+ options = args.getopts("n:t", "name:", "trace")
22
+ filter = if name = options["n"] || options["name"]
23
+ if name =~ /\/(.*)\//
24
+ Regexp.new($1)
25
+ elsif MiniTest::Unit.use_natural_language_case_names?
26
+ # Turn 'sample error1' into 'test_sample_error1'
27
+ name[0..4] == "test_" ? name.gsub(" ", "_") : "test_" + name.gsub(" ", "_")
28
+ else
29
+ name
30
+ end
31
+ else
32
+ /./ # anything - ^test_ already filtered by #tests
33
+ end
34
+
35
+ @trace = options['t'] || options['trace']
36
+
37
+ @@out.puts "Loaded suite #{$0.sub(/\.rb$/, '')}\nStarted"
38
+
39
+ start = Time.now
40
+ run_test_suites filter
41
+
42
+ @@out.puts
43
+ @@out.puts "Finished in #{'%.6f' % (Time.now - start)} seconds."
44
+
45
+ @@out.puts
46
+
47
+ @@out.print "%d tests, " % test_count
48
+ @@out.print "%d assertions, " % assertion_count
49
+ @@out.print Turn::Colorize.fail("%d failures, " % failures)
50
+ @@out.print Turn::Colorize.error("%d errors, " % errors)
51
+ @@out.puts Turn::Colorize.skip("%d skips" % skips)
52
+
53
+ return failures + errors if @test_count > 0 # or return nil...
54
+ end
55
+
56
+ # Overwrite #run_test_suites so that it prints out reports
57
+ # as errors are generated.
58
+ def run_test_suites(filter = /./)
59
+ @test_count, @assertion_count = 0, 0
60
+ old_sync, @@out.sync = @@out.sync, true if @@out.respond_to? :sync=
61
+ TestCase.test_suites.each do |suite|
62
+ test_cases = suite.test_methods.grep(filter)
63
+ if test_cases.size > 0
64
+ @@out.print "\n#{suite}:\n"
65
+ end
66
+
67
+ test_cases.each do |test|
68
+ inst = suite.new test
69
+ inst._assertions = 0
70
+
71
+ t = Time.now
72
+
73
+ @broken = nil
74
+
75
+ @@out.print(case run_testcase(inst, self)
76
+ when :pass
77
+ @broken = false
78
+ Turn::Colorize.pass(pad_with_size "PASS")
79
+ when :error
80
+ @broken = true
81
+ Turn::Colorize.error(pad_with_size "ERROR")
82
+ when :fail
83
+ @broken = true
84
+ Turn::Colorize.fail(pad_with_size "FAIL")
85
+ when :skip
86
+ @broken = false
87
+ Turn::Colorize.skip(pad_with_size "SKIP")
88
+ end)
89
+
90
+
91
+ @@out.print MiniTest::Unit.use_natural_language_case_names? ?
92
+ " #{test.gsub("test_", "").gsub(/_/, " ")}" : " #{test}"
93
+ @@out.print " (%.2fs) " % (Time.now - t)
94
+
95
+ if @broken
96
+ @@out.puts
97
+
98
+ report = @report.last
99
+ @@out.puts pad(report[:message], 10)
100
+
101
+ trace = MiniTest::filter_backtrace(report[:exception].backtrace)
102
+ if @trace
103
+ @@out.print trace.map{|t| pad(t, 10) }.join("\n")
104
+ else
105
+ @@out.print pad(trace.first, 10)
106
+ end
107
+
108
+ @@out.puts
109
+ end
110
+
111
+ @@out.puts
112
+ @test_count += 1
113
+ @assertion_count += inst._assertions
114
+ end
115
+ end
116
+ @@out.sync = old_sync if @@out.respond_to? :sync=
117
+ [@test_count, @assertion_count]
118
+ end
119
+
120
+ def pad(str, size=PADDING_SIZE)
121
+ " " * size + str
122
+ end
123
+
124
+ def pad_with_size(str)
125
+ pad("%5s" % str)
126
+ end
127
+
128
+ # Overwrite #puke method so that is stores a hash
129
+ # with :message and :exception keys.
130
+ def puke(klass, meth, e)
131
+ result = nil
132
+ msg = case e
133
+ when MiniTest::Skip
134
+ @skips += 1
135
+ result = :skip
136
+ e.message
137
+ when MiniTest::Assertion
138
+ @failures += 1
139
+ result = :fail
140
+ e.message
141
+ else
142
+ @errors += 1
143
+ result = :error
144
+ "#{e.class}: #{e.message}\n"
145
+ end
146
+
147
+ @report << {:message => msg, :exception => e}
148
+ result
149
+ end
150
+
151
+ private
152
+ # A wrapper over MiniTest::Unit::TestCase.run() that returns
153
+ # :pass whenever the test succeeds (i.e. run() returns "" or ".")
154
+ def run_testcase(testcase, runner)
155
+ original_result = testcase.run(runner)
156
+ if original_result == "" || original_result == "."
157
+ :pass
158
+ else
159
+ original_result
160
+ end
161
+ end
162
+ end
163
+
@@ -1,116 +1,9 @@
1
- require 'test/unit/ui/console/testrunner'
1
+ #require 'test/unit/ui/console/testrunner'
2
+ require 'turn/autoload'
2
3
  require 'turn/colorize'
4
+ require 'turn/controller'
5
+ require 'turn/runners/testrunner'
3
6
 
4
- module ::Test::Unit
5
- module UI
6
- module Console
7
- class TestRunner
8
- include Turn::Colorize
9
-
10
- # 1.x of test/unut used @io, where as 2.x uses @output.
11
- def turn_out
12
- @turn_out ||= (@io || @output)
13
- end
14
-
15
- alias :t_attach_to_mediator :attach_to_mediator
16
- def attach_to_mediator
17
- @mediator.add_listener(TestRunnerMediator::STARTED, &method(:t_started))
18
- @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:t_finished))
19
- @mediator.add_listener(TestCase::STARTED, &method(:t_test_started))
20
- @mediator.add_listener(TestCase::FINISHED, &method(:t_test_finished))
21
- @mediator.add_listener(TestResult::FAULT, &method(:t_fault))
22
- turn_out.sync = true
23
- @t_cur_file, @t_fault = nil
24
- end
25
-
26
- def t_started( result )
27
- @t_result = result
28
- end
29
-
30
- def t_finished( elapsed_time )
31
- failure = @t_result.failure_count
32
- error = @t_result.error_count
33
- total = @t_result.run_count
34
- pass = total - failure - error
35
-
36
- bar = '=' * 78
37
- if COLORIZE
38
- bar = if pass == total then ::ANSI::Code.green{bar}
39
- else ::ANSI::Code.red{bar} end
40
- end
41
-
42
- turn_out.puts bar
43
- turn_out.puts " pass: %d, fail: %d, error: %d" % [pass, failure, error]
44
- turn_out.puts " total: %d tests with %d assertions in #{elapsed_time} seconds" % [total, @t_result.assertion_count]
45
- turn_out.puts bar
46
- end
47
-
48
- def t_test_started( name )
49
- method, file = name.scan(%r/^([^\(]+)\(([^\)]+)\)/o).flatten!
50
- if @t_cur_file != file
51
- @t_cur_file = file
52
- file = COLORIZE ? ::ANSI::Code.yellow{file} : file
53
- turn_out.puts file
54
- end
55
- turn_out.print " %-69s" % method
56
- end
57
-
58
- def t_test_finished( name )
59
- turn_out.puts " #{PASS}" unless @t_fault
60
- @t_fault = false
61
- end
62
-
63
- def t_fault( fault )
64
- @t_fault = true
65
- msg = "\t"
66
-
67
- case fault
68
- when ::Test::Unit::Error
69
- turn_out.puts ERROR
70
- msg << fault.to_s.split("\n")[2..-1].join("\n\t")
71
- when ::Test::Unit::Failure
72
- test_name = underscore(fault.test_name.match(/\((.*)\)/)[1])
73
- better_location = fault.location.detect{|line|line.include?(test_name)} || fault.location[0]
74
- turn_out.puts " #{FAIL}"
75
- msg << better_location.to_s << "\n\t"
76
- msg << fault.message.gsub("\n","\n\t")
77
- end
78
-
79
- msg = ::ANSI::Code.magenta{msg} if COLORIZE
80
- turn_out.puts msg
81
- end
82
-
83
- private
84
-
85
- # Taken from ActiveSupport::Inflector
86
- def underscore(camel_cased_word)
87
- word = camel_cased_word.to_s.dup
88
- word.gsub!(/::/, '/')
89
- word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
90
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
91
- word.tr!("-", "_")
92
- word.downcase!
93
- word
94
- end
95
-
96
- def setup_mediator
97
- @mediator = create_mediator(@suite)
98
- suite_name = @suite.to_s
99
- if ( @suite.kind_of?(Module) )
100
- suite_name = @suite.name
101
- end
102
- msg = rails? ? "\n" : "Loaded suite #{suite_name}" #always same in rails so scrap it
103
- output(msg)
104
- end
105
-
106
- def rails?
107
- $:.to_s.include? "rails"
108
- end
109
-
110
-
111
- end
112
- end
7
+ Test::Unit::AutoRunner::RUNNERS[:console] = proc do |r|
8
+ Turn::TestRunner
113
9
  end
114
- end
115
-
116
- # EOF
@@ -0,0 +1,116 @@
1
+ require 'test/unit/ui/console/testrunner'
2
+ require 'turn/colorize'
3
+
4
+ module ::Test::Unit
5
+ module UI
6
+ module Console
7
+ class TestRunner
8
+ include Turn::Colorize
9
+
10
+ # 1.x of test/unut used @io, where as 2.x uses @output.
11
+ def turn_out
12
+ @turn_out ||= (@io || @output)
13
+ end
14
+
15
+ alias :t_attach_to_mediator :attach_to_mediator
16
+ def attach_to_mediator
17
+ @mediator.add_listener(TestRunnerMediator::STARTED, &method(:t_started))
18
+ @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:t_finished))
19
+ @mediator.add_listener(TestCase::STARTED, &method(:t_test_started))
20
+ @mediator.add_listener(TestCase::FINISHED, &method(:t_test_finished))
21
+ @mediator.add_listener(TestResult::FAULT, &method(:t_fault))
22
+ turn_out.sync = true
23
+ @t_cur_file, @t_fault = nil
24
+ end
25
+
26
+ def t_started( result )
27
+ @t_result = result
28
+ end
29
+
30
+ def t_finished( elapsed_time )
31
+ failure = @t_result.failure_count
32
+ error = @t_result.error_count
33
+ total = @t_result.run_count
34
+ pass = total - failure - error
35
+
36
+ bar = '=' * 78
37
+ if colorize?
38
+ bar = if pass == total then ::ANSI::Code.green{bar}
39
+ else ::ANSI::Code.red{bar} end
40
+ end
41
+
42
+ turn_out.puts bar
43
+ turn_out.puts " pass: %d, fail: %d, error: %d" % [pass, failure, error]
44
+ turn_out.puts " total: %d tests with %d assertions in #{elapsed_time} seconds" % [total, @t_result.assertion_count]
45
+ turn_out.puts bar
46
+ end
47
+
48
+ def t_test_started( name )
49
+ method, file = name.scan(%r/^([^\(]+)\(([^\)]+)\)/o).flatten!
50
+ if @t_cur_file != file
51
+ @t_cur_file = file
52
+ file = colorize? ? ::ANSI::Code.yellow{file} : file
53
+ turn_out.puts file
54
+ end
55
+ turn_out.print " %-69s" % method
56
+ end
57
+
58
+ def t_test_finished( name )
59
+ turn_out.puts " #{PASS}" unless @t_fault
60
+ @t_fault = false
61
+ end
62
+
63
+ def t_fault( fault )
64
+ @t_fault = true
65
+ msg = "\t"
66
+
67
+ case fault
68
+ when ::Test::Unit::Error
69
+ turn_out.puts ERROR
70
+ msg << fault.to_s.split("\n")[2..-1].join("\n\t")
71
+ when ::Test::Unit::Failure
72
+ test_name = underscore(fault.test_name.match(/\((.*)\)/)[1])
73
+ better_location = fault.location.detect{|line|line.include?(test_name)} || fault.location[0]
74
+ turn_out.puts " #{FAIL}"
75
+ msg << better_location.to_s << "\n\t"
76
+ msg << fault.message.gsub("\n","\n\t")
77
+ end
78
+
79
+ msg = ::ANSI::Code.magenta{msg} if colorize?
80
+ turn_out.puts msg
81
+ end
82
+
83
+ private
84
+
85
+ # Taken from ActiveSupport::Inflector
86
+ def underscore(camel_cased_word)
87
+ word = camel_cased_word.to_s.dup
88
+ word.gsub!(/::/, '/')
89
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
90
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
91
+ word.tr!("-", "_")
92
+ word.downcase!
93
+ word
94
+ end
95
+
96
+ def setup_mediator
97
+ @mediator = create_mediator(@suite)
98
+ suite_name = @suite.to_s
99
+ if ( @suite.kind_of?(Module) )
100
+ suite_name = @suite.name
101
+ end
102
+ msg = rails? ? "\n" : "Loaded suite #{suite_name}" #always same in rails so scrap it
103
+ output(msg)
104
+ end
105
+
106
+ def rails?
107
+ $:.to_s.include? "rails"
108
+ end
109
+
110
+
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ # EOF
@@ -12,46 +12,54 @@ module Turn
12
12
 
13
13
  module Colorize
14
14
 
15
- COLORIZE = defined?(::ANSI::Code) && ENV.has_key?('TERM')
15
+ COLORLESS_TERMINALS = ['dumb']
16
+
17
+ def colorize?
18
+ defined?(::ANSI::Code) &&
19
+ ENV.has_key?('TERM') &&
20
+ !COLORLESS_TERMINALS.include?(ENV['TERM']) &&
21
+ $stdout.tty?
22
+ end
23
+ module_function :colorize?
16
24
 
17
25
  def self.red(string)
18
- COLORIZE ? ::ANSI::Code.red{ string } : string
26
+ colorize? ? ::ANSI::Code.red{ string } : string
19
27
  end
20
28
 
21
29
  def self.green(string)
22
- COLORIZE ? ::ANSI::Code.green{ string } : string
30
+ colorize? ? ::ANSI::Code.green{ string } : string
23
31
  end
24
32
 
25
33
  def self.blue(string)
26
- COLORIZE ? ::ANSI::Code.blue{ string } : string
34
+ colorize? ? ::ANSI::Code.blue{ string } : string
27
35
  end
28
36
 
29
37
  def self.magenta(string)
30
- COLORIZE ? ::ANSI::Code.magenta{ string } : string
38
+ colorize? ? ::ANSI::Code.magenta{ string } : string
31
39
  end
32
40
 
33
41
  def self.bold(string)
34
- COLORIZE ? ::ANSI::Code.bold{ string } : string
42
+ colorize? ? ::ANSI::Code.bold{ string } : string
35
43
  end
36
44
 
37
45
  def self.pass(string)
38
- COLORIZE ? ::ANSI::Code.green{ string } : string
46
+ colorize? ? ::ANSI::Code.green{ string } : string
39
47
  end
40
48
 
41
49
  def self.fail(string)
42
- COLORIZE ? ::ANSI::Code.red{ string } : string
50
+ colorize? ? ::ANSI::Code.red{ string } : string
43
51
  end
44
52
 
45
53
  #def self.error(string)
46
- # COLORIZE ? ::ANSI::Code.white{ ::ANSI::Code.on_red{ string } } : string
54
+ # colorize? ? ::ANSI::Code.white{ ::ANSI::Code.on_red{ string } } : string
47
55
  #end
48
56
 
49
57
  def self.error(string)
50
- COLORIZE ? ::ANSI::Code.yellow{ string } : string
58
+ colorize? ? ::ANSI::Code.yellow{ string } : string
51
59
  end
52
60
 
53
61
  def self.skip(string)
54
- COLORIZE ? ::ANSI::Code.cyan{ string } : string
62
+ colorize? ? ::ANSI::Code.cyan{ string } : string
55
63
  end
56
64
 
57
65
  PASS = pass('PASS')