]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/extensions/main.rb
Show whether a trace is public or private in the trace list, so that a user can easil...
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / extensions / main.rb
1 module Spec
2   module Extensions
3     module Main
4       # Creates and returns a class that includes the ExampleGroupMethods
5       # module. Which ExampleGroup type is created depends on the directory of the file
6       # calling this method. For example, Spec::Rails will use different
7       # classes for specs living in <tt>spec/models</tt>,
8       # <tt>spec/helpers</tt>, <tt>spec/views</tt> and
9       # <tt>spec/controllers</tt>.
10       #
11       # It is also possible to override autodiscovery of the example group
12       # type with an options Hash as the last argument:
13       #
14       #   describe "name", :type => :something_special do ...
15       #
16       # The reason for using different behaviour classes is to have different
17       # matcher methods available from within the <tt>describe</tt> block.
18       #
19       # See Spec::Example::ExampleFactory#register for details about how to
20       # register special implementations.
21       #
22       def describe(*args, &block)
23         raise ArgumentError if args.empty?
24         raise ArgumentError unless block
25         args << {} unless Hash === args.last
26         args.last[:spec_path] = caller(0)[1]
27         Spec::Example::ExampleGroupFactory.create_example_group(*args, &block)
28       end
29       alias :context :describe
30       
31       # Creates an example group that can be shared by other example groups
32       #
33       # == Examples
34       #
35       #  share_examples_for "All Editions" do
36       #    it "all editions behaviour" ...
37       #  end
38       #
39       #  describe SmallEdition do
40       #    it_should_behave_like "All Editions"
41       #  
42       #    it "should do small edition stuff" do
43       #      ...
44       #    end
45       #  end
46       def share_examples_for(name, &block)
47         describe(name, :shared => true, &block)
48       end
49       
50       alias :shared_examples_for :share_examples_for
51       
52       # Creates a Shared Example Group and assigns it to a constant
53       #
54       #  share_as :AllEditions do
55       #    it "should do all editions stuff" ...
56       #  end
57       #
58       #  describe SmallEdition do
59       #    it_should_behave_like AllEditions
60       #  
61       #    it "should do small edition stuff" do
62       #      ...
63       #    end
64       #  end
65       #
66       # And, for those of you who prefer to use something more like Ruby, you
67       # can just include the module directly
68       #
69       #  describe SmallEdition do
70       #    include AllEditions
71       #  
72       #    it "should do small edition stuff" do
73       #      ...
74       #    end
75       #  end
76       def share_as(name, &block)
77         begin
78           Object.const_set(name, share_examples_for(name, &block))
79         rescue NameError => e
80           raise NameError.new(e.message + "\nThe first argument to share_as must be a legal name for a constant\n")
81         end
82       end
83
84     private
85     
86       def rspec_options
87         $rspec_options ||= begin; \
88           parser = ::Spec::Runner::OptionParser.new(STDERR, STDOUT); \
89           parser.order!(ARGV); \
90           $rspec_options = parser.options; \
91         end
92         $rspec_options
93       end
94       
95       def init_rspec_options(options)
96         $rspec_options = options if $rspec_options.nil?
97       end
98     end
99   end
100 end
101
102 include Spec::Extensions::Main