delegate_matcher 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,62 +0,0 @@
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
@@ -1,84 +0,0 @@
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 self.author
10
- @author ||= Author.new
11
- end
12
- end
13
-
14
- subject { Post }
15
-
16
- let(:receiver) { :author }
17
-
18
- it_behaves_like 'a simple delegator' do
19
- before do
20
- class Post
21
- def self.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 self.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' do
40
- before do
41
- class Post
42
- def self.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 self.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 self.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 self.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
@@ -1,85 +0,0 @@
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' 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
@@ -1,86 +0,0 @@
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' 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
@@ -1,15 +0,0 @@
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
@@ -1,84 +0,0 @@
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' 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
@@ -1,102 +0,0 @@
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
- @authors ||= [@author]
12
- end
13
-
14
- def inspect
15
- 'post'
16
- end
17
- end
18
-
19
- subject { Post.new }
20
-
21
- [:@author, '@author'].each do |instance_variable|
22
- let(:receiver) { instance_variable }
23
-
24
- it_behaves_like 'a simple delegator' do
25
- before do
26
- class Post
27
- def name
28
- @author.name
29
- end
30
- end
31
- end
32
- include_examples 'a delegator without a nil check'
33
- end
34
-
35
- it_behaves_like 'a delegator with a nil check' do
36
- before do
37
- class Post
38
- def name
39
- @author.name if @author
40
- end
41
- end
42
- end
43
- end
44
-
45
- it_behaves_like 'a delegator with args and a block' do
46
- before do
47
- class Post
48
- def name(*args, &block)
49
- @author.name(*args, &block)
50
- end
51
- end
52
- end
53
- end
54
-
55
- it_behaves_like 'a delegator with its own block' do
56
- before do
57
- class Post
58
- # rubocop:disable Style/SymbolProc
59
- def tainted?
60
- @authors.all? { |a| a.tainted? }
61
- end
62
- end
63
- end
64
- end
65
-
66
- it_behaves_like 'a delegator with a different method name', :other_name do
67
- before do
68
- class Post
69
- def name
70
- @author.other_name
71
- end
72
- end
73
- end
74
- end
75
-
76
- it_behaves_like 'a delegator with a prefix', 'author' do
77
- before do
78
- class Post
79
- def author_name
80
- @author.name
81
- end
82
- end
83
- end
84
- it { should delegate(:name).to(:@author).with_prefix }
85
- end
86
-
87
- it_behaves_like 'a delegator with a different return value', 'Ann Rand' do
88
- before do
89
- class Post
90
- def name
91
- @author.name
92
- 'Ann Rand'
93
- end
94
- end
95
- end
96
- end
97
- end
98
- end
99
- end
100
- end
101
- end
102
- end
@@ -1,103 +0,0 @@
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
@@ -1,24 +0,0 @@
1
- shared_examples 'a delegator with args' do |*args|
2
- it { should delegate(:name).with(*args).to(receiver) }
3
-
4
- describe 'description and failure messages' do
5
- before { matcher.matches? subject }
6
-
7
- context 'with no args' do
8
- let(:matcher) { delegate(:name).with.to(receiver) }
9
- it { expect(matcher.description).to eq %(delegate name() to #{receiver}) }
10
- end
11
-
12
- context 'with args passed through' do
13
- let(:matcher) { delegate(:name).with('Ms.').to(receiver) }
14
- it { expect(matcher.description).to eq %(delegate name("Ms.") to #{receiver}) }
15
- it { expect(matcher.failure_message_when_negated).to match(/was called with \("Ms."\)/) }
16
- end
17
-
18
- context 'with args changed' do
19
- let(:matcher) { delegate(:name).with('Ms.').to(receiver).with('Mrs.') }
20
- it { expect(matcher.description).to eq %(delegate name("Ms.") to #{receiver}.#{:name}("Mrs.")) }
21
- it { expect(matcher.failure_message).to match(/was called with \("Ms."\)/) }
22
- end
23
- end
24
- end
@@ -1,6 +0,0 @@
1
- shared_examples 'a delegator with args and a block' do |*args|
2
- include_examples 'a delegator with args', *args
3
- include_examples 'a delegator with a block'
4
-
5
- it { should delegate(:name).to(receiver).with(*args).with_block }
6
- end
@@ -1,10 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- module DelegateMatcher
4
- class Author
5
- def name
6
- end
7
- end
8
- end
9
- end
10
- end