parser 2.7.1.3 → 2.7.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,20 +3,25 @@
3
3
  require 'helper'
4
4
 
5
5
  class TestSourceTreeRewriter < Minitest::Test
6
- def setup
7
- @buf = Parser::Source::Buffer.new('(rewriter)',
8
- source: 'puts(:hello, :world)')
6
+ module Setup
7
+ def setup
8
+ @buf = Parser::Source::Buffer.new('(rewriter)',
9
+ source: 'puts(:hello, :world)')
10
+
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
9
17
 
10
- @hello = range(5, 6)
11
- @ll = range(7, 2)
12
- @comma_space = range(11,2)
13
- @world = range(13,6)
14
- @whole = range(0, @buf.source.length)
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
15
22
  end
16
23
 
17
- def range(from, len)
18
- Parser::Source::Range.new(@buf, from, from + len)
19
- end
24
+ include Setup
20
25
 
21
26
  # Returns either:
22
27
  # - yield rewriter
@@ -260,4 +265,97 @@ DIAGNOSTIC
260
265
  [:wrap, @hello.join(@world), '@', '@'],
261
266
  ])
262
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
263
361
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.1.3
4
+ version: 2.7.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - whitequark
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-26 00:00:00.000000000 Z
11
+ date: 2020-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.4.0
19
+ version: 2.4.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.4.0
26
+ version: 2.4.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -302,10 +302,10 @@ licenses:
302
302
  - MIT
303
303
  metadata:
304
304
  bug_tracker_uri: https://github.com/whitequark/parser/issues
305
- changelog_uri: https://github.com/whitequark/parser/blob/v2.7.1.3/CHANGELOG.md
306
- documentation_uri: https://www.rubydoc.info/gems/parser/2.7.1.3
307
- source_code_uri: https://github.com/whitequark/parser/tree/v2.7.1.3
308
- post_install_message:
305
+ changelog_uri: https://github.com/whitequark/parser/blob/v2.7.1.4/CHANGELOG.md
306
+ documentation_uri: https://www.rubydoc.info/gems/parser/2.7.1.4
307
+ source_code_uri: https://github.com/whitequark/parser/tree/v2.7.1.4
308
+ post_install_message:
309
309
  rdoc_options: []
310
310
  require_paths:
311
311
  - lib
@@ -321,7 +321,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
321
321
  version: '0'
322
322
  requirements: []
323
323
  rubygems_version: 3.0.6
324
- signing_key:
324
+ signing_key:
325
325
  specification_version: 4
326
326
  summary: A Ruby parser written in pure Ruby.
327
327
  test_files: