File: decoration_spec.rb

package info (click to toggle)
ruby-tool 0.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 148 kB
  • ctags: 37
  • sloc: ruby: 283; makefile: 3
file content (57 lines) | stat: -rw-r--r-- 1,488 bytes parent folder | download
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
require 'tool/decoration'

describe Tool::Decoration do
  shared_examples :decorate do
    specify "with block" do
      method = nil
      subject.decorate(-> { 42 }) { |m| method = m }
      expect(method).not_to be_nil
      expect(subject.new.send(method)).to be == 42
    end

    specify "without block" do
      method = nil
      subject.decorate { |m| method = m }
      expect(method).to be_nil
      subject.send(:define_method, :foo) { }
      expect(method).to be == :foo
    end

    specify "multiple decorations" do
      calls = []
      subject.decorate { |m| calls << :a }
      subject.decorate { |m| calls << :b }
      expect(calls).to be_empty
      subject.send(:define_method, :foo) { }
      expect(calls).to be == [:a, :b]
    end

    specify "multiple methods" do
      calls = []
      subject.decorate { |m| calls << :a }
      subject.send(:define_method, :foo) { }
      subject.send(:define_method, :bar) { }
      expect(calls).to be == [:a]
    end
  end

  context "extending a class" do
    subject { Class.new.extend(Tool::Decoration) }
    include_examples(:decorate)
  end

  context "including in a module" do
    subject { Class.new.extend(Module.new { include Tool::Decoration }) }
    include_examples(:decorate)
  end

  context "including in a module" do
    subject do
      Class.new(Module) do
        def new(*) Object.new.extend(self) end
        include Tool::Decoration
      end.new
    end
    include_examples(:decorate)
  end
end