transpec 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,7 +22,7 @@ module Transpec
22
22
 
23
23
  fail 'Already replaced deprecated method, cannot allowize.' if @replaced_deprecated_method
24
24
 
25
- unless context.in_example_group?
25
+ unless context.allow_to_receive_available?
26
26
  fail InvalidContextError.new(selector_range, "##{method_name}", '#allow')
27
27
  end
28
28
 
@@ -44,30 +44,31 @@ module Transpec
44
44
  def syntaxes_node
45
45
  return nil unless framework_block_node
46
46
 
47
- @syntaxes_node ||= begin
48
- framework_config_variable_name = first_block_arg_name(framework_block_node)
49
-
50
- AST::Scanner.scan(framework_block_node) do |descendent_node|
51
- next unless descendent_node.type == :send
52
- receiver_node, method_name, arg_node, *_ = *descendent_node
53
- next unless receiver_node == s(:lvar, framework_config_variable_name)
54
- next unless method_name == :syntax=
55
- break arg_node
56
- end
47
+ return @syntaxes_node if instance_variable_defined?(:@syntaxes_node)
48
+
49
+ framework_config_variable_name = first_block_arg_name(framework_block_node)
50
+
51
+ framework_block_node.each_descendent_node do |node|
52
+ next unless node.type == :send
53
+ receiver_node, method_name, arg_node, *_ = *node
54
+ next unless receiver_node == s(:lvar, framework_config_variable_name)
55
+ next unless method_name == :syntax=
56
+ return @syntaxes_node = arg_node
57
57
  end
58
+
59
+ @syntaxes_node = nil
58
60
  end
59
61
 
60
62
  def framework_block_node
61
- @framework_block_node ||= begin
62
- AST::Scanner.scan(@rspec_configure_node) do |descendent_node|
63
- next unless descendent_node.type == :block
64
- send_node = descendent_node.children.first
65
- receiver_node, method_name, *_ = *send_node
66
- next unless receiver_node == s(:lvar, rspec_configure_block_arg_name)
67
- next unless method_name == @framework_config_method_name
68
- # TODO: Check expectation framework.
69
- break descendent_node
70
- end
63
+ return @framework_block_node if instance_variable_defined?(:@framework_block_node)
64
+
65
+ @framework_block_node = @rspec_configure_node.each_descendent_node.find do |node|
66
+ next unless node.type == :block
67
+ send_node = node.children.first
68
+ receiver_node, method_name, *_ = *send_node
69
+ next unless receiver_node == s(:lvar, rspec_configure_block_arg_name)
70
+ method_name == @framework_config_method_name
71
+ # TODO: Check expectation framework.
71
72
  end
72
73
  end
73
74
 
@@ -15,7 +15,7 @@ module Transpec
15
15
  end
16
16
 
17
17
  def expectize!(negative_form = 'not_to', parenthesize_matcher_arg = true)
18
- unless context.in_example_group?
18
+ unless context.expect_to_matcher_available?
19
19
  fail InvalidContextError.new(selector_range, "##{method_name}", '#expect')
20
20
  end
21
21
 
@@ -42,7 +42,7 @@ module Transpec
42
42
  private
43
43
 
44
44
  def convert_to_syntax!(syntax, negative_form)
45
- unless context.in_example_group?
45
+ unless context.non_monkey_patch_mock_available?
46
46
  fail InvalidContextError.new(selector_range, "##{method_name}", "##{syntax}")
47
47
  end
48
48
 
@@ -5,7 +5,7 @@ module Transpec
5
5
  module Version
6
6
  MAJOR = 0
7
7
  MINOR = 2
8
- PATCH = 3
8
+ PATCH = 4
9
9
 
10
10
  def self.to_s
11
11
  [MAJOR, MINOR, PATCH].join('.')
data/spec/.rubocop.yml CHANGED
@@ -17,3 +17,7 @@ Blocks:
17
17
  # Sometimes block alignment does not suit RSpec syntax.
18
18
  BlockAlignment:
19
19
  Enabled: false
20
+
21
+ # Suppress offences for namespace classes only with #describe.
22
+ ClassLength:
23
+ Enabled: false
@@ -28,7 +28,7 @@ end
28
28
 
29
29
  shared_context 'should object' do
30
30
  let(:should_object) do
31
- Transpec::AST::Scanner.scan(ast) do |node, ancestor_nodes, in_example_group_context|
31
+ Transpec::AST::Scanner.scan(ast) do |node, ancestor_nodes|
32
32
  next unless Transpec::Syntax::Should.target_node?(node)
33
33
  return Transpec::Syntax::Should.new(
34
34
  node,
@@ -5,30 +5,32 @@ require 'transpec/context'
5
5
 
6
6
  module Transpec
7
7
  describe Context do
8
+ include ::AST::Sexp
8
9
  include_context 'parsed objects'
10
+ include_context 'isolated environment'
9
11
 
10
- def node_id(node)
11
- id = node.type.to_s
12
- node.children.each do |child|
13
- break if child.is_a?(Parser::AST::Node)
14
- id << " #{child.inspect}"
12
+ describe '#scopes' do
13
+ def node_id(node)
14
+ id = node.type.to_s
15
+ node.children.each do |child|
16
+ break if child.is_a?(Parser::AST::Node)
17
+ id << " #{child.inspect}"
18
+ end
19
+ id
15
20
  end
16
- id
17
- end
18
21
 
19
- describe '#scopes' do
20
22
  let(:source) do
21
23
  <<-END
22
- some_var = 1
24
+ top_level
23
25
 
24
26
  RSpec.configure do |config|
25
27
  config.before do
26
- prepare_something
28
+ in_before
27
29
  end
28
30
  end
29
31
 
30
32
  module SomeModule
31
- SOME_CONST = 1
33
+ in_module
32
34
 
33
35
  describe 'something' do
34
36
  def some_method(some_arg)
@@ -36,9 +38,13 @@ module Transpec
36
38
  end
37
39
 
38
40
  it 'is 1' do
39
- something.should == 1
41
+ in_example
40
42
  end
41
43
  end
44
+
45
+ 1.times do
46
+ in_normal_block
47
+ end
42
48
  end
43
49
  END
44
50
  end
@@ -47,15 +53,15 @@ module Transpec
47
53
  AST::Scanner.scan(ast) do |node, ancestor_nodes|
48
54
  expected_scopes = begin
49
55
  case node_id(node)
50
- when 'lvasgn :some_var'
56
+ when 'send nil :top_level'
51
57
  []
52
- when 'send nil :prepare_something'
53
- [:rspec_configure, :block]
58
+ when 'send nil :in_before'
59
+ [:rspec_configure, :each_before_after]
54
60
  when 'module'
55
61
  []
56
62
  when 'const nil :SomeModule'
57
63
  # [:module] # TODO
58
- when 'casgn nil :SOME_CONST'
64
+ when 'send nil :in_module'
59
65
  [:module]
60
66
  when 'send nil :describe'
61
67
  # [:module] # TODO
@@ -69,8 +75,10 @@ module Transpec
69
75
  # [:module, :example_group] # TODO
70
76
  when 'str "is 1"'
71
77
  # [:module, :example_group] # TODO
72
- when 'send nil :something'
73
- [:module, :example_group, :block]
78
+ when 'send nil :in_example'
79
+ [:module, :example_group, :example]
80
+ when 'send nil :in_normal_block'
81
+ [:module]
74
82
  end
75
83
  end
76
84
 
@@ -87,170 +95,550 @@ module Transpec
87
95
  end
88
96
  end
89
97
 
90
- describe '#in_example_group?' do
98
+ shared_examples 'context inspection methods' do
99
+ let(:context_object) do
100
+ AST::Scanner.scan(ast) do |node, ancestor_nodes|
101
+ next unless node == s(:send, nil, :target)
102
+ return Context.new(ancestor_nodes)
103
+ end
104
+
105
+ fail 'Target node not found!'
106
+ end
107
+
108
+ def eval_with_rspec_in_context(eval_source)
109
+ result_path = 'result'
110
+
111
+ helper_source = <<-END
112
+ def target
113
+ File.open(#{result_path.inspect}, 'w') do |file|
114
+ Marshal.dump(#{eval_source}, file)
115
+ end
116
+ end
117
+ END
118
+
119
+ source_path = 'spec.rb'
120
+ File.write(source_path, helper_source + source)
121
+
122
+ `rspec #{source_path}`
123
+
124
+ Marshal.load(File.read(result_path))
125
+ end
126
+
127
+ describe '#non_monkey_patch_expectation_available?' do
128
+ subject { context_object.non_monkey_patch_expectation_available? }
129
+
130
+ let(:expected) do
131
+ eval_with_rspec_in_context('respond_to?(:expect)')
132
+ end
133
+
134
+ it { should == expected }
135
+ end
136
+
137
+ describe '#non_monkey_patch_mock_available?' do
138
+ subject { context_object.non_monkey_patch_mock_available? }
139
+
140
+ let(:expected) do
141
+ eval_with_rspec_in_context('respond_to?(:allow) && respond_to?(:receive)')
142
+ end
143
+
144
+ it { should == expected }
145
+ end
146
+ end
147
+
148
+ context 'when in top level' do
91
149
  let(:source) do
92
- <<-END
93
- top
150
+ 'target'
151
+ end
152
+
153
+ include_examples 'context inspection methods'
154
+ end
94
155
 
156
+ context 'when in an instance method in top level' do
157
+ let(:source) do
158
+ <<-END
95
159
  def some_method
96
- imethod_top
160
+ target
161
+ end
162
+
163
+ describe('test') { example { target } }
164
+ END
165
+ end
166
+
167
+ include_examples 'context inspection methods'
168
+ end
97
169
 
170
+ context 'when in a block in an instance method in top level' do
171
+ let(:source) do
172
+ <<-END
173
+ def some_method
98
174
  1.times do
99
- block_imethod_top
175
+ target
100
176
  end
101
177
  end
102
178
 
179
+ describe('test') { example { target } }
180
+ END
181
+ end
182
+
183
+ include_examples 'context inspection methods'
184
+ end
185
+
186
+ context 'when in #describe block in top level' do
187
+ let(:source) do
188
+ <<-END
103
189
  describe 'foo' do
104
- describe_top
190
+ target
191
+ end
192
+ END
193
+ end
194
+
195
+ include_examples 'context inspection methods'
196
+ end
105
197
 
198
+ context 'when in an instance method in #describe block in top level' do
199
+ let(:source) do
200
+ <<-END
201
+ describe 'foo' do
106
202
  def some_method
107
- imethod_describe_top
203
+ target
108
204
  end
109
205
 
206
+ example { some_method }
207
+ end
208
+ END
209
+ end
210
+
211
+ include_examples 'context inspection methods'
212
+ end
213
+
214
+ context 'when in #it block in #describe block in top level' do
215
+ let(:source) do
216
+ <<-END
217
+ describe 'foo' do
110
218
  it 'is an example' do
111
- block_describe_top
219
+ target
220
+ end
221
+ end
222
+ END
223
+ end
112
224
 
113
- class SomeClass
114
- class_block_describe_top
225
+ include_examples 'context inspection methods'
226
+ end
115
227
 
116
- def some_method
117
- imethod_class_block_describe_top
118
- end
119
- end
228
+ context 'when in #before block in #describe block in top level' do
229
+ let(:source) do
230
+ <<-END
231
+ describe 'foo' do
232
+ before do
233
+ target
120
234
  end
235
+
236
+ example { }
121
237
  end
238
+ END
239
+ end
122
240
 
123
- module SomeModule
124
- describe 'bar' do
125
- describe_module
241
+ include_examples 'context inspection methods'
242
+ end
126
243
 
127
- def some_method
128
- imethod_describe_module
129
- end
244
+ context 'when in #before(:each) block in #describe block in top level' do
245
+ let(:source) do
246
+ <<-END
247
+ describe 'foo' do
248
+ before(:each) do
249
+ target
250
+ end
130
251
 
131
- it 'is an example' do
132
- block_describe_module
133
- end
252
+ example { }
253
+ end
254
+ END
255
+ end
256
+
257
+ include_examples 'context inspection methods'
258
+ end
259
+
260
+ context 'when in #before(:all) block in #describe block in top level' do
261
+ let(:source) do
262
+ <<-END
263
+ describe 'foo' do
264
+ before(:all) do
265
+ target
134
266
  end
267
+
268
+ example { }
135
269
  end
270
+ END
271
+ end
136
272
 
137
- module AnotherModule
138
- def some_method
139
- imethod_module
273
+ include_examples 'context inspection methods'
274
+ end
275
+
276
+ context 'when in #after block in #describe block in top level' do
277
+ let(:source) do
278
+ <<-END
279
+ describe 'foo' do
280
+ after do
281
+ target
140
282
  end
283
+
284
+ example { }
141
285
  end
286
+ END
287
+ end
142
288
 
143
- class SomeClass
144
- def some_method
145
- imethod_class
289
+ include_examples 'context inspection methods'
290
+ end
291
+
292
+ context 'when in #after(:each) block in #describe block in top level' do
293
+ let(:source) do
294
+ <<-END
295
+ describe 'foo' do
296
+ after(:each) do
297
+ target
146
298
  end
299
+
300
+ example { }
147
301
  end
302
+ END
303
+ end
148
304
 
149
- RSpec.configure do |config|
150
- rspecconfigure
305
+ include_examples 'context inspection methods'
306
+ end
151
307
 
152
- def some_method
153
- imethod_rspecconfigure
308
+ context 'when in #after(:all) block in #describe block in top level' do
309
+ let(:source) do
310
+ <<-END
311
+ describe 'foo' do
312
+ after(:all) do
313
+ target
154
314
  end
155
315
 
156
- config.before do
157
- block_rspecconfigure
316
+ example { }
317
+ end
318
+ END
319
+ end
320
+
321
+ include_examples 'context inspection methods'
322
+ end
323
+
324
+ context 'when in #around block in #describe block in top level' do
325
+ let(:source) do
326
+ <<-END
327
+ describe 'foo' do
328
+ around do
329
+ target
158
330
  end
331
+
332
+ example { }
159
333
  end
160
334
  END
161
335
  end
162
336
 
163
- let(:context_object) do
164
- AST::Scanner.scan(ast) do |node, ancestor_nodes|
165
- next unless node_id(node) == target_node_id
166
- return Context.new(ancestor_nodes)
167
- end
337
+ include_examples 'context inspection methods'
338
+ end
168
339
 
169
- fail 'Target node not found!'
340
+ context 'when in #subject block in #describe block in top level' do
341
+ let(:source) do
342
+ <<-END
343
+ describe 'foo' do
344
+ subject do
345
+ target
346
+ end
347
+
348
+ example { subject }
349
+ end
350
+ END
351
+ end
352
+
353
+ include_examples 'context inspection methods'
354
+ end
355
+
356
+ context 'when in #subject! block in #describe block in top level' do
357
+ let(:source) do
358
+ <<-END
359
+ describe 'foo' do
360
+ subject! do
361
+ target
362
+ end
363
+
364
+ example { subject }
365
+ end
366
+ END
170
367
  end
171
368
 
172
- subject { context_object.in_example_group? }
369
+ include_examples 'context inspection methods'
370
+ end
371
+
372
+ context 'when in #let block in #describe block in top level' do
373
+ let(:source) do
374
+ <<-END
375
+ describe 'foo' do
376
+ let(:something) do
377
+ target
378
+ end
173
379
 
174
- context 'when in top level' do
175
- let(:target_node_id) { 'send nil :top' }
176
- it { should be_false }
380
+ example { something }
381
+ end
382
+ END
177
383
  end
178
384
 
179
- context 'when in an instance method in top level' do
180
- let(:target_node_id) { 'send nil :imethod_top' }
181
- it { should be_true }
385
+ include_examples 'context inspection methods'
386
+ end
387
+
388
+ context 'when in #let! block in #describe block in top level' do
389
+ let(:source) do
390
+ <<-END
391
+ describe 'foo' do
392
+ let!(:something) do
393
+ target
394
+ end
395
+
396
+ example { something }
397
+ end
398
+ END
182
399
  end
183
400
 
184
- context 'when in a block in an instance method in top level' do
185
- let(:target_node_id) { 'send nil :block_imethod_top' }
186
- it { should be_true }
401
+ include_examples 'context inspection methods'
402
+ end
403
+
404
+ context 'when in any other block in #describe block in top level' do
405
+ let(:source) do
406
+ <<-END
407
+ describe 'foo' do
408
+ 1.times do
409
+ target
410
+ end
411
+
412
+ example { }
413
+ end
414
+ END
187
415
  end
188
416
 
189
- context 'when in #describe block in top level' do
190
- let(:target_node_id) { 'send nil :describe_top' }
191
- it { should be_false }
417
+ include_examples 'context inspection methods'
418
+ end
419
+
420
+ context 'when in #before block in singleton method with self in #describe block in top level' do
421
+ let(:source) do
422
+ <<-END
423
+ describe 'foo' do
424
+ def self.some_method
425
+ before do
426
+ target
427
+ end
428
+ end
429
+
430
+ some_method
431
+
432
+ example { something }
433
+ end
434
+ END
192
435
  end
193
436
 
194
- context 'when in an instance method in #describe block in top level' do
195
- let(:target_node_id) { 'send nil :imethod_describe_top' }
196
- it { should be_true }
437
+ include_examples 'context inspection methods'
438
+ end
439
+
440
+ context 'when in #before block in singleton method with other object in #describe block in top level' do
441
+ let(:source) do
442
+ <<-END
443
+ describe 'foo' do
444
+ some_object = 'some object'
445
+
446
+ def some_object.before
447
+ yield
448
+ end
449
+
450
+ def some_object.some_method
451
+ before do
452
+ target
453
+ end
454
+ end
455
+
456
+ some_object.some_method
457
+ end
458
+ END
197
459
  end
198
460
 
199
- context 'when in a block in #describe block in top level' do
200
- let(:target_node_id) { 'send nil :block_describe_top' }
201
- it { should be_true }
461
+ include_examples 'context inspection methods'
462
+ end
463
+
464
+ context 'when in a class in a block in #describe block' do
465
+ let(:source) do
466
+ <<-END
467
+ describe 'foo' do
468
+ it 'is an example' do
469
+ class SomeClass
470
+ target
471
+ end
472
+ end
473
+ end
474
+ END
202
475
  end
203
476
 
204
- context 'when in a class in a block in #describe block' do
205
- let(:target_node_id) { 'send nil :class_block_describe_top' }
206
- it { should be_false }
477
+ include_examples 'context inspection methods'
478
+ end
479
+
480
+ context 'when in an instance method in a class in a block in #describe block' do
481
+ let(:source) do
482
+ <<-END
483
+ describe 'foo' do
484
+ it 'is an example' do
485
+ class SomeClass
486
+ def some_method
487
+ target
488
+ end
489
+ end
490
+
491
+ SomeClass.new.some_method
492
+ end
493
+ end
494
+ END
207
495
  end
208
496
 
209
- context 'when in an instance method in a class in a block in #describe block' do
210
- let(:target_node_id) { 'send nil :imethod_class_block_describe_top' }
211
- it { should be_false }
497
+ include_examples 'context inspection methods'
498
+ end
499
+
500
+ context 'when in #describe block in a module' do
501
+ let(:source) do
502
+ <<-END
503
+ module SomeModule
504
+ describe 'foo' do
505
+ target
506
+ end
507
+ end
508
+ END
212
509
  end
213
510
 
214
- context 'when in #describe block in a module' do
215
- let(:target_node_id) { 'send nil :describe_module' }
216
- it { should be_false }
511
+ include_examples 'context inspection methods'
512
+ end
513
+
514
+ context 'when in an instance method in #describe block in a module' do
515
+ let(:source) do
516
+ <<-END
517
+ module SomeModule
518
+ describe 'foo' do
519
+ def some_method
520
+ target
521
+ end
522
+
523
+ example { some_method }
524
+ end
525
+ end
526
+ END
217
527
  end
218
528
 
219
- context 'when in an instance method in #describe block in a module' do
220
- let(:target_node_id) { 'send nil :imethod_describe_module' }
221
- it { should be_true }
529
+ include_examples 'context inspection methods'
530
+ end
531
+
532
+ context 'when in a block in #describe block in a module' do
533
+ let(:source) do
534
+ <<-END
535
+ module SomeModule
536
+ describe 'foo' do
537
+ it 'is an example' do
538
+ target
539
+ end
540
+ end
541
+ end
542
+ END
222
543
  end
223
544
 
224
- context 'when in a block in #describe block in a module' do
225
- let(:target_node_id) { 'send nil :block_describe_module' }
226
- it { should be_true }
545
+ include_examples 'context inspection methods'
546
+ end
547
+
548
+ context 'when in an instance method in a module' do
549
+ let(:source) do
550
+ <<-END
551
+ module SomeModule
552
+ def some_method
553
+ target
554
+ end
555
+ end
556
+
557
+ describe 'test' do
558
+ include SomeModule
559
+ example { some_method }
560
+ end
561
+ END
227
562
  end
228
563
 
229
- context 'when in an instance method in a module' do
230
- # Instance methods of module can be used by `include SomeModule` in #describe block.
231
- let(:target_node_id) { 'send nil :imethod_module' }
232
- it { should be_true }
564
+ # Instance methods of module can be used by `include SomeModule` in #describe block.
565
+ include_examples 'context inspection methods'
566
+ end
567
+
568
+ context 'when in an instance method in a class' do
569
+ let(:source) do
570
+ <<-END
571
+ class SomeClass
572
+ def some_method
573
+ target
574
+ end
575
+ end
576
+
577
+ describe 'test' do
578
+ example { SomeClass.new.some_method }
579
+ end
580
+ END
233
581
  end
234
582
 
235
- context 'when in an instance method in a class' do
236
- let(:target_node_id) { 'send nil :imethod_class' }
237
- it { should be_false }
583
+ include_examples 'context inspection methods'
584
+ end
585
+
586
+ context 'when in RSpec.configure' do
587
+ let(:source) do
588
+ <<-END
589
+ RSpec.configure do |config|
590
+ target
591
+ end
592
+ END
238
593
  end
239
594
 
240
- context 'when in RSpec.configure' do
241
- let(:target_node_id) { 'send nil :rspecconfigure' }
242
- it { should be_false }
595
+ include_examples 'context inspection methods'
596
+ end
597
+
598
+ context 'when in #before block in RSpec.configure' do
599
+ let(:source) do
600
+ <<-END
601
+ RSpec.configure do |config|
602
+ config.before do
603
+ target
604
+ end
605
+ end
606
+
607
+ describe('test') { example { } }
608
+ END
243
609
  end
244
610
 
245
- context 'when in a block in RSpec.configure' do
246
- let(:target_node_id) { 'send nil :block_rspecconfigure' }
247
- it { should be_true }
611
+ include_examples 'context inspection methods'
612
+ end
613
+
614
+ context 'when in a normal block in RSpec.configure' do
615
+ let(:source) do
616
+ <<-END
617
+ RSpec.configure do |config|
618
+ 1.times do
619
+ target
620
+ end
621
+ end
622
+ END
248
623
  end
249
624
 
250
- context 'when in an instance method in RSpec.configure' do
251
- let(:target_node_id) { 'send nil :imethod_rspecconfigure' }
252
- it { should be_true }
625
+ include_examples 'context inspection methods'
626
+ end
627
+
628
+ context 'when in an instance method in RSpec.configure' do
629
+ let(:source) do
630
+ <<-END
631
+ RSpec.configure do |config|
632
+ def some_method
633
+ target
634
+ end
635
+ end
636
+
637
+ describe('test') { example { some_method } }
638
+ END
253
639
  end
640
+
641
+ include_examples 'context inspection methods'
254
642
  end
255
643
  end
256
644
  end