]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/spec/autotest/rspec_spec.rb
64c020de9dc4aa4ecbeff7414b06b13b41ed9d0a
[rails.git] / vendor / gems / rspec-1.1.2 / spec / autotest / rspec_spec.rb
1 require File.dirname(__FILE__) + "/../autotest_helper"
2
3 class Autotest
4   
5   module AutotestHelper
6     def rspec_output
7       <<-HERE
8 .............PPF
9
10 1)
11 'false should be false' FAILED
12 expected: true,
13      got: false (using ==)
14 ./spec/autotest/rspec_spec.rb:203:
15
16 Finished in 0.158674 seconds
17
18 16 examples, 1 failure, 2 pending
19
20 Pending:
21 Autotest::Rspec handling failed results should return an array of failed examples and errors (TODO)
22 Autotest::Rspec tests/specs for a given file should find all the specs for a given file (TODO)
23 HERE
24     end
25     
26     
27     def common_setup
28       @proc = mock Proc
29       @kernel = mock Kernel
30       @kernel.stub!(:proc).and_return @proc
31
32       File.stub!(:exists).and_return true
33       @windows_alt_separator = "\\"
34       @posix_separator = '/'
35
36       @rspec_output = rspec_output
37     end
38   end
39
40   describe Rspec, "rspec_commands" do
41     it "should contain the various commands, ordered by preference" do
42       Rspec.new.spec_commands.should == [
43         File.expand_path("#{File.dirname(__FILE__)}/../../bin/spec"),
44         "#{Config::CONFIG['bindir']}/spec"
45       ]
46     end
47   end
48   
49   describe Rspec, "selection of rspec command" do
50     include AutotestHelper
51     
52     before :each do
53       common_setup
54       @rspec_autotest = Rspec.new
55     end
56     
57     it "should try to find the spec command if it exists in ./bin and use it above everything else" do
58       File.stub!(:exists?).and_return true
59
60       spec_path = File.expand_path("#{File.dirname(__FILE__)}/../../bin/spec")
61       File.should_receive(:exists?).with(spec_path).and_return true
62       @rspec_autotest.spec_command.should == spec_path
63     end
64
65     it "should otherwise select the default spec command in gem_dir/bin/spec" do
66       @rspec_autotest.stub!(:spec_commands).and_return ["/foo/spec"]
67       Config::CONFIG.stub!(:[]).and_return "/foo"
68       File.should_receive(:exists?).with("/foo/spec").and_return(true)
69
70       @rspec_autotest.spec_command.should == "/foo/spec"
71     end
72     
73     it "should raise an error if no spec command is found at all" do
74       File.stub!(:exists?).and_return false
75       
76       lambda {
77         @rspec_autotest.spec_command
78       }.should raise_error(RspecCommandError, "No spec command could be found!")
79     end
80     
81   end
82   
83   describe Rspec, "selection of rspec command (windows compatibility issues)" do
84     include AutotestHelper
85     
86     before :each do
87       common_setup
88     end
89     
90     it "should use the ALT_SEPARATOR if it is non-nil" do
91       pending("autotest got re-worked so this is failing for the moment")
92       @rspec_autotest = Rspec.new
93       spec_command = File.expand_path("#{File.dirname(__FILE__)}/../../bin/spec")
94       @rspec_autotest.stub!(:spec_commands).and_return [spec_command]
95       @rspec_autotest.spec_command.should == spec_command.gsub('/', '\\')
96     end
97     
98     it "should not use the ALT_SEPATOR if it is nil" do
99       @windows_alt_separator = nil
100       @rspec_autotest = Rspec.new
101       spec_command = File.expand_path("#{File.dirname(__FILE__)}/../../bin/spec")
102       @rspec_autotest.stub!(:spec_commands).and_return [spec_command]
103       @rspec_autotest.spec_command.should == spec_command
104     end
105   end
106
107   describe Rspec, "adding spec.opts --options" do 
108     before :each do
109       @rspec_autotest = Rspec.new
110     end
111
112     it "should return the command line option to add spec.opts if the options file exists" do
113       File.stub!(:exist?).and_return true
114       @rspec_autotest.add_options_if_present.should == "-O spec/spec.opts "
115     end
116
117     it "should return an empty string if no spec.opts exists" do
118       File.stub!(:exist?).and_return false
119       Rspec.new.add_options_if_present.should == ""
120     end
121   end  
122   
123   describe Rspec do
124     before :each do
125       @rspec_autotest = Rspec.new
126       @rspec_autotest.stub!(:ruby).and_return "ruby"
127       @rspec_autotest.stub!(:add_options_if_present).and_return "-O spec/spec.opts"
128       
129       @ruby = @rspec_autotest.ruby
130       @spec_command = @rspec_autotest.spec_command
131       @options = @rspec_autotest.add_options_if_present
132       @files_to_test = {
133         :spec => ["file_one", "file_two"]
134       }
135       # this is not the inner representation of Autotest!
136       @rspec_autotest.stub!(:files_to_test).and_return @files_to_test
137       @files_to_test.stub!(:keys).and_return @files_to_test[:spec]
138       @to_test = @files_to_test.keys.flatten.join ' '
139     end
140     
141     it "should make the apropriate test command" do
142       @rspec_autotest.make_test_cmd(@files_to_test).should == "#{@ruby} -S #{@spec_command} #{@options} #{@to_test}"
143     end
144   end
145   
146   describe Rspec, "test mappings" do
147     before :each do
148       @rspec_autotest = Rspec.new
149       @rspec_autotest.hook :initialize
150     end
151       
152     it "should map all filenames in spec/ which end in .rb" do
153       @rspec_autotest.instance_eval{@test_mappings}.should have_key(%r%^spec/.*\.rb$%)
154     end
155     
156     it "should map all names in lib which end in .rb to the corresponding ones in spec/" do
157       @rspec_autotest.instance_eval{@test_mappings}.should have_key(%r%^lib/(.*)\.rb$%)
158     end
159     
160     it "should find all files in spec/shares/* and the spec helper in spec/spec_helper"  do
161       @rspec_autotest.instance_eval{@test_mappings}.should have_key(%r%^spec/(spec_helper|shared/.*)\.rb$%)
162     end
163   end
164   
165   describe Rspec, "handling results" do
166     include AutotestHelper
167     
168     before :each do
169       common_setup
170       @rspec_autotest = Rspec.new
171       @rspec_autotest.stub!(:hook)
172       
173       @results = mock String
174       @results.stub!(:scan).and_return ""
175     end
176     
177     it "should call hook(:red) if there are failures" do
178       @rspec_autotest.stub!(:consolidate_failures).and_return ["spec/some_spec.rb"]
179       
180       @rspec_autotest.should_receive(:hook).with(:red)
181       @rspec_autotest.handle_results(@results)
182     end
183     
184     it "should call hook(:green) if there are no failures" do
185       @rspec_autotest.stub!(:consolidate_failures).and_return []
186       @rspec_autotest.should_receive(:hook).with(:green)
187       @rspec_autotest.handle_results(@results)
188     end
189   end
190   
191   describe Rspec, "handling failed results" do
192     include AutotestHelper
193     
194     before :each do
195       common_setup
196     end
197     
198     it %(should scan the output into a multi-dimensional array, 
199         consisting of the failing spec's name as the first element, 
200         and the failure as the second) do
201       @rspec_autotest = Rspec.new
202       @rspec_autotest.failed_results(@rspec_output).should == [
203         [
204           "false should be false", 
205           "expected: true,\n     got: false (using ==)\n./spec/autotest/rspec_spec.rb:203:"
206         ]
207       ]
208     end
209   end
210   
211   describe Rspec, "specs for a given file" do
212     before :each do
213       @lib_file = "lib/something.rb"
214       @spec_file = "spec/something_spec.rb"
215       @rspec_autotest = Rspec.new
216       @rspec_autotest.hook :initialize
217
218       @rspec_autotest.instance_variable_set("@files", {@lib_file => Time.now, @spec_file => Time.now})
219       @rspec_autotest.stub!(:find_files_to_test).and_return true
220     end
221     
222     it "should find the spec file for a given lib file" do
223       @rspec_autotest.specs_for_file(@lib_file).should == [@spec_file]
224     end
225     
226     it "should find the spec file if given a spec file" do
227       @rspec_autotest.specs_for_file(@spec_file).should == [@spec_file]
228     end
229     
230     it "should only find the file if the file is being tracked (in @file)"  do
231       @other_file = "lib/some_non_tracked_file"
232       @rspec_autotest.specs_for_file(@other_file).should == []
233     end
234   end
235   
236   describe Rspec, "consolidating failures" do
237     include AutotestHelper
238     
239     before :each do
240       common_setup
241       @rspec_autotest = Rspec.new
242       
243       @spec_file = "./spec/autotest/rspec_spec.rb"
244       @rspec_autotest.instance_variable_set("@files", {@spec_file => Time.now})
245       @rspec_autotest.stub!(:find_files_to_test).and_return true
246     end
247     
248     it "should return no failures if no failures were given in the output" do
249       @rspec_autotest.stub!(:failed_results).and_return [[]]
250       @rspec_autotest.consolidate_failures(@rspec_autotest.failed_results).should == {}
251     end
252     
253     it "should return a hash with the spec filename => spec name for each failure or error" do
254       @rspec_autotest.stub!(:failed_results).and_return([
255         [
256           "false should be false", 
257           "expected: true,\n     got: false (using ==)\n./spec/autotest/rspec_spec.rb:203:"
258         ]
259       ])
260       @rspec_autotest.consolidate_failures(@rspec_autotest.failed_results).should == {@spec_file => ["false should be false"]}
261     end
262     
263   end
264 end