delegate_matcher 0.3 → 0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +3 -3
  3. data/README.md +101 -8
  4. data/README.md.erb +216 -0
  5. data/Rakefile +6 -1
  6. data/delegate_matcher.gemspec +1 -1
  7. data/lib/delegate_matcher/delegate.rb +5 -12
  8. data/lib/delegate_matcher/delegate_matcher.rb +5 -14
  9. data/lib/delegate_matcher/delegation.rb +32 -23
  10. data/lib/delegate_matcher/dispatcher.rb +4 -1
  11. data/lib/delegate_matcher/expected.rb +31 -18
  12. data/lib/delegate_matcher/nil_delegate.rb +3 -2
  13. data/lib/delegate_matcher/stub_delegate.rb +7 -3
  14. data/lib/delegate_matcher/version.rb +1 -1
  15. data/spec/{lib → examples}/active_support_delegation_spec.rb +11 -2
  16. data/spec/{lib → examples}/forwardable_delegation_spec.rb +11 -2
  17. data/spec/shared/author.rb +23 -0
  18. data/spec/{lib/shared/a_simple_delegator.rb → shared/basic.rb} +4 -6
  19. data/spec/{lib/shared → shared}/nil_check.rb +15 -15
  20. data/spec/shared/post_delegation.rb +42 -0
  21. data/spec/shared/post_methods.rb +15 -0
  22. data/spec/spec_helper.rb +1 -1
  23. data/spec/to_a_class_method_spec.rb +37 -0
  24. data/spec/to_a_class_variable_spec.rb +38 -0
  25. data/spec/to_a_constant_spec.rb +37 -0
  26. data/spec/to_an_instance_method_spec.rb +39 -0
  27. data/spec/to_an_instance_variable_spec.rb +35 -0
  28. data/spec/to_an_object_spec.rb +45 -0
  29. data/spec/to_multiple_targets_spec.rb +34 -0
  30. data/spec/with_args_spec.rb +151 -0
  31. data/spec/with_as_spec.rb +25 -0
  32. data/spec/with_block_spec.rb +75 -0
  33. data/spec/with_prefix_spec.rb +37 -0
  34. data/spec/with_return_value_spec.rb +46 -0
  35. data/spec/with_to_spec.rb +53 -0
  36. metadata +45 -43
  37. data/.bundle/config +0 -3
  38. data/spec/lib/aggregate_delegate_matcher_spec.rb +0 -62
  39. data/spec/lib/class_method_spec.rb +0 -84
  40. data/spec/lib/class_variable_spec.rb +0 -85
  41. data/spec/lib/constant_spec.rb +0 -86
  42. data/spec/lib/delegate_spec.rb +0 -15
  43. data/spec/lib/instance_method_spec.rb +0 -84
  44. data/spec/lib/instance_variable_spec.rb +0 -102
  45. data/spec/lib/object_spec.rb +0 -103
  46. data/spec/lib/shared/args.rb +0 -24
  47. data/spec/lib/shared/args_and_a_block.rb +0 -6
  48. data/spec/lib/shared/author.rb +0 -10
  49. data/spec/lib/shared/block.rb +0 -71
  50. data/spec/lib/shared/different_method_name.rb +0 -12
  51. data/spec/lib/shared/different_return_value.rb +0 -19
  52. data/spec/lib/shared/prefix.rb +0 -16
@@ -4,6 +4,7 @@ require 'proc_extensions'
4
4
  module RSpec
5
5
  module Matchers
6
6
  module DelegateMatcher
7
+ # rubocop:disable Metrics/ClassLength
7
8
  class Delegation
8
9
  attr_accessor :expected
9
10
  attr_accessor :dispatcher
@@ -12,12 +13,12 @@ module RSpec
12
13
  def initialize(expected)
13
14
  self.expected = expected
14
15
  self.dispatcher = DelegateMatcher::Dispatcher.new(expected)
15
- self.delegate = StubDelegate.new(expected)
16
+ self.delegate = expected.to.map { |to| StubDelegate.new(expected, to) }
16
17
  end
17
18
 
18
19
  def delegation_ok?
19
20
  dispatcher.call
20
- delegate.received
21
+ delegate.all?(&:received)
21
22
  end
22
23
 
23
24
  def ok?
@@ -28,7 +29,7 @@ module RSpec
28
29
  return true if expected.allow_nil.nil?
29
30
 
30
31
  begin
31
- NilDelegate.new(expected) { dispatcher.call }
32
+ expected.to.each { |to| NilDelegate.new(expected, to) { dispatcher.call } }
32
33
  allow_nil = true
33
34
  rescue NoMethodError
34
35
  allow_nil = false
@@ -38,7 +39,8 @@ module RSpec
38
39
  end
39
40
 
40
41
  def arguments_ok?
41
- expected.as_args.nil? || delegate.args.eql?(expected.as_args)
42
+ args_matcher = Mocks::ArgumentListMatcher.new(*expected.as_args)
43
+ delegate.all? { |delegate| args_matcher.args_match?(*delegate.args) }
42
44
  end
43
45
 
44
46
  # rubocop:disable Metrics/AbcSize
@@ -47,22 +49,27 @@ module RSpec
47
49
  when expected.block.nil?
48
50
  true
49
51
  when expected.block == false
50
- delegate.block.nil?
52
+ delegate.all? { |d| d.block.nil? }
51
53
  when expected.block == true
52
- delegate.block == dispatcher.block
54
+ delegate.all? { |d| d.block == dispatcher.block }
53
55
  else
54
- delegate.block_source == expected.block_source
56
+ delegate.all? { |d| d.block_source == expected.block_source }
55
57
  end
56
58
  end
57
59
 
58
60
  def return_value_ok?
59
- !expected.check_return || dispatcher.return_value == delegate.return_value
61
+ case
62
+ when !expected.check_return then true
63
+ when expected.return_value.nil? then dispatcher.return_value == delegate_return_value
64
+ else dispatcher.return_value == expected.return_value
65
+ end
60
66
  end
61
67
 
62
- def failure_message(negated)
63
- # TODO: Should include the line below
64
- # return nil if negated == delegate.received
68
+ def delegate_return_value
69
+ delegate.length == 1 ? delegate[0].return_value : delegate.map(&:return_value)
70
+ end
65
71
 
72
+ def failure_message(negated)
66
73
  message = [
67
74
  argument_failure_message(negated),
68
75
  block_failure_message(negated),
@@ -77,29 +84,34 @@ module RSpec
77
84
  when expected.as_args.nil? || negated ^ arguments_ok?
78
85
  ''
79
86
  else
80
- "was called with #{delegate.argument_description}"
87
+ "was called with #{delegate[0].argument_description}"
81
88
  end
82
89
  end
83
90
 
84
91
  def block_failure_message(negated)
92
+ proc_source = ProcSource.new(delegate[0].block)
85
93
  case
86
- when expected.block.nil? || (negated ^ block_ok?)
94
+ when expected.block.nil? || negated ^ block_ok?
87
95
  ''
88
96
  when negated
89
97
  "a block was #{expected.block ? '' : 'not '}passed"
90
98
  when expected.block
91
- delegate.block.nil? ? 'a block was not passed' : "a different block '#{ProcSource.new(delegate.block)}' was passed"
99
+ delegate.all? { |d| d.block.nil? } ? 'a block was not passed' : "a different block '#{proc_source}' was passed"
92
100
  else
93
- 'a block was passed'
101
+ %(a block #{proc_source} was passed)
94
102
  end
95
103
  end
96
104
 
97
- def return_value_failure_message(_negated)
105
+ def return_value_failure_message(negated)
98
106
  case
99
- when !delegate.received || return_value_ok?
107
+ when !delegate.any?(&:received) || negated ^ return_value_ok?
108
+ ''
109
+ when negated
100
110
  ''
111
+ when !expected.return_value.nil?
112
+ "a value of \"#{dispatcher.return_value}\" was returned instead of \"#{expected.return_value}\""
101
113
  else
102
- format('a return value of %p was returned instead of the delegate return value', dispatcher.return_value)
114
+ "a value of \"#{dispatcher.return_value}\" was returned instead of \"#{delegate_return_value}\""
103
115
  end
104
116
  end
105
117
 
@@ -108,15 +120,12 @@ module RSpec
108
120
  when expected.allow_nil.nil? || negated ^ allow_nil_ok?
109
121
  ''
110
122
  when negated
111
- "#{expected.to} was #{expected.allow_nil ? '' : 'not '}allowed to be nil"
123
+ %("#{expected.to_description}" was #{expected.allow_nil ? '' : 'not '}allowed to be nil)
112
124
  else
113
- "#{expected.to} was #{expected.allow_nil ? 'not ' : ''}allowed to be nil"
125
+ %("#{expected.to_description}" was #{expected.allow_nil ? 'not ' : ''}allowed to be nil)
114
126
  end
115
127
  end
116
128
  end
117
129
  end
118
130
  end
119
131
  end
120
-
121
- # TODO: use default prefix with constants - lower case method prefix
122
- # TODO: How to handle delegation is delegate_double is called with something else
@@ -9,7 +9,10 @@ module RSpec
9
9
  end
10
10
 
11
11
  def call
12
- self.return_value = expected.subject.send(expected.delegator_method_name, *expected.args, &block)
12
+ args = expected.args
13
+ args = [] if args && args[0].is_a?(Mocks::ArgumentMatchers::NoArgsMatcher)
14
+
15
+ self.return_value = expected.subject.send(expected.delegator_method_name, *args, &block)
13
16
  end
14
17
 
15
18
  def block
@@ -4,11 +4,12 @@ module RSpec
4
4
  class Expected
5
5
  include Block
6
6
 
7
- attr_accessor :subject, :to, :method_name, :as, :allow_nil, :check_return
7
+ attr_accessor :subject, :to, :method_name, :as, :allow_nil, :check_return, :return_value
8
8
  attr_reader :args
9
9
 
10
10
  def initialize
11
11
  self.check_return = true
12
+ # @args = [Mocks::ArgumentMatchers::NoArgsMatcher::INSTANCE]
12
13
  end
13
14
 
14
15
  def prefix=(prefix)
@@ -22,8 +23,8 @@ module RSpec
22
23
  ''
23
24
  when @prefix
24
25
  "#{@prefix}_"
25
- when to.is_a?(String) || to.is_a?(Symbol)
26
- to.to_s.delete('@').downcase + '_'
26
+ when to[0].is_a?(String) || to[0].is_a?(Symbol)
27
+ to[0].to_s.delete('@').downcase + '_'
27
28
  else
28
29
  ''
29
30
  end
@@ -50,6 +51,10 @@ module RSpec
50
51
  "delegate #{delegator_description} to #{delegate_description}#{options_description}"
51
52
  end
52
53
 
54
+ def to_description
55
+ to.join(',')
56
+ end
57
+
53
58
  private
54
59
 
55
60
  def delegator_description
@@ -57,22 +62,23 @@ module RSpec
57
62
  end
58
63
 
59
64
  def delegate_description
60
- case
61
- when !args.eql?(as_args)
62
- "#{to}.#{as}#{as_argument_description}"
63
- when as.to_s.eql?(delegator_method_name)
64
- "#{to}"
65
- else
66
- "#{to}.#{as}"
67
- end
68
- end
69
-
70
- def as_argument_description
71
- argument_description(as_args)
65
+ %("#{to_description}") +
66
+ case
67
+ when !args.eql?(as_args)
68
+ ".#{as}#{argument_description(as_args)}"
69
+ when as.to_s.eql?(delegator_method_name)
70
+ ''
71
+ else
72
+ ".#{as}"
73
+ end
72
74
  end
73
75
 
74
76
  def argument_description(arguments = args)
75
- arguments ? "(#{arguments.map { |a| format('%p', a) }.join(', ')})" : ''
77
+ if arguments
78
+ "(#{arguments.map { |a| a.respond_to?(:description) ? a.description : a.inspect }.join(', ')})"
79
+ else
80
+ ''
81
+ end
76
82
  end
77
83
 
78
84
  def options_description
@@ -99,12 +105,19 @@ module RSpec
99
105
  when block == false
100
106
  ' without a block'
101
107
  else
102
- " with block '#{block_source}'"
108
+ " with block \"#{block_source}\""
103
109
  end
104
110
  end
105
111
 
106
112
  def return_value_description
107
- check_return ? '' : ' without using delegate return value'
113
+ case
114
+ when return_value
115
+ " and return \"#{return_value}\""
116
+ when !check_return
117
+ ' without using delegate return value'
118
+ else
119
+ ''
120
+ end
108
121
  end
109
122
  end
110
123
  end
@@ -2,7 +2,7 @@ module RSpec
2
2
  module Matchers
3
3
  module DelegateMatcher
4
4
  class NilDelegate < Delegate
5
- def initialize(expected, &block)
5
+ def initialize(expected, to, &block)
6
6
  super
7
7
  original_receiver = receiver
8
8
  self.receiver = nil
@@ -24,7 +24,7 @@ module RSpec
24
24
  silence_warnings { subject.class.const_set(name, value) }
25
25
  when a_method?
26
26
  allow(subject).to receive(name) { value }
27
- else # is an object
27
+ else # an object
28
28
  fail 'cannot verify "allow_nil" expectations when delegating to an object' if value.nil?
29
29
  end
30
30
  end
@@ -33,6 +33,7 @@ module RSpec
33
33
  warn_level = $VERBOSE
34
34
  $VERBOSE = nil
35
35
  block.call
36
+ ensure
36
37
  $VERBOSE = warn_level
37
38
  end
38
39
  end
@@ -4,14 +4,18 @@ module RSpec
4
4
  class StubDelegate < Delegate
5
5
  RSpec::Mocks::Syntax.enable_expect(self)
6
6
 
7
- def initialize(expected)
8
- self.expected = expected
9
- self.received = false
7
+ attr_reader :return_value
8
+
9
+ def initialize(expected, to)
10
+ super
11
+ self.return_value = receiver.send(expected.as, *expected.args)
10
12
  stub_receiver
11
13
  end
12
14
 
13
15
  private
14
16
 
17
+ attr_writer :return_value
18
+
15
19
  def stub_receiver
16
20
  allow(receiver).to receive(expected.as) do |*args, &block|
17
21
  self.args = args
@@ -1,3 +1,3 @@
1
1
  module DelegateMatcher
2
- VERSION ||= '0.3'.freeze
2
+ VERSION ||= '0.4'.freeze
3
3
  end
@@ -20,11 +20,19 @@ module ActiveSupportDelegation
20
20
  def initialize
21
21
  @author = Author.new
22
22
  end
23
+
24
+ def inspect
25
+ 'post'
26
+ end
23
27
  end
24
28
 
25
29
  class Author
26
- def name
27
- @name ||= 'Catherine Asaro'
30
+ def initialize
31
+ @name = 'Catherine Asaro'
32
+ end
33
+
34
+ def name(*)
35
+ @name
28
36
  end
29
37
  end
30
38
 
@@ -41,6 +49,7 @@ module ActiveSupportDelegation
41
49
  it { should delegate(:name).to(:author).with_prefix(:writer) }
42
50
  it { should delegate(:name).to(:author).with_block }
43
51
  it { should delegate(:name).to(:author).with('Ms.') }
52
+ it { should delegate(:name).to(:author).with('Ms.').with_block }
44
53
 
45
54
  it { should delegate(:name).to(:class).with_prefix }
46
55
  it { should delegate(:count).to(:@@authors) }
@@ -15,11 +15,19 @@ module ForwardableDelegation
15
15
  def_delegator :author, :name, :writer
16
16
 
17
17
  def_delegator :'author.name', :length, :name_length
18
+
19
+ def inspect
20
+ 'post'
21
+ end
18
22
  end
19
23
 
20
24
  class Author
21
- def name
22
- @name ||= 'Catherine Asaro'
25
+ def initialize
26
+ @name = 'Catherine Asaro'
27
+ end
28
+
29
+ def name(*)
30
+ @name
23
31
  end
24
32
  end
25
33
 
@@ -33,6 +41,7 @@ module ForwardableDelegation
33
41
  it { should delegate(:name).to(:author) }
34
42
  it { should delegate(:name).to(:author).with('Ms.') }
35
43
  it { should delegate(:name).to(:author).with_block }
44
+ it { should delegate(:name).to(:author).with('Ms.').with_block }
36
45
  it { should delegate(:name).to(:author).with_prefix }
37
46
  it { should delegate(:writer).to(:author).as(:name) }
38
47
 
@@ -0,0 +1,23 @@
1
+ module RSpec
2
+ module Matchers
3
+ module DelegateMatcher
4
+ class Author
5
+ def initialize(name = 'Catherine Asaro')
6
+ @name = name
7
+ end
8
+
9
+ def other_name
10
+ 'Other Name'
11
+ end
12
+
13
+ def name(*args, &_)
14
+ "#{args.join}#{@name}"
15
+ end
16
+
17
+ def to_s
18
+ "Author: #{@name}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,17 +1,15 @@
1
- shared_examples 'a simple delegator' do
1
+ shared_examples 'a basic delegator' do
2
2
  it { should delegate(:name).to(receiver) }
3
3
  it { should delegate(:name.to_s).to(receiver) }
4
4
 
5
5
  it { should_not delegate(:to_s).to(receiver) }
6
6
 
7
- include_examples 'a delegator without a block'
8
-
9
7
  describe 'description' do
10
8
  let(:matcher) { delegate(:name).to(receiver) }
11
9
  before { matcher.matches? subject }
12
10
 
13
- it { expect(matcher.description).to eq "delegate name to #{receiver}" }
14
- it { expect(matcher.failure_message).to match(/expected .* to delegate name to #{receiver}/) }
15
- it { expect(matcher.failure_message_when_negated).to match(/expected .* not to delegate name to #{receiver}/) }
11
+ it { expect(matcher.description).to eq %(delegate name to "#{receiver}") }
12
+ it { expect(matcher.failure_message).to match(/expected .* to delegate name to "#{receiver}"/) }
13
+ it { expect(matcher.failure_message_when_negated).to match(/expected .* not to delegate name to "#{receiver}"/) }
16
14
  end
17
15
  end
@@ -1,27 +1,27 @@
1
1
  shared_examples 'a delegator with a nil check' do
2
- it { should delegate(:name).to(receiver).allow_nil }
3
- it { should delegate(:name).to(receiver).allow_nil(true) }
4
- it { should_not delegate(:name).to(receiver).allow_nil(false) }
2
+ it { should delegate(:name_allow_nil).to(receiver).as(:name).allow_nil }
3
+ it { should delegate(:name_allow_nil).to(receiver).as(:name).allow_nil(true) }
4
+ it { should_not delegate(:name_allow_nil).to(receiver).as(:name).allow_nil(false) }
5
5
 
6
6
  describe 'description' do
7
7
  before { matcher.matches? subject }
8
8
 
9
9
  context 'with allow nil at default' do
10
- let(:matcher) { delegate(:name).to(receiver).allow_nil }
11
- it { expect(matcher.description).to eq %(delegate name to #{receiver} with nil allowed) }
12
- it { expect(matcher.failure_message_when_negated).to match(/#{receiver} was allowed to be nil/) }
10
+ let(:matcher) { delegate(:name_allow_nil).to(receiver).as(:name).allow_nil }
11
+ it { expect(matcher.description).to match(/with nil allowed/) }
12
+ it { expect(matcher.failure_message_when_negated).to match(/ was allowed to be nil/) }
13
13
  end
14
14
 
15
15
  context 'with allow nil true' do
16
- let(:matcher) { delegate(:name).to(receiver).allow_nil(true) }
17
- it { expect(matcher.description).to eq %(delegate name to #{receiver} with nil allowed) }
18
- it { expect(matcher.failure_message_when_negated).to match(/#{receiver} was allowed to be nil/) }
16
+ let(:matcher) { delegate(:name_allow_nil).to(receiver).as(:name).allow_nil(true) }
17
+ it { expect(matcher.description).to match(/with nil allowed/) }
18
+ it { expect(matcher.failure_message_when_negated).to match(/ was allowed to be nil/) }
19
19
  end
20
20
 
21
21
  context 'with allow nil false' do
22
- let(:matcher) { delegate(:name).to(receiver).allow_nil(false) }
23
- it { expect(matcher.description).to eq %(delegate name to #{receiver} with nil not allowed) }
24
- it { expect(matcher.failure_message).to match(/#{receiver} was allowed to be nil/) }
22
+ let(:matcher) { delegate(:name_allow_nil).to(receiver).as(:name).allow_nil(false) }
23
+ it { expect(matcher.description).to match(/ with nil not allowed/) }
24
+ it { expect(matcher.failure_message).to match(/ was allowed to be nil/) }
25
25
  end
26
26
  end
27
27
  end
@@ -36,17 +36,17 @@ shared_examples 'a delegator without a nil check' do
36
36
 
37
37
  context 'with allow nil at default' do
38
38
  let(:matcher) { delegate(:name).to(receiver).allow_nil }
39
- it { expect(matcher.failure_message).to match(/#{receiver} was not allowed to be nil/) }
39
+ it { expect(matcher.failure_message).to match(/"#{receiver}" was not allowed to be nil/) }
40
40
  end
41
41
 
42
42
  context 'with allow nil true' do
43
43
  let(:matcher) { delegate(:name).to(receiver).allow_nil(true) }
44
- it { expect(matcher.failure_message).to match(/#{receiver} was not allowed to be nil/) }
44
+ it { expect(matcher.failure_message).to match(/"#{receiver}" was not allowed to be nil/) }
45
45
  end
46
46
 
47
47
  context 'with allow nil false' do
48
48
  let(:matcher) { delegate(:name).to(receiver).allow_nil(false) }
49
- it { expect(matcher.failure_message_when_negated).to match(/#{receiver} was not allowed to be nil/) }
49
+ it { expect(matcher.failure_message_when_negated).to match(/"#{receiver}" was not allowed to be nil/) }
50
50
  end
51
51
  end
52
52
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ shared_context 'Post delegation' do
7
+ subject { klass.new }
8
+
9
+ let(:klass) do
10
+ Class.new do
11
+ include PostMethods
12
+
13
+ def initialize
14
+ @author = Author.new
15
+ end
16
+
17
+ def name
18
+ @author.name
19
+ end
20
+ end
21
+ end
22
+
23
+ before do
24
+ klass.class_eval method_definition
25
+ matcher.matches? subject
26
+ end
27
+
28
+ def method_definition
29
+ klass = self.class
30
+ while klass != Object
31
+ return klass.description if klass.respond_to?(:description) && klass.description =~ /def \w*name/
32
+ klass = klass.parent
33
+ end
34
+
35
+ '# Could not find "def \w*name..." method definition'
36
+ end
37
+
38
+ let(:matcher) { delegate(:name).to(:@author) }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ module RSpec
2
+ module Matchers
3
+ module DelegateMatcher
4
+ module PostMethods
5
+ def to_s
6
+ 'post'
7
+ end
8
+
9
+ def inspect
10
+ 'post'
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
data/spec/spec_helper.rb CHANGED
@@ -15,7 +15,7 @@ Coveralls.wear! if Coveralls.will_run?
15
15
 
16
16
  require 'delegate_matcher'
17
17
 
18
- Dir[File.dirname(__FILE__) + '/lib/shared/*.rb'].each { |f| require f }
18
+ Dir[File.dirname(__FILE__) + '/shared/*.rb'].each { |f| require f }
19
19
 
20
20
  RSpec.configure do |config|
21
21
  config.color = true
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ describe 'class delegation' do
7
+ let(:klass) do
8
+ Class.new do
9
+ include PostMethods
10
+
11
+ class << self
12
+ def author
13
+ @author ||= Author.new
14
+ end
15
+
16
+ def name
17
+ author.name
18
+ end
19
+
20
+ def name_allow_nil
21
+ author.name if author
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ subject { klass }
28
+
29
+ let(:receiver) { :author }
30
+
31
+ it_behaves_like 'a basic delegator'
32
+ it_behaves_like 'a delegator without a nil check'
33
+ it_behaves_like 'a delegator with a nil check'
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ describe 'delegation to a class variable' do
7
+ # Note that defining Post as an anonymous class caused the class variable @@author to not be available,
8
+ # so we create an explicit Post class and remove it after all specs are run
9
+ before(:all) do
10
+ class Post
11
+ include PostMethods
12
+
13
+ # rubocop:disable Style/ClassVars
14
+ @@author = Author.new
15
+
16
+ def name
17
+ @@author.name
18
+ end
19
+
20
+ def name_allow_nil
21
+ @@author.name if @@author
22
+ end
23
+ end
24
+ end
25
+
26
+ subject { Post.new }
27
+
28
+ let(:receiver) { :@@author }
29
+
30
+ it_behaves_like 'a basic delegator'
31
+ it_behaves_like 'a delegator without a nil check'
32
+ it_behaves_like 'a delegator with a nil check'
33
+
34
+ after(:all) { DelegateMatcher.module_eval { remove_const :Post } }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ describe 'class delegation' do
7
+ # Note that defining Post as an anonymous class caused the constant AUTHOR to not be available,
8
+ # so we create an explicit Post class and remove it after all specs are run
9
+ before(:all) do
10
+ class Post
11
+ include PostMethods
12
+
13
+ AUTHOR = Author.new
14
+
15
+ def name
16
+ AUTHOR.name
17
+ end
18
+
19
+ def name_allow_nil
20
+ AUTHOR.name if AUTHOR
21
+ end
22
+ end
23
+ end
24
+
25
+ subject { Post.new }
26
+
27
+ let(:receiver) { :AUTHOR }
28
+
29
+ it_behaves_like 'a basic delegator'
30
+ it_behaves_like 'a delegator without a nil check'
31
+ it_behaves_like 'a delegator with a nil check'
32
+
33
+ after(:all) { DelegateMatcher.module_eval { remove_const :Post } }
34
+ end
35
+ end
36
+ end
37
+ end