transpec 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 184be0fbb9de5a2d47804fc8611719692da19749
4
- data.tar.gz: abcbeecdc14d0061e07e50542a3fa784e4f890af
3
+ metadata.gz: e92704929f93992b64fdcb82e658214425bd23cb
4
+ data.tar.gz: c224583a7f8d0e0c44bf7f3a715d1c3aeff090ca
5
5
  SHA512:
6
- metadata.gz: b6edfa9df6df92ee56fa47a19b1fd389c92835f2e69bdd7a0cd64e9937b7f78af3890fb5cf8fdf408f2c9c167d1d056a5ae0b100f0c655e58edc46bcc7b514a2
7
- data.tar.gz: a863cc647d30798d4c5815eef33e555dcb3a70c38c840277f05e57e398f9f40189346ad8f788d1068cb86f91745532ea2daf44edb97f7415cc8b24e0cc050468
6
+ metadata.gz: 1da1c8cd1bd30a65f25836b87e7ee6454158eeb7f69462b54e13614cbbc4af90002f1625af5077b5868c249498d356efe6dfc06a4b79c05c5ebe497afae015db
7
+ data.tar.gz: 758cb3a86ee46ea070c0a03c814d9c9aa81a82b853e950b5e2ba508ba101df78d63aa24092044609cb52fdc23a5fff90c0646908017d7b1a5c9cbfbb4b1e3748
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## Master
4
4
 
5
+ ## v0.0.10
6
+
7
+ * Support conversion of `at_least(0)`
8
+ * Add `-f` shorthand for `--force` option
9
+
5
10
  ## v0.0.9
6
11
 
7
12
  * Use `--disable allow_to_receive` to disable conversion from `obj.should_receive(:foo).any_number_of_times` to `allow(obj).to receive(:foo)` (Previously it was `--disable expect_to_receive`)
data/README.md CHANGED
@@ -103,6 +103,7 @@ Before converting your specs:
103
103
 
104
104
  * Make sure your project has `rspec` gem dependency `2.14` or later. If not, change your `*.gemspec` or `Gemfile` to do so.
105
105
  * Run `rspec` and check if all the specs pass.
106
+ * Ensure the Git repository is clean. (You don't want to mix up your changes and Transpec's changes, right?)
106
107
 
107
108
  Then, run `transpec` with no arguments in the project root directory:
108
109
 
@@ -123,7 +124,7 @@ After the conversion, run `rspec` again and check if all pass.
123
124
 
124
125
  ## Options
125
126
 
126
- ### `--force`
127
+ ### `-f/--force`
127
128
 
128
129
  Force processing even if the current Git repository is not clean.
129
130
 
@@ -222,7 +223,8 @@ expect(obj).not_to matcher
222
223
  expect(obj).to_not matcher # with `--negative-form to_not`
223
224
  ```
224
225
 
225
- Disabled by: `--disable expect_to_matcher`
226
+ * Disabled by: `--disable expect_to_matcher`
227
+ * Related Information: [Myron Marston » RSpec's New Expectation Syntax](http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax)
226
228
 
227
229
  ### Operator matchers
228
230
 
@@ -242,6 +244,8 @@ expect('string').to match(/^str/)
242
244
  expect([1, 2, 3]).to match_array([2, 1, 3])
243
245
  ```
244
246
 
247
+ * Related Information: [Myron Marston » RSpec's New Expectation Syntax](http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax#almost_all_matchers_are_supported)
248
+
245
249
  ### `be_close` matcher
246
250
 
247
251
  ```ruby
@@ -252,9 +256,10 @@ expect([1, 2, 3]).to match_array([2, 1, 3])
252
256
  (1.0 / 3.0).should be_within(0.001).of(0.333)
253
257
  ```
254
258
 
255
- Disabled by: `--disable deprecated`
259
+ * Disabled by: `--disable deprecated`
260
+ * Related Information: [New be within matcher and RSpec.deprecate fix · rspec/rspec-expectations](https://github.com/rspec/rspec-expectations/pull/32)
256
261
 
257
- ### Error expectations
262
+ ### Expectations on Proc
258
263
 
259
264
  ```ruby
260
265
  # Target
@@ -266,7 +271,8 @@ proc { do_something }.should raise_error
266
271
  expect { do_something }.to raise_error
267
272
  ```
268
273
 
269
- Disabled by: `--disable expect_to_matcher`
274
+ * Disabled by: `--disable expect_to_matcher`
275
+ * Related Information: [Myron Marston » RSpec's New Expectation Syntax](http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax#unification_of_block_vs_value_syntaxes)
270
276
 
271
277
  ### Negative error expectations with specific error
272
278
 
@@ -282,7 +288,8 @@ expect { do_something }.not_to raise_error
282
288
  lambda { do_something }.should_not raise_error # with `--disable expect_to_matcher`
283
289
  ```
284
290
 
285
- Disabled by: `--disable deprecated`
291
+ * Disabled by: `--disable deprecated`
292
+ * Related Information: [Consider deprecating `expect { }.not_to raise_error(SpecificErrorClass)` · rspec/rspec-expectations](https://github.com/rspec/rspec-expectations/issues/231)
286
293
 
287
294
  ### Message expectations
288
295
 
@@ -296,15 +303,18 @@ expect(obj).to receive(:foo)
296
303
  expect_any_instance_of(SomeClass).to receive(:foo)
297
304
  ```
298
305
 
299
- Disabled by: `--disable expect_to_receive`
306
+ * Disabled by: `--disable expect_to_receive`
307
+ * Related Information: [RSpec's new message expectation syntax - Tea is awesome.](http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/)
300
308
 
301
- ### Message expectations with `any_number_of_times`
309
+ ### Message expectations that are actually method stubs
302
310
 
303
311
  ```ruby
304
312
  # Target
305
313
  obj.should_receive(:foo).any_number_of_times
314
+ obj.should_receive(:foo).at_least(0)
306
315
 
307
316
  SomeClass.any_instance.should_receive(:foo).any_number_of_times
317
+ SomeClass.any_instance.should_receive(:foo).at_least(0)
308
318
 
309
319
  # Converted
310
320
  allow(obj).to receive(:foo)
@@ -314,7 +324,8 @@ allow_any_instance_of(SomeClass).to receive(:foo)
314
324
  SomeClass.any_instance.stub(:foo) # with `--disable allow_to_receive`
315
325
  ```
316
326
 
317
- Disabled by: `--disable deprecated`
327
+ * Disabled by: `--disable deprecated`
328
+ * Related Information: [Don't allow at_least(0) · rspec/rspec-mocks](https://github.com/rspec/rspec-mocks/issues/133)
318
329
 
319
330
  ### Method stubs
320
331
 
@@ -339,7 +350,8 @@ allow(obj).to receive(:bar).and_return(2)
339
350
  allow_any_instance_of(SomeClass).to receive(:foo)
340
351
  ```
341
352
 
342
- Disabled by: `--disable allow_to_receive`
353
+ * Disabled by: `--disable allow_to_receive`
354
+ * Related Information: [RSpec's new message expectation syntax - Tea is awesome.](http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/)
343
355
 
344
356
  ### Deprecated method stub aliases
345
357
 
@@ -353,20 +365,23 @@ obj.stub(:foo) # with `--disable allow_to_receive`
353
365
  obj.unstub(:foo)
354
366
  ```
355
367
 
356
- Disabled by: `--disable deprecated`
368
+ * Disabled by: `--disable deprecated`
369
+ * Related Information: [Consider deprecating and/or removing #stub! and #unstub! at some point · rspec/rspec-mocks](https://github.com/rspec/rspec-mocks/issues/122)
357
370
 
358
- ### Method stubs with `any_number_of_times`
371
+ ### Method stubs with deprecated specification of number of times
359
372
 
360
373
  ```ruby
361
374
  # Target
362
375
  obj.stub(:foo).any_number_of_times
376
+ obj.stub(:foo).at_least(0)
363
377
 
364
378
  # Converted
365
379
  allow(obj).to receive(:foo)
366
380
  obj.stub(:foo) # with `--disable allow_to_receive`
367
381
  ```
368
382
 
369
- Disabled by: `--disable deprecated`
383
+ * Disabled by: `--disable deprecated`
384
+ * Related Information: [Don't allow at_least(0) · rspec/rspec-mocks](https://github.com/rspec/rspec-mocks/issues/133)
370
385
 
371
386
  ### Deprecated test double aliases
372
387
 
@@ -379,7 +394,8 @@ mock('something')
379
394
  double('something')
380
395
  ```
381
396
 
382
- Disabled by: `--disable deprecated`
397
+ * Disabled by: `--disable deprecated`
398
+ * Related Information: [Deprecate "stub" for doubles · rspec/rspec-mocks](https://github.com/rspec/rspec-mocks/issues/214)
383
399
 
384
400
  ## Compatibility
385
401
 
data/README.md.erb CHANGED
@@ -76,6 +76,7 @@ Before converting your specs:
76
76
 
77
77
  * Make sure your project has `rspec` gem dependency `<%= rspec_version %>` or later. If not, change your `*.gemspec` or `Gemfile` to do so.
78
78
  * Run `rspec` and check if all the specs pass.
79
+ * Ensure the Git repository is clean. (You don't want to mix up your changes and Transpec's changes, right?)
79
80
 
80
81
  Then, run `transpec` with no arguments in the project root directory:
81
82
 
@@ -96,7 +97,7 @@ After the conversion, run `rspec` again and check if all pass.
96
97
 
97
98
  ## Options
98
99
 
99
- ### `--force`
100
+ ### `-f/--force`
100
101
 
101
102
  Force processing even if the current Git repository is not clean.
102
103
 
@@ -218,7 +219,8 @@ expect(obj).not_to matcher
218
219
  expect(obj).to_not matcher # with `--negative-form to_not`
219
220
  ```
220
221
 
221
- Disabled by: `--disable expect_to_matcher`
222
+ * Disabled by: `--disable expect_to_matcher`
223
+ * Related Information: [Myron Marston » RSpec's New Expectation Syntax](http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax)
222
224
 
223
225
  ### Operator matchers
224
226
 
@@ -238,6 +240,8 @@ expect('string').to match(/^str/)
238
240
  expect([1, 2, 3]).to match_array([2, 1, 3])
239
241
  ```
240
242
 
243
+ * Related Information: [Myron Marston » RSpec's New Expectation Syntax](http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax#almost_all_matchers_are_supported)
244
+
241
245
  ### `be_close` matcher
242
246
 
243
247
  ```ruby
@@ -248,9 +252,10 @@ expect([1, 2, 3]).to match_array([2, 1, 3])
248
252
  (1.0 / 3.0).should be_within(0.001).of(0.333)
249
253
  ```
250
254
 
251
- Disabled by: `--disable deprecated`
255
+ * Disabled by: `--disable deprecated`
256
+ * Related Information: [New be within matcher and RSpec.deprecate fix · rspec/rspec-expectations](https://github.com/rspec/rspec-expectations/pull/32)
252
257
 
253
- ### Error expectations
258
+ ### Expectations on Proc
254
259
 
255
260
  ```ruby
256
261
  # Target
@@ -262,7 +267,8 @@ proc { do_something }.should raise_error
262
267
  expect { do_something }.to raise_error
263
268
  ```
264
269
 
265
- Disabled by: `--disable expect_to_matcher`
270
+ * Disabled by: `--disable expect_to_matcher`
271
+ * Related Information: [Myron Marston » RSpec's New Expectation Syntax](http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax#unification_of_block_vs_value_syntaxes)
266
272
 
267
273
  ### Negative error expectations with specific error
268
274
 
@@ -278,7 +284,8 @@ expect { do_something }.not_to raise_error
278
284
  lambda { do_something }.should_not raise_error # with `--disable expect_to_matcher`
279
285
  ```
280
286
 
281
- Disabled by: `--disable deprecated`
287
+ * Disabled by: `--disable deprecated`
288
+ * Related Information: [Consider deprecating `expect { }.not_to raise_error(SpecificErrorClass)` · rspec/rspec-expectations](https://github.com/rspec/rspec-expectations/issues/231)
282
289
 
283
290
  ### Message expectations
284
291
 
@@ -292,15 +299,18 @@ expect(obj).to receive(:foo)
292
299
  expect_any_instance_of(SomeClass).to receive(:foo)
293
300
  ```
294
301
 
295
- Disabled by: `--disable expect_to_receive`
302
+ * Disabled by: `--disable expect_to_receive`
303
+ * Related Information: [RSpec's new message expectation syntax - Tea is awesome.](http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/)
296
304
 
297
- ### Message expectations with `any_number_of_times`
305
+ ### Message expectations that are actually method stubs
298
306
 
299
307
  ```ruby
300
308
  # Target
301
309
  obj.should_receive(:foo).any_number_of_times
310
+ obj.should_receive(:foo).at_least(0)
302
311
 
303
312
  SomeClass.any_instance.should_receive(:foo).any_number_of_times
313
+ SomeClass.any_instance.should_receive(:foo).at_least(0)
304
314
 
305
315
  # Converted
306
316
  allow(obj).to receive(:foo)
@@ -310,7 +320,8 @@ allow_any_instance_of(SomeClass).to receive(:foo)
310
320
  SomeClass.any_instance.stub(:foo) # with `--disable allow_to_receive`
311
321
  ```
312
322
 
313
- Disabled by: `--disable deprecated`
323
+ * Disabled by: `--disable deprecated`
324
+ * Related Information: [Don't allow at_least(0) · rspec/rspec-mocks](https://github.com/rspec/rspec-mocks/issues/133)
314
325
 
315
326
  ### Method stubs
316
327
 
@@ -335,7 +346,8 @@ allow(obj).to receive(:bar).and_return(2)
335
346
  allow_any_instance_of(SomeClass).to receive(:foo)
336
347
  ```
337
348
 
338
- Disabled by: `--disable allow_to_receive`
349
+ * Disabled by: `--disable allow_to_receive`
350
+ * Related Information: [RSpec's new message expectation syntax - Tea is awesome.](http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/)
339
351
 
340
352
  ### Deprecated method stub aliases
341
353
 
@@ -349,20 +361,23 @@ obj.stub(:foo) # with `--disable allow_to_receive`
349
361
  obj.unstub(:foo)
350
362
  ```
351
363
 
352
- Disabled by: `--disable deprecated`
364
+ * Disabled by: `--disable deprecated`
365
+ * Related Information: [Consider deprecating and/or removing #stub! and #unstub! at some point · rspec/rspec-mocks](https://github.com/rspec/rspec-mocks/issues/122)
353
366
 
354
- ### Method stubs with `any_number_of_times`
367
+ ### Method stubs with deprecated specification of number of times
355
368
 
356
369
  ```ruby
357
370
  # Target
358
371
  obj.stub(:foo).any_number_of_times
372
+ obj.stub(:foo).at_least(0)
359
373
 
360
374
  # Converted
361
375
  allow(obj).to receive(:foo)
362
376
  obj.stub(:foo) # with `--disable allow_to_receive`
363
377
  ```
364
378
 
365
- Disabled by: `--disable deprecated`
379
+ * Disabled by: `--disable deprecated`
380
+ * Related Information: [Don't allow at_least(0) · rspec/rspec-mocks](https://github.com/rspec/rspec-mocks/issues/133)
366
381
 
367
382
  ### Deprecated test double aliases
368
383
 
@@ -375,7 +390,8 @@ mock('something')
375
390
  double('something')
376
391
  ```
377
392
 
378
- Disabled by: `--disable deprecated`
393
+ * Disabled by: `--disable deprecated`
394
+ * Related Information: [Deprecate "stub" for doubles · rspec/rspec-mocks](https://github.com/rspec/rspec-mocks/issues/214)
379
395
 
380
396
  ## Compatibility
381
397
 
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+
3
+ require 'transpec/ast/node'
4
+
5
+ module Transpec
6
+ module AST
7
+ class Builder < Parser::Builders::Default
8
+ def n(type, children, source_map)
9
+ Node.new(type, children, location: source_map)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+
3
+ require 'parser'
4
+
5
+ module Transpec
6
+ module AST
7
+ class Node < Parser::AST::Node
8
+ def each_child_node
9
+ return to_enum(__method__) unless block_given?
10
+
11
+ children.each do |child|
12
+ next unless child.is_a?(self.class)
13
+ yield child
14
+ end
15
+
16
+ self
17
+ end
18
+
19
+ def child_nodes
20
+ each_child_node.to_a
21
+ end
22
+
23
+ def each_descendent_node(&block)
24
+ return to_enum(__method__) unless block_given?
25
+
26
+ each_child_node do |child_node|
27
+ yield child_node
28
+ child_node.each_child_node(&block)
29
+ end
30
+ end
31
+
32
+ def descendent_nodes
33
+ each_descendent_node.to_a
34
+ end
35
+ end
36
+ end
37
+ end
data/lib/transpec/cli.rb CHANGED
@@ -63,7 +63,7 @@ module Transpec
63
63
  parser.banner = "Usage: transpec [options] [files or directories]\n\n"
64
64
 
65
65
  parser.on(
66
- '--force',
66
+ '-f', '--force',
67
67
  'Force processing even if the current Git',
68
68
  'repository is not clean.'
69
69
  ) do
@@ -1,5 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
+ require 'transpec/ast/builder'
3
4
  require 'transpec/ast/scanner'
4
5
  require 'transpec/configuration'
5
6
  require 'transpec/syntax'
@@ -54,7 +55,8 @@ module Transpec
54
55
  end
55
56
 
56
57
  def parse(source_buffer)
57
- parser = Parser::CurrentRuby.new
58
+ builder = AST::Builder.new
59
+ parser = Parser::CurrentRuby.new(builder)
58
60
  ast = parser.parse(source_buffer)
59
61
  ast
60
62
  end
@@ -99,12 +101,12 @@ module Transpec
99
101
  end
100
102
 
101
103
  def process_should_receive(should_receive)
102
- if should_receive.any_number_of_times?
104
+ if should_receive.useless_expectation?
103
105
  if @configuration.replace_deprecated_method?
104
106
  if @configuration.convert_to_allow_to_receive?
105
- should_receive.allowize_any_number_of_times!(@configuration.negative_form_of_to)
107
+ should_receive.allowize_useless_expectation!(@configuration.negative_form_of_to)
106
108
  else
107
- should_receive.stubize_any_number_of_times!
109
+ should_receive.stubize_useless_expectation!
108
110
  end
109
111
  elsif @configuration.convert_to_expect_to_receive?
110
112
  should_receive.expectize!(@configuration.negative_form_of_to)
@@ -125,7 +127,7 @@ module Transpec
125
127
  method_stub.replace_deprecated_method!
126
128
  end
127
129
 
128
- method_stub.remove_any_number_of_times! if @configuration.replace_deprecated_method?
130
+ method_stub.remove_allowance_for_no_message! if @configuration.replace_deprecated_method?
129
131
  end
130
132
 
131
133
  def process_be_close(be_close)
@@ -0,0 +1,73 @@
1
+ # coding: utf-8
2
+
3
+ require 'transpec/syntax'
4
+
5
+ module Transpec
6
+ class Syntax
7
+ module AbleToAllowNoMessage
8
+ include ::AST::Sexp
9
+
10
+ def allow_no_message?
11
+ any_number_of_times? || at_least_zero?
12
+ end
13
+
14
+ def remove_allowance_for_no_message!
15
+ remove_any_number_of_times!
16
+ remove_at_least_zero!
17
+ end
18
+
19
+ private
20
+
21
+ def any_number_of_times?
22
+ !any_number_of_times_node.nil?
23
+ end
24
+
25
+ def at_least_zero?
26
+ !at_least_zero_node.nil?
27
+ end
28
+
29
+ def remove_any_number_of_times!
30
+ return unless any_number_of_times?
31
+ remove_dot_and_method!(any_number_of_times_node)
32
+ end
33
+
34
+ def remove_at_least_zero!
35
+ return unless at_least_zero?
36
+ remove_dot_and_method!(at_least_zero_node)
37
+ end
38
+
39
+ def remove_dot_and_method!(send_node)
40
+ map = send_node.loc
41
+ dot_and_method_range = map.dot.join(map.expression.end)
42
+ remove(dot_and_method_range)
43
+ end
44
+
45
+ def any_number_of_times_node
46
+ each_following_chained_method_node do |chained_node|
47
+ method_name = chained_node.children[1]
48
+ return chained_node if method_name == :any_number_of_times
49
+ end
50
+ end
51
+
52
+ def at_least_zero_node
53
+ each_following_chained_method_node do |chained_node|
54
+ _, method_name, arg_node = *chained_node
55
+ next unless method_name == :at_least
56
+ return chained_node if arg_node == s(:int, 0)
57
+ end
58
+ end
59
+
60
+ def each_following_chained_method_node
61
+ return to_enum(__method__) unless block_given?
62
+
63
+ @ancestor_nodes.reverse.reduce(@node) do |child_node, parent_node|
64
+ return unless [:send, :block].include?(parent_node.type)
65
+ return unless parent_node.children.first == child_node
66
+ yield parent_node, child_node
67
+ parent_node
68
+ end
69
+ nil
70
+ end
71
+ end
72
+ end
73
+ end
@@ -4,7 +4,7 @@ require 'transpec/syntax/send_node_syntax'
4
4
 
5
5
  module Transpec
6
6
  class Syntax
7
- module AnyInstanceable
7
+ module AbleToTargetAnyInstance
8
8
  include SendNodeSyntax
9
9
 
10
10
  def any_instance?
@@ -1,15 +1,15 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require 'transpec/syntax'
4
- require 'transpec/syntax/any_instanceable'
5
- require 'transpec/syntax/any_number_of_timesable'
4
+ require 'transpec/syntax/able_to_allow_no_message'
5
+ require 'transpec/syntax/able_to_target_any_instance'
6
6
  require 'transpec/util'
7
7
  require 'English'
8
8
 
9
9
  module Transpec
10
10
  class Syntax
11
11
  class MethodStub < Syntax
12
- include AnyInstanceable, AnyNumberOfTimesable, Util
12
+ include AbleToAllowNoMessage, AbleToTargetAnyInstance, Util
13
13
 
14
14
  RECEIVER_CLASS_WHITELIST = ['Typhoeus']
15
15
 
@@ -2,13 +2,15 @@
2
2
 
3
3
  require 'transpec/syntax'
4
4
  require 'transpec/syntax/expectizable'
5
- require 'transpec/syntax/any_instanceable'
6
- require 'transpec/syntax/any_number_of_timesable'
5
+ require 'transpec/syntax/able_to_allow_no_message'
6
+ require 'transpec/syntax/able_to_target_any_instance'
7
7
 
8
8
  module Transpec
9
9
  class Syntax
10
10
  class ShouldReceive < Syntax
11
- include Expectizable, AnyInstanceable, AnyNumberOfTimesable
11
+ include Expectizable, AbleToAllowNoMessage, AbleToTargetAnyInstance
12
+
13
+ alias_method :useless_expectation?, :allow_no_message?
12
14
 
13
15
  def positive?
14
16
  method_name == :should_receive
@@ -18,18 +20,18 @@ module Transpec
18
20
  convert_to_syntax!('expect', negative_form)
19
21
  end
20
22
 
21
- def allowize_any_number_of_times!(negative_form = 'not_to')
22
- return unless any_number_of_times?
23
+ def allowize_useless_expectation!(negative_form = 'not_to')
24
+ return unless useless_expectation?
23
25
 
24
26
  convert_to_syntax!('allow', negative_form)
25
- remove_any_number_of_times!
27
+ remove_allowance_for_no_message!
26
28
  end
27
29
 
28
- def stubize_any_number_of_times!(negative_form = 'not_to')
29
- return unless any_number_of_times?
30
+ def stubize_useless_expectation!(negative_form = 'not_to')
31
+ return unless useless_expectation?
30
32
 
31
33
  replace(selector_range, 'stub')
32
- remove_any_number_of_times!
34
+ remove_allowance_for_no_message!
33
35
  end
34
36
 
35
37
  private
@@ -5,7 +5,7 @@ module Transpec
5
5
  module Version
6
6
  MAJOR = 0
7
7
  MINOR = 0
8
- PATCH = 9
8
+ PATCH = 10
9
9
 
10
10
  def self.to_s
11
11
  [MAJOR, MINOR, PATCH].join('.')
@@ -1,5 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
+ require 'transpec/ast/builder'
3
4
  require 'transpec/ast/scanner'
4
5
  require 'transpec/syntax/should'
5
6
  require 'parser'
@@ -15,9 +16,9 @@ shared_context 'parsed objects' do
15
16
  end
16
17
 
17
18
  let(:ast) do
18
- parser = Parser::CurrentRuby.new
19
- ast = parser.parse(source_buffer)
20
- ast
19
+ builder = Transpec::AST::Builder.new
20
+ parser = Parser::CurrentRuby.new(builder)
21
+ parser.parse(source_buffer)
21
22
  end
22
23
 
23
24
  let(:source_rewriter) { Parser::Source::Rewriter.new(source_buffer) }
@@ -0,0 +1,106 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'transpec/ast/node'
5
+
6
+ module Transpec
7
+ module AST
8
+ describe Node do
9
+ include ::AST::Sexp
10
+ include_context 'parsed objects'
11
+
12
+ let(:source) do
13
+ <<-END
14
+ def some_method(arg_a, arg_b)
15
+ do_something(arg_a)
16
+ end
17
+ END
18
+ end
19
+
20
+ # (def :some_method
21
+ # (args
22
+ # (arg :arg_a)
23
+ # (arg :arg_b))
24
+ # (send nil :do_something
25
+ # (lvar :arg_a)))
26
+
27
+ describe '#each_child_node' do
28
+ let(:expected_types) { [:args, :send] }
29
+
30
+ context 'when a block is given' do
31
+ it 'yields each child node' do
32
+ index = 0
33
+
34
+ ast.each_child_node do |node|
35
+ expected_type = expected_types[index]
36
+ node.type.should == expected_type
37
+ index += 1
38
+ end
39
+
40
+ index.should_not == 0
41
+ end
42
+
43
+ it 'returns itself' do
44
+ returned_value = ast.each_child_node { }
45
+ returned_value.should be(ast)
46
+ end
47
+ end
48
+
49
+ context 'when no block is given' do
50
+ it 'returns enumerator' do
51
+ ast.each_child_node.should be_a(Enumerator)
52
+ end
53
+
54
+ describe 'the returned enumerator' do
55
+ it 'enumerates the child nodes' do
56
+ enumerator = ast.each_child_node
57
+
58
+ expected_types.each do |expected_type|
59
+ enumerator.next.type.should == expected_type
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ describe '#each_descendent_node' do
67
+ let(:expected_types) { [:args, :arg, :arg, :send, :lvar] }
68
+
69
+ context 'when a block is given' do
70
+ it 'yields each descendent node with depth first order' do
71
+ index = 0
72
+
73
+ ast.each_descendent_node do |node|
74
+ expected_type = expected_types[index]
75
+ node.type.should == expected_type
76
+ index += 1
77
+ end
78
+
79
+ index.should_not == 0
80
+ end
81
+
82
+ it 'returns itself' do
83
+ returned_value = ast.each_descendent_node { }
84
+ returned_value.should be(ast)
85
+ end
86
+ end
87
+
88
+ context 'when no block is given' do
89
+ it 'returns enumerator' do
90
+ ast.each_descendent_node.should be_a(Enumerator)
91
+ end
92
+
93
+ describe 'the returned enumerator' do
94
+ it 'enumerates the child nodes' do
95
+ enumerator = ast.each_descendent_node
96
+
97
+ expected_types.each do |expected_type|
98
+ enumerator.next.type.should == expected_type
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -145,7 +145,7 @@ module Transpec
145
145
  args.should == ['some_file', '--negative-form', 'to_not', 'some_dir']
146
146
  end
147
147
 
148
- describe '--force option' do
148
+ describe '-f/--force option' do
149
149
  let(:args) { ['--force'] }
150
150
 
151
151
  it 'sets #forced? true' do
@@ -172,8 +172,8 @@ module Transpec
172
172
  end
173
173
  end
174
174
 
175
- context 'when ShouldReceive#any_number_of_times? returns true' do
176
- before { should_receive_object.stub(:any_number_of_times?).and_return(true) }
175
+ context 'when ShouldReceive#useless_expectation? returns true' do
176
+ before { should_receive_object.stub(:useless_expectation?).and_return(true) }
177
177
 
178
178
  context 'and Configuration#replace_deprecated_method? is true' do
179
179
  before { configuration.replace_deprecated_method = true }
@@ -188,8 +188,8 @@ module Transpec
188
188
  context 'and Configuration#negative_form_of_to is "not_to"' do
189
189
  before { configuration.negative_form_of_to = 'not_to' }
190
190
 
191
- it 'invokes ShouldReceive#allowize_any_number_of_times! with "not_to"' do
192
- should_receive_object.should_receive(:allowize_any_number_of_times!).with('not_to')
191
+ it 'invokes ShouldReceive#allowize_useless_expectation! with "not_to"' do
192
+ should_receive_object.should_receive(:allowize_useless_expectation!).with('not_to')
193
193
  rewriter.process_should_receive(should_receive_object)
194
194
  end
195
195
  end
@@ -197,8 +197,8 @@ module Transpec
197
197
  context 'and Configuration#negative_form_of_to is "to_not"' do
198
198
  before { configuration.negative_form_of_to = 'to_not' }
199
199
 
200
- it 'invokes ShouldReceive#allowize_any_number_of_times! with "to_not"' do
201
- should_receive_object.should_receive(:allowize_any_number_of_times!).with('to_not')
200
+ it 'invokes ShouldReceive#allowize_useless_expectation! with "to_not"' do
201
+ should_receive_object.should_receive(:allowize_useless_expectation!).with('to_not')
202
202
  rewriter.process_should_receive(should_receive_object)
203
203
  end
204
204
  end
@@ -213,8 +213,8 @@ module Transpec
213
213
  context "and Configuration#convert_to_expect_to_receive? is #{convert_to_expect_to_receive}" do
214
214
  before { configuration.convert_to_expect_to_receive = convert_to_expect_to_receive }
215
215
 
216
- it 'invokes ShouldReceive#stubize_any_number_of_times!' do
217
- should_receive_object.should_receive(:stubize_any_number_of_times!)
216
+ it 'invokes ShouldReceive#stubize_useless_expectation!' do
217
+ should_receive_object.should_receive(:stubize_useless_expectation!)
218
218
  rewriter.process_should_receive(should_receive_object)
219
219
  end
220
220
  end
@@ -261,8 +261,8 @@ module Transpec
261
261
  end
262
262
  end
263
263
 
264
- context 'when ShouldReceive#any_number_of_times? returns false' do
265
- before { should_receive_object.stub(:any_number_of_times?).and_return(false) }
264
+ context 'when ShouldReceive#useless_expectation? returns false' do
265
+ before { should_receive_object.stub(:useless_expectation?).and_return(false) }
266
266
 
267
267
  context 'and Configuration#convert_to_expect_to_receive? is true' do
268
268
  before { configuration.convert_to_expect_to_receive = true }
@@ -349,16 +349,16 @@ module Transpec
349
349
  end
350
350
  end
351
351
 
352
- shared_examples 'invokes MethodStub#remove_any_number_of_times!' do
353
- it 'invokes MethodStub#remove_any_number_of_times!' do
354
- method_stub_object.should_receive(:remove_any_number_of_times!)
352
+ shared_examples 'invokes MethodStub#remove_allowance_for_no_message!' do
353
+ it 'invokes MethodStub#remove_allowance_for_no_message!' do
354
+ method_stub_object.should_receive(:remove_allowance_for_no_message!)
355
355
  rewriter.process_method_stub(method_stub_object)
356
356
  end
357
357
  end
358
358
 
359
- shared_examples 'does not invoke MethodStub#remove_any_number_of_times!' do
360
- it 'does not invoke MethodStub#remove_any_number_of_times!' do
361
- method_stub_object.should_not_receive(:remove_any_number_of_times!)
359
+ shared_examples 'does not invoke MethodStub#remove_allowance_for_no_message!' do
360
+ it 'does not invoke MethodStub#remove_allowance_for_no_message!' do
361
+ method_stub_object.should_not_receive(:remove_allowance_for_no_message!)
362
362
  rewriter.process_method_stub(method_stub_object)
363
363
  end
364
364
  end
@@ -371,7 +371,7 @@ module Transpec
371
371
 
372
372
  include_examples 'invokes MethodStub#allowize!'
373
373
  include_examples 'does not invoke MethodStub#replace_deprecated_method!'
374
- include_examples 'invokes MethodStub#remove_any_number_of_times!'
374
+ include_examples 'invokes MethodStub#remove_allowance_for_no_message!'
375
375
  end
376
376
 
377
377
  context 'and Configuration#replace_deprecated_method? is false' do
@@ -379,7 +379,7 @@ module Transpec
379
379
 
380
380
  include_examples 'invokes MethodStub#allowize!'
381
381
  include_examples 'does not invoke MethodStub#replace_deprecated_method!'
382
- include_examples 'does not invoke MethodStub#remove_any_number_of_times!'
382
+ include_examples 'does not invoke MethodStub#remove_allowance_for_no_message!'
383
383
  end
384
384
  end
385
385
 
@@ -391,7 +391,7 @@ module Transpec
391
391
 
392
392
  include_examples 'does not invoke MethodStub#allowize!'
393
393
  include_examples 'invokes MethodStub#replace_deprecated_method!'
394
- include_examples 'invokes MethodStub#remove_any_number_of_times!'
394
+ include_examples 'invokes MethodStub#remove_allowance_for_no_message!'
395
395
  end
396
396
 
397
397
  context 'and Configuration#replace_deprecated_method? is false' do
@@ -399,7 +399,7 @@ module Transpec
399
399
 
400
400
  include_examples 'does not invoke MethodStub#allowize!'
401
401
  include_examples 'does not invoke MethodStub#replace_deprecated_method!'
402
- include_examples 'does not invoke MethodStub#remove_any_number_of_times!'
402
+ include_examples 'does not invoke MethodStub#remove_allowance_for_no_message!'
403
403
  end
404
404
  end
405
405
  end
@@ -24,10 +24,8 @@ module Transpec
24
24
  let(:in_example_group_context?) { true }
25
25
 
26
26
  describe '.target_node?' do
27
- include ASTHelper
28
-
29
27
  let(:send_node) do
30
- scan_node(ast, include_origin_node: true) do |node|
28
+ ast.each_descendent_node do |node|
31
29
  next unless node.type == :send
32
30
  method_name = node.children[1]
33
31
  next unless method_name == :stub
@@ -53,7 +51,9 @@ module Transpec
53
51
  context 'when #stub node with Typhoeus receiver is passed' do
54
52
  let(:source) do
55
53
  <<-END
56
- ::Typhoeus.stub(url, :method => method).and_return(response)
54
+ it "is not RSpec's #stub" do
55
+ ::Typhoeus.stub(url, :method => method).and_return(response)
56
+ end
57
57
  END
58
58
  end
59
59
 
@@ -423,8 +423,8 @@ module Transpec
423
423
  end
424
424
  end
425
425
 
426
- describe '#any_number_of_times?' do
427
- subject { method_stub_object.any_number_of_times? }
426
+ describe '#allow_no_message?' do
427
+ subject { method_stub_object.allow_no_message? }
428
428
 
429
429
  context 'when it is `subject.stub(:method).any_number_of_times` form' do
430
430
  let(:source) do
@@ -450,6 +450,18 @@ module Transpec
450
450
  it { should be_true }
451
451
  end
452
452
 
453
+ context 'when it is `subject.stub(:method).at_least(0)` form' do
454
+ let(:source) do
455
+ <<-END
456
+ it 'responds to #foo' do
457
+ subject.stub(:foo).at_least(0)
458
+ end
459
+ END
460
+ end
461
+
462
+ it { should be_true }
463
+ end
464
+
453
465
  context 'when it is `subject.stub(:method)` form' do
454
466
  let(:source) do
455
467
  <<-END
@@ -463,7 +475,7 @@ module Transpec
463
475
  end
464
476
  end
465
477
 
466
- describe '#remove_any_number_of_times!' do
478
+ describe '#remove_allowance_for_no_message!' do
467
479
  context 'when it is `subject.stub(:method).any_number_of_times` form' do
468
480
  let(:source) do
469
481
  <<-END
@@ -482,7 +494,30 @@ module Transpec
482
494
  end
483
495
 
484
496
  it 'removes `.any_number_of_times`' do
485
- method_stub_object.remove_any_number_of_times!
497
+ method_stub_object.remove_allowance_for_no_message!
498
+ rewritten_source.should == expected_source
499
+ end
500
+ end
501
+
502
+ context 'when it is `subject.stub(:method).at_least(0)` form' do
503
+ let(:source) do
504
+ <<-END
505
+ it 'responds to #foo' do
506
+ subject.stub(:foo).at_least(0)
507
+ end
508
+ END
509
+ end
510
+
511
+ let(:expected_source) do
512
+ <<-END
513
+ it 'responds to #foo' do
514
+ subject.stub(:foo)
515
+ end
516
+ END
517
+ end
518
+
519
+ it 'removes `.at_least(0)`' do
520
+ method_stub_object.remove_allowance_for_no_message!
486
521
  rewritten_source.should == expected_source
487
522
  end
488
523
  end
@@ -497,7 +532,7 @@ module Transpec
497
532
  end
498
533
 
499
534
  it 'does nothing' do
500
- method_stub_object.remove_any_number_of_times!
535
+ method_stub_object.remove_allowance_for_no_message!
501
536
  rewritten_source.should == source
502
537
  end
503
538
  end
@@ -283,8 +283,8 @@ module Transpec
283
283
  end
284
284
  end
285
285
 
286
- describe '#any_number_of_times?' do
287
- subject { should_receive_object.any_number_of_times? }
286
+ describe '#useless_expectation?' do
287
+ subject { should_receive_object.useless_expectation? }
288
288
 
289
289
  context 'when it is `subject.should_receive(:method).any_number_of_times` form' do
290
290
  let(:source) do
@@ -310,6 +310,30 @@ module Transpec
310
310
  it { should be_true }
311
311
  end
312
312
 
313
+ context 'when it is `subject.should_receive(:method).at_least(0)` form' do
314
+ let(:source) do
315
+ <<-END
316
+ it 'responds to #foo' do
317
+ subject.should_receive(:foo).at_least(0)
318
+ end
319
+ END
320
+ end
321
+
322
+ it { should be_true }
323
+ end
324
+
325
+ context 'when it is `subject.should_receive(:method).at_least(1)` form' do
326
+ let(:source) do
327
+ <<-END
328
+ it 'receives #foo at least once' do
329
+ subject.should_receive(:foo).with(1).at_least(1)
330
+ end
331
+ END
332
+ end
333
+
334
+ it { should be_false }
335
+ end
336
+
313
337
  context 'when it is `subject.should_receive(:method)` form' do
314
338
  let(:source) do
315
339
  <<-END
@@ -323,7 +347,7 @@ module Transpec
323
347
  end
324
348
  end
325
349
 
326
- describe '#allowize_any_number_of_times!' do
350
+ describe '#allowize_useless_expectation!' do
327
351
  context 'when it is `subject.should_receive(:method).any_number_of_times` form' do
328
352
  let(:source) do
329
353
  <<-END
@@ -342,7 +366,7 @@ module Transpec
342
366
  end
343
367
 
344
368
  it 'converts into `allow(subject).to receive(:method)` form' do
345
- should_receive_object.allowize_any_number_of_times!
369
+ should_receive_object.allowize_useless_expectation!
346
370
  rewritten_source.should == expected_source
347
371
  end
348
372
  end
@@ -365,7 +389,53 @@ module Transpec
365
389
  end
366
390
 
367
391
  it 'converts into `allow_any_instance_of(subject).to receive(:method)` form' do
368
- should_receive_object.allowize_any_number_of_times!
392
+ should_receive_object.allowize_useless_expectation!
393
+ rewritten_source.should == expected_source
394
+ end
395
+ end
396
+
397
+ context 'when it is `subject.should_receive(:method).at_least(0)` form' do
398
+ let(:source) do
399
+ <<-END
400
+ it 'responds to #foo' do
401
+ subject.should_receive(:foo).at_least(0)
402
+ end
403
+ END
404
+ end
405
+
406
+ let(:expected_source) do
407
+ <<-END
408
+ it 'responds to #foo' do
409
+ allow(subject).to receive(:foo)
410
+ end
411
+ END
412
+ end
413
+
414
+ it 'converts into `allow(subject).to receive(:method)` form' do
415
+ should_receive_object.allowize_useless_expectation!
416
+ rewritten_source.should == expected_source
417
+ end
418
+ end
419
+
420
+ context 'when it is `SomeClass.any_instance.should_receive(:method).at_least(0)` form' do
421
+ let(:source) do
422
+ <<-END
423
+ it 'responds to #foo' do
424
+ SomeClass.any_instance.should_receive(:foo).at_least(0)
425
+ end
426
+ END
427
+ end
428
+
429
+ let(:expected_source) do
430
+ <<-END
431
+ it 'responds to #foo' do
432
+ allow_any_instance_of(SomeClass).to receive(:foo)
433
+ end
434
+ END
435
+ end
436
+
437
+ it 'converts into `allow_any_instance_of(subject).to receive(:method)` form' do
438
+ should_receive_object.allowize_useless_expectation!
369
439
  rewritten_source.should == expected_source
370
440
  end
371
441
  end
@@ -380,13 +450,13 @@ module Transpec
380
450
  end
381
451
 
382
452
  it 'does nothing' do
383
- should_receive_object.allowize_any_number_of_times!
453
+ should_receive_object.allowize_useless_expectation!
384
454
  rewritten_source.should == source
385
455
  end
386
456
  end
387
457
  end
388
458
 
389
- describe '#stubize_any_number_of_times!' do
459
+ describe '#stubize_useless_expectation!' do
390
460
  context 'when it is `subject.should_receive(:method).any_number_of_times` form' do
391
461
  let(:source) do
392
462
  <<-END
@@ -405,7 +475,7 @@ module Transpec
405
475
  end
406
476
 
407
477
  it 'converts into `subject.stub(:method)` form' do
408
- should_receive_object.stubize_any_number_of_times!
478
+ should_receive_object.stubize_useless_expectation!
409
479
  rewritten_source.should == expected_source
410
480
  end
411
481
  end
@@ -420,7 +490,7 @@ module Transpec
420
490
  end
421
491
 
422
492
  it 'does nothing' do
423
- should_receive_object.stubize_any_number_of_times!
493
+ should_receive_object.stubize_useless_expectation!
424
494
  rewritten_source.should == source
425
495
  end
426
496
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transpec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Nakayama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-23 00:00:00.000000000 Z
11
+ date: 2013-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -171,6 +171,8 @@ files:
171
171
  - Rakefile
172
172
  - bin/transpec
173
173
  - lib/transpec.rb
174
+ - lib/transpec/ast/builder.rb
175
+ - lib/transpec/ast/node.rb
174
176
  - lib/transpec/ast/scanner.rb
175
177
  - lib/transpec/ast/scope_stack.rb
176
178
  - lib/transpec/cli.rb
@@ -178,8 +180,8 @@ files:
178
180
  - lib/transpec/git.rb
179
181
  - lib/transpec/rewriter.rb
180
182
  - lib/transpec/syntax.rb
181
- - lib/transpec/syntax/any_instanceable.rb
182
- - lib/transpec/syntax/any_number_of_timesable.rb
183
+ - lib/transpec/syntax/able_to_allow_no_message.rb
184
+ - lib/transpec/syntax/able_to_target_any_instance.rb
183
185
  - lib/transpec/syntax/be_close.rb
184
186
  - lib/transpec/syntax/double.rb
185
187
  - lib/transpec/syntax/expectizable.rb
@@ -195,9 +197,9 @@ files:
195
197
  - spec/.rubocop.yml
196
198
  - spec/spec_helper.rb
197
199
  - spec/spec_spec.rb
198
- - spec/support/ast_helper.rb
199
200
  - spec/support/file_helper.rb
200
201
  - spec/support/shared_context.rb
202
+ - spec/transpec/ast/node_spec.rb
201
203
  - spec/transpec/ast/scanner_spec.rb
202
204
  - spec/transpec/ast/scope_stack_spec.rb
203
205
  - spec/transpec/cli_spec.rb
@@ -242,9 +244,9 @@ test_files:
242
244
  - spec/.rubocop.yml
243
245
  - spec/spec_helper.rb
244
246
  - spec/spec_spec.rb
245
- - spec/support/ast_helper.rb
246
247
  - spec/support/file_helper.rb
247
248
  - spec/support/shared_context.rb
249
+ - spec/transpec/ast/node_spec.rb
248
250
  - spec/transpec/ast/scanner_spec.rb
249
251
  - spec/transpec/ast/scope_stack_spec.rb
250
252
  - spec/transpec/cli_spec.rb
@@ -1,42 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'transpec/syntax'
4
-
5
- module Transpec
6
- class Syntax
7
- module AnyNumberOfTimesable
8
- def any_number_of_times?
9
- !any_number_of_times_node.nil?
10
- end
11
-
12
- def remove_any_number_of_times!
13
- return unless any_number_of_times?
14
-
15
- map = any_number_of_times_node.loc
16
- dot_any_number_of_times_range = map.dot.join(map.selector)
17
- remove(dot_any_number_of_times_range)
18
- end
19
-
20
- private
21
-
22
- def any_number_of_times_node
23
- each_following_chained_method_node do |chained_node|
24
- method_name = chained_node.children[1]
25
- return chained_node if method_name == :any_number_of_times
26
- end
27
- end
28
-
29
- def each_following_chained_method_node
30
- return to_enum(__method__) unless block_given?
31
-
32
- @ancestor_nodes.reverse.reduce(@node) do |child_node, parent_node|
33
- return unless [:send, :block].include?(parent_node.type)
34
- return unless parent_node.children.first == child_node
35
- yield parent_node, child_node
36
- parent_node
37
- end
38
- nil
39
- end
40
- end
41
- end
42
- end
@@ -1,15 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module ASTHelper
4
- def scan_node(node, options = {}, &block)
5
- yield node if options[:include_origin_node]
6
-
7
- node.children.each do |child|
8
- next unless child.is_a?(Parser::AST::Node)
9
- yield child
10
- scan_node(child, &block)
11
- end
12
-
13
- nil
14
- end
15
- end