From b0ccb799ec0a0893699470dadda7715fe1afcc35 Mon Sep 17 00:00:00 2001 From: ntalbott Date: Fri, 3 Oct 2003 04:04:26 +0000 Subject: * 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 --- ChangeLog | 18 +++++ lib/test/unit.rb | 114 +++++++++++++++++++++------- lib/test/unit/collector/objectspace.rb | 3 +- test/testunit/collector/test_objectspace.rb | 5 ++ 4 files changed, 111 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index b268f09135..0ece4aa643 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +Fri Oct 3 13:02:00 2003 Nathaniel Talbott + + * 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 * lib/test/unit/assertions.rb: added a default message for #assert, diff --git a/lib/test/unit.rb b/lib/test/unit.rb index b891883c30..08e57f2f17 100644 --- a/lib/test/unit.rb +++ b/lib/test/unit.rb @@ -276,51 +276,109 @@ require 'test/unit/ui/testrunnermediator' require 'test/unit/collector/objectspace' at_exit { - # We can't debug tests run with at_exit unless we add the following: - set_trace_func DEBUGGER__.context.method(:trace_func).to_proc if (defined? DEBUGGER__) - + require 'optparse' if (!Test::Unit::UI::TestRunnerMediator.run?) + output_level = nil runners = { - '--console' => proc do |suite| + :console => proc do |suite| 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) end, - '--gtk' => proc do |suite| + :gtk => proc do |suite| require 'test/unit/ui/gtk/testrunner' Test::Unit::UI::GTK::TestRunner.run(suite) end, - '--fox' => proc do |suite| + :fox => proc do |suite| require 'test/unit/ui/fox/testrunner' Test::Unit::UI::Fox::TestRunner.run(suite) end, } - - unless (ARGV.empty?) - runner = runners[ARGV[0]] - ARGV.shift unless (runner.nil?) - end - runner = runners['--console'] if (runner.nil?) + + runner = runners[:console] + filters = [] + catch(:stop_processing) do + ARGV.options do |o| + 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)} + + o.on_tail + o.on_tail('Deprecated options:') + o.on_tail('--console', 'Console runner (use --runner).') do + warn("Deprecated option (--console).") + runner = runners[:console] + end + o.on_tail('--gtk', 'GTK runner (use --runner).') do + warn("Deprecated option (--gtk).") + runner = runners[:gtk] + end + o.on_tail('--fox', 'Fox runner (use --runner).') do + warn("Deprecated option (--fox).") + runner = runners[:fox] + end + o.on_tail - unless ARGV.empty? - criteria = ARGV.map { |arg| (arg =~ %r{^/(.*)/$}) ? Regexp.new($1) : arg } - filters = [] - criteria.each do - | criterion | - case criterion - when Regexp - filters << proc{|test| criterion =~ test.name} - when /^A-Z/ - filters << proc{|test| criterion == test.class.name} - else - filters << proc{|test| criterion == test.method_name} + begin + o.parse! + rescue OptionParser::ParseError => e + puts e + puts o + exit(1) end end - collector.filter = filters 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$/, '') runner.call(collector.collect(suite_name)) end diff --git a/lib/test/unit/collector/objectspace.rb b/lib/test/unit/collector/objectspace.rb index 162f1ec4f5..ac79980d7f 100644 --- a/lib/test/unit/collector/objectspace.rb +++ b/lib/test/unit/collector/objectspace.rb @@ -10,6 +10,7 @@ module Test def initialize(source=::ObjectSpace) @source = source + @filters = [] end def collect(name=NAME) @@ -23,7 +24,7 @@ module Test end def include(test) - return true unless(@filters) + return true if(@filters.empty?) @filters.each do |filter| return true if(filter.call(test)) end diff --git a/test/testunit/collector/test_objectspace.rb b/test/testunit/collector/test_objectspace.rb index 9e102536a5..37de5a439b 100644 --- a/test/testunit/collector/test_objectspace.rb +++ b/test/testunit/collector/test_objectspace.rb @@ -2,6 +2,7 @@ # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved. # License:: Ruby license. +require 'test/unit' require 'test/unit/collector/objectspace' module Test @@ -38,6 +39,10 @@ module Test expected << @tc1.new('test_1') expected << @tc1.new('test_2') assert_equal(expected, ObjectSpace.new(@object_space).collect("name")) + + c = ObjectSpace.new(@object_space) + c.filter = [] + assert_equal(expected, c.collect("name")) end def test_filtered_collection -- cgit v1.2.3