* lib/test/unit.rb: refactored to use optparse.

* lib/test/unit.rb: added support for selecting the output
	  level from the command-line.

	* lib/test/unit.rb: added a command-line switch to stop processing
	  the command-line, allowing arguments to be passed to tests.

	* lib/test/unit.rb: changed the method for specifying a runner or a
	  filter from the command-line.

	* lib/test/unit/collector/objectspace.rb: fixed a bug causing all
	  tests to be excluded when the filter was set to an empty array.

	* test/testunit/collector/test_objectspace.rb: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ntalbott 2003-10-03 04:04:26 +00:00
parent a3ef2d79fa
commit b0ccb799ec
4 changed files with 111 additions and 29 deletions

View File

@ -1,3 +1,21 @@
Fri Oct 3 13:02:00 2003 Nathaniel Talbott <ntalbott@ruby-lang.org>
* lib/test/unit.rb: refactored to use optparse.
* lib/test/unit.rb: added support for selecting the output
level from the command-line.
* lib/test/unit.rb: added a command-line switch to stop processing
the command-line, allowing arguments to be passed to tests.
* lib/test/unit.rb: changed the method for specifying a runner or a
filter from the command-line.
* lib/test/unit/collector/objectspace.rb: fixed a bug causing all
tests to be excluded when the filter was set to an empty array.
* test/testunit/collector/test_objectspace.rb: ditto.
Fri Oct 3 08:01:00 2003 Nathaniel Talbott <ntalbott@ruby-lang.org> Fri Oct 3 08:01:00 2003 Nathaniel Talbott <ntalbott@ruby-lang.org>
* lib/test/unit/assertions.rb: added a default message for #assert, * lib/test/unit/assertions.rb: added a default message for #assert,

View File

@ -276,51 +276,109 @@ require 'test/unit/ui/testrunnermediator'
require 'test/unit/collector/objectspace' require 'test/unit/collector/objectspace'
at_exit { at_exit {
# We can't debug tests run with at_exit unless we add the following: require 'optparse'
set_trace_func DEBUGGER__.context.method(:trace_func).to_proc if (defined? DEBUGGER__)
if (!Test::Unit::UI::TestRunnerMediator.run?) if (!Test::Unit::UI::TestRunnerMediator.run?)
output_level = nil
runners = { runners = {
'--console' => proc do |suite| :console => proc do |suite|
require 'test/unit/ui/console/testrunner' require 'test/unit/ui/console/testrunner'
passed = Test::Unit::UI::Console::TestRunner.run(suite).passed? output_level ||= Test::Unit::UI::Console::TestRunner::NORMAL
passed = Test::Unit::UI::Console::TestRunner.run(suite, output_level).passed?
exit(passed ? 0 : 1) exit(passed ? 0 : 1)
end, end,
'--gtk' => proc do |suite| :gtk => proc do |suite|
require 'test/unit/ui/gtk/testrunner' require 'test/unit/ui/gtk/testrunner'
Test::Unit::UI::GTK::TestRunner.run(suite) Test::Unit::UI::GTK::TestRunner.run(suite)
end, end,
'--fox' => proc do |suite| :fox => proc do |suite|
require 'test/unit/ui/fox/testrunner' require 'test/unit/ui/fox/testrunner'
Test::Unit::UI::Fox::TestRunner.run(suite) Test::Unit::UI::Fox::TestRunner.run(suite)
end, end,
} }
unless (ARGV.empty?) runner = runners[:console]
runner = runners[ARGV[0]] filters = []
ARGV.shift unless (runner.nil?) catch(:stop_processing) do
end ARGV.options do |o|
runner = runners['--console'] if (runner.nil?) o.program_name = "test/unit.rb"
o.banner = "Test::Unit automatic runner."
o.banner = "#{$0} [options] [-- untouched arguments]"
collector = Test::Unit::Collector::ObjectSpace::new o.on
runner_display = runners.keys.collect{|r| r.to_s.sub(/^(.)/, '[\\1]')}.join(", ")
o.on('-r', '--runner=RUNNER', runners.keys,
"Use the given runner.",
"(" + runner_display + ")"){|r| runner = runners[r]}
o.on('-n', '--name=NAME', String,
"Runs tests matching NAME",
"(patterns may be used).") do |n|
n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
case n
when Regexp
filters << proc{|t| n =~ t.method_name}
else
filters << proc{|t| n == t.method_name}
end
end
o.on('-t', '--testcase=TESTCASE', String,
"Runs tests in TestCases matching TESTCASE",
"(patterns may be used).") do |n|
n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
case n
when Regexp
filters << proc{|t| n =~ t.class.name}
else
filters << proc{|t| n == t.class.name}
end
end
o.on('-v', '--verbose=[LEVEL]', (0..3).to_a.collect{|i| i.to_s},
"Set output level.",
"Levels are:",
" 0 = SILENT",
" 1 = PROGRESS_ONLY",
" 2 = NORMAL",
" 3 = VERBOSE (default)",
" Only valid for the console runner."){|l| output_level = (l ? l.to_i : 3)}
o.on('--',
"Stop processing options so that",
"remaining options will be passed",
"to the test."){throw :stop_processing}
o.on('-h', '--help', 'Display this help.'){puts o; exit(0)}
unless ARGV.empty? o.on_tail
criteria = ARGV.map { |arg| (arg =~ %r{^/(.*)/$}) ? Regexp.new($1) : arg } o.on_tail('Deprecated options:')
filters = [] o.on_tail('--console', 'Console runner (use --runner).') do
criteria.each do warn("Deprecated option (--console).")
| criterion | runner = runners[:console]
case criterion end
when Regexp o.on_tail('--gtk', 'GTK runner (use --runner).') do
filters << proc{|test| criterion =~ test.name} warn("Deprecated option (--gtk).")
when /^A-Z/ runner = runners[:gtk]
filters << proc{|test| criterion == test.class.name} end
else o.on_tail('--fox', 'Fox runner (use --runner).') do
filters << proc{|test| criterion == test.method_name} warn("Deprecated option (--fox).")
runner = runners[:fox]
end
o.on_tail
begin
o.parse!
rescue OptionParser::ParseError => e
puts e
puts o
exit(1)
end end
end end
collector.filter = filters
end end
if(output_level && !(runner == runners[:console]))
puts "Invalid arguments: You can only specify an output level with the console runner."
exit(1)
end
collector = Test::Unit::Collector::ObjectSpace::new
collector.filter = filters
suite_name = $0.sub(/\.rb$/, '') suite_name = $0.sub(/\.rb$/, '')
runner.call(collector.collect(suite_name)) runner.call(collector.collect(suite_name))
end end

View File

@ -10,6 +10,7 @@ module Test
def initialize(source=::ObjectSpace) def initialize(source=::ObjectSpace)
@source = source @source = source
@filters = []
end end
def collect(name=NAME) def collect(name=NAME)
@ -23,7 +24,7 @@ module Test
end end
def include(test) def include(test)
return true unless(@filters) return true if(@filters.empty?)
@filters.each do |filter| @filters.each do |filter|
return true if(filter.call(test)) return true if(filter.call(test))
end end

View File

@ -2,6 +2,7 @@
# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved. # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
# License:: Ruby license. # License:: Ruby license.
require 'test/unit'
require 'test/unit/collector/objectspace' require 'test/unit/collector/objectspace'
module Test module Test
@ -38,6 +39,10 @@ module Test
expected << @tc1.new('test_1') expected << @tc1.new('test_1')
expected << @tc1.new('test_2') expected << @tc1.new('test_2')
assert_equal(expected, ObjectSpace.new(@object_space).collect("name")) assert_equal(expected, ObjectSpace.new(@object_space).collect("name"))
c = ObjectSpace.new(@object_space)
c.filter = []
assert_equal(expected, c.collect("name"))
end end
def test_filtered_collection def test_filtered_collection