diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/library/delegate/delegate_class | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/library/delegate/delegate_class')
6 files changed, 178 insertions, 0 deletions
diff --git a/spec/rubyspec/library/delegate/delegate_class/instance_method_spec.rb b/spec/rubyspec/library/delegate/delegate_class/instance_method_spec.rb new file mode 100644 index 0000000000..61680b9d5a --- /dev/null +++ b/spec/rubyspec/library/delegate/delegate_class/instance_method_spec.rb @@ -0,0 +1,52 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "DelegateClass.instance_method" do + before :all do + @klass = DelegateSpecs::DelegateClass + @obj = @klass.new(DelegateSpecs::Simple.new) + end + + it "returns a method object for public instance methods of the delegated class" do + m = @klass.instance_method(:pub) + m.should be_an_instance_of(UnboundMethod) + m.bind(@obj).call.should == :foo + end + + it "returns a method object for protected instance methods of the delegated class" do + m = @klass.instance_method(:prot) + m.should be_an_instance_of(UnboundMethod) + m.bind(@obj).call.should == :protected + end + + it "raises a NameError for a private instance methods of the delegated class" do + lambda { + @klass.instance_method(:priv) + }.should raise_error(NameError) + end + + it "returns a method object for public instance methods of the DelegateClass class" do + m = @klass.instance_method(:extra) + m.should be_an_instance_of(UnboundMethod) + m.bind(@obj).call.should == :cheese + end + + it "returns a method object for protected instance methods of the DelegateClass class" do + m = @klass.instance_method(:extra_protected) + m.should be_an_instance_of(UnboundMethod) + m.bind(@obj).call.should == :baz + end + + it "returns a method object for private instance methods of the DelegateClass class" do + m = @klass.instance_method(:extra_private) + m.should be_an_instance_of(UnboundMethod) + m.bind(@obj).call.should == :bar + end + + it "raises a NameError for an invalid method name" do + lambda { + @klass.instance_method(:invalid_and_silly_method_name) + }.should raise_error(NameError) + end + +end diff --git a/spec/rubyspec/library/delegate/delegate_class/instance_methods_spec.rb b/spec/rubyspec/library/delegate/delegate_class/instance_methods_spec.rb new file mode 100644 index 0000000000..ae329ab8eb --- /dev/null +++ b/spec/rubyspec/library/delegate/delegate_class/instance_methods_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "DelegateClass.instance_methods" do + before :all do + @methods = DelegateSpecs::DelegateClass.instance_methods + end + + it "includes all public methods of the delegated class" do + @methods.should include :pub + end + + it "includes all protected methods of the delegated class" do + @methods.should include :prot + end + + it "includes instance methods of the DelegateClass class" do + @methods.should include :extra + @methods.should include :extra_protected + end + + it "does not include private methods" do + @methods.should_not include :priv + @methods.should_not include :extra_private + end +end diff --git a/spec/rubyspec/library/delegate/delegate_class/private_instance_methods_spec.rb b/spec/rubyspec/library/delegate/delegate_class/private_instance_methods_spec.rb new file mode 100644 index 0000000000..d93b6d0e3d --- /dev/null +++ b/spec/rubyspec/library/delegate/delegate_class/private_instance_methods_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "DelegateClass.private_instance_methods" do + before :all do + @methods = DelegateSpecs::DelegateClass.private_instance_methods + end + + it "does not include any instance methods of the delegated class" do + @methods.should_not include :pub + @methods.should_not include :prot + @methods.should_not include :priv # since these are not forwarded... + end + + it "includes private instance methods of the DelegateClass class" do + @methods.should include :extra_private + end + + it "does not include public or protected instance methods of the DelegateClass class" do + @methods.should_not include :extra + @methods.should_not include :extra_protected + end +end diff --git a/spec/rubyspec/library/delegate/delegate_class/protected_instance_methods_spec.rb b/spec/rubyspec/library/delegate/delegate_class/protected_instance_methods_spec.rb new file mode 100644 index 0000000000..3ae0270dbd --- /dev/null +++ b/spec/rubyspec/library/delegate/delegate_class/protected_instance_methods_spec.rb @@ -0,0 +1,29 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "DelegateClass.protected_instance_methods" do + before :all do + @methods = DelegateSpecs::DelegateClass.protected_instance_methods + end + + it "does not include public methods of the delegated class" do + @methods.should_not include :pub + end + + it "includes the protected methods of the delegated class" do + @methods.should include :prot + end + + it "includes protected instance methods of the DelegateClass class" do + @methods.should include :extra_protected + end + + it "does not include public instance methods of the DelegateClass class" do + @methods.should_not include :extra + end + + it "does not include private methods" do + @methods.should_not include :priv + @methods.should_not include :extra_private + end +end diff --git a/spec/rubyspec/library/delegate/delegate_class/public_instance_methods_spec.rb b/spec/rubyspec/library/delegate/delegate_class/public_instance_methods_spec.rb new file mode 100644 index 0000000000..e06b55612e --- /dev/null +++ b/spec/rubyspec/library/delegate/delegate_class/public_instance_methods_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "DelegateClass.public_instance_methods" do + before :all do + @methods = DelegateSpecs::DelegateClass.public_instance_methods + end + + it "includes all public methods of the delegated class" do + @methods.should include :pub + end + + it "does not include the protected methods of the delegated class" do + @methods.should_not include :prot + end + + it "includes public instance methods of the DelegateClass class" do + @methods.should include :extra + end + + it "does not include private methods" do + @methods.should_not include :priv + @methods.should_not include :extra_private + end +end diff --git a/spec/rubyspec/library/delegate/delegate_class/respond_to_missing_spec.rb b/spec/rubyspec/library/delegate/delegate_class/respond_to_missing_spec.rb new file mode 100644 index 0000000000..729cfc96c6 --- /dev/null +++ b/spec/rubyspec/library/delegate/delegate_class/respond_to_missing_spec.rb @@ -0,0 +1,23 @@ +require 'delegate' + +describe "DelegateClass#respond_to_missing?" do + it "is used for respond_to? behavior of late-bound delegated methods" do + # From jruby/jruby#3151: + # DelegateClass subtracts Delegate's public API from the target class's instance_methods + # to determine which methods to eagerly delegate. If respond_to_missing? shows up in + # instance_methods, it will get delegated and skip the delegate-aware implementation + # in Delegate. Any methods that must be delegated through method_missing, like methods + # defined after the DelegateClass is created, will fail to dispatch properly. + + cls = Class.new + dcls = DelegateClass(cls) + cdcls = Class.new(dcls) + cdcls_obj = cdcls.new(cls.new) + + cdcls_obj.respond_to?(:foo).should == false + + cls.class_eval { def foo; end } + + cdcls_obj.respond_to?(:foo).should == true + end +end |