summaryrefslogtreecommitdiff
path: root/spec/ruby/library/delegate/delegate_class
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/delegate/delegate_class')
-rw-r--r--spec/ruby/library/delegate/delegate_class/instance_method_spec.rb52
-rw-r--r--spec/ruby/library/delegate/delegate_class/instance_methods_spec.rb26
-rw-r--r--spec/ruby/library/delegate/delegate_class/private_instance_methods_spec.rb23
-rw-r--r--spec/ruby/library/delegate/delegate_class/protected_instance_methods_spec.rb29
-rw-r--r--spec/ruby/library/delegate/delegate_class/public_instance_methods_spec.rb25
-rw-r--r--spec/ruby/library/delegate/delegate_class/respond_to_missing_spec.rb23
6 files changed, 178 insertions, 0 deletions
diff --git a/spec/ruby/library/delegate/delegate_class/instance_method_spec.rb b/spec/ruby/library/delegate/delegate_class/instance_method_spec.rb
new file mode 100644
index 0000000000..61680b9d5a
--- /dev/null
+++ b/spec/ruby/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/ruby/library/delegate/delegate_class/instance_methods_spec.rb b/spec/ruby/library/delegate/delegate_class/instance_methods_spec.rb
new file mode 100644
index 0000000000..ae329ab8eb
--- /dev/null
+++ b/spec/ruby/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/ruby/library/delegate/delegate_class/private_instance_methods_spec.rb b/spec/ruby/library/delegate/delegate_class/private_instance_methods_spec.rb
new file mode 100644
index 0000000000..d93b6d0e3d
--- /dev/null
+++ b/spec/ruby/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/ruby/library/delegate/delegate_class/protected_instance_methods_spec.rb b/spec/ruby/library/delegate/delegate_class/protected_instance_methods_spec.rb
new file mode 100644
index 0000000000..3ae0270dbd
--- /dev/null
+++ b/spec/ruby/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/ruby/library/delegate/delegate_class/public_instance_methods_spec.rb b/spec/ruby/library/delegate/delegate_class/public_instance_methods_spec.rb
new file mode 100644
index 0000000000..e06b55612e
--- /dev/null
+++ b/spec/ruby/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/ruby/library/delegate/delegate_class/respond_to_missing_spec.rb b/spec/ruby/library/delegate/delegate_class/respond_to_missing_spec.rb
new file mode 100644
index 0000000000..729cfc96c6
--- /dev/null
+++ b/spec/ruby/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