2008-10-09 21:18:03 -04:00
|
|
|
############################################################
|
|
|
|
# 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
|
2010-01-06 17:58:07 -05:00
|
|
|
def infect_an_assertion meth, new_name, dont_flip = false
|
|
|
|
# warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
|
|
|
|
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 unless #{!!dont_flip}
|
|
|
|
return MiniTest::Spec.current.#{meth}(self, *args)
|
|
|
|
end
|
|
|
|
EOM
|
|
|
|
end
|
|
|
|
|
|
|
|
def infect_with_assertions(pos_prefix, neg_prefix,
|
|
|
|
skip_re,
|
|
|
|
dont_flip_re = /\c0/,
|
|
|
|
map = {})
|
2008-10-09 21:18:03 -04:00
|
|
|
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
|
|
|
|
|
2010-01-06 17:58:07 -05:00
|
|
|
infect_an_assertion meth, new_name, new_name =~ dont_flip_re
|
2008-10-09 21:18:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Object.infect_with_assertions(:must, :wont,
|
|
|
|
/^(must|wont)$|wont_(throw)|
|
|
|
|
must_(block|not?_|nothing|raise$)/x,
|
2010-01-06 17:58:07 -05:00
|
|
|
/(must|wont)_(include|respond_to)/,
|
2008-10-09 21:18:03 -04:00
|
|
|
/(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
|
2009-06-18 03:31:39 -04:00
|
|
|
stack = MiniTest::Spec.describe_stack
|
|
|
|
name = desc.to_s.split(/\W+/).map { |s| s.capitalize }.join + "Spec"
|
2010-01-06 17:58:07 -05:00
|
|
|
prev = stack.last
|
|
|
|
name = "#{prev == MiniTest::Spec ? nil : prev}::#{name}"
|
|
|
|
cls = Object.class_eval "class #{name} < #{prev}; end; #{name}"
|
2008-10-09 21:18:03 -04:00
|
|
|
|
2009-06-18 03:31:39 -04:00
|
|
|
cls.nuke_test_methods!
|
|
|
|
|
|
|
|
stack.push cls
|
2008-10-09 21:18:03 -04:00
|
|
|
cls.class_eval(&block)
|
2009-06-18 03:31:39 -04:00
|
|
|
stack.pop
|
2008-10-09 21:18:03 -04:00
|
|
|
end
|
2008-12-27 15:51:23 -05:00
|
|
|
private :describe
|
2008-10-09 21:18:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class MiniTest::Spec < MiniTest::Unit::TestCase
|
2009-06-18 03:31:39 -04:00
|
|
|
@@describe_stack = [MiniTest::Spec]
|
|
|
|
def self.describe_stack
|
|
|
|
@@describe_stack
|
|
|
|
end
|
|
|
|
|
2008-10-09 21:18:03 -04:00
|
|
|
def self.current
|
|
|
|
@@current_spec
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize name
|
|
|
|
super
|
|
|
|
@@current_spec = self
|
|
|
|
end
|
|
|
|
|
2009-06-18 03:31:39 -04:00
|
|
|
def self.nuke_test_methods!
|
|
|
|
self.public_instance_methods.grep(/^test_/).each do |name|
|
2010-01-06 17:58:07 -05:00
|
|
|
self.send :undef_method, name
|
2009-06-18 03:31:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.define_inheritable_method name, &block
|
|
|
|
super_method = self.superclass.instance_method name
|
|
|
|
|
|
|
|
define_method name do
|
|
|
|
super_method.bind(self).call if super_method # regular super() warns
|
|
|
|
instance_eval(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-09 21:18:03 -04:00
|
|
|
def self.before(type = :each, &block)
|
2010-01-06 17:58:07 -05:00
|
|
|
if type == :all
|
|
|
|
warn "change before :all to before :each"
|
|
|
|
type = :each
|
|
|
|
end
|
2008-10-09 21:18:03 -04:00
|
|
|
raise "unsupported before type: #{type}" unless type == :each
|
2009-06-18 03:31:39 -04:00
|
|
|
define_inheritable_method :setup, &block
|
2008-10-09 21:18:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.after(type = :each, &block)
|
2010-01-06 17:58:07 -05:00
|
|
|
if type == :all # REFACTOR
|
|
|
|
warn "change before :all to before :each"
|
|
|
|
type = :each
|
|
|
|
end
|
2008-10-09 21:18:03 -04:00
|
|
|
raise "unsupported after type: #{type}" unless type == :each
|
2009-06-18 03:31:39 -04:00
|
|
|
define_inheritable_method :teardown, &block
|
2008-10-09 21:18:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.it desc, &block
|
|
|
|
define_method "test_#{desc.gsub(/\W+/, '_').downcase}", &block
|
|
|
|
end
|
|
|
|
end
|