summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module/shared
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
commit1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch)
treea3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/module/shared
parent75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff)
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/module/shared')
-rw-r--r--spec/ruby/core/module/shared/class_eval.rb115
-rw-r--r--spec/ruby/core/module/shared/class_exec.rb29
-rw-r--r--spec/ruby/core/module/shared/equal_value.rb14
-rw-r--r--spec/ruby/core/module/shared/set_visibility.rb135
4 files changed, 293 insertions, 0 deletions
diff --git a/spec/ruby/core/module/shared/class_eval.rb b/spec/ruby/core/module/shared/class_eval.rb
new file mode 100644
index 0000000000..6bb9668fee
--- /dev/null
+++ b/spec/ruby/core/module/shared/class_eval.rb
@@ -0,0 +1,115 @@
+describe :module_class_eval, shared: true do
+ # TODO: This should probably be replaced with a "should behave like" that uses
+ # the many scoping/binding specs from kernel/eval_spec, since most of those
+ # behaviors are the same for instance_eval. See also module_eval/class_eval.
+
+ it "evaluates a given string in the context of self" do
+ ModuleSpecs.send(@method, "self").should == ModuleSpecs
+ ModuleSpecs.send(@method, "1 + 1").should == 2
+ end
+
+ it "does not add defined methods to other classes" do
+ FalseClass.send(@method) do
+ def foo
+ 'foo'
+ end
+ end
+ lambda {42.foo}.should raise_error(NoMethodError)
+ end
+
+ it "resolves constants in the caller scope" do
+ ModuleSpecs::ClassEvalTest.get_constant_from_scope.should == ModuleSpecs::Lookup
+ end
+
+ it "resolves constants in the caller scope ignoring send" do
+ ModuleSpecs::ClassEvalTest.get_constant_from_scope_with_send(@method).should == ModuleSpecs::Lookup
+ end
+
+ it "resolves constants in the receiver's scope" do
+ ModuleSpecs.send(@method, "Lookup").should == ModuleSpecs::Lookup
+ ModuleSpecs.send(@method, "Lookup::LOOKIE").should == ModuleSpecs::Lookup::LOOKIE
+ end
+
+ it "defines constants in the receiver's scope" do
+ ModuleSpecs.send(@method, "module NewEvaluatedModule;end")
+ ModuleSpecs.const_defined?(:NewEvaluatedModule, false).should == true
+ end
+
+ it "evaluates a given block in the context of self" do
+ ModuleSpecs.send(@method) { self }.should == ModuleSpecs
+ ModuleSpecs.send(@method) { 1 + 1 }.should == 2
+ end
+
+ it "passes the module as the first argument of the block" do
+ given = nil
+ ModuleSpecs.send(@method) do |block_parameter|
+ given = block_parameter
+ end
+ given.should equal ModuleSpecs
+ end
+
+ it "uses the optional filename and lineno parameters for error messages" do
+ ModuleSpecs.send(@method, "[__FILE__, __LINE__]", "test", 102).should == ["test", 102]
+ end
+
+ it "converts a non-string filename to a string using to_str" do
+ (file = mock(__FILE__)).should_receive(:to_str).and_return(__FILE__)
+ ModuleSpecs.send(@method, "1+1", file)
+ end
+
+ it "raises a TypeError when the given filename can't be converted to string using to_str" do
+ (file = mock('123')).should_receive(:to_str).and_return(123)
+ lambda { ModuleSpecs.send(@method, "1+1", file) }.should raise_error(TypeError)
+ end
+
+ it "converts non string eval-string to string using to_str" do
+ (o = mock('1 + 1')).should_receive(:to_str).and_return("1 + 1")
+ ModuleSpecs.send(@method, o).should == 2
+ end
+
+ it "raises a TypeError when the given eval-string can't be converted to string using to_str" do
+ o = mock('x')
+ lambda { ModuleSpecs.send(@method, o) }.should raise_error(TypeError)
+
+ (o = mock('123')).should_receive(:to_str).and_return(123)
+ lambda { ModuleSpecs.send(@method, o) }.should raise_error(TypeError)
+ end
+
+ it "raises an ArgumentError when no arguments and no block are given" do
+ lambda { ModuleSpecs.send(@method) }.should raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError when more than 3 arguments are given" do
+ lambda {
+ ModuleSpecs.send(@method, "1 + 1", "some file", 0, "bogus")
+ }.should raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError when a block and normal arguments are given" do
+ lambda {
+ ModuleSpecs.send(@method, "1 + 1") { 1 + 1 }
+ }.should raise_error(ArgumentError)
+ end
+
+ # This case was found because Rubinius was caching the compiled
+ # version of the string and not duping the methods within the
+ # eval, causing the method addition to change the static scope
+ # of the shared CompiledCode.
+ it "adds methods respecting the lexical constant scope" do
+ code = "def self.attribute; C; end"
+
+ a = Class.new do
+ self::C = "A"
+ end
+
+ b = Class.new do
+ self::C = "B"
+ end
+
+ a.send @method, code
+ b.send @method, code
+
+ a.attribute.should == "A"
+ b.attribute.should == "B"
+ end
+end
diff --git a/spec/ruby/core/module/shared/class_exec.rb b/spec/ruby/core/module/shared/class_exec.rb
new file mode 100644
index 0000000000..c5c18b0a34
--- /dev/null
+++ b/spec/ruby/core/module/shared/class_exec.rb
@@ -0,0 +1,29 @@
+describe :module_class_exec, shared: true do
+ it "does not add defined methods to other classes" do
+ FalseClass.send(@method) do
+ def foo
+ 'foo'
+ end
+ end
+ lambda {42.foo}.should raise_error(NoMethodError)
+ end
+
+ it "defines method in the receiver's scope" do
+ ModuleSpecs::Subclass.send(@method) { def foo; end }
+ ModuleSpecs::Subclass.new.respond_to?(:foo).should == true
+ end
+
+ it "evaluates a given block in the context of self" do
+ ModuleSpecs::Subclass.send(@method) { self }.should == ModuleSpecs::Subclass
+ ModuleSpecs::Subclass.new.send(@method) { 1 + 1 }.should == 2
+ end
+
+ it "raises a LocalJumpError when no block is given" do
+ lambda { ModuleSpecs::Subclass.send(@method) }.should raise_error(LocalJumpError)
+ end
+
+ it "passes arguments to the block" do
+ a = ModuleSpecs::Subclass
+ a.send(@method, 1) { |b| b }.should equal(1)
+ end
+end
diff --git a/spec/ruby/core/module/shared/equal_value.rb b/spec/ruby/core/module/shared/equal_value.rb
new file mode 100644
index 0000000000..f1227d873c
--- /dev/null
+++ b/spec/ruby/core/module/shared/equal_value.rb
@@ -0,0 +1,14 @@
+describe :module_equal, shared: true do
+ it "returns true if self and the given module are the same" do
+ ModuleSpecs.send(@method, ModuleSpecs).should == true
+ ModuleSpecs::Child.send(@method, ModuleSpecs::Child).should == true
+ ModuleSpecs::Parent.send(@method, ModuleSpecs::Parent).should == true
+ ModuleSpecs::Basic.send(@method, ModuleSpecs::Basic).should == true
+ ModuleSpecs::Super.send(@method, ModuleSpecs::Super).should == true
+
+ ModuleSpecs::Child.send(@method, ModuleSpecs).should == false
+ ModuleSpecs::Child.send(@method, ModuleSpecs::Parent).should == false
+ ModuleSpecs::Child.send(@method, ModuleSpecs::Basic).should == false
+ ModuleSpecs::Child.send(@method, ModuleSpecs::Super).should == false
+ end
+end
diff --git a/spec/ruby/core/module/shared/set_visibility.rb b/spec/ruby/core/module/shared/set_visibility.rb
new file mode 100644
index 0000000000..c39d59e05d
--- /dev/null
+++ b/spec/ruby/core/module/shared/set_visibility.rb
@@ -0,0 +1,135 @@
+# -*- encoding: us-ascii -*-
+
+describe :set_visibility, shared: true do
+ it "is a private method" do
+ Module.should have_private_instance_method(@method, false)
+ end
+
+ describe "without arguments" do
+ it "sets visibility to following method definitions" do
+ visibility = @method
+ mod = Module.new {
+ send visibility
+
+ def test1() end
+ def test2() end
+ }
+
+ mod.should send(:"have_#{@method}_instance_method", :test1, false)
+ mod.should send(:"have_#{@method}_instance_method", :test2, false)
+ end
+
+ it "stops setting visibility if the body encounters other visibility setters without arguments" do
+ visibility = @method
+ new_visibility = nil
+ mod = Module.new {
+ send visibility
+ new_visibility = [:protected, :private].find {|vis| vis != visibility }
+ send new_visibility
+ def test1() end
+ }
+
+ mod.should send(:"have_#{new_visibility}_instance_method", :test1, false)
+ end
+
+ it "continues setting visibility if the body encounters other visibility setters with arguments" do
+ visibility = @method
+ mod = Module.new {
+ send visibility
+ def test1() end
+ send([:protected, :private].find {|vis| vis != visibility }, :test1)
+ def test2() end
+ }
+
+ mod.should send(:"have_#{@method}_instance_method", :test2, false)
+ end
+
+ it "does not affect module_evaled method definitions when itself is outside the eval" do
+ visibility = @method
+ mod = Module.new {
+ send visibility
+
+ module_eval { def test1() end }
+ module_eval " def test2() end "
+ }
+
+ mod.should have_public_instance_method(:test1, false)
+ mod.should have_public_instance_method(:test2, false)
+ end
+
+ it "does not affect outside method definitions when itself is inside a module_eval" do
+ visibility = @method
+ mod = Module.new {
+ module_eval { send visibility }
+
+ def test1() end
+ }
+
+ mod.should have_public_instance_method(:test1, false)
+ end
+
+ it "affects normally if itself and method definitions are inside a module_eval" do
+ visibility = @method
+ mod = Module.new {
+ module_eval {
+ send visibility
+
+ def test1() end
+ }
+ }
+
+ mod.should send(:"have_#{@method}_instance_method", :test1, false)
+ end
+
+ it "does not affect method definitions when itself is inside an eval and method definitions are outside" do
+ visibility = @method
+ initialized_visibility = [:public, :protected, :private].find {|sym| sym != visibility }
+ mod = Module.new {
+ send initialized_visibility
+ eval visibility.to_s
+
+ def test1() end
+ }
+
+ mod.should send(:"have_#{initialized_visibility}_instance_method", :test1, false)
+ end
+
+ it "affects evaled method definitions when itself is outside the eval" do
+ visibility = @method
+ mod = Module.new {
+ send visibility
+
+ eval "def test1() end"
+ }
+
+ mod.should send(:"have_#{@method}_instance_method", :test1, false)
+ end
+
+ it "affects normally if itself and following method definitions are inside a eval" do
+ visibility = @method
+ mod = Module.new {
+ eval <<-CODE
+ #{visibility}
+
+ def test1() end
+ CODE
+ }
+
+ mod.should send(:"have_#{@method}_instance_method", :test1, false)
+ end
+
+ describe "within a closure" do
+ it "sets the visibility outside the closure" do
+ visibility = @method
+ mod = Module.new {
+ 1.times {
+ send visibility
+ }
+ def test1() end
+ }
+
+ mod.should send(:"have_#{@method}_instance_method", :test1, false)
+ end
+ end
+ end
+end