parser 2.7.0.5 → 2.7.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +21 -32
  4. data/CHANGELOG.md +59 -1
  5. data/README.md +2 -2
  6. data/Rakefile +2 -1
  7. data/doc/AST_FORMAT.md +106 -3
  8. data/lib/parser.rb +1 -0
  9. data/lib/parser/all.rb +1 -0
  10. data/lib/parser/ast/processor.rb +9 -0
  11. data/lib/parser/builders/default.rb +103 -12
  12. data/lib/parser/context.rb +1 -0
  13. data/lib/parser/current.rb +13 -4
  14. data/lib/parser/diagnostic.rb +1 -1
  15. data/lib/parser/diagnostic/engine.rb +1 -2
  16. data/lib/parser/lexer.rl +15 -1
  17. data/lib/parser/macruby.y +14 -4
  18. data/lib/parser/messages.rb +15 -0
  19. data/lib/parser/meta.rb +4 -4
  20. data/lib/parser/ruby18.y +2 -0
  21. data/lib/parser/ruby19.y +14 -4
  22. data/lib/parser/ruby20.y +14 -4
  23. data/lib/parser/ruby21.y +9 -2
  24. data/lib/parser/ruby22.y +9 -2
  25. data/lib/parser/ruby23.y +9 -2
  26. data/lib/parser/ruby24.y +9 -2
  27. data/lib/parser/ruby25.y +9 -2
  28. data/lib/parser/ruby26.y +9 -2
  29. data/lib/parser/ruby27.y +28 -8
  30. data/lib/parser/ruby28.y +3043 -0
  31. data/lib/parser/rubymotion.y +14 -4
  32. data/lib/parser/runner.rb +26 -2
  33. data/lib/parser/runner/ruby_rewrite.rb +2 -2
  34. data/lib/parser/source/buffer.rb +3 -1
  35. data/lib/parser/source/comment/associator.rb +14 -4
  36. data/lib/parser/source/map/endless_definition.rb +23 -0
  37. data/lib/parser/source/range.rb +17 -1
  38. data/lib/parser/source/tree_rewriter.rb +115 -12
  39. data/lib/parser/source/tree_rewriter/action.rb +135 -26
  40. data/lib/parser/tree_rewriter.rb +1 -2
  41. data/lib/parser/version.rb +1 -1
  42. data/parser.gemspec +3 -2
  43. data/test/helper.rb +49 -6
  44. data/test/parse_helper.rb +27 -23
  45. data/test/test_ast_processor.rb +32 -0
  46. data/test/test_base.rb +1 -1
  47. data/test/test_current.rb +2 -0
  48. data/test/test_diagnostic.rb +6 -7
  49. data/test/test_diagnostic_engine.rb +5 -8
  50. data/test/test_lexer.rb +17 -8
  51. data/test/test_meta.rb +12 -0
  52. data/test/test_parser.rb +477 -54
  53. data/test/test_runner_parse.rb +22 -1
  54. data/test/test_runner_rewrite.rb +1 -1
  55. data/test/test_source_buffer.rb +4 -1
  56. data/test/test_source_comment.rb +2 -2
  57. data/test/test_source_comment_associator.rb +47 -15
  58. data/test/test_source_map.rb +1 -2
  59. data/test/test_source_range.rb +29 -9
  60. data/test/test_source_rewriter.rb +4 -4
  61. data/test/test_source_rewriter_action.rb +2 -2
  62. data/test/test_source_tree_rewriter.rb +201 -13
  63. metadata +19 -12
@@ -7,7 +7,7 @@ class TestRunnerParse < Minitest::Test
7
7
  PATH_TO_RUBY_PARSE = File.expand_path('../bin/ruby-parse', __dir__).freeze
8
8
 
9
9
  def assert_prints(argv, expected_output)
10
- stdout, stderr, status = Open3.capture3(PATH_TO_RUBY_PARSE, *argv)
10
+ stdout, _stderr, status = Open3.capture3(PATH_TO_RUBY_PARSE, *argv)
11
11
 
12
12
  assert_equal 0, status.to_i
13
13
  assert_includes(stdout, expected_output)
@@ -18,6 +18,27 @@ class TestRunnerParse < Minitest::Test
18
18
  's(:int, 123)'
19
19
  end
20
20
 
21
+ def test_emit_modern_ruby
22
+ assert_prints ['-e', '->{}'],
23
+ '(lambda)'
24
+ assert_prints ['-e', 'self[1] = 2'],
25
+ 'indexasgn'
26
+ end
27
+
28
+ def test_emit_legacy
29
+ assert_prints ['--legacy', '-e', '->{}'],
30
+ '(send nil :lambda)'
31
+ assert_prints ['--legacy', '-e', 'self[1] = 2'],
32
+ ':[]='
33
+ end
34
+
35
+ def test_emit_legacy_lambda
36
+ assert_prints ['--legacy-lambda', '-e', '->{}'],
37
+ '(send nil :lambda)'
38
+ assert_prints ['--legacy-lambda', '-e', 'self[1] = 2'],
39
+ 'indexasgn'
40
+ end
41
+
21
42
  def test_emit_json
22
43
  assert_prints ['--emit-json', '-e', '123'],
23
44
  '["int",123]'
@@ -21,7 +21,7 @@ class TestRunnerRewrite < Minitest::Test
21
21
  expected_file = @fixtures_dir + output
22
22
 
23
23
  FileUtils.cp(@fixtures_dir + input, sample_file_expanded)
24
- stdout, stderr, exit_code = Dir.chdir @test_dir do
24
+ stdout, stderr, _exit_code = Dir.chdir @test_dir do
25
25
  Open3.capture3 %Q{
26
26
  #{Shellwords.escape(@ruby_rewrite.to_s)} #{args} \
27
27
  #{Shellwords.escape(sample_file_expanded.to_s)}
@@ -20,6 +20,9 @@ class TestSourceBuffer < Minitest::Test
20
20
 
21
21
  buffer = Parser::Source::Buffer.new('(string)', 5)
22
22
  assert_equal 5, buffer.first_line
23
+
24
+ buffer = Parser::Source::Buffer.new('(string)', source: '2+2')
25
+ assert_equal '2+2', buffer.source
23
26
  end
24
27
 
25
28
  def test_source_setter
@@ -45,7 +48,7 @@ class TestSourceBuffer < Minitest::Test
45
48
  ].join("\n")
46
49
  end
47
50
 
48
- assert_match /invalid byte sequence in UTF\-8/, error.message
51
+ assert_match(/invalid byte sequence in UTF\-8/, error.message)
49
52
  end
50
53
 
51
54
  def test_read
@@ -4,8 +4,8 @@ require 'helper'
4
4
 
5
5
  class TestSourceComment < Minitest::Test
6
6
  def setup
7
- @buf = Parser::Source::Buffer.new('(string)')
8
- @buf.source = "# foo\n=begin foo\nbar\n=end baz\n"
7
+ @buf = Parser::Source::Buffer.new('(string)',
8
+ source: "# foo\n=begin foo\nbar\n=end baz\n")
9
9
  end
10
10
 
11
11
  def range(s, e)
@@ -95,7 +95,7 @@ class Foo
95
95
  end
96
96
  END
97
97
 
98
- klass_node = ast
98
+ _klass_node = ast
99
99
  method_node = ast.children[2]
100
100
  body = method_node.children[2]
101
101
  f1_1_node = body.children[0]
@@ -175,7 +175,7 @@ class Foo
175
175
  end
176
176
  END
177
177
 
178
- klass_node = ast
178
+ _klass_node = ast
179
179
  method_node = ast.children[2]
180
180
  body = method_node.children[2]
181
181
  f1_1_node = body.children[0]
@@ -201,12 +201,12 @@ end
201
201
  end
202
202
 
203
203
  def test_associate_empty_tree
204
- ast, associations = associate("")
204
+ _ast, associations = associate("")
205
205
  assert_equal 0, associations.size
206
206
  end
207
207
 
208
208
  def test_associate_shebang_only
209
- ast, associations = associate(<<-END)
209
+ _ast, associations = associate(<<-END)
210
210
  #!ruby
211
211
  class Foo
212
212
  end
@@ -216,7 +216,7 @@ end
216
216
  end
217
217
 
218
218
  def test_associate_frozen_string_literal
219
- ast, associations = associate(<<-END)
219
+ _ast, associations = associate(<<-END)
220
220
  # frozen_string_literal: true
221
221
  class Foo
222
222
  end
@@ -226,7 +226,7 @@ end
226
226
  end
227
227
 
228
228
  def test_associate_frozen_string_literal_dash_star_dash
229
- ast, associations = associate(<<-END)
229
+ _ast, associations = associate(<<-END)
230
230
  # -*- frozen_string_literal: true -*-
231
231
  class Foo
232
232
  end
@@ -236,7 +236,7 @@ end
236
236
  end
237
237
 
238
238
  def test_associate_frozen_string_literal_no_space_after_colon
239
- ast, associations = associate(<<-END)
239
+ _ast, associations = associate(<<-END)
240
240
  # frozen_string_literal:true
241
241
  class Foo
242
242
  end
@@ -246,7 +246,7 @@ end
246
246
  end
247
247
 
248
248
  def test_associate_warn_indent
249
- ast, associations = associate(<<-END)
249
+ _ast, associations = associate(<<-END)
250
250
  # warn_indent: true
251
251
  class Foo
252
252
  end
@@ -256,7 +256,7 @@ end
256
256
  end
257
257
 
258
258
  def test_associate_warn_indent_dash_star_dash
259
- ast, associations = associate(<<-END)
259
+ _ast, associations = associate(<<-END)
260
260
  # -*- warn_indent: true -*-
261
261
  class Foo
262
262
  end
@@ -266,7 +266,7 @@ end
266
266
  end
267
267
 
268
268
  def test_associate_warn_past_scope
269
- ast, associations = associate(<<-END)
269
+ _ast, associations = associate(<<-END)
270
270
  # warn_past_scope: true
271
271
  class Foo
272
272
  end
@@ -276,7 +276,7 @@ end
276
276
  end
277
277
 
278
278
  def test_associate_warn_past_scope_dash_star_dash
279
- ast, associations = associate(<<-END)
279
+ _ast, associations = associate(<<-END)
280
280
  # -*- warn_past_scope: true -*-
281
281
  class Foo
282
282
  end
@@ -286,7 +286,7 @@ end
286
286
  end
287
287
 
288
288
  def test_associate_multiple
289
- ast, associations = associate(<<-END)
289
+ _ast, associations = associate(<<-END)
290
290
  # frozen_string_literal: true; warn_indent: true
291
291
  class Foo
292
292
  end
@@ -296,7 +296,7 @@ end
296
296
  end
297
297
 
298
298
  def test_associate_multiple_dash_star_dash
299
- ast, associations = associate(<<-END)
299
+ _ast, associations = associate(<<-END)
300
300
  # -*- frozen_string_literal: true; warn_indent: true -*-
301
301
  class Foo
302
302
  end
@@ -306,7 +306,7 @@ end
306
306
  end
307
307
 
308
308
  def test_associate_no_comments
309
- ast, associations = associate(<<-END)
309
+ _ast, associations = associate(<<-END)
310
310
  class Foo
311
311
  end
312
312
  END
@@ -315,7 +315,7 @@ end
315
315
  end
316
316
 
317
317
  def test_associate_comments_after_root_node
318
- ast, associations = associate(<<-END)
318
+ _ast, associations = associate(<<-END)
319
319
  class Foo
320
320
  end
321
321
  # not associated
@@ -364,4 +364,36 @@ x
364
364
  assert_equal ['# bar'],
365
365
  associations[send_node].map(&:text)
366
366
  end
367
+
368
+ def test_associate_conditional_parent_class
369
+ ast, associations = associate(<<-END)
370
+ class Foo
371
+ # bar
372
+ class Bar
373
+ end
374
+ end if some_condition
375
+ END
376
+
377
+ if_node = ast
378
+ _condition, foo_class = if_node.children
379
+ _foo, _sub_class, bar_class = foo_class.children
380
+
381
+ assert_equal 1, associations.size
382
+ assert_equal ['# bar'],
383
+ associations[bar_class].map(&:text)
384
+ end
385
+
386
+ def test_children_in_source_order
387
+ obj = Parser::Source::Comment::Associator.new(nil, nil)
388
+ for_each_node do |node|
389
+ with_loc = node.children.select do |child|
390
+ child.is_a?(AST::Node) && child.loc && child.loc.expression
391
+ end
392
+ slow_sort = with_loc.sort_by.with_index do |child, index| # Index to ensure stable sort
393
+ [child.loc.expression.begin_pos, index]
394
+ end
395
+ optimized = obj.send(:children_in_source_order, node)
396
+ assert_equal slow_sort, optimized, "children_in_source_order incorrect for #{node}"
397
+ end
398
+ end
367
399
  end
@@ -7,8 +7,7 @@ class TestSourceMap < Minitest::Test
7
7
  include ParseHelper
8
8
 
9
9
  def test_to_hash
10
- buf = Parser::Source::Buffer.new("<input>")
11
- buf.source = "1"
10
+ buf = Parser::Source::Buffer.new("<input>", source: "1")
12
11
  ast = parser_for_ruby_version('1.8').parse(buf)
13
12
  assert_equal [:expression, :operator], ast.loc.to_hash.keys.sort_by(&:to_s)
14
13
  end
@@ -4,8 +4,8 @@ require 'helper'
4
4
 
5
5
  class TestSourceRange < Minitest::Test
6
6
  def setup
7
- @buf = Parser::Source::Buffer.new('(string)')
8
- @buf.source = "foobar\nbaz"
7
+ @buf = Parser::Source::Buffer.new('(string)',
8
+ source: "foobar\nbaz")
9
9
  @sr1_3 = Parser::Source::Range.new(@buf, 1, 3)
10
10
  @sr2_2 = Parser::Source::Range.new(@buf, 2, 2)
11
11
  @sr3_3 = Parser::Source::Range.new(@buf, 3, 3)
@@ -95,15 +95,15 @@ class TestSourceRange < Minitest::Test
95
95
 
96
96
  def test_order
97
97
  assert_equal 0, @sr1_3 <=> @sr1_3
98
- assert_equal -1, @sr1_3 <=> @sr5_8
99
- assert_equal -1, @sr2_2 <=> @sr2_6
100
- assert_equal +1, @sr2_6 <=> @sr2_2
98
+ assert_equal(-1, @sr1_3 <=> @sr5_8)
99
+ assert_equal(-1, @sr2_2 <=> @sr2_6)
100
+ assert_equal(+1, @sr2_6 <=> @sr2_2)
101
101
 
102
- assert_equal -1, @sr1_3 <=> @sr2_6
102
+ assert_equal(-1, @sr1_3 <=> @sr2_6)
103
103
 
104
- assert_equal +1, @sr2_2 <=> @sr1_3
105
- assert_equal -1, @sr1_3 <=> @sr2_2
106
- assert_equal -1, @sr5_7 <=> @sr5_8
104
+ assert_equal(+1, @sr2_2 <=> @sr1_3)
105
+ assert_equal(-1, @sr1_3 <=> @sr2_2)
106
+ assert_equal(-1, @sr5_7 <=> @sr5_8)
107
107
 
108
108
  assert_nil @sr1_3 <=> Parser::Source::Range.new(@buf.dup, 1, 3)
109
109
  assert_nil @sr1_3 <=> 4
@@ -155,6 +155,11 @@ class TestSourceRange < Minitest::Test
155
155
  refute sr.is?('bar')
156
156
  end
157
157
 
158
+ def test_to_range
159
+ sr = Parser::Source::Range.new(@buf, 10, 20)
160
+ assert_equal (10...20), sr.to_range
161
+ end
162
+
158
163
  def test_to_s
159
164
  sr = Parser::Source::Range.new(@buf, 8, 9)
160
165
  assert_equal '(string):2:2', sr.to_s
@@ -169,4 +174,19 @@ class TestSourceRange < Minitest::Test
169
174
  assert_equal 1, sr3.begin_pos
170
175
  assert_equal 4, sr3.end_pos
171
176
  end
177
+
178
+ def test_eql_and_hash
179
+ assert_equal false, @sr1_3.eql?(@sr3_3)
180
+ assert @sr1_3.hash != @sr3_3.hash
181
+
182
+ also_1_3 = @sr3_3.with(begin_pos: 1)
183
+ assert_equal true, @sr1_3.eql?(also_1_3)
184
+ assert_equal @sr1_3.hash, also_1_3.hash
185
+
186
+ buf2 = Parser::Source::Buffer.new('(string)',
187
+ source: "foobar\nbaz")
188
+ from_other_buf = Parser::Source::Range.new(buf2, 1, 3)
189
+ assert_equal false, @sr1_3.eql?(from_other_buf)
190
+ assert @sr1_3.hash != from_other_buf.hash
191
+ end
172
192
  end
@@ -4,8 +4,8 @@ require 'helper'
4
4
 
5
5
  class TestSourceRewriter < Minitest::Test
6
6
  def setup
7
- @buf = Parser::Source::Buffer.new('(rewriter)')
8
- @buf.source = 'foo bar baz'
7
+ @buf = Parser::Source::Buffer.new('(rewriter)',
8
+ source: 'foo bar baz')
9
9
  Parser::Source::Rewriter.warned_of_deprecation = true
10
10
  @rewriter = Parser::Source::Rewriter.new(@buf)
11
11
  end
@@ -522,7 +522,7 @@ class TestSourceRewriter < Minitest::Test
522
522
  end
523
523
  end
524
524
 
525
- assert_match /nested/i, error.message
525
+ assert_match(/nested/i, error.message)
526
526
  end
527
527
 
528
528
  def test_process_in_transaction_raises_error
@@ -532,7 +532,7 @@ class TestSourceRewriter < Minitest::Test
532
532
  end
533
533
  end
534
534
 
535
- assert_match /transaction/, error.message
535
+ assert_match(/transaction/, error.message)
536
536
  end
537
537
 
538
538
  def silence_diagnostics
@@ -4,8 +4,8 @@ require 'helper'
4
4
 
5
5
  class TestSourceRewriterAction < Minitest::Test
6
6
  def setup
7
- @buf = Parser::Source::Buffer.new('(rewriter_action)')
8
- @buf.source = 'foo bar baz'
7
+ @buf = Parser::Source::Buffer.new('(rewriter_action)',
8
+ source: 'foo bar baz')
9
9
  end
10
10
 
11
11
  def range(from, len)
@@ -3,25 +3,32 @@
3
3
  require 'helper'
4
4
 
5
5
  class TestSourceTreeRewriter < Minitest::Test
6
- def setup
7
- @buf = Parser::Source::Buffer.new('(rewriter)')
8
- @buf.source = 'puts(:hello, :world)'
6
+ module Setup
7
+ def setup
8
+ @buf = Parser::Source::Buffer.new('(rewriter)',
9
+ source: 'puts(:hello, :world)')
9
10
 
10
- @hello = range(5, 6)
11
- @comma_space = range(11,2)
12
- @world = range(13,6)
13
- end
11
+ @hello = range(5, 6)
12
+ @ll = range(8, 2)
13
+ @comma_space = range(11,2)
14
+ @world = range(13,6)
15
+ @whole = range(0, @buf.source.length)
16
+ end
14
17
 
15
- def range(from, len)
16
- Parser::Source::Range.new(@buf, from, from + len)
18
+ def range(from, len = nil)
19
+ from, len = from.begin, from.end - from.begin unless len
20
+ Parser::Source::Range.new(@buf, from, from + len)
21
+ end
17
22
  end
18
23
 
24
+ include Setup
25
+
19
26
  # Returns either:
20
- # - String (Normal operation)
27
+ # - yield rewriter
21
28
  # - [diagnostic, ...] (Diagnostics)
22
29
  # - Parser::ClobberingError
23
30
  #
24
- def apply(actions, **policy)
31
+ def build(actions, **policy)
25
32
  diagnostics = []
26
33
  diags = -> { diagnostics.flatten.map(&:strip).join("\n") }
27
34
  rewriter = Parser::Source::TreeRewriter.new(@buf, **policy)
@@ -30,14 +37,23 @@ class TestSourceTreeRewriter < Minitest::Test
30
37
  rewriter.public_send(action, range, *args)
31
38
  end
32
39
  if diagnostics.empty?
33
- rewriter.process
40
+ yield rewriter
34
41
  else
35
42
  diags.call
36
43
  end
37
- rescue ::Parser::ClobberingError => e
44
+ rescue ::Parser::ClobberingError => _e
38
45
  [::Parser::ClobberingError, diags.call]
39
46
  end
40
47
 
48
+ # Returns either:
49
+ # - String (Normal operation)
50
+ # - [diagnostic, ...] (Diagnostics)
51
+ # - Parser::ClobberingError
52
+ #
53
+ def apply(actions, **policy)
54
+ build(actions, **policy) { |rewriter| rewriter.process }
55
+ end
56
+
41
57
  # Expects ordered actions to be grouped together
42
58
  def check_actions(expected, grouped_actions, **policy)
43
59
  grouped_actions.permutation do |sequence|
@@ -140,6 +156,16 @@ DIAGNOSTIC
140
156
  [:wrap, @hello, '[', ']']]
141
157
  end
142
158
 
159
+ def test_inserts_on_empty_ranges
160
+ assert_actions_result 'puts({x}:hello[y], :world)',
161
+ [:insert_before, @hello.begin, '{'],
162
+ [:replace, @hello.begin, 'x'],
163
+ [:insert_after, @hello.begin, '}'],
164
+ [:insert_before, @hello.end, '['],
165
+ [:replace, @hello.end, 'y'],
166
+ [:insert_after, @hello.end, ']']
167
+ end
168
+
143
169
  def test_replace_same_range
144
170
  assert_actions_result 'puts(:hey, :world)',
145
171
  [[:replace, @hello, ':hi'],
@@ -170,4 +196,166 @@ DIAGNOSTIC
170
196
  rewriter = Parser::Source::TreeRewriter.new(@buf)
171
197
  assert_raises(IndexError) { rewriter.insert_before(range(0, 100), 'hola') }
172
198
  end
199
+
200
+ def test_empty
201
+ rewriter = Parser::Source::TreeRewriter.new(@buf)
202
+ assert_equal true, rewriter.empty?
203
+
204
+ # This is a trivial wrap
205
+ rewriter.wrap(range(2,3), '', '')
206
+ assert_equal true, rewriter.empty?
207
+
208
+ # This is a trivial deletion
209
+ rewriter.remove(range(2,0))
210
+ assert_equal true, rewriter.empty?
211
+
212
+ rewriter.remove(range(2,3))
213
+ assert_equal false, rewriter.empty?
214
+ end
215
+
216
+ # splits array into two groups, yield all such possible pairs of groups
217
+ # each_split([1, 2, 3, 4]) yields [1, 2], [3, 4];
218
+ # then [1, 3], [2, 4]
219
+ # ...
220
+ # and finally [3, 4], [1, 2]
221
+ def each_split(array)
222
+ n = array.size
223
+ first_split_size = n.div(2)
224
+ splitting = (0...n).to_set
225
+ splitting.to_a.combination(first_split_size) do |indices|
226
+ yield array.values_at(*indices),
227
+ array.values_at(*(splitting - indices))
228
+ end
229
+ end
230
+
231
+ # Checks that `actions+extra` give the same result when
232
+ # made in order or from subgroups that are later merged.
233
+ # The `extra` actions are always added at the end of the second group.
234
+ #
235
+ def check_all_merge_possibilities(actions, extra, **policy)
236
+ expected = apply(actions + extra, **policy)
237
+
238
+ each_split(actions) do |actions_1, actions_2|
239
+ build(actions_1, **policy) do |rewriter_1|
240
+ build(actions_2 + extra, **policy) do |rewriter_2|
241
+ result = rewriter_1.merge(rewriter_2).process
242
+ assert_equal(expected, result,
243
+ "Group 1: #{actions_1.inspect}\n\n" +
244
+ "Group 2: #{(actions_2 + extra).inspect}"
245
+ )
246
+ end
247
+ end
248
+ end
249
+ end
250
+
251
+ def test_merge
252
+ check_all_merge_possibilities([
253
+ [:wrap, @whole, '<', '>'],
254
+ [:replace, @comma_space, ' => '],
255
+ [:wrap, @hello, '!', '!'],
256
+ # Following two wraps must have same value as they
257
+ # will be applied in different orders...
258
+ [:wrap, @hello.join(@world), '{', '}'],
259
+ [:wrap, @hello.join(@world), '{', '}'],
260
+ [:remove, @ll],
261
+ [:replace, @world, ':everybody'],
262
+ [:wrap, @world, '[', ']']
263
+ ],
264
+ [ # ... but this one is always going to be applied last (extra)
265
+ [:wrap, @hello.join(@world), '@', '@'],
266
+ ])
267
+ end
268
+
269
+ def representation_example
270
+ Parser::Source::TreeRewriter.new(@buf)
271
+ .wrap(range(1...10), '(', ')')
272
+ .insert_after(range(2...6), '!')
273
+ .replace(range(2...4), 'foo')
274
+ .remove(range(5...6))
275
+ end
276
+
277
+ def test_nested_actions
278
+ result = representation_example.as_nested_actions
279
+
280
+ assert_equal( [ [:wrap, 1...10, '(', ')'],
281
+ [:wrap, 2...6, '', '!'], # aka "insert_after"
282
+ [:replace, 2...4, 'foo'],
283
+ [:replace, 5...6, ''], # aka "removal"
284
+ ],
285
+ result.each {|arr| arr[1] = arr[1].to_range }
286
+ )
287
+ end
288
+
289
+ def test_ordered_replacements
290
+ result = representation_example.as_replacements
291
+
292
+ assert_equal( [ [ 1...1, '('],
293
+ [ 2...4, 'foo'],
294
+ [ 5...6, ''],
295
+ [ 6...6, '!'],
296
+ [ 10...10, ')'],
297
+ ],
298
+ result.map {|r, s| [r.to_range, s]}
299
+ )
300
+ end
301
+ end
302
+
303
+ class TestSourceTreeRewriterImport < Minitest::Test
304
+ include TestSourceTreeRewriter::Setup
305
+ def setup
306
+ super
307
+ @buf2 = Parser::Source::Buffer.new('(rewriter 2)',
308
+ source: ':hello')
309
+
310
+ @rewriter = Parser::Source::TreeRewriter.new(@buf)
311
+
312
+ @rewriter2 = Parser::Source::TreeRewriter.new(@buf2)
313
+
314
+ @hello2 = range2(0, 6)
315
+ @ll2 = range2(3, 2)
316
+ end
317
+
318
+ def range2(from, len)
319
+ Parser::Source::Range.new(@buf2, from, from + len)
320
+ end
321
+
322
+ def test_import_with_offset
323
+ @rewriter2.wrap(@hello2, '[', ']')
324
+ @rewriter.wrap(@hello.join(@world), '{', '}')
325
+ @rewriter.import!(@rewriter2, offset: @hello.begin_pos)
326
+ assert_equal 'puts({[:hello], :world})', @rewriter.process
327
+ end
328
+
329
+ def test_import_with_offset_from_bigger_source
330
+ @rewriter2.wrap(@ll2, '[', ']')
331
+ @rewriter.wrap(@hello, '{', '}')
332
+ @rewriter2.import!(@rewriter, offset: -@hello.begin_pos)
333
+ assert_equal '{:he[ll]o}', @rewriter2.process
334
+ end
335
+
336
+ def test_import_with_offset_and_self
337
+ @rewriter.wrap(@ll, '[', ']')
338
+ @rewriter.import!(@rewriter, offset: +3)
339
+ @rewriter.replace(range(8,1), '**')
340
+ assert_equal 'puts(:he[**l]o[, ]:world)', @rewriter.process
341
+ @rewriter.import!(@rewriter, offset: -6)
342
+ assert_equal 'pu[**s]([:h]e[**l]o[, ]:world)', @rewriter.process
343
+ end
344
+
345
+ def test_import_with_invalid_offset
346
+ @rewriter.wrap(@ll, '[', ']')
347
+ m = @rewriter.dup.import!(@rewriter, offset: -@ll.begin_pos)
348
+ assert_equal '[pu]ts(:he[ll]o, :world)', m.process
349
+ off = @buf.source.size - @ll.end_pos
350
+ m = @rewriter.dup.import!(@rewriter, offset: off)
351
+ assert_equal 'puts(:he[ll]o, :worl[d)]', m.process
352
+ assert_raises { @rewriter.import!(@rewriter, offset: -@ll.begin_pos - 1) }
353
+ assert_raises { @rewriter.import!(@rewriter, offset: off + 1) }
354
+ assert_equal 'puts(:he[ll]o, :world)', @rewriter.process # Test atomicity of import!
355
+ end
356
+
357
+ def test_empty_import
358
+ assert_equal @rewriter, @rewriter.import!(@rewriter2)
359
+ assert_equal @rewriter, @rewriter.import!(@rewriter, offset: 42)
360
+ end
173
361
  end