summaryrefslogtreecommitdiff
path: root/spec/ruby/shared/basicobject
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/shared/basicobject')
-rw-r--r--spec/ruby/shared/basicobject/method_missing.rb124
-rw-r--r--spec/ruby/shared/basicobject/send.rb128
2 files changed, 252 insertions, 0 deletions
diff --git a/spec/ruby/shared/basicobject/method_missing.rb b/spec/ruby/shared/basicobject/method_missing.rb
new file mode 100644
index 0000000000..330f6a4922
--- /dev/null
+++ b/spec/ruby/shared/basicobject/method_missing.rb
@@ -0,0 +1,124 @@
+require_relative '../../spec_helper'
+require_relative '../../fixtures/basicobject/method_missing'
+
+describe :method_missing_defined_module, shared: true do
+ describe "for a Module with #method_missing defined" do
+ it "is not called when a defined method is called" do
+ @object.method_public.should == :module_public_method
+ end
+
+ it "is called when a not defined method is called" do
+ @object.not_defined_method.should == :module_method_missing
+ end
+
+ it "is called when a protected method is called" do
+ @object.method_protected.should == :module_method_missing
+ end
+
+ it "is called when a private method is called" do
+ @object.method_private.should == :module_method_missing
+ end
+ end
+end
+
+describe :method_missing_module, shared: true do
+ describe "for a Module" do
+ it "raises a NoMethodError when an undefined method is called" do
+ -> { @object.no_such_method }.should.raise(NoMethodError)
+ end
+
+ it "raises a NoMethodError when a protected method is called" do
+ -> { @object.method_protected }.should.raise(NoMethodError)
+ end
+
+ it "raises a NoMethodError when a private method is called" do
+ -> { @object.method_private }.should.raise(NoMethodError)
+ end
+ end
+end
+
+describe :method_missing_defined_class, shared: true do
+ describe "for a Class with #method_missing defined" do
+ it "is not called when a defined method is called" do
+ @object.method_public.should == :class_public_method
+ end
+
+ it "is called when an undefined method is called" do
+ @object.no_such_method.should == :class_method_missing
+ end
+
+ it "is called when an protected method is called" do
+ @object.method_protected.should == :class_method_missing
+ end
+
+ it "is called when an private method is called" do
+ @object.method_private.should == :class_method_missing
+ end
+ end
+end
+
+describe :method_missing_class, shared: true do
+ describe "for a Class" do
+ it "raises a NoMethodError when an undefined method is called" do
+ -> { @object.no_such_method }.should.raise(NoMethodError)
+ end
+
+ it "raises a NoMethodError when a protected method is called" do
+ -> { @object.method_protected }.should.raise(NoMethodError)
+ end
+
+ it "raises a NoMethodError when a private method is called" do
+ -> { @object.method_private }.should.raise(NoMethodError)
+ end
+ end
+end
+
+describe :method_missing_defined_instance, shared: true do
+ describe "for an instance with #method_missing defined" do
+ before :each do
+ @instance = @object.new
+ end
+
+ it "is not called when a defined method is called" do
+ @instance.method_public.should == :instance_public_method
+ end
+
+ it "is called when an undefined method is called" do
+ @instance.no_such_method.should == :instance_method_missing
+ end
+
+ it "is called when an protected method is called" do
+ @instance.method_protected.should == :instance_method_missing
+ end
+
+ it "is called when an private method is called" do
+ @instance.method_private.should == :instance_method_missing
+ end
+ end
+end
+
+describe :method_missing_instance, shared: true do
+ describe "for an instance" do
+ it "raises a NoMethodError when an undefined method is called" do
+ -> { @object.new.no_such_method }.should.raise(NoMethodError)
+ end
+
+ it "raises a NoMethodError when a protected method is called" do
+ -> { @object.new.method_protected }.should.raise(NoMethodError)
+ end
+
+ it "raises a NoMethodError when a private method is called" do
+ -> { @object.new.method_private }.should.raise(NoMethodError)
+ end
+
+ it 'sets the receiver of the raised NoMethodError' do
+ obj = @object.new
+
+ begin
+ obj.method_private
+ rescue NoMethodError => error
+ (error.receiver == obj).should == true
+ end
+ end
+ end
+end
diff --git a/spec/ruby/shared/basicobject/send.rb b/spec/ruby/shared/basicobject/send.rb
new file mode 100644
index 0000000000..d38aea975e
--- /dev/null
+++ b/spec/ruby/shared/basicobject/send.rb
@@ -0,0 +1,128 @@
+module SendSpecs
+end
+
+describe :basicobject_send, shared: true do
+ it "invokes the named method" do
+ class SendSpecs::Foo
+ def bar
+ 'done'
+ end
+ end
+ SendSpecs::Foo.new.send(@method, :bar).should == 'done'
+ end
+
+ it "accepts a String method name" do
+ class SendSpecs::Foo
+ def bar
+ 'done'
+ end
+ end
+ SendSpecs::Foo.new.send(@method, 'bar').should == 'done'
+ end
+
+ it "invokes a class method if called on a class" do
+ class SendSpecs::Foo
+ def self.bar
+ 'done'
+ end
+ end
+ SendSpecs::Foo.send(@method, :bar).should == 'done'
+ end
+
+ it "raises a TypeError if the method name is not a string or symbol" do
+ -> { SendSpecs.send(@method, nil) }.should.raise(TypeError, /not a symbol nor a string/)
+ -> { SendSpecs.send(@method, 42) }.should.raise(TypeError, /not a symbol nor a string/)
+ -> { SendSpecs.send(@method, 3.14) }.should.raise(TypeError, /not a symbol nor a string/)
+ -> { SendSpecs.send(@method, true) }.should.raise(TypeError, /not a symbol nor a string/)
+ end
+
+ it "raises a NameError if the corresponding method can't be found" do
+ class SendSpecs::Foo
+ def bar
+ 'done'
+ end
+ end
+ -> { SendSpecs::Foo.new.send(@method, :syegsywhwua) }.should.raise(NameError)
+ end
+
+ it "raises a NameError if the corresponding singleton method can't be found" do
+ class SendSpecs::Foo
+ def self.bar
+ 'done'
+ end
+ end
+ -> { SendSpecs::Foo.send(@method, :baz) }.should.raise(NameError)
+ end
+
+ it "raises an ArgumentError if no arguments are given" do
+ class SendSpecs::Foo; end
+ -> { SendSpecs::Foo.new.send @method }.should.raise(ArgumentError)
+ end
+
+ it "raises an ArgumentError if called with more arguments than available parameters" do
+ class SendSpecs::Foo
+ def bar; end
+ end
+
+ -> { SendSpecs::Foo.new.send(@method, :bar, :arg) }.should.raise(ArgumentError)
+ end
+
+ it "raises an ArgumentError if called with fewer arguments than required parameters" do
+ class SendSpecs::Foo
+ def foo(arg); end
+ end
+
+ -> { SendSpecs::Foo.new.send(@method, :foo) }.should.raise(ArgumentError)
+ end
+
+ it "succeeds if passed an arbitrary number of arguments as a splat parameter" do
+ class SendSpecs::Foo
+ def baz(*args) args end
+ end
+
+ begin
+ SendSpecs::Foo.new.send(@method, :baz).should == []
+ SendSpecs::Foo.new.send(@method, :baz, :quux).should == [:quux]
+ SendSpecs::Foo.new.send(@method, :baz, :quux, :foo).should == [:quux, :foo]
+ rescue
+ fail
+ end
+ end
+
+ it "succeeds when passing 1 or more arguments as a required and a splat parameter" do
+ class SendSpecs::Foo
+ def baz(first, *rest) [first, *rest] end
+ end
+
+ SendSpecs::Foo.new.send(@method, :baz, :quux).should == [:quux]
+ SendSpecs::Foo.new.send(@method, :baz, :quux, :foo).should == [:quux, :foo]
+ end
+
+ it "succeeds when passing 0 arguments to a method with one parameter with a default" do
+ class SendSpecs::Foo
+ def foo(first = true) first end
+ end
+
+ begin
+ SendSpecs::Foo.new.send(@method, :foo).should == true
+ SendSpecs::Foo.new.send(@method, :foo, :arg).should == :arg
+ rescue
+ fail
+ end
+ end
+
+ it "has a negative arity" do
+ method(@method).arity.should < 0
+ end
+
+ it "invokes module methods with super correctly" do
+ m1 = Module.new { def foo(ary); ary << :m1; end; }
+ m2 = Module.new { def foo(ary = []); super(ary); ary << :m2; end; }
+ c2 = Class.new do
+ include m1
+ include m2
+ end
+
+ c2.new.send(@method, :foo, *[[]]).should == %i[m1 m2]
+ end
+end
v3_3_9&id=ef084cc8f4958c1b6e4ead99136631bef6d8ddba'>commit ef084cc8f4...Takashi Kokubun21 months v3_2_5commit 31d0f1a2e7...nagachika22 months v3_3_4commit be1089c8ec...Takashi Kokubun23 months v3_3_3commit f1c7b6f435...Takashi Kokubun24 months v3_3_2commit e5a195edf6...Takashi Kokubun2 years v3_1_6commit a777087be6...Hiroshi SHIBATA2 years v3_4_0_preview1commit 9d69619623...Nobuyoshi Nakada2 years v3_0_7commit 724a071175...Hiroshi SHIBATA2 years v3_1_5commit 1945f8dc0e...Hiroshi SHIBATA2 years v3_3_1commit c56cd86388...NARUSE, Yui2 years v3_2_4commit af471c0e01...nagachika2 years v3_2_3commit 52bb2ac0a6...nagachika2 years v3_3_0commit 5124f9ac75...NARUSE, Yui2 years v3_3_0_rc1commit a49643340e...NARUSE, Yui2 years v3_3_0_preview3commit 60e19a0b5f...Nobuyoshi Nakada3 years v3_3_0_preview2commit e50fcca9a7...Nathan Froyd3 years v3_3_0_preview1commit a1b01e7701...Yuichiro Kaneko3 years v3_0_6commit 23a532679b...NAKAMURA Usaku3 years v2_7_8commit 1f4d455848...NAKAMURA Usaku3 years v3_2_2commit e51014f9c0...NARUSE, Yui3 years v3_1_4commit 957bb7cb81...Hiroshi SHIBATA3 years v3_2_1commit 31819e82c8...NARUSE, Yui3 years v3_2_0commit a528908271...NARUSE, Yui3 years v3_2_0_rc1commit 81e274c990...Lars Kanis4 years v2_7_7commit 168ec2b1e5...NAKAMURA Usaku4 years v3_0_5commit ba5cf0f7c5...Kazuki Yamaguchi4 years v3_1_3commit 1a6b16756e...nagachika4 years v3_2_0_preview3commit 28611be6ee...Hiroshi SHIBATA4 years v3_2_0_preview2commit 35cfc9a3bb...Kevin Newton4 years v2_6_10commit 7b4ea5bb73...usa4 years v3_0_4commit 3fa771dded...nagachika4 years v2_7_6commit c9c2245c0a...NAKAMURA Usaku4 years v3_1_2commit 4491bb740a...NARUSE, Yui4 years v3_2_0_preview1commit f801386f0c...Nobuyoshi Nakada4 years v3_1_1commit 53f5fc4236...NARUSE, Yui4 years v3_1_0commit fb4df44d16...NARUSE, Yui4 years v2_6_9commit 8e26731f9e...usa5 years v2_7_5commit f69aeb8314...NAKAMURA Usaku5 years v3_0_3commit 3fb7d2cadc...nagachika5 years v3_1_0_preview1commit 5a3b2e6141...Nobuyoshi Nakada5 years v2_7_4commit a21a3b7d23...Yusuke Endoh5 years v2_6_8commit 768423edc2...usa5 years v3_0_2commit 0db68f0233...nagachika5 years v2_7_3commit 6847ee089d...nagachika5 years v3_0_1commit 0fb782ee38...Nobuyoshi Nakada5 years v2_6_7commit 930143880a...usa5 years v2_5_9commit ee47403ce0...usa5 years v3_0_0commit 95aff21468...TAKANO Mitsuhiro5 years v3_0_0_rc2commit a89932799c...NARUSE, Yui5 years v3_0_0_rc1commit 8680ae9cbd...Nobuyoshi Nakada5 years v3_0_0_preview2commit d7a16670c3...Nobuyoshi Nakada5 years v2_7_2commit 5445e04352...nagachika6 years v3_0_0_preview1commit 0096d2b895...Koichi Sasada6 years v2_5_8commit 4992d9fd70...usa6 years v2_4_10commit 27f6ad737b...usa6 years v2_6_6commit 27958c2bd6...nagachika6 years v2_7_1commit a0c7c23c9c...NARUSE, Yui6 years v2_7_0commit 647ee6f091...aycabta6 years v2_7_0_rc2commit 75acbd5f00...Yusuke Endoh6 years v2_7_0_rc1commit 8a40dce0ff...git6 years v2_7_0_preview3commit b563439274...Nobuyoshi Nakada7 years v2_7_0_preview2commit 02aadf1032...Yusuke Endoh7 years v2_4_9commit 7c94ba3401...usa7 years v2_4_8commit 52f881b82a...usa7 years v2_5_7commit 1c39daae0f...usa7 years v2_6_5commit 37c2cd3fa4...nagachika7 years v2_6_4commit 6315e42c22...nagachika7 years v2_5_6commit 189a36cfab...usa7 years v2_4_7commit c914780585...usa7 years v1_0_971003commit 7ad198827b...Yukihiro Matsumoto7 years v1_0_971015commit 2a4ba10e2d...Yukihiro Matsumoto7 years v1_0_971021commit 9b01ce6954...Yukihiro Matsumoto7 years v1_0_971118commit cfd31fa21b...Yukihiro Matsumoto7 years v1_0_971125commit ab261638f5...Yukihiro Matsumoto7 years v1_0_971204commit fb0fe24512...Yukihiro Matsumoto7 years v1_0_971209commit 66541bbb36...Yukihiro Matsumoto7 years v1_0_971225commit 4207990990...Yukihiro Matsumoto7 years v0_71commit 4e65eab7ab...Yukihiro Matsumoto7 years v0_72commit 11e21a36bc...Yukihiro Matsumoto7 years v0_73commit b2420d8ffa...Yukihiro Matsumoto7 years v0_76commit 8bf1c909dc...Yukihiro Matsumoto7 years v0_95commit fca49a8a69...Yukihiro Matsumoto7 years v0_99_4_961224commit 554b989ba1...Yukihiro Matsumoto7 years v1_0_961225commit ce930d0429...Yukihiro Matsumoto7 years v1_0_971002commit 10d21745c8...Yukihiro Matsumoto7 years v0_62commit b3f9ba5a37...Yukihiro Matsumoto7 years v0_63commit bd0c733b77...Yukihiro Matsumoto7 years v0_64commit 5d828b25d4...Yukihiro Matsumoto7 years v0_65commit 897cf06695...Yukihiro Matsumoto7 years v0_66commit c080fb6d10...Yukihiro Matsumoto7 years v0_67commit 2f106ab85c...Yukihiro Matsumoto7 years v0_68commit 881c5a9c32...Yukihiro Matsumoto7 years v0_69commit d349889e77...Yukihiro Matsumoto7 years v0_50commit 6e30904136...Yukihiro Matsumoto7 years v0_51commit eed5c920dd...Yukihiro Matsumoto7 years v0_52commit 173976c97c...Yukihiro Matsumoto7 years v0_54commit 29f237a8b1...Yukihiro Matsumoto7 years v0_55commit c31025779d...Yukihiro Matsumoto7 years v0_56commit 4dfd93c72a...Yukihiro Matsumoto7 years v0_60commit 00e36aa09f...Yukihiro Matsumoto7 years v0_49commit 200e0ee2fd...Yukihiro Matsumoto7 years v2_7_0_preview1commit c55db6aa27...git7 years v2_6_3commit 257fda2518...naruse7 years v2_4_6commit 5b36edf4ca...usa7 years v2_5_5commit f11c0b253c...nagachika7 years v2_5_4commit 1a84920668...nagachika7 years v2_6_2commit 300089b723...naruse7 years v2_6_1commit e6d1c72bec...naruse7 years v2_6_0commit c1af7b1e1d...naruse7 years v2_6_0_rc2commit c54428bbd7...naruse7 years v2_6_0_rc1commit c7ee7e4205...naruse8 years v2_6_0_preview3commit 7ccad5680d...naruse8 years v2_5_3commit c4b6652223...nagachika8 years v2_4_5commit a0143aa5e4...usa8 years v2_3_8commit 8d23556886...usa8 years v2_5_2commit bf508be28b...nagachika8 years v2_6_0_preview2commit 6f59db30c1...naruse8 years v2_5_1commit 85883dc393...naruse8 years v2_3_7commit 9bfe7fc5cb...usa8 years v2_2_10commit 933bb2b8b5...usa8 years v2_4_4commit a8197e08f5...nagachika8 years v2_6_0_preview1commit ce0e3fc1b1...naruse8 years v2_5_0commit 4e0a512972...naruse8 years v2_3_6commit 2551734626...usa8 years v2_2_9commit 72113d58cd...usa8 years v2_4_3commit a5ec07c73f...nagachika8 years v2_5_0_rc1commit c6f401b6b8...naruse8 years v2_5_0_preview1commit f11fbd79fd...naruse9 years v2_2_8commit f3c3c788aa...usa9 years v2_3_5commit e07613e27e...usa9 years v2_4_2commit 595af866bb...nagachika9 years v2_3_4commit 4bd69735af...nagachika9 years v2_2_7commit 530165c294...usa9 years v2_4_1commit 820605ba3c...naruse9 years v2_4_0commit d4bb726b71...naruse9 years v2_4_0_rc1commit 55b2febff0...naruse9 years v2_3_3commit c91cb76f8d...nagachika10 years v2_2_6commit 1c091e3480...usa10 years v2_3_2commit 9d222264d5...nagachika10 years v2_4_0_preview3commit 81234c5eca...naruse10 years v2_4_0_preview2commit e11c22602a...naruse10 years v2_4_0_preview1commit 8183c05322...naruse10 years v2_2_5commit 449169fd8c...usa10 years v2_3_1commit 5827d8e887...nagachika10 years v2_1_10commit 410b031acb...usa10 years v2_1_9commit 22b2eface0...usa10 years v2_3_0commit d40ea2afa6...naruse10 years v2_0_0_648commit 03ec9ed5c9...usa10 years v2_1_8commit 4876b9a68c...usa10 years v2_2_4commit 9081c2c61a...nagachika10 years v2_3_0_preview2commit e3434401ac...naruse10 years v2_3_0_preview1commit 9993701c7d...naruse