lib/rgot/m.rb in rgot-1.1.0 vs lib/rgot/m.rb in rgot-1.2.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'stringio'
require 'etc'
require 'timeout'
module Rgot
@@ -10,55 +12,85 @@
:timeout,
:cpu,
:thread,
); end
- def initialize(tests:, benchmarks:, examples:, opts: Options.new)
- cpu = opts.cpu || (Etc.respond_to?(:nprocessors) ? Etc.nprocessors : '1').to_s
- @cpu_list = cpu.split(',').map { |i|
- j = i.to_i
- raise Rgot::OptionError, "invalid value #{i.inspect} for --cpu" unless 0 < j
- j
- }
- @thread_list = (opts.thread || "1").split(',').map { |i|
- j = i.to_i
- raise Rgot::OptionError, "invalid value #{i.inspect} for --thread" unless 0 < j
- j
- }
+ def initialize(tests:, benchmarks:, examples:, test_module: nil, opts: Options.new)
+ unless test_module
+ raise "Require `test_module` keyword" if Gem::Version.new("2.0") <= Gem::Version.new(Rgot::VERSION)
+ warn "`Rgot::M#initialize` will require the `test_module` keyword in the next major version."
+ end
+
@tests = tests
@benchmarks = benchmarks
@examples = examples
+ @test_module = test_module
@opts = opts
+ @cpu_list = nil
+ @thread_list = nil
end
def run
+ duration = Rgot.now
test_ok = false
example_ok = false
+ if @tests.empty? && @benchmarks.empty? && @examples.empty?
+ warn "rgot: warning: no tests to run"
+ end
+
+ begin
+ parse_option
+ rescue Rgot::OptionError
+ puts sprintf("%s\t%s\t%.3fs", "FAIL", @test_module, Rgot.now - duration)
+ raise
+ end
+
Timeout.timeout(@opts.timeout.to_f) do
test_ok = run_tests
example_ok = run_examples
end
+
if !test_ok || !example_ok
puts "FAIL"
- return 1
+ puts "exit status 1"
+ puts sprintf("%s\t%s\t%.3fs", "FAIL", @test_module, Rgot.now - duration)
+
+ 1
+ else
+ puts "PASS"
+ run_benchmarks
+ puts sprintf("%s\t%s\t%.3fs", "ok ", @test_module, Rgot.now - duration)
+
+ 0
end
- puts "PASS"
- run_benchmarks
- 0
end
private
+ def parse_option
+ cpu = @opts.cpu || (Etc.respond_to?(:nprocessors) ? Etc.nprocessors : '1').to_s
+ @cpu_list = cpu.split(',').map { |i|
+ j = i.to_i
+ raise Rgot::OptionError, "invalid value #{i.inspect} for --cpu" unless 0 < j
+ j
+ }
+
+ @thread_list = (@opts.thread || "1").split(',').map { |i|
+ j = i.to_i
+ raise Rgot::OptionError, "invalid value #{i.inspect} for --thread" unless 0 < j
+ j
+ }
+ end
+
def run_tests
ok = true
@tests.each do |test|
t = T.new(test.module, test.name.to_sym)
if Rgot.verbose?
puts "=== RUN #{test.name}"
end
t.run
- t.report
if t.failed?
ok = false
end
end
ok