rr-to-rspec-converter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1f5c86c0d425dadbe259db326a03ead4254953a94ba52522c949cb265c45abbd
4
+ data.tar.gz: e97822920943a7cce484dbad587633434763c724377d3f16de26bc6ddc1db6a1
5
+ SHA512:
6
+ metadata.gz: 13bd7d7f0e7e8f056debe76046f51e5e7c2065649623ca15b4b17a83a847b16dfb69fc10db0ff73f0ee37966c5bf59ef8c07671f89a901777eaa7ae333961f7a
7
+ data.tar.gz: 8c92565876a91de02a30d6899f95a323750102718cf2d4df998ebac4d2cb95c22908a9b1c82f5f5847944a1a29aae9852839ad8554bbe0139514660c0b86c283
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .idea
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rr-to-rspec-converter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Karl Varga
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,320 @@
1
+ # RR to RSpec Converter
2
+
3
+ This is a modification of the [Rails5 Spec Converter](https://github.com/tjgrathwell/rr-to-rspec-converter) tool to handle converting from [RR v1.1.2](https://github.com/rr/rr/blob/master/doc/03_api_overview.md) to [RSpec v3.6.0](https://relishapp.com/rspec/rspec-mocks/v/3-6/docs) syntax for mocking and stubbing. This is a basic implementation to handle the needs of a large codebase of 6000+ tests. It handles most usages but is not exhaustive.
4
+
5
+ * [Installation](#installation)
6
+ * [Usage](#usage)
7
+ * [Development](#development)
8
+ * [RR](#rr)
9
+ + [Mocking and Stubbing](#mocking-and-stubbing)
10
+ - [#mock](#%23mock)
11
+ - [#mock!](#%23mock)
12
+ - [#stub](#%23stub)
13
+ - [#stub!](#%23stub)
14
+ - [#dont_allow](#%23dont_allow)
15
+ - [#dont_allow!](#%23dont_allow)
16
+ - [#proxy](#%23proxy)
17
+ - [#proxy!](#%23proxy)
18
+ - [#instance_of](#%23instance_of)
19
+ - [#instance_of!](#%23instance_of)
20
+ - [#any_instance_of](#%23any_instance_of)
21
+ - [#with_any_args](#%23with_any_args)
22
+ - [#with_no_args](#%23with_no_args)
23
+ - [#times](#%23times)
24
+ - [#any_times](#%23any_times)
25
+ - [#returns](#%23returns)
26
+ - [#yields](#%23yields)
27
+ - [#with](#%23with)
28
+ - [#never](#%23never)
29
+ - [#once](#%23once)
30
+ - [#at_least](#%23at_least)
31
+ - [#at_most](#%23at_most)
32
+ + [Matchers](#matchers)
33
+ - [#anything](#%23anything)
34
+ - [#is_a](#%23is_a)
35
+ - [#numeric](#%23numeric)
36
+ - [#boolean](#%23boolean)
37
+ - [#duck_type](#%23duck_type)
38
+ - [#hash_including](#%23hash_including)
39
+ - [#rr_satisfy](#%23rr_satisfy)
40
+ + [Module References](#module-references)
41
+ - [RR::WildcardMatchers::HashIncluding](#rrwildcardmatchershashincluding)
42
+ - [RR::WildcardMatchers::Satisfy](#rrwildcardmatcherssatisfy)
43
+ - [RR.reset](#rrreset)
44
+ * [Manual Cleanup](#manual-cleanup)
45
+ * [Configure RSpec](#configure-rspec)
46
+ * [License](#license)
47
+ ## Installation
48
+
49
+ Install the gem standalone like so:
50
+
51
+ $ gem install rr-to-rspec-converter
52
+
53
+ ## Usage
54
+
55
+ Make sure you've committed everything to Git first, then
56
+
57
+ $ cd some-project
58
+ $ rr-to-rspec-converter
59
+
60
+ This will update all the files in that directory matching the globs `spec/**/*_spec.rb` or `test/**/*_test.rb`.
61
+
62
+ If you want to specify a specific set of files instead, you can run `rr-to-rspec-converter path_to_my_files`.
63
+
64
+ By default it will make some noise, run with `rr-to-rspec-converter --quiet` if you want it not to.
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ ## RR
71
+
72
+ The following lists each RR API method and the conversion that is applied. Some cases are not handled. To see which ones, search for `unhandled` in this document.
73
+
74
+ ### Mocking and Stubbing
75
+
76
+ #### #mock
77
+
78
+ mock(view).render.with_any_args.twice do |*args|
79
+ if args.first == {:partial => "user_info"}
80
+ "User Info"
81
+ else
82
+ "Stuff in the view #{args.inspect}"
83
+ end
84
+ end
85
+
86
+ => expect(object).to receive(:method).with(arguments).and_return(return_val)
87
+
88
+ #### #mock!
89
+
90
+ * Nested `mock!`, e.g. `mock.something.mock!.method { result }` => `and_return(double(method: result))`
91
+
92
+
93
+ Simnple usage:
94
+
95
+ mock!
96
+ =>
97
+ instance_double("ConsoleNotifier")
98
+ class_double("ConsoleNotifier")
99
+ [object_double(User.new, :save => true)](https://relishapp.com/rspec/rspec-mocks/docs/verifying-doubles/using-an-object-double)
100
+ object_double("MyApp::LOGGER", :info => nil).as_stubbed_const
101
+
102
+ Then assert expectations with `have_received` or `receive`.
103
+
104
+ #### #stub
105
+
106
+ By itself:
107
+
108
+ x = stub
109
+
110
+ => [double](https://relishapp.com/rspec/rspec-mocks/v/3-8/docs/basics/partial-test-doubles)
111
+
112
+ With method:
113
+
114
+ x = stub(object).method.with(args).returns { return_val }
115
+
116
+ => allow(object).to receive(:method).with(arguments).and_return(return_val)
117
+
118
+ #### #stub!
119
+
120
+ **Unhandled**
121
+
122
+ =>
123
+ instance_double("ConsoleNotifier")
124
+ class_double("ConsoleNotifier")
125
+ object_double(User.new, :save => true)
126
+ object_double("MyApp::LOGGER", :info => nil).as_stubbed_const
127
+
128
+ #### #dont_allow
129
+
130
+ Normal usage:
131
+
132
+ dont_allow(object).method(args)
133
+
134
+ => expect(object).not_to receive(:method).with(args)
135
+
136
+ Inside `any_instance_of` block (**Unhandled*):*
137
+
138
+ any_instance_of(klass) do |o|
139
+ dont_allow(object).method(args)
140
+ end
141
+
142
+ => expect_any_instance_of(klass).not_to receive(:method).with(args)
143
+
144
+ #### #dont_allow!
145
+
146
+ **Unhandled**
147
+
148
+ #### #proxy
149
+
150
+ **Unhandled**
151
+
152
+ =>
153
+ and_call_original, or
154
+ and_wrap_original
155
+
156
+ allow(Calculator).to receive(:add).and_call_original
157
+ allow(Calculator).to receive(:add).with(2, 3).and_return(-5)
158
+
159
+ expect(API).to receive(:solve_for).and_wrap_original { |m, *args| m.call(*args).first(5) }
160
+ expect(API.solve_for(100)).to eq [1,2,3,4,5]
161
+
162
+ #### #proxy!
163
+
164
+ **Unhandled**
165
+
166
+ #### #instance_of
167
+
168
+ => instance_of
169
+
170
+ #### #instance_of!
171
+
172
+ **Unhandled**
173
+
174
+ #### #any_instance_of
175
+
176
+ any_instance_of(User) do |u|
177
+ stub(u).valid? { false }
178
+ end
179
+ or
180
+ any_instance_of(User, :valid? => false)
181
+ or
182
+ any_instance_of(User, :valid? => lambda { false })
183
+
184
+ =>
185
+
186
+ allow_any_instance_of(klass).to receive_messages(:method => return_value)
187
+ expect_any_instance_of(klass).to receive_messages(:method => return_value)
188
+ allow_any_instance_of(klass).to receive(:method => return_value)
189
+ expect_any_instance_of(klass).to receive(:method => return_value)
190
+
191
+
192
+ #### #with_any_args
193
+
194
+ mock(PetitionGroup).find.with_any_args.returns(petition_group)
195
+
196
+ => with(any_args)
197
+
198
+ #### #with_no_args
199
+
200
+ => with(no_args)
201
+
202
+ #### #times
203
+
204
+ =>
205
+ times(0) => expect().not_to receive()
206
+ times(1) => once
207
+ times(2) => twice
208
+ times(n) => exactly(n).times **Unhandled**
209
+ times(any_times) => allow().to receive() OR at_least(:once)
210
+
211
+ #### #any_times
212
+
213
+ Converts to `at_least(:once)` but this interpretation may be incorrect. The intention of the test may be to `allow` the message to be received - in which case `mock` should not have been used.
214
+
215
+ .any_times
216
+ .times(any_times)
217
+
218
+ => allow().to receive() OR at_least(:once)
219
+
220
+ #### #returns
221
+
222
+ mock(PetitionGroup).find.with_any_args.returns(petition_group)
223
+
224
+ => and_return
225
+
226
+ #### #yields
227
+
228
+ **Unhandled**
229
+
230
+ #### #with
231
+
232
+ => with
233
+
234
+ #### #never
235
+
236
+ => expect(object).not_to receive(:method)
237
+
238
+ #### #once
239
+
240
+ => once
241
+
242
+ #### #at_least
243
+
244
+ =>
245
+ at_least(0) => Use `allow` instead of `expect` **Unhandled**
246
+ at_least(1) => at_least(:once)
247
+ at_least(2) => at_least(:twice)
248
+ at_least(n) => at_least(n).times
249
+
250
+ #### #at_most
251
+
252
+ =>
253
+ at_most(1) => at_most(:once)
254
+ at_most(2) => at_most(:twice)
255
+ at_most(n) => at_most(n).times
256
+
257
+ ### Matchers
258
+
259
+ #### #anything
260
+
261
+ => anything
262
+
263
+ #### #is_a
264
+
265
+ => kind_of
266
+
267
+ #### #numeric
268
+
269
+ => kind_of(Numeric)
270
+
271
+ #### #boolean
272
+
273
+ => boolean
274
+
275
+ #### #duck_type
276
+
277
+ => duck_type
278
+
279
+ #### #hash_including
280
+
281
+ => hash_including
282
+
283
+ #### #rr_satisfy
284
+
285
+ Use RSpec `satisfy` method. Would need to rewrite to use arguments from block, which alters the intent of the test. Will probably require manual fixes post conversion.
286
+
287
+ => satisfy
288
+
289
+ ### Module References
290
+
291
+ #### RR::WildcardMatchers::HashIncluding
292
+
293
+ => hash_including
294
+
295
+ #### RR::WildcardMatchers::Satisfy
296
+
297
+ => rr_satisfy
298
+
299
+ #### RR.reset
300
+
301
+ => removes line
302
+
303
+ ## Manual Cleanup
304
+
305
+ * Remove all comments that reference `/\brr\b/i`
306
+ * Remove 'rr' from Gemfile
307
+ * Remove `config.mock_with :rr` from `spec/spec_helper.rb`
308
+
309
+ ## Configure RSpec
310
+
311
+ RSpec.configure do |config|
312
+ config.mock_with :rspec do |mocks|
313
+ mocks.verify_partial_doubles = true
314
+ end
315
+ end
316
+
317
+ ## License
318
+
319
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
320
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec_converter)
5
+
6
+ task :default => :spec_converter
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rr_to_rspec_converter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
4
+ require 'rr_to_rspec_converter/cli'
5
+
6
+ result = RrToRspecConverter::CLI.new.run
7
+ exit(result ? 0 : 1)
@@ -0,0 +1,48 @@
1
+ require 'optparse'
2
+ require 'rr_to_rspec_converter/text_transformer'
3
+ require 'rr_to_rspec_converter/text_transformer_options'
4
+ require 'rr_to_rspec_converter/version'
5
+
6
+ module RrToRspecConverter
7
+ class CLI
8
+ def initialize
9
+ @options = RrToRspecConverter::TextTransformerOptions.new
10
+ OptionParser.new do |opts|
11
+ opts.banner = "Usage: rr-to-rspec-converter [options] [files]"
12
+
13
+ opts.on("--version", "Print version number") do |q|
14
+ puts RrToRspecConverter::VERSION
15
+ exit
16
+ end
17
+
18
+ opts.on("-q", "--quiet", "Run quietly") do |q|
19
+ @options.quiet = q
20
+ end
21
+ end.parse!
22
+
23
+ @files = ARGV
24
+ end
25
+
26
+ def run
27
+ paths = @files.length > 0 ? @files : ["spec/**/*.rb", "test/**/*.rb"]
28
+
29
+ paths.each do |path|
30
+ Dir.glob(path) do |file_path|
31
+ log "Processing: #{file_path}"
32
+
33
+ original_content = File.read(file_path)
34
+ @options.file_path = file_path
35
+ transformed_content = RrToRspecConverter::TextTransformer.new(original_content, @options).transform
36
+ File.write(file_path, transformed_content)
37
+ rescue Errno::EISDIR
38
+ end
39
+ end
40
+ end
41
+
42
+ def log(str)
43
+ return if @options.quiet?
44
+
45
+ puts str
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,203 @@
1
+ require 'parser/current'
2
+ require 'astrolabe/builder'
3
+ require 'rr_to_rspec_converter/text_transformer_options'
4
+
5
+ module RrToRspecConverter
6
+ class TextTransformer
7
+ def initialize(content, options = RrToRspecConverter::TextTransformerOptions.new)
8
+ @options = options
9
+ @content = content
10
+
11
+ @source_buffer = Parser::Source::Buffer.new('(string)')
12
+ @source_buffer.source = @content
13
+
14
+ ast_builder = Astrolabe::Builder.new
15
+ @parser = Parser::CurrentRuby.new(ast_builder)
16
+
17
+ @source_rewriter = Parser::Source::TreeRewriter.new(@source_buffer)
18
+ end
19
+
20
+ def transform
21
+ root_node = @parser.parse(@source_buffer)
22
+ unless root_node
23
+ log "Parser saw some unparsable content, skipping...\n\n"
24
+ return @source_rewriter.process
25
+ end
26
+
27
+ root_node.each_node(:send) do |node|
28
+ target, verb, action, *args = node.children
29
+
30
+ if verb == :with_any_args
31
+ @source_rewriter.replace(node.loc.selector, 'with(any_args)')
32
+ elsif verb == :any_times
33
+ @source_rewriter.replace(node.loc.selector, 'at_least(:once)') if node.parent.children[1] != :times
34
+ elsif verb == :returns
35
+ @source_rewriter.replace(node.loc.selector, 'and_return')
36
+ elsif verb == :times
37
+ if action&.int_type?
38
+ times = action.children[0]
39
+ if times == 1
40
+ @source_rewriter.replace(range(target.loc.expression.end_pos, node.loc.expression.end_pos), '.once')
41
+ elsif times == 2
42
+ @source_rewriter.replace(range(target.loc.expression.end_pos, node.loc.expression.end_pos), '.twice')
43
+ end
44
+ elsif action&.send_type? && action&.children[1] == :any_times
45
+ @source_rewriter.replace(range(target.loc.expression.end_pos, node.loc.expression.end_pos), '.at_least(:once)')
46
+ end
47
+ elsif verb == :at_least && action.int_type?
48
+ times = action.children[0]
49
+ if times == 1
50
+ @source_rewriter.replace(range(target.loc.expression.end_pos, node.loc.expression.end_pos), '.at_least(:once)')
51
+ elsif times == 2
52
+ @source_rewriter.replace(range(target.loc.expression.end_pos, node.loc.expression.end_pos), '.at_least(:twice)')
53
+ else
54
+ @source_rewriter.replace(range(target.loc.expression.end_pos, node.loc.expression.end_pos), ".at_least(#{times}).times")
55
+ end
56
+ elsif verb == :at_most && action.int_type?
57
+ times = action.children[0]
58
+ if times == 1
59
+ @source_rewriter.replace(range(target.loc.expression.end_pos, node.loc.expression.end_pos), '.at_most(:once)')
60
+ elsif times == 2
61
+ @source_rewriter.replace(range(target.loc.expression.end_pos, node.loc.expression.end_pos), '.at_most(:twice)')
62
+ else
63
+ @source_rewriter.replace(range(target.loc.expression.end_pos, node.loc.expression.end_pos), ".at_most(#{times}).times")
64
+ end
65
+ elsif verb == :is_a
66
+ @source_rewriter.replace(node.loc.selector, 'kind_of')
67
+ elsif verb == :numeric
68
+ @source_rewriter.replace(node.loc.selector, 'kind_of(Numeric)')
69
+
70
+ # RR::WildcardMatchers::HashIncluding => hash_including
71
+ elsif verb == :new && target.const_type? && target.children.last == :HashIncluding
72
+ range = range(target.loc.expression.begin_pos, node.loc.selector.end_pos)
73
+ @source_rewriter.replace(range, 'hash_including')
74
+
75
+ # RR::WildcardMatchers::Satisfy => rr_satsify
76
+ elsif verb == :new && target.const_type? && target.children.last == :Satisfy
77
+ range = range(target.loc.expression.begin_pos, node.loc.selector.end_pos)
78
+ @source_rewriter.replace(range, 'rr_satisfy')
79
+
80
+ # rr_satisfy => satisfy
81
+ elsif verb == :rr_satisfy
82
+ @source_rewriter.replace(node.loc.selector, 'satisfy')
83
+
84
+ # RR.reset => removed
85
+ elsif verb == :reset && target.const_type? && target.children.last == :RR
86
+ sibling = node.root? ? nil : node.parent.children[node.sibling_index + 1]
87
+ end_pos = sibling ? sibling.loc.expression.begin_pos : node.loc.expression.end_pos
88
+ @source_rewriter.remove(range(node.loc.expression.begin_pos, end_pos))
89
+
90
+ # any_instance_of(klass, method: return) => allow_any_instance_of
91
+ # any_instance_of(klass) { block } => allow_any_instance_of / expect_any_instance_of
92
+ elsif verb == :any_instance_of
93
+ class_name = action.loc.expression.source
94
+ if !args.empty?
95
+ stubs = []
96
+ indent = line_indent(node)
97
+ node.each_node(:pair) do |pair|
98
+ method_name = pair.children.first.loc.expression.source.sub(/^:/,'')
99
+ method_result = pair.children.last.loc.expression.source
100
+ stubs << "allow_any_instance_of(#{class_name}).to receive(:#{method_name}).and_return(#{method_result})"
101
+ end
102
+
103
+ @source_rewriter.replace(node.loc.expression, stubs.join("\n#{indent}"))
104
+ else # parent is a block type
105
+ begin_node = node.parent.children.last
106
+ begin_node.each_node(:send) do |send_node|
107
+ stub_method = send_node.children[1]
108
+ receive_method = send_node.parent.children[1]
109
+ has_args = !send_node.parent.children[2].nil?
110
+ next unless [:stub, :mock].include?(stub_method)
111
+
112
+ expectation = remove_never_node(send_node) ? 'not_to' : 'to'
113
+ expect_method = stub_method == :stub ? 'allow' : 'expect'
114
+ @source_rewriter.replace(range(send_node.parent.loc.expression.begin_pos, send_node.parent.loc.selector.end_pos), "#{expect_method}_any_instance_of(#{class_name}).#{expectation} receive(:#{receive_method})#{has_args ? '.with' : ''}")
115
+ end
116
+ # Remove the any_instance_of
117
+ # If it's a begin block, we have to go a level deeper
118
+ first_statement = begin_node.begin_type? ? begin_node.children.first : begin_node
119
+ last_statement = begin_node.begin_type? ? begin_node.children.last : begin_node
120
+ @source_rewriter.remove(range(node.loc.expression.begin_pos, first_statement.loc.expression.begin_pos)) # start of block
121
+ @source_rewriter.remove(range(last_statement.loc.expression.end_pos, node.parent.loc.expression.end_pos)) # end of block
122
+ end
123
+
124
+ # mock => expect().to receive
125
+ elsif verb == :mock
126
+ next if node.each_ancestor(:block).find { |n| n.children[0].children[1] == :any_instance_of }
127
+ if action.nil?
128
+ @source_rewriter.replace(node.loc.selector, 'double')
129
+ else
130
+ expectation = remove_never_node(node) ? 'not_to' : 'to'
131
+
132
+ @source_rewriter.replace(node.loc.selector, 'expect')
133
+ method_name = node.parent.loc.selector.source
134
+ has_args = !node.parent.children[2].nil?
135
+ @source_rewriter.replace(node.parent.loc.selector, "#{expectation} receive(:#{method_name})#{has_args ? '.with' : ''}")
136
+ end
137
+
138
+ # stub => allow().to receive and double
139
+ elsif verb == :stub
140
+ next if node.each_ancestor(:block).find { |n| n.children[0].children[1] == :any_instance_of }
141
+ if action.nil?
142
+ @source_rewriter.replace(node.loc.selector, 'double')
143
+ else
144
+ @source_rewriter.replace(node.loc.selector, 'allow')
145
+ if node.parent.send_type?
146
+ method_name = node.parent.loc.selector.source
147
+ has_args = !node.parent.children[2].nil?
148
+ @source_rewriter.replace(node.parent.loc.selector, "to receive(:#{method_name})#{has_args ? '.with' : ''}")
149
+ else
150
+ # Stubbing a method on Kernel
151
+ method_name = node.children[2].children[1]
152
+ @source_rewriter.replace(node.loc.expression, "allow(Kernel).to receive(:#{method_name})")
153
+ end
154
+ end
155
+
156
+ # dont_allow => expect().not_to receive
157
+ elsif verb == :dont_allow
158
+ @source_rewriter.replace(node.loc.selector, 'expect')
159
+ if args.empty?
160
+ method_name = node.parent.loc.selector.source
161
+ has_args = !node.parent.children[2].nil?
162
+ @source_rewriter.replace(node.parent.loc.selector, "not_to receive(:#{method_name})#{has_args ? '.with' : ''}")
163
+ else
164
+ # dont_allow(object, :method) syntax
165
+ method_name = args.first.children[0]
166
+ @source_rewriter.replace(range(action.loc.expression.end_pos, node.loc.expression.end_pos), ").not_to receive(:#{method_name})")
167
+ end
168
+ end
169
+ end
170
+
171
+ @source_rewriter.process
172
+ end
173
+
174
+ private
175
+
176
+ def range(start_pos, end_pos)
177
+ Parser::Source::Range.new(@source_buffer, start_pos, end_pos)
178
+ end
179
+
180
+ # Find any never or times(0) methods chained in the expression and remove them.
181
+ # Return true if a node was found and removed, false otherwise.
182
+ def remove_never_node(node)
183
+ if never_node = node.each_ancestor(:send).find { |n| n.children[1] == :never }
184
+ @source_rewriter.remove(range(never_node.children.first.loc.expression.end_pos, never_node.loc.selector.end_pos))
185
+ return true
186
+ elsif times_zero_node = node.each_ancestor(:send).find { |n| n.children[1] == :times && n.children[2].int_type? && n.children[2].children[0] == 0 }
187
+ @source_rewriter.remove(range(times_zero_node.children[0].loc.expression.end_pos, times_zero_node.loc.expression.end_pos))
188
+ return true
189
+ end
190
+ false
191
+ end
192
+
193
+ def line_indent(node)
194
+ node.loc.expression.source_line.match(/^(\s*)/)[1]
195
+ end
196
+
197
+ def log(str)
198
+ return if @options.quiet?
199
+
200
+ puts str
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,15 @@
1
+ module RrToRspecConverter
2
+ class TextTransformerOptions
3
+ attr_accessor :file_path
4
+ attr_writer :quiet
5
+
6
+ def initialize
7
+ @file_path = nil
8
+ @quiet = false
9
+ end
10
+
11
+ def quiet?
12
+ @quiet
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module RrToRspecConverter
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'rr_to_rspec_converter/version'
2
+ require 'rr_to_rspec_converter/text_transformer'
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rr_to_rspec_converter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rr-to-rspec-converter"
8
+ spec.version = RrToRspecConverter::VERSION
9
+ spec.authors = ["Karl Varga"]
10
+ spec.email = ["kjvarga@gmail.com"]
11
+
12
+ spec.summary = %q{A tool to convert RR mocks/stubs to RSpec syntax}
13
+ spec.description = %q{Automatic conversion of RR mocks and stubs to modern RSpec syntax.}
14
+ spec.homepage = "https://github.com/kjvarga/rr-to-rspec-converter"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency 'parser', '~> 2.3'
23
+ spec.add_runtime_dependency 'astrolabe', '~> 1.2'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.10'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.8'
28
+ spec.add_development_dependency 'byebug', '~> 10.0'
29
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rr-to-rspec-converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Karl Varga
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: astrolabe
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description: Automatic conversion of RR mocks and stubs to modern RSpec syntax.
98
+ email:
99
+ - kjvarga@gmail.com
100
+ executables:
101
+ - rr-to-rspec-converter
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".ruby-version"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - exe/rr-to-rspec-converter
116
+ - lib/rr_to_rspec_converter.rb
117
+ - lib/rr_to_rspec_converter/cli.rb
118
+ - lib/rr_to_rspec_converter/text_transformer.rb
119
+ - lib/rr_to_rspec_converter/text_transformer_options.rb
120
+ - lib/rr_to_rspec_converter/version.rb
121
+ - rr-to-rspec-converter.gemspec
122
+ homepage: https://github.com/kjvarga/rr-to-rspec-converter
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.7.6
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: A tool to convert RR mocks/stubs to RSpec syntax
146
+ test_files: []