File: test_source_tree_rewriter.rb

package info (click to toggle)
ruby-whitequark-parser 3.3.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,828 kB
  • sloc: yacc: 40,699; ruby: 20,395; makefile: 12; sh: 8
file content (372 lines) | stat: -rw-r--r-- 12,126 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# frozen_string_literal: true

require 'helper'

class TestSourceTreeRewriter < Minitest::Test
  module Setup
    def setup
      @buf = Parser::Source::Buffer.new('(rewriter)',
        source: 'puts(:hello, :world)')

      @hello = range(5, 6)
      @ll = range(8, 2)
      @comma_space = range(11,2)
      @world = range(13,6)
      @whole = range(0, @buf.source.length)
    end

    def range(from, len = nil)
      from, len = from.begin, from.end - from.begin unless len
      Parser::Source::Range.new(@buf, from, from + len)
    end
  end

  include Setup

  # Returns either:
  #  - yield rewriter
  #  - [diagnostic, ...] (Diagnostics)
  #  - Parser::ClobberingError
  #
  def build(actions, **policy)
    diagnostics = []
    diags = -> { diagnostics.flatten.map(&:strip).join("\n") }
    rewriter = Parser::Source::TreeRewriter.new(@buf, **policy)
    rewriter.diagnostics.consumer = -> diag { diagnostics << diag.render }
    actions.each do |action, range, *args|
      rewriter.public_send(action, range, *args)
    end
    if diagnostics.empty?
      yield rewriter
    else
      diags.call
    end
  rescue ::Parser::ClobberingError => _e
    [::Parser::ClobberingError, diags.call]
  end

  # Returns either:
  #  - String (Normal operation)
  #  - [diagnostic, ...] (Diagnostics)
  #  - Parser::ClobberingError
  #
  def apply(actions, **policy)
    build(actions, **policy) { |rewriter| rewriter.process }
  end

  # Expects ordered actions to be grouped together
  def check_actions(expected, grouped_actions, **policy)
    grouped_actions.permutation do |sequence|
      # [action, [action, action]]  => [action, action, action]
      # except we can't use flatten because "action" are arrays themselves
      actions = sequence.flat_map { |group| group.first.is_a?(Array) ? group : [group] }
      assert_equal(expected, apply(actions, **policy))
    end
  end

  def assert_actions_result(expected, *actions, **rest)
    if expected == :raise
      diagnostic = rest.values.first
      check_actions([::Parser::ClobberingError, diagnostic], actions)
    elsif rest.empty?
      check_actions(expected, actions)
    else
      policy, diagnostic = rest.first
      check_actions(expected, actions, policy => :accept)
      check_actions(diagnostic, actions, policy => :warn)
      diagnostic.gsub!(/warning: /, 'error: ')
      check_actions([::Parser::ClobberingError, diagnostic], actions, policy => :raise)
    end
  end

  ### Simple cases

  def test_remove
    assert_actions_result 'puts(, :world)', [:remove, @hello]
  end

  def test_insert_before
    assert_actions_result 'puts(:hello, 42, :world)', [:insert_before, @world, '42, ']
  end

  def test_insert_after
    assert_actions_result 'puts(:hello, 42, :world)', [:insert_after, @hello, ', 42']
  end

  def test_wrap
    assert_actions_result 'puts([:hello], :world)', [:wrap, @hello, '[', ']']
  end

  def test_replace
    assert_actions_result 'puts(:hi, :world)', [:replace, @hello, ':hi']
  end

  #
  # All other cases, as per doc
  #

  def test_crossing_non_deletions
    check = [
      [:wrap, '(', ')'],
      [:remove],
      [:replace, 'xx'],
    ]
    check.combination(2) do |(action, *args), (action_b, *args_b)|
      next if action == :remove && action_b == :remove
      assert_actions_result :raise,
                            [[action, @hello.join(@comma_space), *args],
                             [action_b, @world.join(@comma_space), *args_b]],
                            diagnostic: <<-DIAGNOSTIC.chomp
(rewriter):1:12: error: the rewriting action on:
(rewriter):1: puts(:hello, :world)
(rewriter):1:            ^~~~~~~~
(rewriter):1:6: error: is crossing that on:
(rewriter):1: puts(:hello, :world)
(rewriter):1:      ^~~~~~~~
DIAGNOSTIC
    end
  end


  def test_crossing_deletions
    assert_actions_result 'puts()',
                          [[:remove, @hello.join(@comma_space)],
                           [:remove, @world.join(@comma_space)]],
                          crossing_deletions: <<-DIAGNOSTIC.chomp
(rewriter):1:12: warning: the deletion of:
(rewriter):1: puts(:hello, :world)
(rewriter):1:            ^~~~~~~~
(rewriter):1:6: warning: is crossing:
(rewriter):1: puts(:hello, :world)
(rewriter):1:      ^~~~~~~~
DIAGNOSTIC
  end

  def test_multiple_actions
    assert_actions_result 'puts({:hello => [:everybody]})',
                          [:replace, @comma_space, ' => '],
                          [:wrap, @hello.join(@world), '{', '}'],
                          [:replace, @world, ':everybody'],
                          [:wrap, @world, '[', ']']
  end

  def test_wraps_same_range
    assert_actions_result 'puts([(:hello)], :world)',
                           [[:wrap, @hello, '(', ')'],
                            [:wrap, @hello, '[', ']']]
  end

  def test_inserts_on_empty_ranges
    assert_actions_result 'puts({x}:hello[y], :world)',
      [:insert_before, @hello.begin, '{'],
      [:replace, @hello.begin, 'x'],
      [:insert_after, @hello.begin, '}'],
      [:insert_before, @hello.end, '['],
      [:replace, @hello.end, 'y'],
      [:insert_after, @hello.end, ']']
  end

  def test_replace_same_range
    assert_actions_result 'puts(:hey, :world)',
                           [[:replace, @hello, ':hi'],
                            [:replace, @hello, ':hey']],
                           different_replacements: <<-DIAGNOSTIC.chomp
(rewriter):1:6: warning: different replacements: :hey vs :hi
(rewriter):1: puts(:hello, :world)
(rewriter):1:      ^~~~~~
DIAGNOSTIC
  end

  def test_swallowed_insertions
    assert_actions_result 'puts(:hi)',
                           [[:wrap, @hello.adjust(begin_pos: 1), '__', '__'],
                            [:replace, @world.adjust(end_pos: -2), 'xx'],
                            [:replace, @hello.join(@world), ':hi']],
                           swallowed_insertions: <<-DIAGNOSTIC.chomp
(rewriter):1:6: warning: this replacement:
(rewriter):1: puts(:hello, :world)
(rewriter):1:      ^~~~~~~~~~~~~~
(rewriter):1:7: warning: swallows some inner rewriting actions:
(rewriter):1: puts(:hello, :world)
(rewriter):1:       ^~~~~  ~~~~
DIAGNOSTIC
  end

  def test_out_of_range_ranges
    rewriter = Parser::Source::TreeRewriter.new(@buf)
    assert_raises(IndexError) { rewriter.insert_before(range(0, 100), 'hola') }
  end

  def test_empty
    rewriter = Parser::Source::TreeRewriter.new(@buf)
    assert_equal true, rewriter.empty?

    # This is a trivial wrap
    rewriter.wrap(range(2,3), '', '')
    assert_equal true, rewriter.empty?

    # This is a trivial deletion
    rewriter.remove(range(2,0))
    assert_equal true, rewriter.empty?

    rewriter.remove(range(2,3))
    assert_equal false, rewriter.empty?
  end

  # splits array into two groups, yield all such possible pairs of groups
  # each_split([1, 2, 3, 4]) yields [1, 2], [3, 4];
  #                            then [1, 3], [2, 4]
  #                                 ...
  #                     and finally [3, 4], [1, 2]
  def each_split(array)
    n = array.size
    first_split_size = n.div(2)
    splitting = (0...n).to_set
    splitting.to_a.combination(first_split_size) do |indices|
      yield array.values_at(*indices),
            array.values_at(*(splitting - indices))
    end
  end

  # Checks that `actions+extra` give the same result when
  # made in order or from subgroups that are later merged.
  # The `extra` actions are always added at the end of the second group.
  #
  def check_all_merge_possibilities(actions, extra, **policy)
    expected = apply(actions + extra, **policy)

    each_split(actions) do |actions_1, actions_2|
      build(actions_1, **policy) do |rewriter_1|
        build(actions_2 + extra, **policy) do |rewriter_2|
          result = rewriter_1.merge(rewriter_2).process
          assert_equal(expected, result,
            "Group 1: #{actions_1.inspect}\n\n" +
            "Group 2: #{(actions_2 + extra).inspect}"
          )
        end
      end
    end
  end

  def test_merge
    check_all_merge_possibilities([
      [:wrap, @whole, '<', '>'],
      [:replace, @comma_space, ' => '],
      [:wrap, @hello, '!', '!'],
      # Following two wraps must have same value as they
      # will be applied in different orders...
      [:wrap, @hello.join(@world), '{', '}'],
      [:wrap, @hello.join(@world), '{', '}'],
      [:remove, @ll],
      [:replace, @world, ':everybody'],
      [:wrap, @world, '[', ']']
    ],
    [ # ... but this one is always going to be applied last (extra)
      [:wrap, @hello.join(@world), '@', '@'],
    ])
  end

  def representation_example
    Parser::Source::TreeRewriter.new(@buf)
      .wrap(range(1...10), '(', ')')
      .insert_after(range(2...6), '!')
      .replace(range(2...4), 'foo')
      .remove(range(5...6))
  end

  def test_nested_actions
    result = representation_example.as_nested_actions

    assert_equal( [ [:wrap, 1...10, '(', ')'],
                    [:wrap, 2...6, '', '!'],  # aka "insert_after"
                    [:replace, 2...4, 'foo'],
                    [:replace, 5...6, ''],  # aka "removal"
                  ],
                  result.each {|arr| arr[1] = arr[1].to_range }
                )
  end

  def test_ordered_replacements
    result = representation_example.as_replacements

    assert_equal( [ [ 1...1, '('],
                    [ 2...4, 'foo'],
                    [ 5...6, ''],
                    [ 6...6, '!'],
                    [ 10...10, ')'],
                  ],
                  result.map {|r, s| [r.to_range, s]}
                )
  end
end

class TestSourceTreeRewriterImport < Minitest::Test
  include TestSourceTreeRewriter::Setup
  def setup
    super
    @buf2 = Parser::Source::Buffer.new('(rewriter 2)',
      source: ':hello')

    @rewriter = Parser::Source::TreeRewriter.new(@buf)

    @rewriter2 = Parser::Source::TreeRewriter.new(@buf2)

    @hello2 = range2(0, 6)
    @ll2 = range2(3, 2)
  end

  def range2(from, len)
    Parser::Source::Range.new(@buf2, from, from + len)
  end

  def test_import_with_offset
    @rewriter2.wrap(@hello2, '[', ']')
    @rewriter.wrap(@hello.join(@world), '{', '}')
    @rewriter.import!(@rewriter2, offset: @hello.begin_pos)
    assert_equal 'puts({[:hello], :world})', @rewriter.process
  end

  def test_import_with_offset_from_bigger_source
    @rewriter2.wrap(@ll2, '[', ']')
    @rewriter.wrap(@hello, '{', '}')
    @rewriter2.import!(@rewriter, offset: -@hello.begin_pos)
    assert_equal '{:he[ll]o}', @rewriter2.process
  end

  def test_import_with_offset_and_self
    @rewriter.wrap(@ll, '[', ']')
    @rewriter.import!(@rewriter, offset: +3)
    @rewriter.replace(range(8,1), '**')
    assert_equal 'puts(:he[**l]o[, ]:world)', @rewriter.process
    @rewriter.import!(@rewriter, offset: -6)
    assert_equal 'pu[**s]([:h]e[**l]o[, ]:world)', @rewriter.process
  end

  def test_import_with_invalid_offset
    @rewriter.wrap(@ll, '[', ']')
    m = @rewriter.dup.import!(@rewriter, offset: -@ll.begin_pos)
    assert_equal '[pu]ts(:he[ll]o, :world)', m.process
    off = @buf.source.size - @ll.end_pos
    m = @rewriter.dup.import!(@rewriter, offset: off)
    assert_equal 'puts(:he[ll]o, :worl[d)]', m.process
    assert_raises { @rewriter.import!(@rewriter, offset: -@ll.begin_pos - 1) }
    assert_raises { @rewriter.import!(@rewriter, offset: off + 1) }
    assert_equal 'puts(:he[ll]o, :world)', @rewriter.process # Test atomicity of import!
  end

  def test_empty_import
    assert_equal @rewriter, @rewriter.import!(@rewriter2)
    assert_equal @rewriter, @rewriter.import!(@rewriter, offset: 42)
  end

  def test_inspect
    assert_equal '#<Parser::Source::TreeRewriter (rewriter): empty>', @rewriter.inspect
    @rewriter.insert_before(@hello, '[')
    @rewriter.replace(@ll, 'xxx')
    @rewriter.remove(@comma_space)
    assert_equal '#<Parser::Source::TreeRewriter (rewriter): +"["@5, ^"xxx"@8...10, -11...13>', @rewriter.inspect
    @rewriter.insert_before(@hello, '{')
    @rewriter.remove(@world)
    assert_equal '#<Parser::Source::TreeRewriter (rewriter): +"{["@5, ^"xxx"@8...10, -11...13, …>', @rewriter.inspect
  end
end