aboutsummaryrefslogtreecommitdiffstats
path: root/lib/minitest/spec.rb
diff options
context:
space:
mode:
authorryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-02 04:48:43 +0000
committerryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-02 04:48:43 +0000
commitf8e5c7c79e720d3b0af3cb96f27d421f08eb7744 (patch)
tree0d950c668cf9141d516152aa7ca117d62afa3676 /lib/minitest/spec.rb
parentce3029cd19dc40f4ffe09b8c6633500bd182b324 (diff)
downloadruby-f8e5c7c79e720d3b0af3cb96f27d421f08eb7744.tar.gz
Imported minitest 4.7.4 (r8483)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40553 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/minitest/spec.rb')
-rw-r--r--lib/minitest/spec.rb275
1 files changed, 143 insertions, 132 deletions
diff --git a/lib/minitest/spec.rb b/lib/minitest/spec.rb
index 7588982038..c8584f6e4a 100644
--- a/lib/minitest/spec.rb
+++ b/lib/minitest/spec.rb
@@ -66,7 +66,7 @@ module Kernel # :nodoc:
def describe desc, additional_desc = nil, &block # :doc:
stack = MiniTest::Spec.describe_stack
name = [stack.last, desc, additional_desc].compact.join("::")
- sclas = stack.last || if Class === self && self < MiniTest::Spec then
+ sclas = stack.last || if Class === self && is_a?(MiniTest::Spec::DSL) then
self
else
MiniTest::Spec.spec_type desc
@@ -88,173 +88,184 @@ end
# For a list of expectations, see MiniTest::Expectations.
class MiniTest::Spec < MiniTest::Unit::TestCase
- ##
- # Contains pairs of matchers and Spec classes to be used to
- # calculate the superclass of a top-level describe. This allows for
- # automatically customizable spec types.
- #
- # See: register_spec_type and spec_type
-
- TYPES = [[//, MiniTest::Spec]]
-
- ##
- # Register a new type of spec that matches the spec's description.
- # This method can take either a Regexp and a spec class or a spec
- # class and a block that takes the description and returns true if
- # it matches.
- #
- # Eg:
- #
- # register_spec_type(/Controller$/, MiniTest::Spec::Rails)
- #
- # or:
- #
- # register_spec_type(MiniTest::Spec::RailsModel) do |desc|
- # desc.superclass == ActiveRecord::Base
- # end
-
- def self.register_spec_type(*args, &block)
- if block then
- matcher, klass = block, args.first
- else
- matcher, klass = *args
- end
- TYPES.unshift [matcher, klass]
- end
##
- # Figure out the spec class to use based on a spec's description. Eg:
- #
- # spec_type("BlahController") # => MiniTest::Spec::Rails
-
- def self.spec_type desc
- TYPES.find { |matcher, klass|
- if matcher.respond_to? :call then
- matcher.call desc
+ # Oh look! A MiniTest::Spec::DSL module! Eat your heart out DHH.
+
+ module DSL
+ ##
+ # Contains pairs of matchers and Spec classes to be used to
+ # calculate the superclass of a top-level describe. This allows for
+ # automatically customizable spec types.
+ #
+ # See: register_spec_type and spec_type
+
+ TYPES = [[//, MiniTest::Spec]]
+
+ ##
+ # Register a new type of spec that matches the spec's description.
+ # This method can take either a Regexp and a spec class or a spec
+ # class and a block that takes the description and returns true if
+ # it matches.
+ #
+ # Eg:
+ #
+ # register_spec_type(/Controller$/, MiniTest::Spec::Rails)
+ #
+ # or:
+ #
+ # register_spec_type(MiniTest::Spec::RailsModel) do |desc|
+ # desc.superclass == ActiveRecord::Base
+ # end
+
+ def register_spec_type(*args, &block)
+ if block then
+ matcher, klass = block, args.first
else
- matcher === desc.to_s
+ matcher, klass = *args
end
- }.last
- end
-
- @@describe_stack = []
- def self.describe_stack # :nodoc:
- @@describe_stack
- end
+ TYPES.unshift [matcher, klass]
+ end
- ##
- # Returns the children of this spec.
+ ##
+ # Figure out the spec class to use based on a spec's description. Eg:
+ #
+ # spec_type("BlahController") # => MiniTest::Spec::Rails
- def self.children
- @children ||= []
- end
+ def spec_type desc
+ TYPES.find { |matcher, klass|
+ if matcher.respond_to? :call then
+ matcher.call desc
+ else
+ matcher === desc.to_s
+ end
+ }.last
+ end
- def self.nuke_test_methods! # :nodoc:
- self.public_instance_methods.grep(/^test_/).each do |name|
- self.send :undef_method, name
+ @@describe_stack = []
+ def describe_stack # :nodoc:
+ @@describe_stack
end
- end
- ##
- # 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.
+ ##
+ # Returns the children of this spec.
- def self.before type = nil, &block
- define_method :setup do
- super()
- self.instance_eval(&block)
+ def children
+ @children ||= []
end
- end
- ##
- # 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 nuke_test_methods! # :nodoc:
+ self.public_instance_methods.grep(/^test_/).each do |name|
+ self.send :undef_method, name
+ end
+ end
- def self.after type = nil, &block
- define_method :teardown do
- self.instance_eval(&block)
- super()
+ ##
+ # 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 before type = nil, &block
+ define_method :setup do
+ super()
+ self.instance_eval(&block)
+ end
end
- end
- ##
- # 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.
- #
- # This is also aliased to #specify and doesn't require a +desc+ arg.
- #
- # Hint: If you _do_ want inheritence, use minitest/unit. You can mix
- # and match between assertions and expectations as much as you want.
+ ##
+ # 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 after type = nil, &block
+ define_method :teardown do
+ self.instance_eval(&block)
+ super()
+ end
+ end
+
+ ##
+ # 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 inheritance, so this goes way out of
+ # its way to make sure that expectations aren't inherited.
+ #
+ # This is also aliased to #specify and doesn't require a +desc+ arg.
+ #
+ # Hint: If you _do_ want inheritence, use minitest/unit. You can mix
+ # and match between assertions and expectations as much as you want.
+
+ def it desc = "anonymous", &block
+ block ||= proc { skip "(no tests defined)" }
- def self.it desc = "anonymous", &block
- block ||= proc { skip "(no tests defined)" }
+ @specs ||= 0
+ @specs += 1
- @specs ||= 0
- @specs += 1
+ name = "test_%04d_%s" % [ @specs, desc ]
- name = "test_%04d_%s" % [ @specs, desc ]
+ define_method name, &block
- define_method name, &block
+ self.children.each do |mod|
+ mod.send :undef_method, name if mod.public_method_defined? name
+ end
- self.children.each do |mod|
- mod.send :undef_method, name if mod.public_method_defined? name
+ name
end
- name
- end
+ ##
+ # Essentially, define an accessor for +name+ with +block+.
+ #
+ # Why use let instead of def? I honestly don't know.
- ##
- # Essentially, define an accessor for +name+ with +block+.
- #
- # Why use let instead of def? I honestly don't know.
+ def let name, &block
+ define_method name do
+ @_memoized ||= {}
+ @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) }
+ end
+ end
- def self.let name, &block
- define_method name do
- @_memoized ||= {}
- @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) }
+ ##
+ # Another lazy man's accessor generator. Made even more lazy by
+ # setting the name for you to +subject+.
+
+ def subject &block
+ let :subject, &block
end
- end
- ##
- # Another lazy man's accessor generator. Made even more lazy by
- # setting the name for you to +subject+.
+ def create name, desc # :nodoc:
+ cls = Class.new(self) do
+ @name = name
+ @desc = desc
- def self.subject &block
- let :subject, &block
- end
+ nuke_test_methods!
+ end
- def self.create name, desc # :nodoc:
- cls = Class.new(self) do
- @name = name
- @desc = desc
+ children << cls
- nuke_test_methods!
+ cls
end
- children << cls
-
- cls
- end
+ def name # :nodoc:
+ defined?(@name) ? @name : super
+ end
- def self.to_s # :nodoc:
- defined?(@name) ? @name : super
- end
+ def to_s # :nodoc:
+ name # Can't alias due to 1.8.7, not sure why
+ end
- # :stopdoc:
- class << self
+ # :stopdoc:
attr_reader :desc
alias :specify :it
- alias :name :to_s
+ # :startdoc:
end
- # :startdoc:
+
+ extend DSL
+
+ TYPES = DSL::TYPES # :nodoc:
end
##