delegate_matcher 0.0.3 → 0.1

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/.bundle/config +3 -0
  3. data/.gitignore +3 -1
  4. data/.rubocop.yml +8 -0
  5. data/.travis.yml +7 -2
  6. data/Gemfile.lock +90 -37
  7. data/Guardfile +4 -0
  8. data/README.md +106 -98
  9. data/Rakefile +3 -1
  10. data/delegate_matcher.gemspec +17 -8
  11. data/lib/delegate_matcher/delegate.rb +79 -0
  12. data/lib/delegate_matcher/delegate_matcher.rb +41 -266
  13. data/lib/delegate_matcher/delegation.rb +115 -0
  14. data/lib/delegate_matcher/dispatcher.rb +26 -0
  15. data/lib/delegate_matcher/expected.rb +108 -0
  16. data/lib/delegate_matcher/nil_delegate.rb +41 -0
  17. data/lib/delegate_matcher/stub_delegate.rb +26 -0
  18. data/lib/delegate_matcher/version.rb +1 -1
  19. data/lib/delegate_matcher.rb +9 -0
  20. data/spec/lib/active_support_delegation_spec.rb +24 -29
  21. data/spec/lib/aggregate_delegate_matcher_spec.rb +62 -0
  22. data/spec/lib/delegate_spec.rb +15 -0
  23. data/spec/lib/delegate_to_class_variable_spec.rb +85 -0
  24. data/spec/lib/delegate_to_constant_spec.rb +86 -0
  25. data/spec/lib/delegate_to_instance_variable_spec.rb +86 -0
  26. data/spec/lib/delegate_to_method_spec.rb +84 -0
  27. data/spec/lib/delegate_to_object_spec.rb +103 -0
  28. data/spec/lib/forwardable_delegation_spec.rb +14 -13
  29. data/spec/lib/shared/a_simple_delegator.rb +17 -0
  30. data/spec/lib/shared/args.rb +24 -0
  31. data/spec/lib/shared/args_and_a_block.rb +6 -0
  32. data/spec/lib/shared/author.rb +10 -0
  33. data/spec/lib/shared/block.rb +45 -0
  34. data/spec/lib/shared/different_method_name.rb +12 -0
  35. data/spec/lib/shared/different_return_value.rb +19 -0
  36. data/spec/lib/shared/nil_check.rb +52 -0
  37. data/spec/lib/shared/prefix.rb +16 -0
  38. data/spec/spec_helper.rb +6 -2
  39. metadata +85 -7
  40. data/spec/lib/delegate_matcher_spec.rb +0 -467
  41. data/spec/lib/version_spec.rb +0 -7
@@ -0,0 +1,26 @@
1
+ module RSpec
2
+ module Matchers
3
+ module DelegateMatcher
4
+ class StubDelegate < Delegate
5
+ RSpec::Mocks::Syntax.enable_expect(self)
6
+
7
+ def initialize(expected)
8
+ self.expected = expected
9
+ self.received = false
10
+ stub_receiver
11
+ end
12
+
13
+ private
14
+
15
+ def stub_receiver
16
+ allow(receiver).to receive(expected.as) do |*args, &block|
17
+ self.args = args
18
+ self.block = block
19
+ self.received = true
20
+ return_value
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module DelegateMatcher
2
- VERSION ||= '0.0.3'.freeze
2
+ VERSION ||= '0.1'.freeze
3
3
  end
@@ -1,2 +1,11 @@
1
+ require 'forwardable'
2
+
1
3
  require 'delegate_matcher/version'
4
+ require 'delegate_matcher/expected'
5
+ require 'delegate_matcher/dispatcher'
6
+ require 'delegate_matcher/delegate'
7
+ require 'delegate_matcher/nil_delegate'
8
+ require 'delegate_matcher/stub_delegate'
9
+ require 'delegate_matcher/delegation'
10
+
2
11
  require 'delegate_matcher/delegate_matcher'
@@ -2,52 +2,47 @@ require 'spec_helper'
2
2
  require 'active_support/core_ext/module/delegation'
3
3
 
4
4
  module ActiveSupportDelegation
5
+ # rubocop:disable Style/ClassVars
5
6
  class Post
6
7
  attr_accessor :author
7
8
 
8
- class_variable_set(:@@authors, ['Ann Rand', 'Catherine Asaro'])
9
- GENRES ||= ['Fiction', 'Science Fiction']
10
-
11
- delegate :name, to: :author
12
- delegate :name, to: :author, prefix: true
13
- delegate :name, to: :author, prefix: :writer
14
- delegate :name_with_nil_check, to: :author, allow_nil: true
15
- delegate :name_with_arg, to: :author
16
- delegate :name_with_block, to: :author
17
- delegate :count, to: :@@authors
18
- delegate :first, to: :GENRES
19
- delegate :name, to: :class, prefix: true
9
+ @@authors = ['Ann Rand', 'Catherine Asaro']
10
+ GENRES = ['Fiction', 'Science Fiction']
11
+
12
+ delegate :name, to: :author, allow_nil: true
13
+ delegate :name, to: :author, prefix: true
14
+ delegate :name, to: :author, prefix: :writer
15
+ delegate :name, to: :class, prefix: true
16
+ delegate :count, to: :@@authors
17
+ delegate :first, to: :GENRES
18
+
19
+ def initialize
20
+ @author = Author.new
21
+ end
20
22
  end
21
23
 
22
24
  class Author
23
25
  def name
24
26
  'Catherine Asaro'
25
27
  end
26
-
27
- def name_with_nil_check
28
- name
29
- end
30
-
31
- def name_with_arg(arg)
32
- "#{arg} #{name}"
33
- end
34
-
35
- def name_with_block(&block)
36
- "#{block.call} #{name}"
37
- end
38
28
  end
39
29
 
40
30
  describe Post do
41
- it { should delegate(:name).to(:author) }
31
+ let(:author) { subject.author }
32
+
33
+ it { expect(subject.name).to eq 'Catherine Asaro' }
34
+
35
+ it { should delegate(:name).to(author) }
42
36
  it { should delegate(:name).to(:@author) }
43
- it { should delegate(:name_with_nil_check).to(:author).allow_nil }
37
+ it { should delegate(:name).to(:author) }
38
+ it { should delegate(:name).to(:author).allow_nil }
44
39
  it { should delegate(:name).to(:author).with_prefix }
45
40
  it { should delegate(:name).to(:author).with_prefix(:writer) }
41
+ it { should delegate(:name).to(:author).with_block }
42
+ it { should delegate(:name).to(:author).with('Ms.') }
46
43
 
47
- it { should delegate(:name_with_arg).to(:author).with('Ms.') }
48
- it { should delegate(:name_with_block).to(:author).with_block }
44
+ it { should delegate(:name).to(:class).with_prefix }
49
45
  it { should delegate(:count).to(:@@authors) }
50
46
  it { should delegate(:first).to(:GENRES) }
51
- it { should delegate(:name).to(:class).with_prefix }
52
47
  end
53
48
  end
@@ -0,0 +1,62 @@
1
+ # noinspection RubyResolve
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'DelegateTo matcher' do
6
+ # :nocov:
7
+ let(:post) do
8
+ Class.new do
9
+ attr_accessor :authors, :any
10
+
11
+ def any_alive?
12
+ authors.any?(&:alive?)
13
+ end
14
+
15
+ def all_alive?
16
+ authors.all?(&:alive?)
17
+ end
18
+
19
+ def inspect
20
+ 'post'
21
+ end
22
+ end.new
23
+ end
24
+
25
+ let(:authors) do
26
+ klass = Class.new do
27
+ attr_accessor :alive
28
+
29
+ def name
30
+ 'Catherine Asaro'
31
+ end
32
+
33
+ def alive?
34
+ alive
35
+ end
36
+
37
+ def inspect
38
+ 'author'
39
+ end
40
+ end
41
+ [klass.new, klass.new]
42
+ end
43
+ # :nocov:
44
+
45
+ subject { post }
46
+ before { post.authors = authors }
47
+
48
+ # xdescribe 'to_any' do
49
+ # context('with no authors alive') { its(:any_alive?) { should be false } }
50
+ # context('with one author alive') { before { authors.first.alive = true }; its(:any_alive?) { should be true } }
51
+ #
52
+ # it { should delegate(:any_alive?).to_any(:authors) }
53
+ # end
54
+ #
55
+ # xdescribe 'to_all' do
56
+ # context('with no authors alive') { its(:all_alive?) { should be false } }
57
+ # context('with one author alive') { before { authors.first.alive = true }; its(:all_alive?) { should be false } }
58
+ # context('with all authors alive') { before { authors.each { |a| a.alive = true } }; its(:all_alive?) { should be true } }
59
+ #
60
+ # it { should delegate(:all_alive?).to_all(authors) }
61
+ # end
62
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ describe 'errors' do
7
+ it 'should require a "to"' do
8
+ expect { should delegate(:name) }.to raise_error do |error|
9
+ expect(error.message).to match(/need to provide a "to"/)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ module ToClassVariable
7
+ describe 'delegation to a class variable' do
8
+ class Post
9
+ # rubocop:disable Style/ClassVars
10
+ @@author = Author.new
11
+ end
12
+
13
+ subject { Post.new }
14
+
15
+ [:@@author, '@@author'].each do |class_variable|
16
+ let(:receiver) { class_variable }
17
+
18
+ it_behaves_like 'a simple delegator' do
19
+ before do
20
+ class Post
21
+ def name
22
+ @@author.name
23
+ end
24
+ end
25
+ end
26
+ include_examples 'a delegator without a nil check'
27
+ end
28
+
29
+ it_behaves_like 'a delegator with a nil check' do
30
+ before do
31
+ class Post
32
+ def name
33
+ @@author.name if @@author
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ it_behaves_like 'a delegator with args and a block', :arg1 do
40
+ before do
41
+ class Post
42
+ def name(*args, &block)
43
+ @@author.name(*args, &block)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ it_behaves_like 'a delegator with a different method name', :other_name do
50
+ before do
51
+ class Post
52
+ def name
53
+ @@author.other_name
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ it_behaves_like 'a delegator with a prefix', 'author' do
60
+ before do
61
+ class Post
62
+ def author_name
63
+ @@author.name
64
+ end
65
+ end
66
+ end
67
+ it { should delegate(:name).to(:@@author).with_prefix }
68
+ end
69
+
70
+ it_behaves_like 'a delegator with a different return value', 'Ann Rand' do
71
+ before do
72
+ class Post
73
+ def name
74
+ @@author.name
75
+ 'Ann Rand'
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ module ToConstant
7
+ describe 'delegation to a constant' do
8
+ class Post
9
+ AUTHOR = Author.new
10
+ end
11
+
12
+ subject { Post.new }
13
+
14
+ let(:receiver) { :AUTHOR }
15
+
16
+ [:AUTHOR, 'AUTHOR'].each do |constant|
17
+ let(:receiver) { constant }
18
+
19
+ it_behaves_like 'a simple delegator' do
20
+ before do
21
+ class Post
22
+ def name
23
+ AUTHOR.name
24
+ end
25
+ end
26
+ end
27
+ include_examples 'a delegator without a nil check'
28
+ end
29
+
30
+ it_behaves_like 'a delegator with a nil check' do
31
+ before do
32
+ class Post
33
+ def name
34
+ AUTHOR.name if AUTHOR
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ it_behaves_like 'a delegator with args and a block', :arg1 do
41
+ before do
42
+ class Post
43
+ def name(*args, &block)
44
+ AUTHOR.name(*args, &block)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ it_behaves_like 'a delegator with a different method name', :other_name do
51
+ before do
52
+ class Post
53
+ def name
54
+ AUTHOR.other_name
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ it_behaves_like 'a delegator with a prefix', 'author' do
61
+ before do
62
+ class Post
63
+ def author_name
64
+ AUTHOR.name
65
+ end
66
+ end
67
+ end
68
+ it { should delegate(:name).to(:AUTHOR).with_prefix }
69
+ end
70
+
71
+ it_behaves_like 'a delegator with a different return value', 'Ann Rand' do
72
+ before do
73
+ class Post
74
+ def name
75
+ AUTHOR.name
76
+ 'Ann Rand'
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ module ToInstanceVariable
7
+ describe 'delegation to an instance variable' do
8
+ class Post
9
+ def initialize
10
+ @author = Author.new
11
+ end
12
+ end
13
+
14
+ subject { Post.new }
15
+
16
+ [:@author, '@author'].each do |instance_variable|
17
+ let(:receiver) { instance_variable }
18
+
19
+ it_behaves_like 'a simple delegator' do
20
+ before do
21
+ class Post
22
+ def name
23
+ @author.name
24
+ end
25
+ end
26
+ end
27
+ include_examples 'a delegator without a nil check'
28
+ end
29
+
30
+ it_behaves_like 'a delegator with a nil check' do
31
+ before do
32
+ class Post
33
+ def name
34
+ @author.name if @author
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ it_behaves_like 'a delegator with args and a block', :arg1 do
41
+ before do
42
+ class Post
43
+ def name(*args, &block)
44
+ @author.name(*args, &block)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ it_behaves_like 'a delegator with a different method name', :other_name do
51
+ before do
52
+ class Post
53
+ def name
54
+ @author.other_name
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ it_behaves_like 'a delegator with a prefix', 'author' do
61
+ before do
62
+ class Post
63
+ def author_name
64
+ @author.name
65
+ end
66
+ end
67
+ end
68
+ it { should delegate(:name).to(:@author).with_prefix }
69
+ end
70
+
71
+ it_behaves_like 'a delegator with a different return value', 'Ann Rand' do
72
+ before do
73
+ class Post
74
+ def name
75
+ @author.name
76
+ 'Ann Rand'
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ module ToMethod
7
+ describe 'delegation to a method' do
8
+ class Post
9
+ def author
10
+ @author ||= Author.new
11
+ end
12
+ end
13
+
14
+ subject { Post.new }
15
+
16
+ let(:receiver) { :author }
17
+
18
+ it_behaves_like 'a simple delegator' do
19
+ before do
20
+ class Post
21
+ def name
22
+ author.name
23
+ end
24
+ end
25
+ end
26
+ include_examples 'a delegator without a nil check'
27
+ end
28
+
29
+ it_behaves_like 'a delegator with a nil check' do
30
+ before do
31
+ class Post
32
+ def name
33
+ author.name if author
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ it_behaves_like 'a delegator with args and a block', :arg1 do
40
+ before do
41
+ class Post
42
+ def name(*args, &block)
43
+ author.name(*args, &block)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ it_behaves_like 'a delegator with a different method name', :other_name do
50
+ before do
51
+ class Post
52
+ def name
53
+ author.other_name
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ it_behaves_like 'a delegator with a prefix', 'author' do
60
+ before do
61
+ class Post
62
+ def author_name
63
+ author.name
64
+ end
65
+ end
66
+ end
67
+ it { should delegate(:name).to(:author).with_prefix }
68
+ end
69
+
70
+ it_behaves_like 'a delegator with a different return value', 'Ann Rand' do
71
+ before do
72
+ class Post
73
+ def name
74
+ author.name
75
+ 'Ann Rand'
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module DelegateMatcher
6
+ module ToObject
7
+ describe 'delegation to an object' do
8
+ class Post
9
+ def initialize(author)
10
+ @author = author
11
+ end
12
+ end
13
+
14
+ subject { Post.new(author) }
15
+
16
+ let(:author) { Author.new }
17
+ let(:receiver) { author }
18
+
19
+ it 'should fail if a nil check is specified' do
20
+ expect { should delegate(:name).to(author).allow_nil }.to raise_error do |error|
21
+ expect(error.message).to match(/cannot verify "allow_nil" expectations when delegating to an object/)
22
+ end
23
+ end
24
+
25
+ it_behaves_like 'a simple delegator' do
26
+ before do
27
+ class Post
28
+ def name
29
+ @author.name
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ it_behaves_like 'a delegator with args and a block', :arg1 do
36
+ before do
37
+ class Post
38
+ def name(*args, &block)
39
+ @author.name(*args, &block)
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ it_behaves_like 'a delegator with a different method name', :other_name do
46
+ before do
47
+ class Post
48
+ def name
49
+ @author.other_name
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ describe 'prefix' do
56
+ before do
57
+ class Post
58
+ def author_name
59
+ @author.name
60
+ end
61
+ end
62
+ end
63
+
64
+ it { should delegate(:name).to(author).with_prefix('author') }
65
+ it { should delegate(:name).to(author).with_prefix(:author) }
66
+
67
+ describe "description with prefix'" do
68
+ let(:matcher) { delegate(:name).to(author).with_prefix('author') }
69
+ before { matcher.matches? subject }
70
+
71
+ it { expect(matcher.description).to eq "delegate author_name to #{author}.name" }
72
+ it { expect(matcher.failure_message).to match(/expected .* to delegate author_name to #{author}.name/) }
73
+ it { expect(matcher.failure_message_when_negated).to match(/expected .* not to delegate author_name to #{author}.name/) }
74
+ end
75
+ end
76
+
77
+ describe 'default prefix' do
78
+ before do
79
+ class Post
80
+ def name
81
+ @author.name
82
+ end
83
+ end
84
+ end
85
+
86
+ it { should delegate(:name).to(author).with_prefix }
87
+ end
88
+
89
+ it_behaves_like 'a delegator with a different return value', 'Ann Rand' do
90
+ before do
91
+ class Post
92
+ def name
93
+ @author.name
94
+ 'Ann Rand'
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -6,31 +6,32 @@ module ForwardableDelegation
6
6
 
7
7
  attr_accessor :author
8
8
 
9
+ def initialize
10
+ @author = Author.new
11
+ end
12
+
9
13
  def_delegator :author, :name
14
+ def_delegator :author, :name, :author_name
10
15
  def_delegator :author, :name, :writer
11
- def_delegator :author, :name_with_arg
12
- def_delegator :author, :name_with_block
13
16
  end
14
17
 
15
18
  class Author
16
19
  def name
17
20
  'Catherine Asaro'
18
21
  end
19
-
20
- def name_with_arg(arg)
21
- "#{arg} #{name}"
22
- end
23
-
24
- def name_with_block(&block)
25
- "#{block.call} #{name}"
26
- end
27
22
  end
28
23
 
29
24
  describe Post do
30
- it { should delegate(:name).to(:author) }
25
+ let(:author) { subject.author }
26
+
27
+ it { expect(subject.name).to eq 'Catherine Asaro' }
28
+
29
+ it { should delegate(:name).to(author) }
31
30
  it { should delegate(:name).to(:@author) }
31
+ it { should delegate(:name).to(:author) }
32
+ it { should delegate(:name).to(:author).with('Ms.') }
33
+ it { should delegate(:name).to(:author).with_block }
34
+ it { should delegate(:name).to(:author).with_prefix }
32
35
  it { should delegate(:writer).to(:author).as(:name) }
33
- it { should delegate(:name_with_arg).to(:author).with('Ms.') }
34
- it { should delegate(:name_with_block).to(:author).with_block }
35
36
  end
36
37
  end