diff options
Diffstat (limited to 'spec/ruby/language/alias_spec.rb')
| -rw-r--r-- | spec/ruby/language/alias_spec.rb | 82 |
1 files changed, 65 insertions, 17 deletions
diff --git a/spec/ruby/language/alias_spec.rb b/spec/ruby/language/alias_spec.rb index e9f0050e17..4b3d36d308 100644 --- a/spec/ruby/language/alias_spec.rb +++ b/spec/ruby/language/alias_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../spec_helper', __FILE__) +require_relative '../spec_helper' class AliasObject attr :foo @@ -38,20 +38,29 @@ describe "The alias keyword" do @obj.a.should == 5 end - it "works with a doubule quoted symbol on the left-hand side" do + it "works with a double quoted symbol on the left-hand side" do @meta.class_eval do alias :"a" value end @obj.a.should == 5 end - it "works with an interoplated symbol on the left-hand side" do + it "works with an interpolated symbol on the left-hand side" do @meta.class_eval do alias :"#{'a'}" value end @obj.a.should == 5 end + it "works with an interpolated symbol with non-literal embedded expression on the left-hand side" do + @meta.class_eval do + eval %Q{ + alias :"#{'a' + ''.to_s}" value + } + end + @obj.a.should == 5 + end + it "works with a simple symbol on the right-hand side" do @meta.class_eval do alias a :value @@ -66,20 +75,29 @@ describe "The alias keyword" do @obj.a.should == 5 end - it "works with a doubule quoted symbol on the right-hand side" do + it "works with a double quoted symbol on the right-hand side" do @meta.class_eval do alias a :"value" end @obj.a.should == 5 end - it "works with an interoplated symbol on the right-hand side" do + it "works with an interpolated symbol on the right-hand side" do @meta.class_eval do alias a :"#{'value'}" end @obj.a.should == 5 end + it "works with an interpolated symbol with non-literal embedded expression on the right-hand side" do + @meta.class_eval do + eval %Q{ + alias a :"#{'value' + ''.to_s}" + } + end + @obj.a.should == 5 + end + it "adds the new method to the list of methods" do original_methods = @obj.methods @meta.class_eval do @@ -122,7 +140,7 @@ describe "The alias keyword" do end @obj.__value.should == 5 - lambda { AliasObject.new.__value }.should raise_error(NoMethodError) + -> { AliasObject.new.__value }.should.raise(NoMethodError) end it "operates on the class/module metaclass when used in instance_eval" do @@ -131,7 +149,7 @@ describe "The alias keyword" do end AliasObject.__klass_method.should == 7 - lambda { Object.__klass_method }.should raise_error(NoMethodError) + -> { Object.__klass_method }.should.raise(NoMethodError) end it "operates on the class/module metaclass when used in instance_exec" do @@ -140,7 +158,7 @@ describe "The alias keyword" do end AliasObject.__klass_method2.should == 7 - lambda { Object.__klass_method2 }.should raise_error(NoMethodError) + -> { Object.__klass_method2 }.should.raise(NoMethodError) end it "operates on methods defined via attr, attr_reader, and attr_accessor" do @@ -204,7 +222,7 @@ describe "The alias keyword" do end it "operates on methods with splat arguments defined in a superclass using text block for class eval" do - class Sub < AliasObject;end + subclass = Class.new(AliasObject) AliasObject.class_eval <<-code def test(*args) 4 @@ -215,32 +233,62 @@ describe "The alias keyword" do alias test_without_check test alias test test_with_check code - Sub.new.test("testing").should == 4 + subclass.new.test("testing").should == 4 end - it "is not allowed against Fixnum or String instances" do - lambda do + it "is not allowed against Integer or String instances" do + -> do 1.instance_eval do alias :foo :to_s end - end.should raise_error(TypeError) + end.should.raise(TypeError) - lambda do + -> do :blah.instance_eval do alias :foo :to_s end - end.should raise_error(TypeError) + end.should.raise(TypeError) end it "on top level defines the alias on Object" do # because it defines on the default definee / current module - ruby_exe("def foo; end; alias bla foo; print method(:bla).owner", escape: true).should == "Object" + ruby_exe("def foo; end; alias bla foo; print method(:bla).owner").should == "Object" end it "raises a NameError when passed a missing name" do - lambda { @meta.class_eval { alias undef_method not_exist } }.should raise_error(NameError) { |e| + -> { @meta.class_eval { alias undef_method not_exist } }.should.raise(NameError) { |e| # a NameError and not a NoMethodError e.class.should == NameError } end + + it "defines the method on the aliased class when the original method is from a parent class" do + parent = Class.new do + def parent_method + end + end + child = Class.new(parent) do + alias parent_method_alias parent_method + end + + child.instance_method(:parent_method_alias).owner.should == child + child.instance_methods(false).should.include?(:parent_method_alias) + end +end + +describe "The alias keyword" do + it "can create a new global variable, synonym of the original" do + code = '$a = 1; alias $b $a; p [$a, $b]; $b = 2; p [$a, $b]' + ruby_exe(code).should == "[1, 1]\n[2, 2]\n" + end + + it "can override an existing global variable and make them synonyms" do + code = '$a = 1; $b = 2; alias $b $a; p [$a, $b]; $b = 3; p [$a, $b]' + ruby_exe(code).should == "[1, 1]\n[3, 3]\n" + end + + it "supports aliasing twice the same global variables" do + code = '$a = 1; alias $b $a; alias $b $a; p [$a, $b]' + ruby_exe(code).should == "[1, 1]\n" + end end |
