Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / lib / spec / runner / extensions / kernel.rb
blob75f2c335ec371a857a79fccfd588127bf4147e6b
1 module Kernel
2   # Creates and registers an instance of a Spec::DSL::Behaviour (or a subclass).
3   # The instantiated behaviour class depends on the directory of the file
4   # calling this method. For example, Spec::Rails will use different
5   # classes for specs living in <tt>spec/models</tt>, <tt>spec/helpers</tt>, 
6   # <tt>spec/views</tt> and <tt>spec/controllers</tt>.
7   #
8   # It is also possible to override autodiscovery of the behaviour class 
9   # with an options Hash as the last argument:
10   #
11   #   describe "name", :behaviour_type => :something_special do ...
12   #
13   # The reason for using different behaviour classes is to have
14   # different matcher methods available from within the <tt>describe</tt>
15   # block.
16   #
17   # See Spec::DSL::BehaviourFactory#add_behaviour_class for details about 
18   # how to register special Spec::DSL::Behaviour implementations.
19   #
20   def describe(*args, &block)
21     raise ArgumentError if args.empty?
22     args << {} unless Hash === args.last
23     args.last[:spec_path] = caller(0)[1]
24     register_behaviour(Spec::DSL::BehaviourFactory.create(*args, &block))
25   end
26   alias :context :describe
27   
28   def respond_to(*names)
29     Spec::Matchers::RespondTo.new(*names)
30   end
31   
32 private
34   def register_behaviour(behaviour)
35     if behaviour.shared?
36       Spec::DSL::Behaviour.add_shared_behaviour(behaviour)
37     else
38       behaviour_runner.add_behaviour(behaviour)
39     end
40   end
42   def behaviour_runner
43     # TODO: Figure out a better way to get this considered "covered" and keep this statement on multiple lines 
44     unless $behaviour_runner; \
45       $behaviour_runner = ::Spec::Runner::OptionParser.new.create_behaviour_runner(ARGV.dup, STDERR, STDOUT, false); \
46       at_exit { $behaviour_runner.run(nil, false) }; \
47     end
48     $behaviour_runner
49   end
50 end