File: require_relative_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (309 lines) | stat: -rw-r--r-- 12,186 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../../../fixtures/code_loading', __FILE__)

ruby_version_is "1.9" do
  describe "Kernel#require_relative with a relative path" do
    it "needs to be reviewed for spec completeness"

    before :each do
      CodeLoadingSpecs.spec_setup
      @dir = "../../fixtures/code"
      @abs_dir = File.expand_path(@dir, File.dirname(__FILE__))
      @path = "#{@dir}/load_fixture.rb"
      @abs_path = File.expand_path(@path, File.dirname(__FILE__))
    end

    after :each do
      CodeLoadingSpecs.spec_cleanup
    end

    it "loads a path relative to the current file" do
      require_relative(@path).should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    it "loads a file defining many methods" do
      require_relative("#{@dir}/methods_fixture.rb").should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    it "raises a LoadError if the file does not exist" do
      lambda { require_relative("#{@dir}/nonexistent.rb") }.should raise_error(LoadError)
      ScratchPad.recorded.should == []
    end

    it "raises a LoadError if basepath does not exist" do
      lambda { eval("require_relative('#{@dir}/nonexistent.rb')") }.should raise_error(LoadError)
    end

    it "calls #to_str on non-String objects" do
      name = mock("load_fixture.rb mock")
      name.should_receive(:to_str).and_return(@path)
      require_relative(name).should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    it "raises a TypeError if argument does not respond to #to_str" do
      lambda { require_relative(nil) }.should raise_error(TypeError)
      lambda { require_relative(42) }.should raise_error(TypeError)
      lambda {
        require_relative([@path,@path])
      }.should raise_error(TypeError)
    end

    it "raises a TypeError if passed an object that has #to_s but not #to_str" do
      name = mock("load_fixture.rb mock")
      name.stub!(:to_s).and_return(@path)
      lambda { require_relative(name) }.should raise_error(TypeError)
    end

    it "raises a TypeError if #to_str does not return a String" do
      name = mock("#to_str returns nil")
      name.should_receive(:to_str).at_least(1).times.and_return(nil)
      lambda { require_relative(name) }.should raise_error(TypeError)
    end

    it "calls #to_path on non-String objects" do
      name = mock("load_fixture.rb mock")
      name.should_receive(:to_path).and_return(@path)
      require_relative(name).should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    it "calls #to_str on non-String objects returned by #to_path" do
      name = mock("load_fixture.rb mock")
      to_path = mock("load_fixture_rb #to_path mock")
      name.should_receive(:to_path).and_return(to_path)
      to_path.should_receive(:to_str).and_return(@path)
      require_relative(name).should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    describe "(file extensions)" do
      it "loads a .rb extensioned file when passed a non-extensioned path" do
        require_relative("#{@dir}/load_fixture").should be_true
        ScratchPad.recorded.should == [:loaded]
      end

      it "loads a .rb extensioned file when a C-extension file of the same name is loaded" do
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.bundle"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.dylib"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.so"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.dll"
        require_relative(@path).should be_true
        ScratchPad.recorded.should == [:loaded]
      end

      it "does not load a C-extension file if a .rb extensioned file is already loaded" do
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.rb"
        require_relative("#{@dir}/load_fixture").should be_false
        ScratchPad.recorded.should == []
      end

      it "loads a .rb extensioned file when passed a non-.rb extensioned path" do
        require_relative("#{@dir}/load_fixture.ext").should be_true
        ScratchPad.recorded.should == [:loaded]
        $LOADED_FEATURES.should include "#{@abs_dir}/load_fixture.ext.rb"
      end

      it "loads a .rb extensioned file when a complex-extensioned C-extension file of the same name is loaded" do
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.bundle"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.dylib"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.so"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.dll"
        require_relative("#{@dir}/load_fixture.ext").should be_true
        ScratchPad.recorded.should == [:loaded]
        $LOADED_FEATURES.should include "#{@abs_dir}/load_fixture.ext.rb"
      end

      it "does not load a C-extension file if a complex-extensioned .rb file is already loaded" do
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.rb"
        require_relative("#{@dir}/load_fixture.ext").should be_false
        ScratchPad.recorded.should == []
      end
    end

    describe "($LOAD_FEATURES)" do
      it "stores an absolute path" do
        require_relative(@path).should be_true
        $LOADED_FEATURES.should == [@abs_path]
      end

      it "does not store the path if the load fails" do
        lambda { require_relative("#{@dir}/raise_fixture.rb") }.should raise_error(RuntimeError)
        $LOADED_FEATURES.should == []
      end

      it "does not load an absolute path that is already stored" do
        $LOADED_FEATURES << @abs_path
        require_relative(@path).should be_false
        ScratchPad.recorded.should == []
      end

      it "adds the suffix of the resolved filename" do
        require_relative("#{@dir}/load_fixture").should be_true
        $LOADED_FEATURES.should == ["#{@abs_dir}/load_fixture.rb"]
      end

      it "loads a path for a file already loaded with a relative path" do
        $LOAD_PATH << s = File.expand_path(@dir)
        $LOADED_FEATURES << "load_fixture.rb" << "load_fixture"
        require_relative(@path).should be_true
        $LOADED_FEATURES.should include(@abs_path)
        ScratchPad.recorded.should == [:loaded]
      end
    end
  end
  describe "Kernel#require_relative with an absolute path" do
    it "needs to be reviewed for spec completeness"

    before :each do
      CodeLoadingSpecs.spec_setup
      @dir = File.expand_path "../../fixtures/code", File.dirname(__FILE__)
      @abs_dir = @dir
      @path = File.join @dir, "load_fixture.rb"
      @abs_path = @path
    end

    after :each do
      CodeLoadingSpecs.spec_cleanup
    end

    it "loads a path relative to the current file" do
      require_relative(@path).should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    it "loads a file defining many methods" do
      require_relative("#{@dir}/methods_fixture.rb").should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    it "raises a LoadError if the file does not exist" do
      lambda { require_relative("#{@dir}/nonexistent.rb") }.should raise_error(LoadError)
      ScratchPad.recorded.should == []
    end

    it "raises a LoadError if basepath does not exist" do
      lambda { eval("require_relative('#{@dir}/nonexistent.rb')") }.should raise_error(LoadError)
    end

    it "calls #to_str on non-String objects" do
      name = mock("load_fixture.rb mock")
      name.should_receive(:to_str).and_return(@path)
      require_relative(name).should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    it "raises a TypeError if argument does not respond to #to_str" do
      lambda { require_relative(nil) }.should raise_error(TypeError)
      lambda { require_relative(42) }.should raise_error(TypeError)
      lambda {
        require_relative([@path,@path])
      }.should raise_error(TypeError)
    end

    it "raises a TypeError if passed an object that has #to_s but not #to_str" do
      name = mock("load_fixture.rb mock")
      name.stub!(:to_s).and_return(@path)
      lambda { require_relative(name) }.should raise_error(TypeError)
    end

    it "raises a TypeError if #to_str does not return a String" do
      name = mock("#to_str returns nil")
      name.should_receive(:to_str).at_least(1).times.and_return(nil)
      lambda { require_relative(name) }.should raise_error(TypeError)
    end

    it "calls #to_path on non-String objects" do
      name = mock("load_fixture.rb mock")
      name.should_receive(:to_path).and_return(@path)
      require_relative(name).should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    it "calls #to_str on non-String objects returned by #to_path" do
      name = mock("load_fixture.rb mock")
      to_path = mock("load_fixture_rb #to_path mock")
      name.should_receive(:to_path).and_return(to_path)
      to_path.should_receive(:to_str).and_return(@path)
      require_relative(name).should be_true
      ScratchPad.recorded.should == [:loaded]
    end

    describe "(file extensions)" do
      it "loads a .rb extensioned file when passed a non-extensioned path" do
        require_relative("#{@dir}/load_fixture").should be_true
        ScratchPad.recorded.should == [:loaded]
      end

      it "loads a .rb extensioned file when a C-extension file of the same name is loaded" do
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.bundle"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.dylib"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.so"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.dll"
        require_relative(@path).should be_true
        ScratchPad.recorded.should == [:loaded]
      end

      it "does not load a C-extension file if a .rb extensioned file is already loaded" do
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.rb"
        require_relative("#{@dir}/load_fixture").should be_false
        ScratchPad.recorded.should == []
      end

      it "loads a .rb extensioned file when passed a non-.rb extensioned path" do
        require_relative("#{@dir}/load_fixture.ext").should be_true
        ScratchPad.recorded.should == [:loaded]
        $LOADED_FEATURES.should include "#{@abs_dir}/load_fixture.ext.rb"
      end

      it "loads a .rb extensioned file when a complex-extensioned C-extension file of the same name is loaded" do
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.bundle"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.dylib"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.so"
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.dll"
        require_relative("#{@dir}/load_fixture.ext").should be_true
        ScratchPad.recorded.should == [:loaded]
        $LOADED_FEATURES.should include "#{@abs_dir}/load_fixture.ext.rb"
      end

      it "does not load a C-extension file if a complex-extensioned .rb file is already loaded" do
        $LOADED_FEATURES << "#{@abs_dir}/load_fixture.ext.rb"
        require_relative("#{@dir}/load_fixture.ext").should be_false
        ScratchPad.recorded.should == []
      end
    end

    describe "($LOAD_FEATURES)" do
      it "stores an absolute path" do
        require_relative(@path).should be_true
        $LOADED_FEATURES.should == [@abs_path]
      end

      it "does not store the path if the load fails" do
        lambda { require_relative("#{@dir}/raise_fixture.rb") }.should raise_error(RuntimeError)
        $LOADED_FEATURES.should == []
      end

      it "does not load an absolute path that is already stored" do
        $LOADED_FEATURES << @abs_path
        require_relative(@path).should be_false
        ScratchPad.recorded.should == []
      end

      it "adds the suffix of the resolved filename" do
        require_relative("#{@dir}/load_fixture").should be_true
        $LOADED_FEATURES.should == ["#{@abs_dir}/load_fixture.rb"]
      end

      it "loads a path for a file already loaded with a relative path" do
        $LOAD_PATH << s = File.expand_path(@dir)
        $LOADED_FEATURES << "load_fixture.rb" << "load_fixture"
        require_relative(@path).should be_true
        $LOADED_FEATURES.should include(@abs_path)
        ScratchPad.recorded.should == [:loaded]
      end
    end
  end
end