diff options
author | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-10-10 01:18:03 +0000 |
---|---|---|
committer | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-10-10 01:18:03 +0000 |
commit | 7ebbb3871e473dca485993759b07a9a86d84d70c (patch) | |
tree | 7ccd6a46b401d455cd0dc8de7037d90fbc4a7f70 /lib/minitest/spec.rb | |
parent | 30e76a6af3f1ea21552a566680a19254e1c005fb (diff) |
Added minitest 1.3.0
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19740 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/minitest/spec.rb')
-rw-r--r-- | lib/minitest/spec.rb | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/minitest/spec.rb b/lib/minitest/spec.rb new file mode 100644 index 0000000000..130910ed85 --- /dev/null +++ b/lib/minitest/spec.rb @@ -0,0 +1,88 @@ +############################################################ +# This file is imported from a different project. +# DO NOT make modifications in this repo. +# File a patch instead and assign it to Ryan Davis +############################################################ + +#!/usr/bin/ruby -w + +require 'minitest/unit' + +class Module + def infect_with_assertions pos_prefix, neg_prefix, skip_re, map = {} + MiniTest::Assertions.public_instance_methods(false).each do |meth| + meth = meth.to_s + + new_name = case meth + when /^assert/ then + meth.sub(/^assert/, pos_prefix.to_s) + when /^refute/ then + meth.sub(/^refute/, neg_prefix.to_s) + end + next unless new_name + next if new_name =~ skip_re + + regexp, replacement = map.find { |re, _| new_name =~ re } + new_name.sub! regexp, replacement if replacement + + # warn "%-22p -> %p %p" % [meth, new_name, regexp] + self.class_eval <<-EOM + def #{new_name} *args, &block + return MiniTest::Spec.current.#{meth}(*args, &self) if Proc === self + return MiniTest::Spec.current.#{meth}(args.first, self) if args.size == 1 + return MiniTest::Spec.current.#{meth}(self, *args) + end + EOM + end + end +end + +Object.infect_with_assertions(:must, :wont, + /^(must|wont)$|wont_(throw)| + must_(block|not?_|nothing|raise$)/x, + /(must_throw)s/ => '\1', + /(?!not)_same/ => '_be_same_as', + /_in_/ => '_be_within_', + /_operator/ => '_be', + /_includes/ => '_include', + /(must|wont)_(.*_of|nil|empty)/ => '\1_be_\2', + /must_raises/ => 'must_raise') + +class Object + alias :must_be_close_to :must_be_within_delta + alias :wont_be_close_to :wont_be_within_delta +end + +module Kernel + def describe desc, &block + cls = Class.new(MiniTest::Spec) + Object.const_set desc.to_s.split(/\W+/).map { |s| s.capitalize }.join, cls + + cls.class_eval(&block) + end +end + +class MiniTest::Spec < MiniTest::Unit::TestCase + def self.current + @@current_spec + end + + def initialize name + super + @@current_spec = self + end + + def self.before(type = :each, &block) + raise "unsupported before type: #{type}" unless type == :each + define_method :setup, &block + end + + def self.after(type = :each, &block) + raise "unsupported after type: #{type}" unless type == :each + define_method :teardown, &block + end + + def self.it desc, &block + define_method "test_#{desc.gsub(/\W+/, '_').downcase}", &block + end +end |