File: bind_call_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (58 lines) | stat: -rw-r--r-- 2,410 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "UnboundMethod#bind_call" do
  before :each do
    @normal_um = UnboundMethodSpecs::Methods.new.method(:foo).unbind
    @parent_um = UnboundMethodSpecs::Parent.new.method(:foo).unbind
    @child1_um = UnboundMethodSpecs::Child1.new.method(:foo).unbind
    @child2_um = UnboundMethodSpecs::Child2.new.method(:foo).unbind
    @normal_um_super = UnboundMethodSpecs::Mod.instance_method(:foo_super)
    @parent_um_super = UnboundMethodSpecs::Parent.new.method(:foo_super).unbind
  end

  it "raises TypeError if object is not kind_of? the Module the method defined in" do
    -> { @normal_um.bind_call(UnboundMethodSpecs::B.new) }.should raise_error(TypeError)
  end

  it "binds and calls the method if object is kind_of the Module the method defined in" do
    @normal_um.bind_call(UnboundMethodSpecs::Methods.new).should == true
  end

  it "binds and calls the method on any object when UnboundMethod is unbound from a module" do
    UnboundMethodSpecs::Mod.instance_method(:from_mod).bind_call(Object.new).should == nil
  end

  it "binds and calls the method for any object kind_of? the Module the method is defined in" do
    @parent_um.bind_call(UnboundMethodSpecs::Child1.new).should == nil
    @child1_um.bind_call(UnboundMethodSpecs::Parent.new).should == nil
    @child2_um.bind_call(UnboundMethodSpecs::Child1.new).should == nil
  end

  it "binds and calls a Kernel method retrieved from Object on BasicObject" do
    Object.instance_method(:instance_of?).bind_call(BasicObject.new, BasicObject).should == true
  end

  it "binds and calls a Parent's class method to any Child's class methods" do
    um = UnboundMethodSpecs::Parent.method(:class_method).unbind
    um.bind_call(UnboundMethodSpecs::Child1).should == "I am UnboundMethodSpecs::Child1"
  end

  it "will raise when binding a an object singleton's method to another object" do
    other = UnboundMethodSpecs::Parent.new
    p = UnboundMethodSpecs::Parent.new
    class << p
      def singleton_method
        :single
      end
    end
    um = p.method(:singleton_method).unbind
    ->{ um.bind_call(other) }.should raise_error(TypeError)
  end

  it "allows calling super for module methods bound to hierarchies that do not already have that module" do
    p = UnboundMethodSpecs::Parent.new

    @normal_um_super.bind_call(p).should == true
  end
end