summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module/used_refinements_spec.rb
blob: 40dd4a444e1a8b122841549862135c7c8a6d4fff (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require_relative '../../spec_helper'

describe "Module.used_refinements" do
  it "returns list of all refinements imported in the current scope" do
    refinement_int = nil
    refinement_str = nil
    ScratchPad.record []

    m1 = Module.new do
      refine Integer do
        refinement_int = self
      end
    end

    m2 = Module.new do
      refine String do
        refinement_str = self
      end
    end

    Module.new do
      using m1
      using m2

      Module.used_refinements.each { |r| ScratchPad << r }
    end

    ScratchPad.recorded.sort_by(&:object_id).should == [refinement_int, refinement_str].sort_by(&:object_id)
  end

  it "returns empty array if does not have any refinements imported" do
    used_refinements = nil

    Module.new do
      used_refinements = Module.used_refinements
    end

    used_refinements.should == []
  end

  it "ignores refinements imported in a module that is included into the current one" do
    used_refinements = nil

    m1 = Module.new do
      refine Integer do
        nil
      end
    end

    m2 = Module.new do
      using m1
    end

    Module.new do
      include m2

      used_refinements = Module.used_refinements
    end

    used_refinements.should == []
  end

  it "returns refinements even not defined directly in a module refinements are imported from" do
    used_refinements = nil
    ScratchPad.record []

    m1 = Module.new do
      refine Integer do
        ScratchPad << self
      end
    end

    m2 = Module.new do
      include m1
    end

    Module.new do
      using m2

      used_refinements = Module.used_refinements
    end

    used_refinements.should == ScratchPad.recorded
  end
end