diff options
Diffstat (limited to 'spec/ruby/core/kernel/autoload_spec.rb')
| -rw-r--r-- | spec/ruby/core/kernel/autoload_spec.rb | 85 |
1 files changed, 71 insertions, 14 deletions
diff --git a/spec/ruby/core/kernel/autoload_spec.rb b/spec/ruby/core/kernel/autoload_spec.rb index b2aab5a895..46783734c4 100644 --- a/spec/ruby/core/kernel/autoload_spec.rb +++ b/spec/ruby/core/kernel/autoload_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' # These specs only illustrate the basic autoload cases # and where toplevel autoload behaves differently from @@ -7,7 +7,9 @@ require File.expand_path('../fixtures/classes', __FILE__) autoload :KSAutoloadA, "autoload_a.rb" autoload :KSAutoloadB, fixture(__FILE__, "autoload_b.rb") -autoload :KSAutoloadC, fixture(__FILE__, "autoload_c.rb") +define_autoload_KSAutoloadCallsRequire = -> { + autoload :KSAutoloadCallsRequire, "main_autoload_not_exist.rb" +} def check_autoload(const) autoload? const @@ -23,7 +25,7 @@ describe "Kernel#autoload" do end it "is a private method" do - Kernel.should have_private_instance_method(:autoload) + Kernel.private_instance_methods(false).should.include?(:autoload) end it "registers a file to load the first time the named constant is accessed" do @@ -35,17 +37,19 @@ describe "Kernel#autoload" do end it "sets the autoload constant in Object's constant table" do - Object.should have_constant(:KSAutoloadA) + Object.should.const_defined?(:KSAutoloadA, false) end it "loads the file when the constant is accessed" do KSAutoloadB.loaded.should == :ksautoload_b end - it "does not call Kernel.require or Kernel.load to load the file" do - Kernel.should_not_receive(:require) - Kernel.should_not_receive(:load) - KSAutoloadC.loaded.should == :ksautoload_c + it "calls main.require(path) to load the file" do + define_autoload_KSAutoloadCallsRequire.call + main = TOPLEVEL_BINDING.eval("self") + main.should_receive(:require).with("main_autoload_not_exist.rb") + # The constant won't be defined since require is mocked to do nothing + -> { KSAutoloadCallsRequire }.should.raise(NameError) end it "can autoload in instance_eval" do @@ -55,16 +59,50 @@ describe "Kernel#autoload" do end end + describe "inside a Class.new method body" do + # NOTE: this spec is being discussed in https://github.com/ruby/spec/pull/839 + it "should define on the new anonymous class" do + cls = Class.new do + def go + autoload :Object, 'bogus' + autoload? :Object + end + end + + cls.new.go.should == 'bogus' + cls.autoload?(:Object).should == 'bogus' + end + end + describe "when Object is frozen" do it "raises a FrozenError before defining the constant" do - ruby_exe(fixture(__FILE__, "autoload_frozen.rb")).should == "#{frozen_error_class} - nil" + ruby_exe(fixture(__FILE__, "autoload_frozen.rb")).should == "FrozenError - nil" + end + end + + describe "when called from included module's method" do + before :all do + @path = fixture(__FILE__, "autoload_from_included_module.rb") + KernelSpecs::AutoloadMethodIncluder.new.setup_autoload(@path) + end + + it "setups the autoload on the included module" do + KernelSpecs::AutoloadMethod.autoload?(:AutoloadFromIncludedModule).should == @path + end + + it "the autoload is reachable from the class too" do + KernelSpecs::AutoloadMethodIncluder.autoload?(:AutoloadFromIncludedModule).should == @path + end + + it "the autoload relative to the included module works" do + KernelSpecs::AutoloadMethod::AutoloadFromIncludedModule.loaded.should == :autoload_from_included_module end end end describe "Kernel#autoload?" do it "is a private method" do - Kernel.should have_private_instance_method(:autoload?) + Kernel.private_instance_methods(false).should.include?(:autoload?) end it "returns the name of the file that will be autoloaded" do @@ -72,7 +110,7 @@ describe "Kernel#autoload?" do end it "returns nil if no file has been registered for a constant" do - check_autoload(:Manualload).should be_nil + check_autoload(:Manualload).should == nil end end @@ -99,7 +137,7 @@ describe "Kernel.autoload" do end it "sets the autoload constant in Object's constant table" do - Object.should have_constant(:KSAutoloadBB) + Object.should.const_defined?(:KSAutoloadBB, false) end it "calls #to_path on non-String filenames" do @@ -107,6 +145,25 @@ describe "Kernel.autoload" do p.should_receive(:to_path).and_return @non_existent Kernel.autoload :KSAutoloadAA, p end + + describe "when called from included module's method" do + before :all do + @path = fixture(__FILE__, "autoload_from_included_module2.rb") + KernelSpecs::AutoloadMethodIncluder2.new.setup_autoload(@path) + end + + it "setups the autoload on the included module" do + KernelSpecs::AutoloadMethod2.autoload?(:AutoloadFromIncludedModule2).should == @path + end + + it "the autoload is reachable from the class too" do + KernelSpecs::AutoloadMethodIncluder2.autoload?(:AutoloadFromIncludedModule2).should == @path + end + + it "the autoload relative to the included module works" do + KernelSpecs::AutoloadMethod2::AutoloadFromIncludedModule2.loaded.should == :autoload_from_included_module2 + end + end end describe "Kernel.autoload?" do @@ -116,6 +173,6 @@ describe "Kernel.autoload?" do end it "returns nil if no file has been registered for a constant" do - Kernel.autoload?(:Manualload).should be_nil + Kernel.autoload?(:Manualload).should == nil end end |
