delegate_matcher 0.0.3 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,467 +0,0 @@
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
- describe 'delegation to instance method' do
187
- it { should delegate(:name).to(:author) }
188
- it { should delegate(:writer).to(:author).as(:name) }
189
- it { should_not delegate(:age).to(:author) }
190
- end
191
-
192
- describe 'delegation to class method' do
193
- it { should delegate(:class_name).to(:class).as(:name) }
194
- end
195
-
196
- describe 'delegation to instance variable' do
197
- it { should delegate(:name).to(:@author) }
198
- it { should delegate(:writer).to(:@author).as(:name) }
199
- it { should_not delegate(:age).to(:@author) }
200
- end
201
-
202
- describe 'delegation to class variable' do
203
- it { should delegate(:count).to(:@@authors) }
204
- end
205
-
206
- describe 'delegation to constant' do
207
- it { should delegate(:first).to(:GENRES) }
208
- end
209
-
210
- describe 'delegation to object' do
211
- it { should delegate(:name).to(author) }
212
- it { should delegate(:writer).to(author).as(:name) }
213
-
214
- it { should_not delegate(:age).to(author) }
215
- end
216
-
217
- describe 'return value' do
218
- it { should_not delegate(:name_with_bad_return).to(:author) }
219
- end
220
-
221
- describe 'with_prefix' do
222
- it { should delegate(:name).to(:author).with_prefix }
223
- it { should delegate(:name).to(:author).with_prefix(:writer) }
224
- it { should delegate(:name).to(:author).with_prefix('writer') }
225
- end
226
-
227
- describe 'allow_nil' do
228
- context 'when delegator checks that delegate is nil' do
229
- before { post.author = nil }
230
-
231
- it { should delegate(:name_with_nil_check).to(:author).allow_nil(true) }
232
- it { should delegate(:name_with_nil_check).to(:author).allow_nil }
233
-
234
- it { should_not delegate(:name_with_nil_check).to(:author).allow_nil(false) }
235
- it { should_not delegate(:name_with_nil_check_and_bad_return).to(:author).allow_nil }
236
- end
237
-
238
- context 'when delegator does not check that delegate is nil' do
239
- it { should delegate(:name).to(:author).allow_nil(false) }
240
- it { should_not delegate(:name).to(:author).allow_nil(true) }
241
- it { should_not delegate(:name).to(:author).allow_nil }
242
- end
243
- end
244
-
245
- describe 'with arguments' do
246
- it { should delegate(:name_with_arg).with('Ms.').to(:author) }
247
- it { should delegate(:name_with_arg).with('Ms.').to(:author).with('Ms.') }
248
- it { should delegate(:name_with_multiple_args).with('The', 'author').to(:author) }
249
- it { should delegate(:name_with_optional_arg).with('The author').to(:author) }
250
- it { should delegate(:name_with_optional_arg).with('The', 'author').to(:author) }
251
- it { should delegate(:name_with_default_arg).to(:author) }
252
- it { should delegate(:name_with_default_arg).with('The author').to(:author) }
253
-
254
- it { should delegate(:name_with_different_arg_and_block).with('Ms.').to(:author).with('Miss') }
255
- it { should_not delegate(:name_with_different_arg_and_block).with('Ms.').to(:author).with('Ms.') }
256
- end
257
-
258
- describe 'with a block' do
259
- it { should delegate(:name_with_block).to(:author).with_a_block }
260
- it { should delegate(:name_with_block).to(:author).with_block }
261
-
262
- it { should_not delegate(:name).to(:author).with_a_block }
263
- it { should_not delegate(:name).to(:author).with_block }
264
- it { should_not delegate(:name_with_different_block).to(:author).with_a_block }
265
- end
266
-
267
- describe 'without a block' do
268
- it { should delegate(:name).to(:author).without_a_block }
269
-
270
- it { should_not delegate(:name_with_block).to(:author).without_block }
271
- it { should_not delegate(:name_with_block).to(:author).without_a_block }
272
- it { should_not delegate(:name_with_different_block).to(:author).without_a_block }
273
- end
274
-
275
- describe 'arguments and blocks' do
276
- it { should delegate(:name_with_arg_and_block).to(:author).with(true).with_block }
277
- end
278
-
279
- describe 'should raise error' do
280
- it 'with "to" not specified' do
281
- expect { should delegate(:name) }.to raise_error do |error|
282
- expect(error.message).to match(/need to provide a "to"/)
283
- end
284
- end
285
-
286
- it 'with an invalid "to"' do
287
- expect { should delegate(:name).to(:invalid_delegate) }.to raise_error do |error|
288
- expect(error.message).to match(/does not respond to invalid_delegate/)
289
- end
290
- end
291
-
292
- it 'with delegate that requires arguments' do
293
- expect { should delegate(:name).to(:name_with_arg) }.to raise_error do |error|
294
- expect(error.message).to match(/name_with_arg method expects parameters/)
295
- end
296
- end
297
-
298
- it 'with delegate method argument mismatch' do
299
- expect { should delegate(:name_with_arg).to(:author) }.to raise_error do |error|
300
- expect(error.message).to match(/wrong number of arguments/)
301
- end
302
- end
303
-
304
- it 'with delegation to an object with "allow_nil" expectations' do
305
- expect { should delegate(:name).to(author).allow_nil }.to raise_error do |error|
306
- expect(error.message).to match(/cannot verify "allow_nil" expectations when delegating to an object/)
307
- end
308
- end
309
-
310
- it 'with delegation to a constant with "allow_nil" expectations' do
311
- expect { should should delegate(:first).to(:GENRES).allow_nil }.to raise_error do |error|
312
- expect(error.message).to match(/cannot verify "allow_nil" expectations when delegating to a constant/)
313
- end
314
- end
315
- end
316
-
317
- describe 'description' do
318
- let(:matcher) { self.class.parent_groups[1].description }
319
- subject { eval matcher }
320
- before { subject.matches? post }
321
-
322
- context 'delegate(:name).to(:author)' do
323
- its(:description) { should eq 'delegate name to author' }
324
- its(:failure_message_when_negated) { should match(/expected .* not to delegate name to author/) }
325
- end
326
-
327
- context 'delegate(:age).to(:author)' do
328
- its(:description) { should eq 'delegate age to author' }
329
- its(:failure_message) { should match(/expected .* to delegate age to author/) }
330
- end
331
-
332
- context 'delegate(:name).to(:author).with_prefix' do
333
- its(:description) { should eq 'delegate author_name to author.name' }
334
- end
335
-
336
- context 'delegate(:writer).to(:author).as(:name)' do
337
- its(:description) { should eq 'delegate writer to author.name' }
338
- end
339
-
340
- context 'delegate(:name).to(:@author)' do
341
- its(:description) { should eq 'delegate name to @author' }
342
- end
343
-
344
- context 'delegate(:name).to(:author).with_prefix' do
345
- its(:description) { should eq 'delegate author_name to author.name' }
346
- end
347
-
348
- context 'delegate(:name).to(:author).with_prefix("writer")' do
349
- its(:description) { should eq 'delegate writer_name to author.name' }
350
- end
351
-
352
- context 'delegate(:writer).to(:author).as("name")' do
353
- its(:description) { should eq 'delegate writer to author.name' }
354
- end
355
-
356
- context 'delegate(:name_with_bad_return).to(:author)' do
357
- its(:description) { should eq 'delegate name_with_bad_return to author' }
358
- its(:failure_message) { should match(/a return value of "Ann Rand" was returned instead of the delegate return value/) }
359
- end
360
-
361
- context 'with allow_nil true' do
362
- context 'delegate(:name).to(:author).allow_nil' do
363
- its(:description) { should eq 'delegate name to author with nil allowed' }
364
- its(:failure_message) { should match(/author was not allowed to be nil/) }
365
- end
366
-
367
- context 'delegate(:name).to(:author).allow_nil(true)' 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_with_nil_check_and_bad_return).to(:author).allow_nil' do
373
- its(:failure_message) { should match(/did not return nil/) }
374
- end
375
-
376
- context 'delegate(:name_with_nil_check).to(:author).allow_nil' do
377
- its(:failure_message_when_negated) { should match(/author was allowed to be nil/) }
378
- end
379
- end
380
-
381
- context 'with allow_nil false' do
382
- context 'delegate(:name_with_nil_check).to(:author).allow_nil(false)' do
383
- its(:description) { should eq 'delegate name_with_nil_check to author with nil not allowed' }
384
- its(:failure_message) { should match(/author was allowed to be nil/) }
385
- end
386
-
387
- context 'delegate(:name).to(:author).allow_nil(false)' do
388
- its(:failure_message_when_negated) { should match(/author was not allowed to be nil/) }
389
- end
390
- end
391
-
392
- context 'with arguments' do
393
- context 'delegate(:name_with_multiple_args).with("Ms.", "Phd").to(:author)' do
394
- its(:description) { should eq 'delegate name_with_multiple_args("Ms.", "Phd") to author' }
395
- end
396
-
397
- context 'delegate(:name_with_different_arg_and_block).with("Ms.").to(:author)' do
398
- its(:description) { should eq 'delegate name_with_different_arg_and_block("Ms.") to author' }
399
- its(:failure_message) { should match(/was called with \("Miss"\)/) }
400
- end
401
-
402
- context 'delegate(:name_with_different_arg_and_block).with("Ms.").to(:author).with("Miss")' do
403
- its(:description) { should eq 'delegate name_with_different_arg_and_block("Ms.") to author.name_with_different_arg_and_block("Miss")' }
404
- its(:failure_message_when_negated) { should match(/was called with \("Miss"\)/) }
405
- end
406
-
407
- context 'delegate(:name_with_arg2).with("The author").to(:author).as(:name_with_arg)' do
408
- its(:description) { should eq 'delegate name_with_arg2("The author") to author.name_with_arg' }
409
- end
410
- end
411
-
412
- context 'with a block' do
413
- context 'delegate(:name).to(:author).with_a_block' do
414
- its(:description) { should eq 'delegate name to author with a block' }
415
- its(:failure_message) { should match(/a block was not passed/) }
416
- end
417
-
418
- context 'delegate(:name_with_different_block).to(:author).with_a_block' do
419
- its(:failure_message) { should match(/a different block .+ was passed/) }
420
- end
421
-
422
- context 'delegate(:name_with_block).to(:author).with_a_block' do
423
- its(:failure_message_when_negated) { should match(/a block was passed/) }
424
- end
425
-
426
- context 'and arguments' do
427
- context 'delegate(:name_with_different_arg_and_block).with("Ms.").to(:author).with_a_block' do
428
- its(:description) { should eq 'delegate name_with_different_arg_and_block("Ms.") to author with a block' }
429
- its(:failure_message) { should match(/was called with \("Miss"\) /) }
430
- its(:failure_message) { should match(/and a different block .+ was passed/) }
431
- end
432
-
433
- context 'delegate(:name_with_arg_and_block).to(:author).with(true).with_a_block' do
434
- its(:failure_message_when_negated) { should match(/was called with \(true\) /) }
435
- its(:failure_message_when_negated) { should match(/and a block was passed/) }
436
- end
437
- end
438
- end
439
-
440
- context 'without a block' do
441
- context 'delegate(:name_with_block).to(:author).without_a_block' do
442
- its(:description) { should eq 'delegate name_with_block to author without a block' }
443
- its(:failure_message) { should match(/a block was passed/) }
444
- end
445
-
446
- context 'delegate(:name_with_different_block).to(:author).without_a_block' do
447
- its(:failure_message) { should match(/a block was passed/) }
448
- end
449
-
450
- context 'delegate(:name).to(:author).without_a_block' do
451
- its(:failure_message_when_negated) { should match(/a block was not passed/) }
452
- end
453
-
454
- context 'and arguments' do
455
- context 'delegate(:name_with_different_arg_and_block).to(:author).with("Miss").without_a_block' do
456
- its(:description) { should eq 'delegate name_with_different_arg_and_block("Miss") to author without a block' }
457
- its(:failure_message) { should match(/a block was passed/) }
458
- end
459
-
460
- context 'delegate(:name_with_arg).to(:author).with("Miss").without_a_block' do
461
- its(:failure_message_when_negated) { should match(/was called with \("Miss"\) /) }
462
- its(:failure_message_when_negated) { should match(/and a block was not passed/) }
463
- end
464
- end
465
- end
466
- end
467
- end
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Version' do
4
- before { load './lib/delegate_matcher/version.rb' }
5
-
6
- it('should be present') { expect(DelegateMatcher::VERSION).to_not be_empty }
7
- end