summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module/extend_object_spec.rb
blob: e66b87efef1a43934df5b8a89ef849749c1229a6 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Module#extend_object" do
  before :each do
    ScratchPad.clear
  end

  it "is a private method" do
    Module.should have_private_instance_method(:extend_object)
  end

  describe "on Class" do
    it "is undefined" do
      Class.should_not have_private_instance_method(:extend_object, true)
    end

    it "raises a TypeError if calling after rebinded to Class" do
      -> {
        Module.instance_method(:extend_object).bind(Class.new).call Object.new
      }.should raise_error(TypeError)
    end
  end

  it "is called when #extend is called on an object" do
    ModuleSpecs::ExtendObject.should_receive(:extend_object)
    obj = mock("extended object")
    obj.extend ModuleSpecs::ExtendObject
  end

  it "extends the given object with its constants and methods by default" do
    obj = mock("extended direct")
    ModuleSpecs::ExtendObject.send :extend_object, obj

    obj.test_method.should == "hello test"
    obj.singleton_class.const_get(:C).should == :test
  end

  it "is called even when private" do
    obj = mock("extended private")
    obj.extend ModuleSpecs::ExtendObjectPrivate
    ScratchPad.recorded.should == :extended
  end

  ruby_version_is ''...'2.7' do
    it "does not copy own tainted status to the given object" do
      other = Object.new
      Module.new.taint.send :extend_object, other
      other.tainted?.should be_false
    end

    it "does not copy own untrusted status to the given object" do
      other = Object.new
      Module.new.untrust.send :extend_object, other
      other.untrusted?.should be_false
    end
  end

  describe "when given a frozen object" do
    before :each do
      @receiver = Module.new
      @object = Object.new.freeze
    end

    it "raises a RuntimeError before extending the object" do
      -> { @receiver.send(:extend_object, @object) }.should raise_error(RuntimeError)
      @object.should_not be_kind_of(@receiver)
    end
  end
end