delegate_matcher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module DelegateMatcher
2
+ VERSION ||= '0.0.1'.freeze
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'delegate_matcher/version'
2
+ require 'delegate_matcher/delegate_matcher'
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ require 'active_support/core_ext/module/delegation'
3
+
4
+ module ActiveSupportDelegation
5
+ class Post
6
+ attr_accessor :author
7
+
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
20
+ end
21
+
22
+ class Author
23
+ def name
24
+ 'Catherine Asaro'
25
+ 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
+ end
39
+
40
+ describe Post do
41
+ it { should delegate(:name).to(:author) }
42
+ it { should delegate(:name).to(:@author) }
43
+ it { should delegate(:name_with_nil_check).to(:author).allow_nil }
44
+ it { should delegate(:name).to(:author).with_prefix }
45
+ it { should delegate(:name).to(:author).with_prefix(:writer) }
46
+
47
+ it { should delegate(:name_with_arg).to(:author).with('Ms.') }
48
+ it { should delegate(:name_with_block).to(:author).with_block }
49
+ it { should delegate(:count).to(:@@authors) }
50
+ it { should delegate(:first).to(:GENRES) }
51
+ it { should delegate(:name).to(:class).with_prefix }
52
+ end
53
+ end
@@ -0,0 +1,472 @@
1
+ # noinspection RubyResolve
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Delegate matcher' do
6
+ let(:post) do
7
+ Class.new do
8
+ attr_accessor :author
9
+
10
+ class_variable_set(:@@authors, ['Ann Rand', 'Catherine Asaro'])
11
+ GENRES ||= ['Fiction', 'Science Fiction']
12
+
13
+ def name
14
+ author.name
15
+ end
16
+
17
+ def name_with_bad_return
18
+ author.name_with_bad_return
19
+ 'Ann Rand'
20
+ end
21
+
22
+ def name_with_nil_check
23
+ author.name_with_nil_check if author
24
+ end
25
+
26
+ def name_with_nil_check_and_bad_return
27
+ author.name_with_nil_check_and_bad_return if author
28
+ 'Ann Rand'
29
+ end
30
+
31
+ def author_name
32
+ author.name
33
+ end
34
+
35
+ def writer
36
+ author.name
37
+ end
38
+
39
+ def writer_name
40
+ author.name
41
+ end
42
+
43
+ def name_with_arg(arg)
44
+ author.name_with_arg(arg)
45
+ end
46
+
47
+ def name_with_arg2(arg)
48
+ author.name_with_arg(arg)
49
+ end
50
+
51
+ def name_with_default_arg(arg = 'The author')
52
+ author.name_with_default_arg(arg)
53
+ end
54
+
55
+ def name_with_multiple_args(arg1, arg2)
56
+ author.name_with_multiple_args(arg1, arg2)
57
+ end
58
+
59
+ def name_with_optional_arg(*address)
60
+ author.name_with_optional_arg(*address)
61
+ end
62
+
63
+ def name_with_block(&block)
64
+ author.name_with_block(&block)
65
+ end
66
+
67
+ def name_with_different_block(&_block)
68
+ author.name_with_different_block(&proc {})
69
+ end
70
+
71
+ def name_with_arg_and_block(arg, &block)
72
+ author.name_with_arg_and_block(arg, &block)
73
+ end
74
+
75
+ def name_with_different_arg_and_block(_arg, &_block)
76
+ author.name_with_different_arg_and_block('Miss', &proc {})
77
+ end
78
+
79
+ def age
80
+ 60
81
+ end
82
+
83
+ def class_name
84
+ self.class.name
85
+ end
86
+
87
+ def count
88
+ self.class.class_variable_get(:@@authors).count
89
+ end
90
+
91
+ def first
92
+ GENRES.first
93
+ end
94
+
95
+ def inspect
96
+ 'post'
97
+ end
98
+ end.new
99
+ end
100
+
101
+ let(:author) do
102
+ Class.new do
103
+ def name
104
+ 'Catherine Asaro'
105
+ end
106
+
107
+ def name_with_bad_return
108
+ name
109
+ end
110
+
111
+ def name_with_nil_check
112
+ name
113
+ end
114
+
115
+ def name_with_nil_check_and_bad_return
116
+ 'Ann Rand'
117
+ end
118
+
119
+ def name_with_arg(arg)
120
+ "#{arg} #{name}"
121
+ end
122
+
123
+ def name_with_default_arg(arg = 'The author')
124
+ "#{arg} #{name}"
125
+ end
126
+
127
+ def name_with_multiple_args(arg1, arg2)
128
+ "#{arg1} #{arg2} #{name}"
129
+ end
130
+
131
+ def name_with_optional_arg(*args)
132
+ "#{[args, name].flatten.join(' ')}"
133
+ end
134
+
135
+ def name_with_block(&block)
136
+ "#{block.call} #{name}"
137
+ end
138
+
139
+ def name_with_arg_and_block(arg, &block)
140
+ "#{arg} #{block.call} #{name}"
141
+ end
142
+
143
+ def name_with_different_arg_and_block(arg, &block)
144
+ "#{arg} #{block.call} #{name}"
145
+ end
146
+
147
+ def inspect
148
+ 'author'
149
+ end
150
+ end.new
151
+ end
152
+
153
+ subject { post }
154
+ before { post.author = author }
155
+
156
+ describe 'test support' do
157
+ its(:name) { should eq 'Catherine Asaro' }
158
+ its(:name_with_nil_check) { should eq 'Catherine Asaro' }
159
+ its(:name_with_default_arg) { should eq 'The author Catherine Asaro' }
160
+ its(:author_name) { should eq 'Catherine Asaro' }
161
+ its(:writer) { should eq 'Catherine Asaro' }
162
+ its(:writer_name) { should eq 'Catherine Asaro' }
163
+ its(:age) { should eq 60 }
164
+ its(:count) { should eq 2 }
165
+ its(:first) { should eq 'Fiction' }
166
+ its(:class_name) { should be_nil }
167
+
168
+ it { expect(post.name_with_arg('The author')).to eq 'The author Catherine Asaro' }
169
+ it { expect(post.name_with_arg2('The author')).to eq 'The author Catherine Asaro' }
170
+ it { expect(post.name_with_default_arg('The famous')).to eq 'The famous Catherine Asaro' }
171
+ it { expect(post.name_with_multiple_args('The', 'author')).to eq 'The author Catherine Asaro' }
172
+ it { expect(post.name_with_optional_arg).to eq 'Catherine Asaro' }
173
+ it { expect(post.name_with_optional_arg('The author')).to eq 'The author Catherine Asaro' }
174
+ it { expect(post.name_with_optional_arg('The', 'author')).to eq 'The author Catherine Asaro' }
175
+ it { expect(post.name_with_block { 'The author' }).to eq 'The author Catherine Asaro' }
176
+ it { expect(post.name_with_arg_and_block('The') { 'author' }).to eq 'The author Catherine Asaro' }
177
+
178
+ context 'with nil author' do
179
+ before { post.author = nil }
180
+
181
+ it { expect(post.name_with_nil_check).to be_nil }
182
+ it { expect(post.name_with_nil_check_and_bad_return).to eq 'Ann Rand' }
183
+ end
184
+ end
185
+
186
+ it 'should have a VERSION' do
187
+ load './lib/delegate_matcher/version.rb'
188
+ expect(DelegateMatcher::VERSION).to_not be_empty
189
+ end
190
+
191
+ describe 'delegation to instance method' do
192
+ it { should delegate(:name).to(:author) }
193
+ it { should delegate(:writer).to(:author).as(:name) }
194
+ it { should_not delegate(:age).to(:author) }
195
+ end
196
+
197
+ describe 'delegation to class method' do
198
+ it { should delegate(:class_name).to(:class).as(:name) }
199
+ end
200
+
201
+ describe 'delegation to instance variable' do
202
+ it { should delegate(:name).to(:@author) }
203
+ it { should delegate(:writer).to(:@author).as(:name) }
204
+ it { should_not delegate(:age).to(:@author) }
205
+ end
206
+
207
+ describe 'delegation to class variable' do
208
+ it { should delegate(:count).to(:@@authors) }
209
+ end
210
+
211
+ describe 'delegation to constant' do
212
+ it { should delegate(:first).to(:GENRES) }
213
+ end
214
+
215
+ describe 'delegation to object' do
216
+ it { should delegate(:name).to(author) }
217
+ it { should delegate(:writer).to(author).as(:name) }
218
+
219
+ it { should_not delegate(:age).to(author) }
220
+ end
221
+
222
+ describe 'return value' do
223
+ it { should_not delegate(:name_with_bad_return).to(:author) }
224
+ end
225
+
226
+ describe 'with_prefix' do
227
+ it { should delegate(:name).to(:author).with_prefix }
228
+ it { should delegate(:name).to(:author).with_prefix(:writer) }
229
+ it { should delegate(:name).to(:author).with_prefix('writer') }
230
+ end
231
+
232
+ describe 'allow_nil' do
233
+ context 'when delegator checks that delegate is nil' do
234
+ before { post.author = nil }
235
+
236
+ it { should delegate(:name_with_nil_check).to(:author).allow_nil(true) }
237
+ it { should delegate(:name_with_nil_check).to(:author).allow_nil }
238
+
239
+ it { should_not delegate(:name_with_nil_check).to(:author).allow_nil(false) }
240
+ it { should_not delegate(:name_with_nil_check_and_bad_return).to(:author).allow_nil }
241
+ end
242
+
243
+ context 'when delegator does not check that delegate is nil' do
244
+ it { should delegate(:name).to(:author).allow_nil(false) }
245
+ it { should_not delegate(:name).to(:author).allow_nil(true) }
246
+ it { should_not delegate(:name).to(:author).allow_nil }
247
+ end
248
+ end
249
+
250
+ describe 'with arguments' do
251
+ it { should delegate(:name_with_arg).with('Ms.').to(:author) }
252
+ it { should delegate(:name_with_arg).with('Ms.').to(:author).with('Ms.') }
253
+ it { should delegate(:name_with_multiple_args).with('The', 'author').to(:author) }
254
+ it { should delegate(:name_with_optional_arg).with('The author').to(:author) }
255
+ it { should delegate(:name_with_optional_arg).with('The', 'author').to(:author) }
256
+ it { should delegate(:name_with_default_arg).to(:author) }
257
+ it { should delegate(:name_with_default_arg).with('The author').to(:author) }
258
+
259
+ it { should delegate(:name_with_different_arg_and_block).with('Ms.').to(:author).with('Miss') }
260
+ it { should_not delegate(:name_with_different_arg_and_block).with('Ms.').to(:author).with('Ms.') }
261
+ end
262
+
263
+ describe 'with a block' do
264
+ it { should delegate(:name_with_block).to(:author).with_a_block }
265
+ it { should delegate(:name_with_block).to(:author).with_block }
266
+
267
+ it { should_not delegate(:name).to(:author).with_a_block }
268
+ it { should_not delegate(:name).to(:author).with_block }
269
+ it { should_not delegate(:name_with_different_block).to(:author).with_a_block }
270
+ end
271
+
272
+ describe 'without a block' do
273
+ it { should delegate(:name).to(:author).without_a_block }
274
+
275
+ it { should_not delegate(:name_with_block).to(:author).without_block }
276
+ it { should_not delegate(:name_with_block).to(:author).without_a_block }
277
+ it { should_not delegate(:name_with_different_block).to(:author).without_a_block }
278
+ end
279
+
280
+ describe 'arguments and blocks' do
281
+ it { should delegate(:name_with_arg_and_block).to(:author).with(true).with_block }
282
+ end
283
+
284
+ describe 'should raise error' do
285
+ it 'with "to" not specified' do
286
+ expect { should delegate(:name) }.to raise_error do |error|
287
+ expect(error.message).to match(/need to provide a "to"/)
288
+ end
289
+ end
290
+
291
+ it 'with an invalid "to"' do
292
+ expect { should delegate(:name).to(:invalid_delegate) }.to raise_error do |error|
293
+ expect(error.message).to match(/does not respond to invalid_delegate/)
294
+ end
295
+ end
296
+
297
+ it 'with delegate that requires arguments' do
298
+ expect { should delegate(:name).to(:name_with_arg) }.to raise_error do |error|
299
+ expect(error.message).to match(/name_with_arg method expects parameters/)
300
+ end
301
+ end
302
+
303
+ it 'with delegate method argument mismatch' do
304
+ expect { should delegate(:name_with_arg).to(:author) }.to raise_error do |error|
305
+ expect(error.message).to match(/wrong number of arguments/)
306
+ end
307
+ end
308
+
309
+ it 'with delegation to an object with "allow_nil" expectations' do
310
+ expect { should delegate(:name).to(author).allow_nil }.to raise_error do |error|
311
+ expect(error.message).to match(/cannot verify "allow_nil" expectations when delegating to an object/)
312
+ end
313
+ end
314
+
315
+ it 'with delegation to a constant with "allow_nil" expectations' do
316
+ expect { should should delegate(:first).to(:GENRES).allow_nil }.to raise_error do |error|
317
+ expect(error.message).to match(/cannot verify "allow_nil" expectations when delegating to a constant/)
318
+ end
319
+ end
320
+ end
321
+
322
+ describe 'description' do
323
+ let(:matcher) { self.class.parent_groups[1].description }
324
+ subject { eval matcher }
325
+ before { subject.matches? post }
326
+
327
+ context 'delegate(:name).to(:author)' do
328
+ its(:description) { should eq 'delegate name to author' }
329
+ its(:failure_message_when_negated) { should match(/expected .* not to delegate name to author/) }
330
+ end
331
+
332
+ context 'delegate(:age).to(:author)' do
333
+ its(:description) { should eq 'delegate age to author' }
334
+ its(:failure_message) { should match(/expected .* to delegate age to author/) }
335
+ end
336
+
337
+ context 'delegate(:name).to(:author).with_prefix' do
338
+ its(:description) { should eq 'delegate author_name to author.name' }
339
+ end
340
+
341
+ context 'delegate(:writer).to(:author).as(:name)' do
342
+ its(:description) { should eq 'delegate writer to author.name' }
343
+ end
344
+
345
+ context 'delegate(:name).to(:@author)' do
346
+ its(:description) { should eq 'delegate name to @author' }
347
+ end
348
+
349
+ context 'delegate(:name).to(:author).with_prefix' do
350
+ its(:description) { should eq 'delegate author_name to author.name' }
351
+ end
352
+
353
+ context 'delegate(:name).to(:author).with_prefix("writer")' do
354
+ its(:description) { should eq 'delegate writer_name to author.name' }
355
+ end
356
+
357
+ context 'delegate(:writer).to(:author).as("name")' do
358
+ its(:description) { should eq 'delegate writer to author.name' }
359
+ end
360
+
361
+ context 'delegate(:name_with_bad_return).to(:author)' do
362
+ its(:description) { should eq 'delegate name_with_bad_return to author' }
363
+ its(:failure_message) { should match(/a return value of "Ann Rand" was returned instead of the delegate return value/) }
364
+ end
365
+
366
+ context 'with allow_nil true' do
367
+ context 'delegate(:name).to(:author).allow_nil' do
368
+ its(:description) { should eq 'delegate name to author with nil allowed' }
369
+ its(:failure_message) { should match(/author was not allowed to be nil/) }
370
+ end
371
+
372
+ context 'delegate(:name).to(:author).allow_nil(true)' do
373
+ its(:description) { should eq 'delegate name to author with nil allowed' }
374
+ its(:failure_message) { should match(/author was not allowed to be nil/) }
375
+ end
376
+
377
+ context 'delegate(:name_with_nil_check_and_bad_return).to(:author).allow_nil' do
378
+ its(:failure_message) { should match(/did not return nil/) }
379
+ end
380
+
381
+ context 'delegate(:name_with_nil_check).to(:author).allow_nil' do
382
+ its(:failure_message_when_negated) { should match(/author was allowed to be nil/) }
383
+ end
384
+ end
385
+
386
+ context 'with allow_nil false' do
387
+ context 'delegate(:name_with_nil_check).to(:author).allow_nil(false)' do
388
+ its(:description) { should eq 'delegate name_with_nil_check to author with nil not allowed' }
389
+ its(:failure_message) { should match(/author was allowed to be nil/) }
390
+ end
391
+
392
+ context 'delegate(:name).to(:author).allow_nil(false)' do
393
+ its(:failure_message_when_negated) { should match(/author was not allowed to be nil/) }
394
+ end
395
+ end
396
+
397
+ context 'with arguments' do
398
+ context 'delegate(:name_with_multiple_args).with("Ms.", "Phd").to(:author)' do
399
+ its(:description) { should eq 'delegate name_with_multiple_args("Ms.", "Phd") to author' }
400
+ end
401
+
402
+ context 'delegate(:name_with_different_arg_and_block).with("Ms.").to(:author)' do
403
+ its(:description) { should eq 'delegate name_with_different_arg_and_block("Ms.") to author' }
404
+ its(:failure_message) { should match(/was called with \("Miss"\)/) }
405
+ end
406
+
407
+ context 'delegate(:name_with_different_arg_and_block).with("Ms.").to(:author).with("Miss")' do
408
+ its(:description) { should eq 'delegate name_with_different_arg_and_block("Ms.") to author.name_with_different_arg_and_block("Miss")' }
409
+ its(:failure_message_when_negated) { should match(/was called with \("Miss"\)/) }
410
+ end
411
+
412
+ context 'delegate(:name_with_arg2).with("The author").to(:author).as(:name_with_arg)' do
413
+ its(:description) { should eq 'delegate name_with_arg2("The author") to author.name_with_arg' }
414
+ end
415
+ end
416
+
417
+ context 'with a block' do
418
+ context 'delegate(:name).to(:author).with_a_block' do
419
+ its(:description) { should eq 'delegate name to author with a block' }
420
+ its(:failure_message) { should match(/a block was not passed/) }
421
+ end
422
+
423
+ context 'delegate(:name_with_different_block).to(:author).with_a_block' do
424
+ its(:failure_message) { should match(/a different block .+ was passed/) }
425
+ end
426
+
427
+ context 'delegate(:name_with_block).to(:author).with_a_block' do
428
+ its(:failure_message_when_negated) { should match(/a block was passed/) }
429
+ end
430
+
431
+ context 'and arguments' do
432
+ context 'delegate(:name_with_different_arg_and_block).with("Ms.").to(:author).with_a_block' do
433
+ its(:description) { should eq 'delegate name_with_different_arg_and_block("Ms.") to author with a block' }
434
+ its(:failure_message) { should match(/was called with \("Miss"\) /) }
435
+ its(:failure_message) { should match(/and a different block .+ was passed/) }
436
+ end
437
+
438
+ context 'delegate(:name_with_arg_and_block).to(:author).with(true).with_a_block' do
439
+ its(:failure_message_when_negated) { should match(/was called with \(true\) /) }
440
+ its(:failure_message_when_negated) { should match(/and a block was passed/) }
441
+ end
442
+ end
443
+ end
444
+
445
+ context 'without a block' do
446
+ context 'delegate(:name_with_block).to(:author).without_a_block' do
447
+ its(:description) { should eq 'delegate name_with_block to author without a block' }
448
+ its(:failure_message) { should match(/a block was passed/) }
449
+ end
450
+
451
+ context 'delegate(:name_with_different_block).to(:author).without_a_block' do
452
+ its(:failure_message) { should match(/a block was passed/) }
453
+ end
454
+
455
+ context 'delegate(:name).to(:author).without_a_block' do
456
+ its(:failure_message_when_negated) { should match(/a block was not passed/) }
457
+ end
458
+
459
+ context 'and arguments' do
460
+ context 'delegate(:name_with_different_arg_and_block).to(:author).with("Miss").without_a_block' do
461
+ its(:description) { should eq 'delegate name_with_different_arg_and_block("Miss") to author without a block' }
462
+ its(:failure_message) { should match(/a block was passed/) }
463
+ end
464
+
465
+ context 'delegate(:name_with_arg).to(:author).with("Miss").without_a_block' do
466
+ its(:failure_message_when_negated) { should match(/was called with \("Miss"\) /) }
467
+ its(:failure_message_when_negated) { should match(/and a block was not passed/) }
468
+ end
469
+ end
470
+ end
471
+ end
472
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ module ForwardableDelegation
4
+ class Post
5
+ extend Forwardable
6
+
7
+ attr_accessor :author
8
+
9
+ def_delegator :author, :name
10
+ def_delegator :author, :name, :writer
11
+ def_delegator :author, :name_with_arg
12
+ def_delegator :author, :name_with_block
13
+ end
14
+
15
+ class Author
16
+ def name
17
+ 'Catherine Asaro'
18
+ 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
+ end
28
+
29
+ describe Post do
30
+ it { should delegate(:name).to(:author) }
31
+ it { should delegate(:name).to(:@author) }
32
+ 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
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ require 'rspec'
2
+ require 'rspec/its'
3
+ require 'coveralls'
4
+
5
+ require 'simplecov'
6
+
7
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
8
+ SimpleCov::Formatter::HTMLFormatter,
9
+ Coveralls::SimpleCov::Formatter
10
+ ]
11
+ SimpleCov.start
12
+
13
+ # require 'pry'
14
+ # require 'awesome_print'
15
+
16
+ # I18n.enforce_available_locales = true
17
+ Coveralls.wear!
18
+
19
+ require 'delegate_matcher'
20
+
21
+ RSpec.configure do |config|
22
+ config.color = true
23
+ config.filter_run focus: true
24
+ config.run_all_when_everything_filtered = true
25
+ end