public
Fork of dchelimsky/rspec
Description: Behaviour Driven Development framework for Ruby
Homepage: http://rspec.info
Clone URL: git://github.com/jpshackelford/rspec.git
rspec / spec / autotest / rspec_spec.rb
100644 143 lines (116 sloc) 4.464 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require File.dirname(__FILE__) + "/../autotest_helper"
 
class Autotest
  
  module AutotestHelper
    def rspec_output
      <<-HERE
.............PPF
 
1)
'false should be false' FAILED
expected: true,
got: false (using ==)
./spec/autotest/rspec_spec.rb:203:
 
Finished in 0.158674 seconds
 
16 examples, 1 failure, 2 pending
 
Pending:
Autotest::Rspec handling failed results should return an array of failed examples and errors (TODO)
Autotest::Rspec tests/specs for a given file should find all the specs for a given file (TODO)
HERE
    end
    
    def common_setup
      @proc = mock Proc
      @kernel = mock Kernel
      @kernel.stub!(:proc).and_return @proc
 
      File.stub!(:exists).and_return true
      @windows_alt_separator = "\\"
      @posix_separator = '/'
 
      @rspec_output = rspec_output
    end
  end
 
  describe Rspec do
    describe "adding spec.opts --options" do
      before(:each) do
        @rspec_autotest = Rspec.new
      end
 
      it "should return the command line option to add spec.opts if the options file exists" do
        File.stub!(:exist?).and_return true
        @rspec_autotest.add_options_if_present.should == "-O spec/spec.opts "
      end
 
      it "should return an empty string if no spec.opts exists" do
        File.stub!(:exist?).and_return false
        Rspec.new.add_options_if_present.should == ""
      end
    end
  
    describe "commands" do
      before(:each) do
        @rspec_autotest = Rspec.new
        @rspec_autotest.stub!(:ruby).and_return "ruby"
        @rspec_autotest.stub!(:add_options_if_present).and_return "-O spec/spec.opts"
      
        @ruby = @rspec_autotest.ruby
        @options = @rspec_autotest.add_options_if_present
        @files_to_test = {
          :spec => ["file_one", "file_two"]
        }
        # this is not the inner representation of Autotest!
        @rspec_autotest.stub!(:files_to_test).and_return @files_to_test
        @files_to_test.stub!(:keys).and_return @files_to_test[:spec]
        @to_test = @files_to_test.keys.flatten.join ' '
      end
    
      it "should make the apropriate test command" do
        @rspec_autotest.make_test_cmd(@files_to_test).should == "#{@ruby} -S #{@to_test} #{@options}"
      end
    end
  
    describe "mappings" do
    
      before(:each) do
        @lib_file = "lib/something.rb"
        @spec_file = "spec/something_spec.rb"
        @rspec_autotest = Rspec.new
        @rspec_autotest.hook :initialize
      end
    
      it "should find the spec file for a given lib file" do
        @rspec_autotest.should map_specs([@spec_file]).to(@lib_file)
      end
    
      it "should find the spec file if given a spec file" do
        @rspec_autotest.should map_specs([@spec_file]).to(@spec_file)
      end
    
      it "should only find the file if the file is being tracked (in @file)" do
        @rspec_autotest.should map_specs([]).to("lib/untracked_file")
      end
    end
  
    describe "consolidating failures" do
      include AutotestHelper
    
      before(:each) do
        common_setup
        @rspec_autotest = Rspec.new
      
        @spec_file = "spec/autotest/some_spec.rb"
        @rspec_autotest.instance_variable_set("@files", {@spec_file => Time.now})
        @rspec_autotest.stub!(:find_files_to_test).and_return true
      end
    
      it "should return no failures if no failures were given in the output" do
        @rspec_autotest.consolidate_failures([[]]).should == {}
      end
    
      it "should return a hash with the spec filename => spec name for each failure or error" do
        @rspec_autotest.stub!(:test_files_for).and_return "spec/autotest/some_spec.rb"
        failures = [
          [
            "false should be false",
            "expected: true,\n got: false (using ==)\n#{@spec_file}:203:"
          ]
        ]
        @rspec_autotest.consolidate_failures(failures).should == {
          @spec_file => ["false should be false"]
        }
      end
    
      it "should not include the subject file" do
        subject_file = "lib/autotest/some.rb"
        @rspec_autotest.stub!(:test_files_for).and_return "spec/autotest/some_spec.rb"
        failures = [
          [
            "false should be false",
            "expected: true,\n got: false (using ==)\n#{subject_file}:143:\n#{@spec_file}:203:"
          ]
        ]
        @rspec_autotest.consolidate_failures(failures).keys.should_not include(subject_file)
      end
    end
  end
end