2011-02-19 15:44:08 -05:00
|
|
|
######################################################################
|
|
|
|
# This file is imported from the minitest project.
|
|
|
|
# DO NOT make modifications in this repo. They _will_ be reverted!
|
|
|
|
# File a patch instead and assign it to Ryan Davis.
|
|
|
|
######################################################################
|
2008-10-09 21:18:03 -04:00
|
|
|
|
|
|
|
#!/usr/bin/ruby -w
|
|
|
|
|
|
|
|
require 'minitest/unit'
|
|
|
|
|
|
|
|
class Module
|
2010-09-23 17:27:19 -04:00
|
|
|
def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
|
2010-01-06 17:58:07 -05:00
|
|
|
# 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
|
|
|
|
|
2010-09-23 17:27:19 -04:00
|
|
|
##
|
|
|
|
# Create your own expectations from MiniTest::Assertions using a
|
|
|
|
# flexible set of rules. If you don't like must/wont, then this
|
|
|
|
# method is your friend. For an example of its usage see the bottom
|
|
|
|
# of minitest/spec.rb.
|
|
|
|
|
2010-01-06 17:58:07 -05:00
|
|
|
def infect_with_assertions(pos_prefix, neg_prefix,
|
|
|
|
skip_re,
|
|
|
|
dont_flip_re = /\c0/,
|
|
|
|
map = {})
|
2010-09-23 17:27:19 -04:00
|
|
|
MiniTest::Assertions.public_instance_methods(false).sort.each do |meth|
|
2008-10-09 21:18:03 -04:00
|
|
|
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-09-23 17:27:19 -04:00
|
|
|
puts "\n##\n# :method: #{new_name}\n# See MiniTest::Assertions##{meth}" if
|
|
|
|
$0 == __FILE__
|
|
|
|
|
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
|
|
|
|
|
|
|
|
module Kernel
|
2010-03-27 21:37:07 -04:00
|
|
|
##
|
|
|
|
# Describe a series of expectations for a given target +desc+.
|
|
|
|
#
|
|
|
|
# TODO: find good tutorial url.
|
|
|
|
#
|
|
|
|
# Defines a test class subclassing from either
|
|
|
|
# MiniTest::Unit::TestCase or from the surrounding describe's class.
|
|
|
|
|
2008-10-09 21:18:03 -04:00
|
|
|
def describe desc, &block
|
2009-06-18 03:31:39 -04:00
|
|
|
stack = MiniTest::Spec.describe_stack
|
2010-12-01 00:33:32 -05:00
|
|
|
name = [stack.last, desc].compact.join("::")
|
|
|
|
cls = Class.new(stack.last || MiniTest::Spec)
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
# omg this sucks
|
|
|
|
(class << cls; self; end).send(:define_method, :to_s) { name }
|
|
|
|
# :startdoc:
|
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
|
2010-09-01 04:40:53 -04:00
|
|
|
cls
|
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
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
class Module
|
|
|
|
def classes type = Object # :nodoc:
|
|
|
|
constants.map { |n| const_get n }.find_all { |c|
|
|
|
|
c.class == Class and type > c
|
|
|
|
} - [self]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-23 17:27:19 -04:00
|
|
|
##
|
|
|
|
# MiniTest::Spec -- The faster, better, less-magical spec framework!
|
|
|
|
#
|
|
|
|
# For a list of expectations, see Object.
|
|
|
|
|
|
|
|
|
2008-10-09 21:18:03 -04:00
|
|
|
class MiniTest::Spec < MiniTest::Unit::TestCase
|
2010-12-01 00:33:32 -05:00
|
|
|
@@describe_stack = []
|
2010-03-27 21:37:07 -04:00
|
|
|
def self.describe_stack # :nodoc:
|
2009-06-18 03:31:39 -04:00
|
|
|
@@describe_stack
|
|
|
|
end
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
def self.current # :nodoc:
|
2008-10-09 21:18:03 -04:00
|
|
|
@@current_spec
|
|
|
|
end
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
def initialize name # :nodoc:
|
2008-10-09 21:18:03 -04:00
|
|
|
super
|
|
|
|
@@current_spec = self
|
|
|
|
end
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
def self.nuke_test_methods! # :nodoc:
|
2009-06-18 03:31:39 -04:00
|
|
|
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
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
def self.define_inheritable_method name, &block # :nodoc:
|
2009-06-18 03:31:39 -04:00
|
|
|
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
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
##
|
|
|
|
# Define a 'before' action. Inherits the way normal methods should.
|
|
|
|
#
|
|
|
|
# NOTE: +type+ is ignored and is only there to make porting easier.
|
|
|
|
#
|
|
|
|
# Equivalent to MiniTest::Unit::TestCase#setup.
|
|
|
|
|
|
|
|
def self.before type = :each, &block
|
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
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
##
|
|
|
|
# Define an 'after' action. Inherits the way normal methods should.
|
|
|
|
#
|
|
|
|
# NOTE: +type+ is ignored and is only there to make porting easier.
|
|
|
|
#
|
|
|
|
# Equivalent to MiniTest::Unit::TestCase#teardown.
|
|
|
|
|
|
|
|
def self.after type = :each, &block
|
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
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
##
|
|
|
|
# Define an expectation with name +desc+. Name gets morphed to a
|
|
|
|
# proper test method name. For some freakish reason, people who
|
|
|
|
# write specs don't like class inheritence, so this goes way out of
|
|
|
|
# its way to make sure that expectations aren't inherited.
|
|
|
|
#
|
|
|
|
# Hint: If you _do_ want inheritence, use minitest/unit. You can mix
|
|
|
|
# and match between assertions and expectations as much as you want.
|
|
|
|
|
2008-10-09 21:18:03 -04:00
|
|
|
def self.it desc, &block
|
2010-03-27 21:37:07 -04:00
|
|
|
block ||= proc { skip "(no tests defined)" }
|
|
|
|
|
|
|
|
@specs ||= 0
|
|
|
|
@specs += 1
|
|
|
|
|
|
|
|
name = "test_%04d_%s" % [ @specs, desc.gsub(/\W+/, '_').downcase ]
|
|
|
|
|
|
|
|
define_method name, &block
|
|
|
|
|
|
|
|
classes(MiniTest::Spec).each do |mod|
|
|
|
|
mod.send :undef_method, name if mod.respond_to? name
|
|
|
|
end
|
2008-10-09 21:18:03 -04:00
|
|
|
end
|
2010-09-23 17:27:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Object.infect_with_assertions(:must, :wont,
|
|
|
|
/^(must|wont)$|wont_(throw)|
|
|
|
|
must_(block|not?_|nothing|raise$)/x,
|
|
|
|
/(must|wont)_(include|respond_to)/,
|
|
|
|
/(must_throw)s/ => '\1',
|
|
|
|
/(?!not)_same/ => '_be_same_as',
|
|
|
|
/_in_/ => '_be_within_',
|
|
|
|
/_operator/ => '_be',
|
|
|
|
/_includes/ => '_include',
|
|
|
|
/(must|wont)_(.*_of|nil|silent|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
|
|
|
|
|
|
|
|
if $0 == __FILE__ then
|
|
|
|
{ "must" => "assert", "wont" => "refute" }.each do |a, b|
|
|
|
|
puts "\n"
|
|
|
|
puts "##"
|
|
|
|
puts "# :method: #{a}_be_close_to"
|
|
|
|
puts "# See MiniTest::Assertions##{b}_in_delta"
|
|
|
|
end
|
|
|
|
end
|
2010-03-27 21:37:07 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_be
|
2010-09-23 17:27:19 -04:00
|
|
|
# See MiniTest::Assertions#assert_operator
|
2010-03-27 21:37:07 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_be_close_to
|
|
|
|
# See MiniTest::Assertions#assert_in_delta
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_be_empty
|
|
|
|
# See MiniTest::Assertions#assert_empty
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_be_instance_of
|
|
|
|
# See MiniTest::Assertions#assert_instance_of
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_be_kind_of
|
|
|
|
# See MiniTest::Assertions#assert_kind_of
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_be_nil
|
|
|
|
# See MiniTest::Assertions#assert_nil
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_be_same_as
|
|
|
|
# See MiniTest::Assertions#assert_same
|
|
|
|
|
2010-09-01 04:40:53 -04:00
|
|
|
##
|
|
|
|
# :method: must_be_silent
|
|
|
|
# See MiniTest::Assertions#assert_silent
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
##
|
|
|
|
# :method: must_be_within_delta
|
|
|
|
# See MiniTest::Assertions#assert_in_delta
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_be_within_epsilon
|
|
|
|
# See MiniTest::Assertions#assert_in_epsilon
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_equal
|
|
|
|
# See MiniTest::Assertions#assert_equal
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_include
|
|
|
|
# See MiniTest::Assertions#assert_includes
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_match
|
|
|
|
# See MiniTest::Assertions#assert_match
|
|
|
|
|
2010-09-01 04:40:53 -04:00
|
|
|
##
|
|
|
|
# :method: must_output
|
|
|
|
# See MiniTest::Assertions#assert_output
|
|
|
|
|
2010-03-27 21:37:07 -04:00
|
|
|
##
|
|
|
|
# :method: must_raise
|
|
|
|
# See MiniTest::Assertions#assert_raises
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_respond_to
|
|
|
|
# See MiniTest::Assertions#assert_respond_to
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_send
|
|
|
|
# See MiniTest::Assertions#assert_send
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: must_throw
|
2010-09-23 17:27:19 -04:00
|
|
|
# See MiniTest::Assertions#assert_throws
|
2010-03-27 21:37:07 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_be
|
2010-09-23 17:27:19 -04:00
|
|
|
# See MiniTest::Assertions#refute_operator
|
2010-03-27 21:37:07 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_be_close_to
|
|
|
|
# See MiniTest::Assertions#refute_in_delta
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_be_empty
|
|
|
|
# See MiniTest::Assertions#refute_empty
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_be_instance_of
|
|
|
|
# See MiniTest::Assertions#refute_instance_of
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_be_kind_of
|
|
|
|
# See MiniTest::Assertions#refute_kind_of
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_be_nil
|
|
|
|
# See MiniTest::Assertions#refute_nil
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_be_same_as
|
|
|
|
# See MiniTest::Assertions#refute_same
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_be_within_delta
|
|
|
|
# See MiniTest::Assertions#refute_in_delta
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_be_within_epsilon
|
|
|
|
# See MiniTest::Assertions#refute_in_epsilon
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_equal
|
|
|
|
# See MiniTest::Assertions#refute_equal
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_include
|
|
|
|
# See MiniTest::Assertions#refute_includes
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_match
|
|
|
|
# See MiniTest::Assertions#refute_match
|
|
|
|
|
|
|
|
##
|
|
|
|
# :method: wont_respond_to
|
|
|
|
# See MiniTest::Assertions#refute_respond_to
|
2008-10-09 21:18:03 -04:00
|
|
|
end
|