diff options
Diffstat (limited to 'spec/ruby/core/module')
-rw-r--r-- | spec/ruby/core/module/autoload_spec.rb | 21 | ||||
-rw-r--r-- | spec/ruby/core/module/fixtures/autoload_self_during_require.rb | 5 | ||||
-rw-r--r-- | spec/ruby/core/module/fixtures/classes.rb | 3 | ||||
-rw-r--r-- | spec/ruby/core/module/fixtures/multi/foo.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/module/fixtures/multi/foo/bar_baz.rb | 11 | ||||
-rw-r--r-- | spec/ruby/core/module/shared/set_visibility.rb | 17 | ||||
-rw-r--r-- | spec/ruby/core/module/to_s_spec.rb | 31 | ||||
-rw-r--r-- | spec/ruby/core/module/undef_method_spec.rb | 33 |
8 files changed, 123 insertions, 4 deletions
diff --git a/spec/ruby/core/module/autoload_spec.rb b/spec/ruby/core/module/autoload_spec.rb index df434e8e60..db95704cc7 100644 --- a/spec/ruby/core/module/autoload_spec.rb +++ b/spec/ruby/core/module/autoload_spec.rb @@ -652,6 +652,27 @@ describe "Module#autoload" do ModuleSpecs::Autoload::AutoloadDuringRequire.should be_kind_of(Class) end + it "does not call #require a second time and does not warn if feature sets and trigger autoload on itself" do + main = TOPLEVEL_BINDING.eval("self") + main.should_not_receive(:require) + + -> { + Kernel.require fixture(__FILE__, "autoload_self_during_require.rb") + }.should_not complain(verbose: true) + ModuleSpecs::Autoload::AutoloadSelfDuringRequire.should be_kind_of(Class) + end + + it "handles multiple autoloads in the same file" do + $LOAD_PATH.unshift(File.expand_path('../fixtures/multi', __FILE__)) + begin + require 'foo/bar_baz' + ModuleSpecs::Autoload::Foo::Bar.should be_kind_of(Class) + ModuleSpecs::Autoload::Foo::Baz.should be_kind_of(Class) + ensure + $LOAD_PATH.shift + end + end + it "calls #to_path on non-string filenames" do p = mock('path') p.should_receive(:to_path).and_return @non_existent diff --git a/spec/ruby/core/module/fixtures/autoload_self_during_require.rb b/spec/ruby/core/module/fixtures/autoload_self_during_require.rb new file mode 100644 index 0000000000..f4a514a807 --- /dev/null +++ b/spec/ruby/core/module/fixtures/autoload_self_during_require.rb @@ -0,0 +1,5 @@ +module ModuleSpecs::Autoload + autoload :AutoloadSelfDuringRequire, __FILE__ + class AutoloadSelfDuringRequire + end +end diff --git a/spec/ruby/core/module/fixtures/classes.rb b/spec/ruby/core/module/fixtures/classes.rb index ef70eaf9cf..40777cdbbd 100644 --- a/spec/ruby/core/module/fixtures/classes.rb +++ b/spec/ruby/core/module/fixtures/classes.rb @@ -6,6 +6,9 @@ module ModuleSpecs CONST = :plain_constant + class NamedClass + end + module PrivConstModule PRIVATE_CONSTANT = 1 private_constant :PRIVATE_CONSTANT diff --git a/spec/ruby/core/module/fixtures/multi/foo.rb b/spec/ruby/core/module/fixtures/multi/foo.rb new file mode 100644 index 0000000000..549996f08f --- /dev/null +++ b/spec/ruby/core/module/fixtures/multi/foo.rb @@ -0,0 +1,6 @@ +module ModuleSpecs::Autoload + module Foo + autoload :Bar, 'foo/bar_baz' + autoload :Baz, 'foo/bar_baz' + end +end diff --git a/spec/ruby/core/module/fixtures/multi/foo/bar_baz.rb b/spec/ruby/core/module/fixtures/multi/foo/bar_baz.rb new file mode 100644 index 0000000000..53d3849e1f --- /dev/null +++ b/spec/ruby/core/module/fixtures/multi/foo/bar_baz.rb @@ -0,0 +1,11 @@ +require 'foo' + +module ModuleSpecs::Autoload + module Foo + class Bar + end + + class Baz + end + end +end diff --git a/spec/ruby/core/module/shared/set_visibility.rb b/spec/ruby/core/module/shared/set_visibility.rb index c39d59e05d..a04b1a54a0 100644 --- a/spec/ruby/core/module/shared/set_visibility.rb +++ b/spec/ruby/core/module/shared/set_visibility.rb @@ -5,6 +5,23 @@ describe :set_visibility, shared: true do Module.should have_private_instance_method(@method, false) end + describe "with argument" do + it "does not clone method from the ancestor when setting to the same visibility in a child" do + visibility = @method + parent = Module.new { + def test_method; end + send(visibility, :test_method) + } + + child = Module.new { + include parent + send(visibility, :test_method) + } + + child.should_not send(:"have_#{visibility}_instance_method", :test_method, false) + end + end + describe "without arguments" do it "sets visibility to following method definitions" do visibility = @method diff --git a/spec/ruby/core/module/to_s_spec.rb b/spec/ruby/core/module/to_s_spec.rb index bebdcf6e1b..29f6ecf726 100644 --- a/spec/ruby/core/module/to_s_spec.rb +++ b/spec/ruby/core/module/to_s_spec.rb @@ -2,17 +2,44 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' describe "Module#to_s" do + it 'returns the name of the module if it has a name' do + Enumerable.to_s.should == 'Enumerable' + String.to_s.should == 'String' + end + it "returns the full constant path leading to the module" do ModuleSpecs::LookupMod.to_s.should == "ModuleSpecs::LookupMod" end it "works with an anonymous module" do m = Module.new - m.to_s.should =~ /#<Module:0x[0-9a-f]+>/ + m.to_s.should =~ /\A#<Module:0x\h+>\z/ end it "works with an anonymous class" do c = Class.new - c.to_s.should =~ /#<Class:0x[0-9a-f]+>/ + c.to_s.should =~ /\A#<Class:0x\h+>\z/ + end + + it 'for the singleton class of an object of an anonymous class' do + klass = Class.new + obj = klass.new + sclass = obj.singleton_class + sclass.to_s.should == "#<Class:#{obj}>" + sclass.to_s.should =~ /\A#<Class:#<#{klass}:0x\h+>>\z/ + sclass.to_s.should =~ /\A#<Class:#<#<Class:0x\h+>:0x\h+>>\z/ + end + + it 'for a singleton class of a module includes the module name' do + ModuleSpecs.singleton_class.to_s.should == '#<Class:ModuleSpecs>' + end + + it 'for a metaclass includes the class name' do + ModuleSpecs::NamedClass.singleton_class.to_s.should == '#<Class:ModuleSpecs::NamedClass>' + end + + it 'for objects includes class name and object ID' do + obj = ModuleSpecs::NamedClass.new + obj.singleton_class.to_s.should =~ /\A#<Class:#<ModuleSpecs::NamedClass:0x\h+>>\z/ end end diff --git a/spec/ruby/core/module/undef_method_spec.rb b/spec/ruby/core/module/undef_method_spec.rb index 9b2c9240f4..b101778962 100644 --- a/spec/ruby/core/module/undef_method_spec.rb +++ b/spec/ruby/core/module/undef_method_spec.rb @@ -56,8 +56,37 @@ describe "Module#undef_method" do @module.send(:undef_method, :method_to_undef).should equal(@module) end - it "raises a NameError when passed a missing name" do - -> { @module.send :undef_method, :not_exist }.should raise_error(NameError) { |e| + it "raises a NameError when passed a missing name for a module" do + -> { @module.send :undef_method, :not_exist }.should raise_error(NameError, /undefined method `not_exist' for module `#{@module}'/) { |e| + # a NameError and not a NoMethodError + e.class.should == NameError + } + end + + it "raises a NameError when passed a missing name for a class" do + klass = Class.new + -> { klass.send :undef_method, :not_exist }.should raise_error(NameError, /undefined method `not_exist' for class `#{klass}'/) { |e| + # a NameError and not a NoMethodError + e.class.should == NameError + } + end + + it "raises a NameError when passed a missing name for a singleton class" do + klass = Class.new + obj = klass.new + sclass = obj.singleton_class + + -> { sclass.send :undef_method, :not_exist }.should raise_error(NameError, /undefined method `not_exist' for class `#{sclass}'/) { |e| + e.message.should include('`#<Class:#<#<Class:') + + # a NameError and not a NoMethodError + e.class.should == NameError + } + end + + it "raises a NameError when passed a missing name for a metaclass" do + klass = String.singleton_class + -> { klass.send :undef_method, :not_exist }.should raise_error(NameError, /undefined method `not_exist' for class `String'/) { |e| # a NameError and not a NoMethodError e.class.should == NameError } |