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
require '../src/absrequire'

describe 'absrequire' do
  before :all do
    @original_cwd = File.expand_path(File.dirname(__FILE__))
  end
  
  before do
    @cwd = File.join(@original_cwd, 'fixtures')
    Dir.chdir @cwd
    @old_load_paths = $:.dup
    $LOADED_SCRIPTS.replace({})
  end
  
  after do
    $:.replace(@old_load_paths)
  end
  
  it 'should add an unloaded file to $LOADED_SCRIPTS' do
    absrequire 'bar'
    $LOADED_SCRIPTS[File.join(@cwd, 'bar.rb')].should == 'bar'
  end
  
  it "should load an unloaded file" do
    should_receive(:load).with(File.join(@cwd, 'bar.rb'))
    absrequire 'bar'
  end
  
  it "should ignore subsequent calls to absrequire with the same argument" do
    should_receive(:load).with(File.join(@cwd, 'bar.rb')).once
    absrequire 'bar'
    absrequire 'bar'
  end
  
  it "should not load the same file more than once given different require paths" do
    should_receive(:load).with(File.join(@cwd, 'bar.rb')).once
    absrequire 'bar'
    absrequire 'lib/../bar'
  end
  
  it "should not allow files to shadow each other" do
    $:.replace(['lib', '.'])
    
    should_receive(:load).with(File.join(@cwd, 'lib', 'bar.rb'))
    should_receive(:load).with(File.join(@cwd, 'bar.rb'))
    
    absrequire 'lib/bar'
    absrequire 'bar'
  end
  
  it "should respect the load paths array when considering load order" do
    should_receive(:load).with(File.join(@cwd, 'lib', 'bar.rb'))
    $:.replace(['lib', '.'])
    absrequire 'bar'
  end
  
  it "should allow loading files that don't end in '.rb'" do
    should_receive(:load).with(File.join(@cwd, 'foo'))
    absrequire 'foo'
  end
  
  it "should allow loading with an explicit extension" do
    should_receive(:load).with(File.join(@cwd, 'lib', 'bar.rb'))
    absrequire 'lib/bar.rb'
  end
  
  it "should not prevent loading different files that were mapped from the same argument to absrequire" do
    should_receive(:load).with(File.join(@cwd, 'bar.rb'))
    should_receive(:load).with(File.join(@cwd, 'lib', 'bar.rb'))
    
    absrequire 'bar'
    Dir.chdir 'lib'
    absrequire 'bar'
  end
end