File: mimic_spec.rb

package info (click to toggle)
ruby-naught 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: ruby: 1,091; makefile: 6
file content (123 lines) | stat: -rw-r--r-- 3,018 bytes parent folder | download | duplicates (2)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require 'spec_helper'
require 'logger'

describe 'null object mimicking a class' do
  class User
    attr_reader :login
  end

  module Authorizable
    def authorized_for?(_); end
  end

  class LibraryPatron < User
    include Authorizable
    attr_reader :name

    def member?; end

    def notify_of_overdue_books(_); end
  end

  subject(:null) { mimic_class.new }
  let(:mimic_class) do
    Naught.build do |b|
      b.mimic LibraryPatron
    end
  end
  it 'responds to all methods defined on the target class' do
    expect(null.member?).to be_nil
    expect(null.name).to be_nil
    expect(null.notify_of_overdue_books(['The Grapes of Wrath'])).to be_nil
  end

  it 'does not respond to methods not defined on the target class' do
    expect { null.foobar }.to raise_error(NoMethodError)
  end

  it 'reports which messages it does and does not respond to' do
    expect(null).to respond_to(:member?)
    expect(null).to respond_to(:name)
    expect(null).to respond_to(:notify_of_overdue_books)
    expect(null).not_to respond_to(:foobar)
  end
  it 'has an informative inspect string' do
    expect(null.inspect).to eq('<null:LibraryPatron>')
  end

  it 'excludes Object methods from being mimicked' do
    expect(null.object_id).not_to be_nil
    expect(null.hash).not_to be_nil
  end

  it 'includes inherited methods' do
    expect(null.authorized_for?('something')).to be_nil
    expect(null.login).to be_nil
  end

  describe 'with include_super: false' do
    let(:mimic_class) do
      Naught.build do |b|
        b.mimic LibraryPatron, :include_super => false
      end
    end

    it 'excludes inherited methods' do
      expect(null).to_not respond_to(:authorized_for?)
      expect(null).to_not respond_to(:login)
    end
  end

  describe 'with an instance as example' do
    let(:mimic_class) do
      milton = LibraryPatron.new
      def milton.stapler; end
      Naught.build do |b|
        b.mimic :example => milton
      end
    end

    it 'responds to method defined only on the example instance' do
      expect(null).to respond_to(:stapler)
    end

    it 'responds to method defined on the class of the instance' do
      expect(null).to respond_to(:member?)
    end
  end
end

describe 'using mimic with black_hole' do
  subject(:null) { mimic_class.new }
  let(:mimic_class) do
    Naught.build do |b|
      b.mimic Logger
      b.black_hole
    end
  end

  shared_examples_for 'a black hole mimic' do
    it 'returns self from mimicked methods' do
      expect(null.info).to equal(null)
      expect(null.error).to equal(null)
      expect(null << 'test').to equal(null)
    end

    it 'does not respond to methods not defined on the target class' do
      expect { null.foobar }.to raise_error(NoMethodError)
    end
  end

  it_should_behave_like 'a black hole mimic'

  describe '(reverse order)' do
    let(:mimic_class) do
      Naught.build do |b|
        b.black_hole
        b.mimic Logger
      end
    end

    it_should_behave_like 'a black hole mimic'
  end
end