diff options
Diffstat (limited to 'lib/test')
| -rw-r--r-- | lib/test/unit.rb | 185 | ||||
| -rw-r--r-- | lib/test/unit/assertions.rb | 161 | ||||
| -rw-r--r-- | lib/test/unit/testcase.rb | 15 |
3 files changed, 0 insertions, 361 deletions
diff --git a/lib/test/unit.rb b/lib/test/unit.rb deleted file mode 100644 index 87c9d1b022..0000000000 --- a/lib/test/unit.rb +++ /dev/null @@ -1,185 +0,0 @@ -# test/unit compatibility layer using minitest. - -require 'minitest/unit' -require 'test/unit/assertions' -require 'test/unit/testcase' -require 'optparse' - -module Test - module Unit - TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest' - - module RunCount - @@run_count = 0 - - def self.have_run? - @@run_count.nonzero? - end - - def run(*) - @@run_count += 1 - super - end - - def run_once - return if have_run? - return if $! # don't run if there was an exception - yield - end - module_function :run_once - end - - module Options - def initialize(&block) - @init_hook = block - super(&nil) - end - - def process_args(args = []) - options = {} - OptionParser.new do |opts| - setup_options(opts, options) - opts.parse!(args) - end - args = @init_hook.call(args, options) if @init_hook - non_options(args, options) - options - end - - private - def setup_options(opts, options) - opts.banner = 'minitest options:' - opts.version = MiniTest::Unit::VERSION - - opts.on '-h', '--help', 'Display this help.' do - puts opts - exit - end - - opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m| - options[:seed] = m.to_i - end - - opts.on '-v', '--verbose', "Verbose. Show progress processing files." do - options[:verbose] = true - end - - opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a| - options[:filter] = a - end - end - - def non_options(files, options) - end - end - - module GlobOption - include Options - - def setup_options(parser, options) - super - parser.on '-x', '--exclude PATTERN' do |pattern| - (options[:reject] ||= []) << pattern - end - end - - def non_options(files, options) - paths = [options.delete(:base_directory), nil].compact - if reject = options.delete(:reject) - reject_pat = Regexp.union(reject.map {|r| /#{r}/ }) - end - files.map! {|f| - f = f.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR - [*paths, nil].any? do |prefix| - path = prefix ? "#{prefix}/#{f}" : f - if !(match = Dir["#{path}/**/test_*.rb"]).empty? - if reject - match.reject! {|n| - n[(prefix.length+1)..-1] if prefix - reject_pat =~ n - } - end - break match - elsif !reject or reject_pat !~ f and File.exist? path - break path - end - end or - raise ArgumentError, "file not found: #{f}" - } - files.flatten! - super(files, options) - end - end - - module LoadPathOption - include Options - - def setup_options(parser, options) - super - parser.on '-Idirectory' do |dirs| - dirs.split(':').each { |d| $LOAD_PATH.unshift d } - end - end - end - - module RequireFiles - def non_options(files, options) - super - files.each {|f| - d = File.dirname(path = File.expand_path(f)) - unless $:.include? d - $: << d - end - begin - require path - rescue LoadError - puts "#{f}: #{$!}" - end - } - end - end - - def self.new(*args, &block) - Mini.class_eval do - include Test::Unit::RequireFiles - end - Mini.new(*args, &block) - end - - class Mini < MiniTest::Unit - include Test::Unit::GlobOption - include Test::Unit::LoadPathOption - include Test::Unit::RunCount - include Test::Unit::Options - - class << self; undef autorun; end - def self.autorun - at_exit { - Test::Unit::RunCount.run_once { - exit(Test::Unit::Mini.new.run(ARGV) || true) - } - } unless @@installed_at_exit - @@installed_at_exit = true - end - - def run(*args) - result = super - abort if @interrupt - result - end - - def run_test_suites(*args) - old_sync = @@out.sync if @@out.respond_to?(:sync=) - @interrupt = false - super - rescue Interrupt - @interrupt = true - [@test_count, @assertion_count] - ensure - @@out.sync = old_sync if @@out.respond_to?(:sync=) - end - end - end -end - -Test::Unit::Mini.autorun diff --git a/lib/test/unit/assertions.rb b/lib/test/unit/assertions.rb deleted file mode 100644 index ce28042c49..0000000000 --- a/lib/test/unit/assertions.rb +++ /dev/null @@ -1,161 +0,0 @@ -require 'minitest/unit' -require 'pp' - -module Test - module Unit - module Assertions - include MiniTest::Assertions - - def mu_pp(obj) - obj.pretty_inspect.chomp - end - - UNASSIGNED = Object.new # :nodoc: - - def assert(test, msg = UNASSIGNED) - case msg - when UNASSIGNED - msg = nil - when String, Proc - else - bt = caller.reject { |s| s.rindex(MiniTest::MINI_DIR, 0) } - raise ArgumentError, "assertion message must be String or Proc, but #{msg.class} was given.", bt - end - super - end - - def assert_raise(*args, &b) - assert_raises(*args, &b) - end - - def assert_nothing_raised(*args) - self._assertions += 1 - if Module === args.last - msg = nil - else - msg = args.pop - end - begin - line = __LINE__; yield - rescue MiniTest::Skip - raise - rescue Exception => e - bt = e.backtrace - as = e.instance_of?(MiniTest::Assertion) - if as - ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o - bt.reject! {|ln| ans =~ ln} - end - if ((args.empty? && !as) || - args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a }) - msg = message(msg) { "Exception raised:\n<#{mu_pp(e)}>" } - raise MiniTest::Assertion, msg.call, bt - else - raise - end - end - nil - end - - def assert_nothing_thrown(msg=nil) - begin - yield - rescue ArgumentError => error - raise error if /\Auncaught throw (.+)\z/m !~ error.message - msg = message(msg) { "<#{$1}> was thrown when nothing was expected" } - flunk(msg) - end - assert(true, "Expected nothing to be thrown") - end - - def assert_equal(exp, act, msg = nil) - msg = message(msg) { - exp_str = mu_pp(exp) - act_str = mu_pp(act) - exp_comment = '' - act_comment = '' - if exp_str == act_str - if (exp.is_a?(String) && act.is_a?(String)) || - (exp.is_a?(Regexp) && act.is_a?(Regexp)) - exp_comment = " (#{exp.encoding})" - act_comment = " (#{act.encoding})" - elsif exp.is_a?(Float) && act.is_a?(Float) - exp_str = "%\#.#{Float::DIG+2}g" % exp - act_str = "%\#.#{Float::DIG+2}g" % act - elsif exp.is_a?(Time) && act.is_a?(Time) - if exp.subsec * 1000_000_000 == exp.nsec - exp_comment = " (#{exp.nsec}[ns])" - else - exp_comment = " (subsec=#{exp.subsec})" - end - if act.subsec * 1000_000_000 == act.nsec - act_comment = " (#{act.nsec}[ns])" - else - act_comment = " (subsec=#{act.subsec})" - end - elsif exp.class != act.class - # a subclass of Range, for example. - exp_comment = " (#{exp.class})" - act_comment = " (#{act.class})" - end - elsif !Encoding.compatible?(exp_str, act_str) - if exp.is_a?(String) && act.is_a?(String) - exp_str = exp.dump - act_str = act.dump - exp_comment = " (#{exp.encoding})" - act_comment = " (#{act.encoding})" - else - exp_str = exp_str.dump - act_str = act_str.dump - end - end - "<#{exp_str}>#{exp_comment} expected but was\n<#{act_str}>#{act_comment}" - } - assert(exp == act, msg) - end - - def assert_not_nil(exp, msg=nil) - msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" } - assert(!exp.nil?, msg) - end - - def assert_not_equal(exp, act, msg=nil) - msg = message(msg) { "<#{mu_pp(exp)}> expected to be != to\n<#{mu_pp(act)}>" } - assert(exp != act, msg) - end - - def assert_no_match(regexp, string, msg=nil) - assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.") - self._assertions -= 1 - msg = message(msg) { "<#{mu_pp(regexp)}> expected to not match\n<#{mu_pp(string)}>" } - assert(regexp !~ string, msg) - end - - def assert_not_same(expected, actual, message="") - msg = message(msg) { build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__) } -<?> -with id <?> expected to not be equal\\? to -<?> -with id <?>. -EOT - assert(!actual.equal?(expected), msg) - end - - # get rid of overcounting - def assert_respond_to obj, meth, msg = nil - super if !caller[0].rindex(MiniTest::MINI_DIR, 0) || !obj.respond_to?(meth) - end - - ms = instance_methods(true).map {|sym| sym.to_s } - ms.grep(/\Arefute_/) do |m| - mname = ('assert_not_' << m.to_s[/.*?_(.*)/, 1]) - alias_method(mname, m) unless ms.include? mname - end - - def build_message(head, template=nil, *arguments) - template &&= template.chomp - template.gsub(/\?/) { mu_pp(arguments.shift) } - end - end - end -end diff --git a/lib/test/unit/testcase.rb b/lib/test/unit/testcase.rb deleted file mode 100644 index ec3d362c21..0000000000 --- a/lib/test/unit/testcase.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'test/unit/assertions' - -module Test - module Unit - # remove silly TestCase class - remove_const(:TestCase) if defined?(self::TestCase) - - class TestCase < MiniTest::Unit::TestCase - include Assertions - def self.test_order - :sorted - end - end - end -end |
