Skip to content
Browse files

Add configuration API.

  • Loading branch information...
1 parent be7a9ea commit 4ae9cf2a564063e007577aaafdfd4fed547a1763 @trans trans committed Jul 27, 2011
Showing with 174 additions and 94 deletions.
  1. +10 −0 .test.rb
  2. +52 −44 lib/test/cli.rb
  3. +101 −50 lib/test/runner.rb
  4. +11 −0 test/basic_case.rb
View
10 .test.rb
@@ -0,0 +1,10 @@
+Test.run(:common) do |run|
+ run.files << 'test/*_case.rb'
+end
+
+Test.run(:cov) do |run|
+ SimpleCov.start do |cov|
+ cov.coverage_dir = 'log/coverage'
+ end
+end
+
View
96 lib/test/cli.rb
@@ -1,5 +1,6 @@
module Test
+ require 'test/config'
require 'test/runner'
# Command line interface.
@@ -9,23 +10,26 @@ class Runner
# to the original Ruby cli test tool.
#
# @example
- # .testrb
+ # .test.rb
# .config/test.rb
#
- RC_GLOB = '{.testrb,.test.rb,.config/test.rb,config/test.rb}'
+ RC_GLOB = '{.,.config/,config/}test.rb'
# Test runner command line interface.
#
def self.cli(*argv)
- begin
- files, options = cli_options(argv)
+ runner = new
- cli_loadrc(files, options)
+ config_file = Dir[RC_GLOB].first
+ load config_file if config_file
- suite = $TEST_SUITE || []
- runner = new(suite, options)
- success = runner.run
+ cli_options(runner, argv)
+
+ begin
+ # Add standarad location if it exists.
+ $LOAD_PATH.unshift(File.expand_path('lib')) if File.directory?('lib')
+ success = runner.run
exit -1 unless success
rescue => error
raise error if $DEBUG
@@ -34,33 +38,56 @@ def self.cli(*argv)
end
#
- def self.cli_options(argv)
+ def self.cli_options(runner, argv)
require 'optparse'
- options = { :loadpath=>[], :requires=>[], :namespace=>[], :tags=>[], :casematch=>[] }
+
+ config = Test.config.dup
+ config_loaded = false
+
+ common = config.delete('common')
+ default = config.delete('default')
+
+ common.call(runner) if common
+
OptionParser.new do |opt|
opt.banner = "Usage: ruby-test [options] [files ...]"
+
+ unless config.empty?
+ opt.separator "PRESET OPTIONS:"
+ config.each do |name, block|
+ opt.on("--#{name}") do
+ block.call(runner)
+ end
+ end
+ end
+
+ opt.separator "CONFIG OPTIONS:"
+
opt.on '-f', '--format NAME', 'report format' do |name|
- options[:format] = name
+ runner.format = name
+ end
+ opt.on '-t', '--tag TAG', 'select tests by tag' do |tag|
+ runner.tags << tag
end
- #opt.on '-t', '--tag TAG', 'select tests by tag' do |tag|
- # options[:tags] << tag
- #end
#opt.on '-n', '--namespace NAME', 'select tests by target component' do |namespace|
# options[:namespace] << namespace
#end
opt.on '-m', '--match TEXT', 'select tests by description' do |text|
- options[:match] << text
+ runner.match << text
end
- opt.on '-I', '--loadpath PATH', 'add to $LOAD_PATH' do |path|
- paths = path.split(/[:;]/)
- options[:loadpath].concat(paths)
+ opt.on '-I', '--loadpath PATH', 'add to $LOAD_PATH' do |paths|
+ paths.split(/[:;]/).reverse_each do |path|
+ $LOAD_PATH.unshift path
+ end
end
- opt.on "-r FILE" , 'require file(s) (before doing anything else)' do |files|
- files = files.split(/[:;]/)
- options[:requires].concat(files)
+ opt.on '-r', '--require FILE', 'require file' do |file|
+ files.each{ |file| require file }
end
- #opt.on('-o', '--output DIRECTORY', 'log directory'){ |dir|
- # options[:output] = dir
+ opt.on '-v' , '--verbose', 'provide extra detailed report' do
+ runner.verbose = true
+ end
+ #opt.on('--log DIRECTORY', 'log directory'){ |dir|
+ # options[:log] = dir
#}
opt.on_tail("--[no-]ansi" , 'turn on/off ANSI colors'){ |v| $ansi = v }
opt.on_tail("--debug" , 'turn on debugging mode'){ $DEBUG = true }
@@ -75,28 +102,9 @@ def self.cli_options(argv)
}
end.parse!(argv)
- files = argv
- files = files.map{ |f| Dir[f] }.flatten
- files = files.map{ |f| File.directory?(f) ? Dir[File.join(f, '**/*.rb')] : f }
- files = files.flatten.uniq
- files = files.map{ |f| File.expand_path(f) }
-
- return files, options
- end
-
- #
- def self.cli_loadrc(files, options)
- if file = Dir[RC_GLOB].first
- load File.expand_path(file)
- end
-
- loadpath = options.delete(:loadpath) || ['lib'] # + ['lib'] ?
- requires = options.delete(:requires) || []
-
- loadpath.each{ |path| $LOAD_PATH.unshift(path) }
- requires.each{ |path| require(path) }
+ default.call(runner) if default && !config_loaded
- files.each{ |path| require(path) }
+ runner.files.concat(argv)
end
end
View
151 lib/test/runner.rb
@@ -1,20 +1,81 @@
module Test
+ # Runner will need the Recorder and Pending classes.
+ if RUBY_VERSION < '1.9'
+ require 'test/recorder'
+ require 'test/exception'
+ else
+ require_relative 'recorder'
+ require_relative 'exception'
+ end
+
$TEST_SUITE = [] unless defined?($TEST_SUITE)
# The Test::Runner class handles the execution of tests.
#
class Runner
+ # / / / D E F A U L T S / / /
+
# Default report is in the old "dot-progress" format.
- DEFAULT_REPORT_FORMAT = 'dotprogress'
+ DEFAULT_FORMAT = 'dotprogress'
+
+ #
+ def self.suite
+ $TEST_SUITE
+ end
+
+ #
+ def self.files
+ @files ||= []
+ end
+
+ #
+ def self.format
+ @format || DEFAULT_FORMAT
+ end
+
+ #
+ def self.format=(format)
+ @format = format
+ end
+
+ #
+ def self.verbose
+ @verbose
+ end
- # Test suite to run. This is a list of complient of tests and testcases.
+ #
+ def self.verbose=(boolean)
+ @verbose = !!boolean
+ end
+
+ #
+ def self.match
+ @match ||= []
+ end
+
+ #
+ def self.tags
+ @tags ||= []
+ end
+
+ # / / / A T T R I B U T E S / / /
+
+ # Test suite to run. This is a list of compliant test units and test cases.
attr :suite
+ # Test files to load.
+ attr :files
+
# Reporter format name, or name fragment, used to look up reporter class.
attr :format
+ #
+ def format=(name)
+ @format = name.to_s
+ end
+
# Matching text used to filter which tests are run.
attr :match
@@ -33,6 +94,21 @@ class Runner
# @options[:cover]
#end
+ # New Runner.
+ #
+ # @param [Array] suite
+ # A list of compliant tests/testcases.
+ #
+ def initialize(options={}, &block)
+ @suite = options[:suite] || self.class.suite
+ @files = options[:files] || self.class.files
+ @format = options[:format] || self.class.format
+ @tags = options[:tags] || self.class.tags
+ @match = options[:match] || self.class.match
+
+ block.call(self) if block
+ end
+
# The reporter to use for ouput.
attr :reporter
@@ -43,37 +119,29 @@ class Runner
# reporter instances.
attr :observers
- # New Runner.
- #
- # @param [Array] suite
- # A list of compliant tests/testcases.
- #
- def initialize(suite, options={})
- @suite = suite #|| $TESTS
- #@options = options
-
- @format = options[:format] || DEFAULT_REPORT_FORMAT
- @match = options[:match]
- @tags = options[:tags]
-
- @reporter = reporter_load(format)
- @recorder = Recorder.new
- @observers = [ @reporter, @recorder ]
- end
-
# Run test suite.
#
# @return [Boolean]
# That the tests ran without error or failure.
#
def run
+ files_resolved.each do |file|
+ require file
+ end
+
+ @reporter = reporter_load(format)
+ @recorder = Recorder.new
+ @observers = [@reporter, @recorder]
+
observers.each{ |o| o.start_suite(suite) }
run_case(suite)
observers.each{ |o| o.finish_suite(suite) }
recorder.success?
end
+ private
+
# Run a test case.
#
def run_case(cases)
@@ -135,9 +203,9 @@ def select(cases)
else
cases.each do |tc|
next if tc.respond_to?(:skip?) && tc.skip?
- next if match && match !~ tc.to_s
+ next if !match.empty? && !match.any?{ |m| m =~ tc.to_s }
if tc.respond_to?(:tags)
- tc_tags = [tc.tags].flatten
+ tc_tags = [tc.tags].flatten.map{ |t| t.to_s }
next if (tags & tc_tags).empty?
end
selected << tc
@@ -174,35 +242,18 @@ def reporter_list
end
end
- end
-
- #
- class Selection
- def initialize(test_object)
- @test_object
- end
- def call
- @test_object.call
- end
- def each(&block)
- @test_object.each(&block)
- end
- def to_s
- @test_object.to_s
- end
- def subtext
- @test_object.subtext
+ # Files can be globs and directories which need to be
+ # resolved to a list of files.
+ #
+ def files_resolved
+ list = files.flatten
+ list = list.map{ |f| Dir[f] }.flatten
+ list = list.map{ |f| File.directory?(f) ? Dir[File.join(f, '**/*.rb')] : f }
+ list = list.flatten.uniq
+ list = list.map{ |f| File.expand_path(f) }
+ list
end
- # any others?
- end
-end
+ end
-# Runner will need the Recorder and Pending classes.
-if RUBY_VERSION < '1.9'
- require 'test/recorder'
- require 'test/exception'
-else
- require_relative 'recorder'
- require_relative 'exception'
end
View
11 test/basic_case.rb
@@ -0,0 +1,11 @@
+test = Object.new
+
+def test.call
+end
+
+def test.to_s
+ "Basic Test"
+end
+
+$TEST_SUITE << test
+

0 comments on commit 4ae9cf2

Please sign in to comment.
Something went wrong with that request. Please try again.