unparser 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unparser
4
+ class Emitter
5
+ # Emitter for lambda nodes
6
+ class Lambda < self
7
+ include Terminated
8
+
9
+ handle :lambda
10
+
11
+ private
12
+
13
+ # Perform dispatch
14
+ #
15
+ # @return [undefined]
16
+ #
17
+ # @api private
18
+ #
19
+ def dispatch
20
+ write('->')
21
+ end
22
+
23
+ end # Lambda
24
+ end # Emitter
25
+ end # Unparser
@@ -6,9 +6,6 @@ module Unparser
6
6
 
7
7
  # Array literal emitter
8
8
  class Array < self
9
- OPEN = '['.freeze
10
- CLOSE = ']'.freeze
11
-
12
9
  handle :array
13
10
 
14
11
  private
@@ -20,7 +17,7 @@ module Unparser
20
17
  # @api private
21
18
  #
22
19
  def dispatch
23
- parentheses(OPEN, CLOSE) do
20
+ parentheses(*BRACKETS_SQUARE) do
24
21
  delimited(children)
25
22
  end
26
23
  end
@@ -6,10 +6,10 @@ module Unparser
6
6
  class Meta < self
7
7
  include Terminated
8
8
 
9
- handle(:__FILE__, :__LINE__)
9
+ handle(:__ENCODING__, :__FILE__, :__LINE__)
10
10
 
11
11
  def dispatch
12
- write(node.type.to_s) # (e.g. literally write '__FILE__' or '__LINE__')
12
+ write(node.type.to_s)
13
13
  end
14
14
  end # Meta
15
15
  end # Emitter
@@ -12,7 +12,7 @@ module Unparser
12
12
 
13
13
  define_group :rescue_bodies, 1..-2
14
14
 
15
- EMBEDDED_TYPES = %i[def defs kwbegin ensure].to_set.freeze
15
+ EMBEDDED_TYPES = %i[block def defs kwbegin ensure].to_set.freeze
16
16
 
17
17
  NOINDENT_STANDALONE_RESCUE = %i[root begin pair_rocket pair_colon lvasgn ivasgn].to_set.freeze
18
18
 
@@ -8,12 +8,10 @@ module Unparser
8
8
 
9
9
  handle :send
10
10
 
11
- INDEX_PARENS = IceNine.deep_freeze(%w([ ]))
12
- NORMAL_PARENS = IceNine.deep_freeze(%w[( )])
13
-
14
- INDEX_REFERENCE = :'[]'
15
- INDEX_ASSIGN = :'[]='
16
- ASSIGN_SUFFIX = '='.freeze
11
+ ASSIGN_SUFFIX = '='.freeze
12
+ INDEX_ASSIGN = :'[]='
13
+ INDEX_REFERENCE = :'[]'
14
+ NON_ASSIGN_RANGE = (0..-2).freeze
17
15
 
18
16
  children :receiver, :selector
19
17
 
@@ -50,23 +48,6 @@ module Unparser
50
48
  # @api private
51
49
  #
52
50
  def effective_emitter_class
53
- case selector
54
- when INDEX_REFERENCE
55
- Index::Reference
56
- when INDEX_ASSIGN
57
- Index::Assign
58
- else
59
- non_index_emitter
60
- end
61
- end
62
-
63
- # Return non index emitter
64
- #
65
- # @return [Class:Emitter]
66
- #
67
- # @api private
68
- #
69
- def non_index_emitter
70
51
  if binary_operator?
71
52
  Binary
72
53
  elsif unary_operator?
@@ -115,11 +96,7 @@ module Unparser
115
96
  # @api private
116
97
  #
117
98
  def emit_selector
118
- name = string_selector
119
- if mlhs?
120
- name = name[0..-2]
121
- end
122
- write(name)
99
+ write(mlhs? ? non_assignment_selector : string_selector)
123
100
  end
124
101
 
125
102
  # Test for mlhs
@@ -199,6 +176,15 @@ module Unparser
199
176
  local_variable_scope.local_variable_defined_for_node?(node, selector)
200
177
  end
201
178
 
179
+ # The non assignment selector
180
+ #
181
+ # @return [String]
182
+ #
183
+ # @api private
184
+ def non_assignment_selector
185
+ string_selector[NON_ASSIGN_RANGE]
186
+ end
187
+
202
188
  end # Send
203
189
  end # Emitter
204
190
  end # Unparser
@@ -7,6 +7,8 @@ module Unparser
7
7
  class AttributeAssignment < self
8
8
  include Unterminated
9
9
 
10
+ children :receiver, :selector, :first_argument
11
+
10
12
  # Perform regular dispatch
11
13
  #
12
14
  # @return [undefined]
@@ -16,8 +18,13 @@ module Unparser
16
18
  def dispatch
17
19
  emit_receiver
18
20
  emit_attribute
19
- emit_operator
20
- visit(arguments.first)
21
+ write(T_ASN)
22
+
23
+ if arguments.one?
24
+ visit(first_argument)
25
+ else
26
+ parentheses { delimited(arguments) }
27
+ end
21
28
  end
22
29
 
23
30
  private
@@ -40,29 +47,8 @@ module Unparser
40
47
  # @api private
41
48
  #
42
49
  def emit_attribute
43
- write(attribute_name)
44
- end
45
-
46
- # Emit assignment operator
47
- #
48
- # @return [undefined]
49
- #
50
- # @api private
51
- #
52
- def emit_operator
53
- write(WS, T_ASN, WS)
54
- end
55
-
56
- # Return attribute name
57
- #
58
- # @return [String]
59
- #
60
- # @api private
61
- #
62
- def attribute_name
63
- string_selector[0..-2]
50
+ write(non_assignment_selector)
64
51
  end
65
-
66
52
  end # AttributeAssignment
67
53
  end # Send
68
54
  end # Emitter
@@ -6,22 +6,11 @@ require 'morpher'
6
6
  require 'devtools/spec_helper'
7
7
 
8
8
  require 'parser/current'
9
- require 'parser/ruby19'
10
- require 'parser/ruby20'
11
- require 'parser/ruby21'
12
- require 'parser/ruby22'
13
9
 
14
10
  module SpecHelper
15
11
  def s(type, *children)
16
12
  Parser::AST::Node.new(type, children)
17
13
  end
18
-
19
- def strip(source)
20
- source = source.rstrip
21
- indent = source.scan(/^\s*/).min_by(&:length)
22
- source.gsub(/^#{indent}/, '')
23
- end
24
-
25
14
  end
26
15
 
27
16
  RSpec.configure do |config|
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Unparser::Comments, '#consume' do
4
4
 
5
5
  let(:ast_and_comments) do
6
- Parser::CurrentRuby.parse_with_comments(<<-RUBY)
6
+ Unparser.parse_with_comments(<<~'RUBY')
7
7
  def hi # EOL 1
8
8
  end # EOL 2
9
9
  RUBY
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Unparser::Comments, '#take_all' do
4
4
 
5
5
  let(:ast_and_comments) do
6
- Parser::CurrentRuby.parse_with_comments(<<-RUBY)
6
+ Unparser.parse_with_comments(<<~'RUBY')
7
7
  def hi # EOL 1
8
8
  end # EOL 2
9
9
  RUBY
@@ -9,7 +9,7 @@ describe Unparser::Comments, '#take_before' do
9
9
  context 'usual case' do
10
10
 
11
11
  let(:ast_and_comments) do
12
- Parser::CurrentRuby.parse_with_comments(<<-RUBY)
12
+ Unparser.parse_with_comments(<<~'RUBY')
13
13
  def hi # EOL 1
14
14
  # comment
15
15
  end # EOL 2
@@ -29,7 +29,7 @@ describe Unparser::Comments, '#take_before' do
29
29
  context 'when node does not respond to source part' do
30
30
 
31
31
  let(:ast_and_comments) do
32
- Parser::CurrentRuby.parse_with_comments(<<-RUBY)
32
+ Unparser.parse_with_comments(<<~'RUBY')
33
33
  expression ? :foo : :bar # EOL 1
34
34
  # EOL 2
35
35
  RUBY
@@ -3,12 +3,12 @@ require 'spec_helper'
3
3
  describe Unparser::Comments, '#take_eol_comments' do
4
4
 
5
5
  let(:ast_and_comments) do
6
- Parser::CurrentRuby.parse_with_comments(<<-RUBY)
7
- def hi # EOL 1
8
- =begin
9
- doc comment
10
- =end
11
- end # EOL 2
6
+ Unparser.parse_with_comments(<<~'RUBY')
7
+ def hi # EOL 1
8
+ =begin
9
+ doc comment
10
+ =end
11
+ end # EOL 2
12
12
  RUBY
13
13
  end
14
14
  let(:ast) { ast_and_comments[0] }
@@ -1,95 +1,114 @@
1
1
  require 'spec_helper'
2
- require 'parser/all'
3
- require 'support/parser_class_generator'
4
2
 
5
3
  describe Unparser, mutant_expression: 'Unparser::Emitter*' do
6
- describe '.unparse' do
7
-
8
- RUBY_VERSION_PARSERS = IceNine.deep_freeze(
9
- '2.5' => Parser::Ruby25
10
- )
4
+ describe '.buffer' do
5
+ let(:source) { 'a + b' }
11
6
 
12
- RUBY_VERSIONS = RUBY_VERSION_PARSERS.keys.freeze
13
-
14
- def self.builder_options
15
- @builder_options ||= {}
7
+ def apply
8
+ described_class.buffer(source)
16
9
  end
17
10
 
18
- def self.builder_options=(options)
19
- @builder_options = options
11
+ it 'returns parser buffer with expected name' do
12
+ expect(apply.name).to eql('(string)')
20
13
  end
21
14
 
22
- def self.ruby_versions
23
- @ruby_versions ||= RUBY_VERSIONS
15
+ it 'returns parser buffer with pre-filled source' do
16
+ expect(apply.source).to eql(source)
24
17
  end
18
+ end
25
19
 
26
- def self.ruby_versions=(versions)
27
- @ruby_versions = versions
20
+ describe '.parser' do
21
+ let(:invalid_source_buffer) { Unparser.buffer('a +') }
22
+
23
+ def apply
24
+ described_class.parser
28
25
  end
29
26
 
30
- def self.with_ruby_versions(beginning_at: nil, ending_at: nil, only: nil)
31
- original_ruby_versions = ruby_versions
32
- if only
33
- self.ruby_versions = only & ruby_versions # intersection
34
- else
35
- if ending_at
36
- idx = ruby_versions.index(ending_at) || fail('Invalid Ruby specified')
37
- self.ruby_versions = ruby_versions[0..idx]
38
- end
39
- if beginning_at
40
- idx = ruby_versions.index(beginning_at) || fail('Invalid Ruby specified')
41
- self.ruby_versions = ruby_versions[idx..-1]
27
+ context 'failure' do
28
+ def apply
29
+ super.tap do |parser|
30
+ parser.diagnostics.consumer = ->(_) {}
42
31
  end
43
32
  end
44
33
 
45
- yield
34
+ it 'returns a parser that fails with syntax error' do
35
+ expect { apply.parse(invalid_source_buffer) }
36
+ .to raise_error(Parser::SyntaxError)
37
+ end
38
+ end
39
+
40
+ context 'warnings' do
41
+ before do
42
+ allow(Kernel).to receive(:warn)
43
+ end
44
+
45
+ it 'returns a parser that warns on diagnostics' do
46
+ expect { apply.parse(invalid_source_buffer) }
47
+ .to raise_error(Parser::SyntaxError)
48
+
49
+ expect(Kernel).to have_received(:warn)
50
+ .with([
51
+ "(string):1:4: error: unexpected token $end",
52
+ "(string):1: a +", "(string):1: "
53
+ ])
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '.parse' do
59
+ def apply
60
+ described_class.parse('self[1]=2')
61
+ end
46
62
 
47
- self.ruby_versions = original_ruby_versions
63
+ it 'returns expected AST' do
64
+ expect(apply).to eql(s(:indexasgn, s(:self), s(:int, 1), s(:int, 2)))
48
65
  end
66
+ end
49
67
 
50
- def self.current_parsers
51
- ruby_versions.map do |ruby_version|
52
- if builder_options != {}
53
- ParserClassGenerator.generate_with_options(parser_for_ruby_version(ruby_version), builder_options)
54
- else
55
- parser_for_ruby_version(ruby_version)
68
+ describe '.unparse' do
69
+ let(:builder_options) { {} }
70
+
71
+ def parser
72
+ Unparser.parser.tap do |parser|
73
+ builder_options.each do |name, value|
74
+ parser.builder.public_send(:"#{name}=", value)
56
75
  end
57
76
  end
58
77
  end
59
78
 
60
- def self.with_builder_options(options)
61
- original_options = builder_options
62
- self.builder_options = builder_options.merge(options)
63
-
64
- yield
79
+ def buffer(input)
80
+ Unparser.buffer(input)
81
+ end
65
82
 
66
- self.builder_options = original_options
83
+ def parse_with_comments(string)
84
+ parser.parse_with_comments(buffer(string))
67
85
  end
68
86
 
69
- def self.parser_for_ruby_version(version)
70
- RUBY_VERSION_PARSERS.fetch(version) do
71
- raise "Unrecognized Ruby version #{version}"
87
+ def self.with_builder_options(options, &block)
88
+ context "with #{options}" do
89
+ let(:builder_options) { options }
90
+
91
+ class_eval(&block)
72
92
  end
73
93
  end
74
94
 
75
- def assert_round_trip(input, parser)
76
- ast, comments = parser.parse_with_comments(input)
95
+ def assert_round_trip(string, parser)
96
+ ast, comments = parse_with_comments(string)
77
97
  generated = Unparser.unparse(ast, comments)
78
- expect(generated).to eql(input)
79
- generated_ast, _comments = parser.parse_with_comments(generated)
98
+ expect(generated).to eql(string.chomp)
99
+ generated_ast, _comments = parse_with_comments(generated)
80
100
  expect(ast == generated_ast).to be(true)
81
101
  end
82
102
 
83
103
  def assert_generates_from_string(parser, string, expected)
84
- string = strip(string)
85
- ast_with_comments = parser.parse_with_comments(string)
86
- assert_generates_from_ast(parser, ast_with_comments, expected)
104
+ ast_with_comments = parse_with_comments(string)
105
+ assert_generates_from_ast(parser, ast_with_comments, expected.chomp)
87
106
  end
88
107
 
89
108
  def assert_generates_from_ast(parser, ast_with_comments, expected)
90
109
  generated = Unparser.unparse(*ast_with_comments)
91
110
  expect(generated).to eql(expected)
92
- ast, comments = parser.parse_with_comments(generated)
111
+ ast, comments = parse_with_comments(generated)
93
112
  expect(Unparser.unparse(ast, comments)).to eql(expected)
94
113
  end
95
114
 
@@ -104,42 +123,37 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
104
123
  assert_source("#{expression}.foo")
105
124
  end
106
125
 
107
- def self.assert_generates(ast_or_string, expected)
108
- current_parsers.each do |parser|
109
- it "should generate #{ast_or_string} as #{expected} under #{parser.inspect}" do
110
- if ast_or_string.is_a?(String)
111
- expected = strip(expected)
112
- assert_generates_from_string(parser, ast_or_string, expected)
113
- else
114
- assert_generates_from_ast(parser, [ast_or_string, []], expected)
115
- end
126
+ def self.assert_generates(input, expected)
127
+ it "should generate #{input} as #{expected}" do
128
+ if input.is_a?(String)
129
+ assert_generates_from_string(parser, input, expected)
130
+ else
131
+ assert_generates_from_ast(parser, [input, []], expected)
116
132
  end
117
133
  end
118
134
  end
119
135
 
120
136
  def self.assert_round_trip(input)
121
- current_parsers.each do |parser|
122
- it "should round trip #{input} under #{parser.inspect}" do
123
- assert_round_trip(input, parser)
124
- end
137
+ it "should round trip #{input}" do
138
+ assert_round_trip(input, parser)
125
139
  end
126
140
  end
127
141
 
128
142
  def self.assert_source(input)
129
- assert_round_trip(strip(input))
143
+ assert_round_trip(input)
130
144
  end
131
145
 
132
146
  context 'kwargs' do
133
- assert_source <<-RUBY
147
+ assert_source <<~RUBY
134
148
  def foo(bar:, baz:)
135
149
  end
136
150
  RUBY
137
151
 
138
- assert_source <<-RUBY
152
+ assert_source <<~RUBY
139
153
  foo(**bar)
140
154
  RUBY
141
155
 
142
- assert_source <<-RUBY
156
+ assert_source <<~RUBY
143
157
  def foo(bar:, baz: "value")
144
158
  end
145
159
  RUBY
@@ -193,7 +207,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
193
207
  assert_terminated '"foo bar"'
194
208
  assert_terminated '"foo\nbar"'
195
209
  # Within indentation
196
- assert_generates <<-'RUBY', <<-'RUBY'
210
+ assert_generates <<~'RUBY', <<~'RUBY'
197
211
  if foo
198
212
  "
199
213
  #{foo}
@@ -233,7 +247,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
233
247
  assert_terminated '/\n/'
234
248
  assert_terminated "/\n/x"
235
249
  # Within indentation
236
- assert_source <<-RUBY
250
+ assert_source <<~RUBY
237
251
  if foo
238
252
  /
239
253
  /
@@ -320,7 +334,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
320
334
  end
321
335
 
322
336
  context 'magic keywords' do
323
- assert_generates '__ENCODING__', 'Encoding::UTF_8'
337
+ assert_source '__ENCODING__'
324
338
 
325
339
  # These two assertions don't actually need to be wrapped in this block since `true` is the default,
326
340
  # but it is helpful to contrast with the assertions farther down.
@@ -353,7 +367,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
353
367
  context 'lvar introduction from condition' do
354
368
  assert_source 'foo = bar while foo'
355
369
  assert_source 'foo = bar until foo'
356
- assert_source <<-'RUBY'
370
+ assert_source <<~'RUBY'
357
371
  foo = exp
358
372
  while foo
359
373
  foo = bar
@@ -366,7 +380,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
366
380
  # pair = :foo
367
381
  # foo
368
382
  # end
369
- assert_source <<-'RUBY'
383
+ assert_source <<~'RUBY'
370
384
  if foo do |pair|
371
385
  pair
372
386
  end
@@ -375,13 +389,13 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
375
389
  end
376
390
  RUBY
377
391
 
378
- assert_source <<-'RUBY'
392
+ assert_source <<~'RUBY'
379
393
  while foo
380
394
  foo = bar
381
395
  end
382
396
  RUBY
383
397
 
384
- assert_source <<-'RUBY'
398
+ assert_source <<~'RUBY'
385
399
  each do |bar|
386
400
  while foo
387
401
  foo = bar
@@ -389,13 +403,13 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
389
403
  end
390
404
  RUBY
391
405
 
392
- assert_source <<-'RUBY'
406
+ assert_source <<~'RUBY'
393
407
  def foo
394
408
  foo = bar while foo != baz
395
409
  end
396
410
  RUBY
397
411
 
398
- assert_source <<-'RUBY'
412
+ assert_source <<~'RUBY'
399
413
  each do |baz|
400
414
  while foo
401
415
  foo = bar
@@ -403,7 +417,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
403
417
  end
404
418
  RUBY
405
419
 
406
- assert_source <<-'RUBY'
420
+ assert_source <<~'RUBY'
407
421
  each do |foo|
408
422
  while foo
409
423
  foo = bar
@@ -441,7 +455,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
441
455
  assert_unterminated "#{keyword} *nil"
442
456
  assert_unterminated "#{keyword} *foo, bar"
443
457
 
444
- assert_generates <<-RUBY, <<-RUBY
458
+ assert_generates <<~RUBY, <<~RUBY
445
459
  foo do |bar|
446
460
  bar =~ // || #{keyword}
447
461
  baz
@@ -453,7 +467,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
453
467
  end
454
468
  RUBY
455
469
 
456
- assert_generates <<-RUBY, <<-RUBY
470
+ assert_generates <<~RUBY, <<~RUBY
457
471
  #{keyword}(a ? b : c)
458
472
  RUBY
459
473
  #{keyword} (if a
@@ -484,91 +498,101 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
484
498
  assert_terminated 'foo(&(foo || bar))'
485
499
  assert_terminated 'foo(*arguments)'
486
500
  assert_terminated 'foo(*arguments)'
487
- assert_source <<-'RUBY'
501
+
502
+ assert_source <<~'RUBY'
488
503
  foo do
489
504
  end
490
505
  RUBY
491
506
 
492
- assert_source <<-'RUBY'
493
- foo(1) do
494
- nil
507
+ assert_source <<~'RUBY'
508
+ foo do |a|
495
509
  end
496
510
  RUBY
497
511
 
498
- assert_source <<-'RUBY'
512
+ assert_source <<~'RUBY'
513
+ foo do |a, |
514
+ end
515
+ RUBY
516
+
517
+ assert_source <<~'RUBY'
499
518
  foo do |a, b|
519
+ end
520
+ RUBY
521
+
522
+ assert_source <<~'RUBY'
523
+ foo(1) do
500
524
  nil
501
525
  end
502
526
  RUBY
503
527
 
504
- assert_source <<-'RUBY'
528
+ assert_source <<~'RUBY'
505
529
  foo do |a, *b|
506
530
  nil
507
531
  end
508
532
  RUBY
509
533
 
510
- assert_source <<-'RUBY'
534
+ assert_source <<~'RUBY'
511
535
  foo do |a, *|
512
536
  nil
513
537
  end
514
538
  RUBY
515
539
 
516
- assert_source <<-'RUBY'
540
+ assert_source <<~'RUBY'
517
541
  foo do
518
542
  bar
519
543
  end
520
544
  RUBY
521
545
 
522
- assert_source <<-'RUBY'
546
+ assert_source <<~'RUBY'
523
547
  foo.bar(*args)
524
548
  RUBY
525
549
 
526
- assert_source <<-'RUBY'
550
+ assert_source <<~'RUBY'
527
551
  foo.bar do |(a)|
528
552
  d
529
553
  end
530
554
  RUBY
531
555
 
532
- assert_source <<-'RUBY'
556
+ assert_source <<~'RUBY'
533
557
  foo.bar do |(a, b), c|
534
558
  d
535
559
  end
536
560
  RUBY
537
561
 
538
- assert_source <<-'RUBY'
562
+ assert_source <<~'RUBY'
539
563
  foo.bar do |*a; b|
540
564
  end
541
565
  RUBY
542
566
 
543
- assert_source <<-'RUBY'
567
+ assert_source <<~'RUBY'
544
568
  foo.bar do |a; b|
545
569
  end
546
570
  RUBY
547
571
 
548
- assert_source <<-'RUBY'
572
+ assert_source <<~'RUBY'
549
573
  foo.bar do |; a, b|
550
574
  end
551
575
  RUBY
552
576
 
553
- assert_source <<-'RUBY'
577
+ assert_source <<~'RUBY'
554
578
  foo.bar do |((*))|
555
579
  d
556
580
  end
557
581
  RUBY
558
582
 
559
- assert_source <<-'RUBY'
583
+ assert_source <<~'RUBY'
560
584
  foo.bar do |(a, (*))|
561
585
  d
562
586
  end
563
587
  RUBY
564
588
 
565
- assert_source <<-'RUBY'
589
+ assert_source <<~'RUBY'
566
590
  foo.bar do |(a, b)|
567
591
  d
568
592
  end
569
593
  RUBY
570
594
 
571
- assert_source <<-'RUBY'
595
+ assert_source <<~'RUBY'
572
596
  foo.bar do
573
597
  end.baz
574
598
  RUBY
@@ -578,17 +602,17 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
578
602
  assert_unterminated 'a || return'
579
603
  assert_unterminated 'foo << (bar * baz)'
580
604
 
581
- assert_source <<-'RUBY'
605
+ assert_source <<~'RUBY'
582
606
  foo ||= (a, _ = b)
583
607
  RUBY
584
608
 
585
- assert_source <<-'RUBY'
609
+ assert_source <<~'RUBY'
586
610
  begin
587
611
  rescue
588
612
  end.bar
589
613
  RUBY
590
614
 
591
- assert_source <<-'RUBY'
615
+ assert_source <<~'RUBY'
592
616
  case (def foo
593
617
  end
594
618
  :bar)
@@ -596,58 +620,58 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
596
620
  end.baz
597
621
  RUBY
598
622
 
599
- assert_source <<-'RUBY'
623
+ assert_source <<~'RUBY'
600
624
  case foo
601
625
  when bar
602
626
  end.baz
603
627
  RUBY
604
628
 
605
- assert_source <<-'RUBY'
629
+ assert_source <<~'RUBY'
606
630
  class << self
607
631
  end.bar
608
632
  RUBY
609
633
 
610
- assert_source <<-'RUBY'
634
+ assert_source <<~'RUBY'
611
635
  def self.foo
612
636
  end.bar
613
637
  RUBY
614
638
 
615
- assert_source <<-'RUBY'
639
+ assert_source <<~'RUBY'
616
640
  def foo
617
641
  end.bar
618
642
  RUBY
619
643
 
620
- assert_source <<-'RUBY'
644
+ assert_source <<~'RUBY'
621
645
  until foo
622
646
  end.bar
623
647
  RUBY
624
648
 
625
- assert_source <<-'RUBY'
649
+ assert_source <<~'RUBY'
626
650
  while foo
627
651
  end.bar
628
652
  RUBY
629
653
 
630
- assert_source <<-'RUBY'
654
+ assert_source <<~'RUBY'
631
655
  loop do
632
656
  end.bar
633
657
  RUBY
634
658
 
635
- assert_source <<-'RUBY'
659
+ assert_source <<~'RUBY'
636
660
  class Foo
637
661
  end.bar
638
662
  RUBY
639
663
 
640
- assert_source <<-'RUBY'
664
+ assert_source <<~'RUBY'
641
665
  module Foo
642
666
  end.bar
643
667
  RUBY
644
668
 
645
- assert_source <<-'RUBY'
669
+ assert_source <<~'RUBY'
646
670
  if foo
647
671
  end.baz
648
672
  RUBY
649
673
 
650
- assert_source <<-'RUBY'
674
+ assert_source <<~'RUBY'
651
675
  local = 1
652
676
  local.bar
653
677
  RUBY
@@ -657,15 +681,16 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
657
681
  assert_terminated 'foo.bar(*args, foo)'
658
682
  assert_terminated 'foo.bar(foo, *args)'
659
683
  assert_terminated 'foo.bar(foo, *args, &block)'
660
- assert_source <<-'RUBY'
684
+ assert_source <<~'RUBY'
661
685
  foo(bar, *args)
662
686
  RUBY
663
687
 
664
688
  assert_terminated 'foo(*args, &block)'
665
689
  assert_terminated 'foo.bar(&baz)'
666
690
  assert_terminated 'foo.bar(:baz, &baz)'
667
- assert_terminated 'foo.bar = :baz'
668
- assert_unterminated 'self.foo = :bar'
691
+ assert_terminated 'foo.bar=:baz'
692
+
693
+ assert_unterminated 'self.foo=:bar'
669
694
 
670
695
  assert_terminated 'foo.bar(baz: boz)'
671
696
  assert_terminated 'foo.bar(foo, "baz" => boz)'
@@ -676,17 +701,17 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
676
701
  context 'begin; end' do
677
702
  assert_generates s(:begin), ''
678
703
 
679
- assert_source <<-'RUBY'
704
+ assert_source <<~'RUBY'
680
705
  begin
681
706
  end
682
707
  RUBY
683
708
 
684
- assert_source <<-'RUBY'
709
+ assert_source <<~'RUBY'
685
710
  foo
686
711
  bar
687
712
  RUBY
688
713
 
689
- assert_source <<-'RUBY'
714
+ assert_source <<~'RUBY'
690
715
  begin
691
716
  foo
692
717
  bar
@@ -695,7 +720,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
695
720
  end
696
721
 
697
722
  context 'begin / rescue / ensure' do
698
- assert_source <<-'RUBY'
723
+ assert_source <<~'RUBY'
699
724
  begin
700
725
  foo
701
726
  ensure
@@ -704,7 +729,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
704
729
  end
705
730
  RUBY
706
731
 
707
- assert_source <<-'RUBY'
732
+ assert_source <<~'RUBY'
708
733
  begin
709
734
  foo
710
735
  rescue
@@ -712,7 +737,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
712
737
  end
713
738
  RUBY
714
739
 
715
- assert_source <<-'RUBY'
740
+ assert_source <<~'RUBY'
716
741
  begin
717
742
  begin
718
743
  foo
@@ -725,14 +750,14 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
725
750
  end
726
751
  RUBY
727
752
 
728
- assert_source <<-'RUBY'
753
+ assert_source <<~'RUBY'
729
754
  begin
730
755
  raise(Exception) rescue foo = bar
731
756
  rescue Exception
732
757
  end
733
758
  RUBY
734
759
 
735
- assert_source <<-'RUBY'
760
+ assert_source <<~'RUBY'
736
761
  begin
737
762
  foo
738
763
  bar
@@ -742,7 +767,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
742
767
  end
743
768
  RUBY
744
769
 
745
- assert_source <<-'RUBY'
770
+ assert_source <<~'RUBY'
746
771
  begin
747
772
  foo
748
773
  rescue Exception
@@ -750,7 +775,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
750
775
  end
751
776
  RUBY
752
777
 
753
- assert_source <<-'RUBY'
778
+ assert_source <<~'RUBY'
754
779
  begin
755
780
  foo
756
781
  rescue => bar
@@ -758,7 +783,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
758
783
  end
759
784
  RUBY
760
785
 
761
- assert_source <<-'RUBY'
786
+ assert_source <<~'RUBY'
762
787
  begin
763
788
  foo
764
789
  rescue Exception, Other => bar
@@ -766,44 +791,44 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
766
791
  end
767
792
  RUBY
768
793
 
769
- assert_source <<-'RUBY'
794
+ assert_source <<~'RUBY'
770
795
  class << self
771
796
  undef :bar rescue nil
772
797
  end
773
798
  RUBY
774
799
 
775
- assert_source <<-'RUBY'
800
+ assert_source <<~'RUBY'
776
801
  module Foo
777
802
  undef :bar rescue nil
778
803
  end
779
804
  RUBY
780
805
 
781
- assert_source <<-'RUBY'
806
+ assert_source <<~'RUBY'
782
807
  class Foo
783
808
  undef :bar rescue nil
784
809
  end
785
810
  RUBY
786
811
 
787
- assert_source <<-'RUBY'
812
+ assert_source <<~'RUBY'
788
813
  begin
789
814
  rescue Exception => e
790
815
  end
791
816
  RUBY
792
817
 
793
- assert_source <<-'RUBY'
818
+ assert_source <<~'RUBY'
794
819
  begin
795
820
  ensure
796
821
  end
797
822
  RUBY
798
823
 
799
- assert_source <<-'RUBY'
824
+ assert_source <<~'RUBY'
800
825
  begin
801
826
  rescue
802
827
  ensure
803
828
  end
804
829
  RUBY
805
830
 
806
- assert_source <<-'RUBY'
831
+ assert_source <<~'RUBY'
807
832
  begin
808
833
  foo
809
834
  rescue Exception => bar
@@ -811,7 +836,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
811
836
  end
812
837
  RUBY
813
838
 
814
- assert_source <<-'RUBY'
839
+ assert_source <<~'RUBY'
815
840
  begin
816
841
  bar
817
842
  rescue SomeError, *bar
@@ -819,7 +844,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
819
844
  end
820
845
  RUBY
821
846
 
822
- assert_source <<-'RUBY'
847
+ assert_source <<~'RUBY'
823
848
  begin
824
849
  bar
825
850
  rescue SomeError, *bar => exception
@@ -827,7 +852,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
827
852
  end
828
853
  RUBY
829
854
 
830
- assert_source <<-'RUBY'
855
+ assert_source <<~'RUBY'
831
856
  begin
832
857
  bar
833
858
  rescue *bar
@@ -835,14 +860,14 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
835
860
  end
836
861
  RUBY
837
862
 
838
- assert_source <<-'RUBY'
863
+ assert_source <<~'RUBY'
839
864
  begin
840
865
  bar
841
866
  rescue LoadError
842
867
  end
843
868
  RUBY
844
869
 
845
- assert_source <<-'RUBY'
870
+ assert_source <<~'RUBY'
846
871
  begin
847
872
  bar
848
873
  rescue
@@ -851,7 +876,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
851
876
  end
852
877
  RUBY
853
878
 
854
- assert_source <<-'RUBY'
879
+ assert_source <<~'RUBY'
855
880
  begin
856
881
  bar
857
882
  rescue *bar => exception
@@ -859,19 +884,94 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
859
884
  end
860
885
  RUBY
861
886
 
887
+ assert_source <<~'RUBY'
888
+ m do
889
+ rescue Exception => e
890
+ end
891
+ RUBY
892
+
893
+ assert_source <<~'RUBY'
894
+ m do
895
+ ensure
896
+ end
897
+ RUBY
898
+
899
+ assert_source <<~'RUBY'
900
+ m do
901
+ rescue
902
+ ensure
903
+ end
904
+ RUBY
905
+
906
+ assert_source <<~'RUBY'
907
+ m do
908
+ foo
909
+ rescue Exception => bar
910
+ bar
911
+ end
912
+ RUBY
913
+
914
+ assert_source <<~'RUBY'
915
+ m do
916
+ bar
917
+ rescue SomeError, *bar
918
+ baz
919
+ end
920
+ RUBY
921
+
922
+ assert_source <<~'RUBY'
923
+ m do
924
+ bar
925
+ rescue SomeError, *bar => exception
926
+ baz
927
+ end
928
+ RUBY
929
+
930
+ assert_source <<~'RUBY'
931
+ m do
932
+ bar
933
+ rescue *bar
934
+ baz
935
+ end
936
+ RUBY
937
+
938
+ assert_source <<~'RUBY'
939
+ m do
940
+ bar
941
+ rescue LoadError
942
+ end
943
+ RUBY
944
+
945
+ assert_source <<~'RUBY'
946
+ m do
947
+ bar
948
+ rescue
949
+ else
950
+ baz
951
+ end
952
+ RUBY
953
+
954
+ assert_source <<~'RUBY'
955
+ m do
956
+ bar
957
+ rescue *bar => exception
958
+ baz
959
+ end
960
+ RUBY
961
+
862
962
  assert_source 'foo rescue bar'
863
963
  assert_source 'foo rescue return bar'
864
964
  assert_source 'x = (foo rescue return bar)'
865
965
 
866
966
  %w(while until if).each do |keyword|
867
- assert_source <<-RUBY
967
+ assert_source <<~RUBY
868
968
  #{keyword} (
869
969
  foo rescue false
870
970
  )
871
971
  end
872
972
  RUBY
873
973
 
874
- assert_generates <<-RUBY, <<-GENERATED
974
+ assert_generates <<~RUBY, <<~GENERATED
875
975
  foo rescue false #{keyword} true
876
976
  RUBY
877
977
  #{keyword} true
@@ -880,7 +980,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
880
980
  GENERATED
881
981
  end
882
982
 
883
- assert_generates <<-'RUBY', <<-GENERATED
983
+ assert_generates <<~'RUBY', <<~GENERATED
884
984
  case (foo rescue false)
885
985
  when true
886
986
  end
@@ -902,31 +1002,31 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
902
1002
  assert_source 'super(&block)'
903
1003
  assert_source 'super(a, &block)'
904
1004
 
905
- assert_source <<-'RUBY'
1005
+ assert_source <<~'RUBY'
906
1006
  super(a do
907
1007
  foo
908
1008
  end)
909
1009
  RUBY
910
1010
 
911
- assert_source <<-'RUBY'
1011
+ assert_source <<~'RUBY'
912
1012
  super do
913
1013
  foo
914
1014
  end
915
1015
  RUBY
916
1016
 
917
- assert_source <<-'RUBY'
1017
+ assert_source <<~'RUBY'
918
1018
  super(a) do
919
1019
  foo
920
1020
  end
921
1021
  RUBY
922
1022
 
923
- assert_source <<-'RUBY'
1023
+ assert_source <<~'RUBY'
924
1024
  super() do
925
1025
  foo
926
1026
  end
927
1027
  RUBY
928
1028
 
929
- assert_source <<-'RUBY'
1029
+ assert_source <<~'RUBY'
930
1030
  super(a, b) do
931
1031
  foo
932
1032
  end
@@ -940,7 +1040,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
940
1040
  end
941
1041
 
942
1042
  context 'BEGIN' do
943
- assert_source <<-'RUBY'
1043
+ assert_source <<~'RUBY'
944
1044
  BEGIN {
945
1045
  foo
946
1046
  }
@@ -948,7 +1048,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
948
1048
  end
949
1049
 
950
1050
  context 'END' do
951
- assert_source <<-'RUBY'
1051
+ assert_source <<~'RUBY'
952
1052
  END {
953
1053
  foo
954
1054
  }
@@ -956,11 +1056,11 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
956
1056
  end
957
1057
 
958
1058
  context 'alias' do
959
- assert_source <<-'RUBY'
1059
+ assert_source <<~'RUBY'
960
1060
  alias $foo $bar
961
1061
  RUBY
962
1062
 
963
- assert_source <<-'RUBY'
1063
+ assert_source <<~'RUBY'
964
1064
  alias :foo :bar
965
1065
  RUBY
966
1066
  end
@@ -980,19 +1080,19 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
980
1080
  end
981
1081
 
982
1082
  context 'if statement' do
983
- assert_source <<-'RUBY'
1083
+ assert_source <<~'RUBY'
984
1084
  if /foo/
985
1085
  bar
986
1086
  end
987
1087
  RUBY
988
1088
 
989
- assert_source <<-'RUBY'
1089
+ assert_source <<~'RUBY'
990
1090
  if 3
991
1091
  9
992
1092
  end
993
1093
  RUBY
994
1094
 
995
- assert_source <<-'RUBY'
1095
+ assert_source <<~'RUBY'
996
1096
  if 4
997
1097
  5
998
1098
  else
@@ -1000,32 +1100,32 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1000
1100
  end
1001
1101
  RUBY
1002
1102
 
1003
- assert_source <<-'RUBY'
1103
+ assert_source <<~'RUBY'
1004
1104
  unless 3
1005
1105
  nil
1006
1106
  end
1007
1107
  RUBY
1008
1108
 
1009
- assert_source <<-'RUBY'
1109
+ assert_source <<~'RUBY'
1010
1110
  unless 3
1011
1111
  9
1012
1112
  end
1013
1113
  RUBY
1014
1114
 
1015
- assert_source <<-'RUBY'
1115
+ assert_source <<~'RUBY'
1016
1116
  if foo
1017
1117
  end
1018
1118
  RUBY
1019
1119
 
1020
- assert_source <<-'RUBY'
1120
+ assert_source <<~'RUBY'
1021
1121
  foo = bar if foo
1022
1122
  RUBY
1023
1123
 
1024
- assert_source <<-'RUBY'
1124
+ assert_source <<~'RUBY'
1025
1125
  foo = bar unless foo
1026
1126
  RUBY
1027
1127
 
1028
- assert_source <<-'RUBY'
1128
+ assert_source <<~'RUBY'
1029
1129
  def foo(*foo)
1030
1130
  unless foo
1031
1131
  foo = bar
@@ -1033,7 +1133,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1033
1133
  end
1034
1134
  RUBY
1035
1135
 
1036
- assert_source <<-'RUBY'
1136
+ assert_source <<~'RUBY'
1037
1137
  each do |foo|
1038
1138
  unless foo
1039
1139
  foo = bar
@@ -1045,18 +1145,18 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1045
1145
  context 'def' do
1046
1146
  context 'on instance' do
1047
1147
 
1048
- assert_source <<-'RUBY'
1148
+ assert_source <<~'RUBY'
1049
1149
  def foo
1050
1150
  end
1051
1151
  RUBY
1052
1152
 
1053
- assert_source <<-'RUBY'
1153
+ assert_source <<~'RUBY'
1054
1154
  def foo
1055
1155
  bar
1056
1156
  end
1057
1157
  RUBY
1058
1158
 
1059
- assert_source <<-'RUBY'
1159
+ assert_source <<~'RUBY'
1060
1160
  def foo
1061
1161
  foo
1062
1162
  rescue
@@ -1066,7 +1166,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1066
1166
  end
1067
1167
  RUBY
1068
1168
 
1069
- assert_source <<-'RUBY'
1169
+ assert_source <<~'RUBY'
1070
1170
  begin
1071
1171
  foo
1072
1172
  ensure
@@ -1074,7 +1174,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1074
1174
  end
1075
1175
  RUBY
1076
1176
 
1077
- assert_source <<-'RUBY'
1177
+ assert_source <<~'RUBY'
1078
1178
  def foo
1079
1179
  bar
1080
1180
  ensure
@@ -1082,7 +1182,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1082
1182
  end
1083
1183
  RUBY
1084
1184
 
1085
- assert_source <<-'RUBY'
1185
+ assert_source <<~'RUBY'
1086
1186
  def self.foo
1087
1187
  bar
1088
1188
  rescue
@@ -1090,7 +1190,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1090
1190
  end
1091
1191
  RUBY
1092
1192
 
1093
- assert_source <<-'RUBY'
1193
+ assert_source <<~'RUBY'
1094
1194
  def foo
1095
1195
  bar
1096
1196
  rescue
@@ -1098,134 +1198,134 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1098
1198
  end
1099
1199
  RUBY
1100
1200
 
1101
- assert_source <<-'RUBY'
1201
+ assert_source <<~'RUBY'
1102
1202
  def foo(bar)
1103
1203
  bar
1104
1204
  end
1105
1205
  RUBY
1106
1206
 
1107
- assert_source <<-'RUBY'
1207
+ assert_source <<~'RUBY'
1108
1208
  def foo(bar, baz)
1109
1209
  bar
1110
1210
  end
1111
1211
  RUBY
1112
1212
 
1113
- assert_source <<-'RUBY'
1213
+ assert_source <<~'RUBY'
1114
1214
  def foo(bar = ())
1115
1215
  bar
1116
1216
  end
1117
1217
  RUBY
1118
1218
 
1119
- assert_source <<-'RUBY'
1219
+ assert_source <<~'RUBY'
1120
1220
  def foo(bar = (baz
1121
1221
  nil))
1122
1222
  end
1123
1223
  RUBY
1124
1224
 
1125
- assert_source <<-'RUBY'
1225
+ assert_source <<~'RUBY'
1126
1226
  def foo(bar = true)
1127
1227
  bar
1128
1228
  end
1129
1229
  RUBY
1130
1230
 
1131
- assert_source <<-'RUBY'
1231
+ assert_source <<~'RUBY'
1132
1232
  def foo(bar, baz = true)
1133
1233
  bar
1134
1234
  end
1135
1235
  RUBY
1136
1236
 
1137
- assert_source <<-'RUBY'
1237
+ assert_source <<~'RUBY'
1138
1238
  def foo(bar: 1)
1139
1239
  end
1140
1240
  RUBY
1141
1241
 
1142
- assert_source <<-'RUBY'
1242
+ assert_source <<~'RUBY'
1143
1243
  def foo(*)
1144
1244
  bar
1145
1245
  end
1146
1246
  RUBY
1147
1247
 
1148
- assert_source <<-'RUBY'
1248
+ assert_source <<~'RUBY'
1149
1249
  def foo(*bar)
1150
1250
  bar
1151
1251
  end
1152
1252
  RUBY
1153
1253
 
1154
- assert_source <<-'RUBY'
1254
+ assert_source <<~'RUBY'
1155
1255
  def foo(bar, *baz)
1156
1256
  bar
1157
1257
  end
1158
1258
  RUBY
1159
1259
 
1160
- assert_source <<-'RUBY'
1260
+ assert_source <<~'RUBY'
1161
1261
  def foo(baz = true, *bor)
1162
1262
  bar
1163
1263
  end
1164
1264
  RUBY
1165
1265
 
1166
- assert_source <<-'RUBY'
1266
+ assert_source <<~'RUBY'
1167
1267
  def foo(baz = true, *bor, &block)
1168
1268
  bar
1169
1269
  end
1170
1270
  RUBY
1171
1271
 
1172
- assert_source <<-'RUBY'
1272
+ assert_source <<~'RUBY'
1173
1273
  def foo(bar, baz = true, *bor)
1174
1274
  bar
1175
1275
  end
1176
1276
  RUBY
1177
1277
 
1178
- assert_source <<-'RUBY'
1278
+ assert_source <<~'RUBY'
1179
1279
  def foo(&block)
1180
1280
  bar
1181
1281
  end
1182
1282
  RUBY
1183
1283
 
1184
- assert_source <<-'RUBY'
1284
+ assert_source <<~'RUBY'
1185
1285
  def foo(bar, &block)
1186
1286
  bar
1187
1287
  end
1188
1288
  RUBY
1189
1289
 
1190
- assert_source <<-'RUBY'
1290
+ assert_source <<~'RUBY'
1191
1291
  def foo
1192
1292
  bar
1193
1293
  baz
1194
1294
  end
1195
1295
  RUBY
1196
1296
 
1197
- assert_source <<-'RUBY'
1297
+ assert_source <<~'RUBY'
1198
1298
  def (foo do |bar|
1199
1299
  end).bar
1200
1300
  bar
1201
1301
  end
1202
1302
  RUBY
1203
1303
 
1204
- assert_source <<-'RUBY'
1304
+ assert_source <<~'RUBY'
1205
1305
  def (foo(1)).bar
1206
1306
  bar
1207
1307
  end
1208
1308
  RUBY
1209
1309
 
1210
- assert_source <<-'RUBY'
1310
+ assert_source <<~'RUBY'
1211
1311
  def (Foo::Bar.baz).bar
1212
1312
  baz
1213
1313
  end
1214
1314
  RUBY
1215
1315
 
1216
- assert_source <<-'RUBY'
1316
+ assert_source <<~'RUBY'
1217
1317
  def (Foo::Bar).bar
1218
1318
  baz
1219
1319
  end
1220
1320
  RUBY
1221
1321
 
1222
- assert_source <<-'RUBY'
1322
+ assert_source <<~'RUBY'
1223
1323
  def Foo.bar
1224
1324
  baz
1225
1325
  end
1226
1326
  RUBY
1227
1327
 
1228
- assert_source <<-'RUBY'
1328
+ assert_source <<~'RUBY'
1229
1329
  def foo.bar
1230
1330
  baz
1231
1331
  end
@@ -1233,25 +1333,25 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1233
1333
  end
1234
1334
 
1235
1335
  context 'on singleton' do
1236
- assert_source <<-'RUBY'
1336
+ assert_source <<~'RUBY'
1237
1337
  def self.foo
1238
1338
  end
1239
1339
  RUBY
1240
1340
 
1241
- assert_source <<-'RUBY'
1341
+ assert_source <<~'RUBY'
1242
1342
  def self.foo
1243
1343
  bar
1244
1344
  end
1245
1345
  RUBY
1246
1346
 
1247
- assert_source <<-'RUBY'
1347
+ assert_source <<~'RUBY'
1248
1348
  def self.foo
1249
1349
  bar
1250
1350
  baz
1251
1351
  end
1252
1352
  RUBY
1253
1353
 
1254
- assert_source <<-'RUBY'
1354
+ assert_source <<~'RUBY'
1255
1355
  def Foo.bar
1256
1356
  bar
1257
1357
  end
@@ -1260,43 +1360,43 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1260
1360
  end
1261
1361
 
1262
1362
  context 'class' do
1263
- assert_source <<-'RUBY'
1363
+ assert_source <<~'RUBY'
1264
1364
  class TestClass
1265
1365
  end
1266
1366
  RUBY
1267
1367
 
1268
- assert_source <<-'RUBY'
1368
+ assert_source <<~'RUBY'
1269
1369
  class << some_object
1270
1370
  end
1271
1371
  RUBY
1272
1372
 
1273
- assert_source <<-'RUBY'
1373
+ assert_source <<~'RUBY'
1274
1374
  class << some_object
1275
1375
  the_body
1276
1376
  end
1277
1377
  RUBY
1278
1378
 
1279
- assert_source <<-'RUBY'
1379
+ assert_source <<~'RUBY'
1280
1380
  class SomeNameSpace::TestClass
1281
1381
  end
1282
1382
  RUBY
1283
1383
 
1284
- assert_source <<-'RUBY'
1384
+ assert_source <<~'RUBY'
1285
1385
  class Some::Name::Space::TestClass
1286
1386
  end
1287
1387
  RUBY
1288
1388
 
1289
- assert_source <<-'RUBY'
1389
+ assert_source <<~'RUBY'
1290
1390
  class TestClass < Object
1291
1391
  end
1292
1392
  RUBY
1293
1393
 
1294
- assert_source <<-'RUBY'
1394
+ assert_source <<~'RUBY'
1295
1395
  class TestClass < SomeNameSpace::Object
1296
1396
  end
1297
1397
  RUBY
1298
1398
 
1299
- assert_source <<-'RUBY'
1399
+ assert_source <<~'RUBY'
1300
1400
  class TestClass
1301
1401
  def foo
1302
1402
  :bar
@@ -1304,7 +1404,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1304
1404
  end
1305
1405
  RUBY
1306
1406
 
1307
- assert_source <<-'RUBY'
1407
+ assert_source <<~'RUBY'
1308
1408
  class ::TestClass
1309
1409
  end
1310
1410
  RUBY
@@ -1312,22 +1412,22 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1312
1412
 
1313
1413
  context 'module' do
1314
1414
 
1315
- assert_source <<-'RUBY'
1415
+ assert_source <<~'RUBY'
1316
1416
  module TestModule
1317
1417
  end
1318
1418
  RUBY
1319
1419
 
1320
- assert_source <<-'RUBY'
1420
+ assert_source <<~'RUBY'
1321
1421
  module SomeNameSpace::TestModule
1322
1422
  end
1323
1423
  RUBY
1324
1424
 
1325
- assert_source <<-'RUBY'
1425
+ assert_source <<~'RUBY'
1326
1426
  module Some::Name::Space::TestModule
1327
1427
  end
1328
1428
  RUBY
1329
1429
 
1330
- assert_source <<-'RUBY'
1430
+ assert_source <<~'RUBY'
1331
1431
  module TestModule
1332
1432
  def foo
1333
1433
  :bar
@@ -1351,7 +1451,10 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1351
1451
  assert_source 'foo[a, b] = value'
1352
1452
  assert_source 'foo[1..2] = value'
1353
1453
  assert_source 'foo.[]=()'
1454
+ assert_source 'foo.[]=true'
1455
+ assert_source 'foo.[]=(1, 2)'
1354
1456
  assert_source 'foo[] = 1'
1457
+ assert_unterminated 'foo[] = 1'
1355
1458
 
1356
1459
  %w(+ - * / % & | || &&).each do |operator|
1357
1460
  context "with #{operator}" do
@@ -1362,31 +1465,46 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1362
1465
  end
1363
1466
 
1364
1467
  context 'defined?' do
1365
- assert_source <<-'RUBY'
1468
+ assert_source <<~'RUBY'
1366
1469
  defined?(@foo)
1367
1470
  RUBY
1368
1471
 
1369
- assert_source <<-'RUBY'
1472
+ assert_source <<~'RUBY'
1370
1473
  defined?(Foo)
1371
1474
  RUBY
1372
1475
 
1373
- assert_source <<-'RUBY'
1476
+ assert_source <<~'RUBY'
1374
1477
  defined?((a, b = [1, 2]))
1375
1478
  RUBY
1376
1479
  end
1377
1480
  end
1378
1481
 
1379
1482
  context 'lambda' do
1380
- assert_source <<-'RUBY'
1483
+ assert_source <<~'RUBY'
1381
1484
  lambda do
1382
1485
  end
1383
1486
  RUBY
1384
1487
 
1385
- assert_source <<-'RUBY'
1488
+ assert_source <<~'RUBY'
1386
1489
  lambda do |a, b|
1387
1490
  a
1388
1491
  end
1389
1492
  RUBY
1493
+
1494
+ assert_source <<~'RUBY'
1495
+ ->() do
1496
+ end
1497
+ RUBY
1498
+
1499
+ assert_source <<~'RUBY'
1500
+ ->(a) do
1501
+ end
1502
+ RUBY
1503
+
1504
+ assert_source <<~'RUBY'
1505
+ ->(a, b) do
1506
+ end
1507
+ RUBY
1390
1508
  end
1391
1509
 
1392
1510
  context 'match operators' do
@@ -1450,7 +1568,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1450
1568
 
1451
1569
  context 'flip flops' do
1452
1570
  context 'inclusive' do
1453
- assert_source <<-'RUBY'
1571
+ assert_source <<~'RUBY'
1454
1572
  if ((i == 4)..(i == 4))
1455
1573
  foo
1456
1574
  end
@@ -1458,7 +1576,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1458
1576
  end
1459
1577
 
1460
1578
  context 'exclusive' do
1461
- assert_source <<-'RUBY'
1579
+ assert_source <<~'RUBY'
1462
1580
  if ((i == 4)...(i == 4))
1463
1581
  foo
1464
1582
  end
@@ -1467,7 +1585,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1467
1585
  end
1468
1586
 
1469
1587
  context 'case statement' do
1470
- assert_source <<-'RUBY'
1588
+ assert_source <<~'RUBY'
1471
1589
  case
1472
1590
  when bar
1473
1591
  baz
@@ -1476,7 +1594,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1476
1594
  end
1477
1595
  RUBY
1478
1596
 
1479
- assert_source <<-'RUBY'
1597
+ assert_source <<~'RUBY'
1480
1598
  case foo
1481
1599
  when bar
1482
1600
  when baz
@@ -1484,7 +1602,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1484
1602
  end
1485
1603
  RUBY
1486
1604
 
1487
- assert_source <<-'RUBY'
1605
+ assert_source <<~'RUBY'
1488
1606
  case foo
1489
1607
  when bar
1490
1608
  baz
@@ -1493,21 +1611,21 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1493
1611
  end
1494
1612
  RUBY
1495
1613
 
1496
- assert_source <<-'RUBY'
1614
+ assert_source <<~'RUBY'
1497
1615
  case foo
1498
1616
  when bar, baz
1499
1617
  :other
1500
1618
  end
1501
1619
  RUBY
1502
1620
 
1503
- assert_source <<-'RUBY'
1621
+ assert_source <<~'RUBY'
1504
1622
  case foo
1505
1623
  when *bar
1506
1624
  :value
1507
1625
  end
1508
1626
  RUBY
1509
1627
 
1510
- assert_source <<-'RUBY'
1628
+ assert_source <<~'RUBY'
1511
1629
  case foo
1512
1630
  when bar
1513
1631
  baz
@@ -1518,24 +1636,24 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1518
1636
  end
1519
1637
 
1520
1638
  context 'for' do
1521
- assert_source <<-'RUBY'
1639
+ assert_source <<~'RUBY'
1522
1640
  bar(for a in bar do
1523
1641
  baz
1524
1642
  end)
1525
1643
  RUBY
1526
- assert_source <<-'RUBY'
1644
+ assert_source <<~'RUBY'
1527
1645
  for a in bar do
1528
1646
  baz
1529
1647
  end
1530
1648
  RUBY
1531
1649
 
1532
- assert_source <<-'RUBY'
1650
+ assert_source <<~'RUBY'
1533
1651
  for a, *b in bar do
1534
1652
  baz
1535
1653
  end
1536
1654
  RUBY
1537
1655
 
1538
- assert_source <<-'RUBY'
1656
+ assert_source <<~'RUBY'
1539
1657
  for a, b in bar do
1540
1658
  baz
1541
1659
  end
@@ -1554,7 +1672,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1554
1672
  end
1555
1673
 
1556
1674
  context 'loop' do
1557
- assert_source <<-'RUBY'
1675
+ assert_source <<~'RUBY'
1558
1676
  loop do
1559
1677
  foo
1560
1678
  end
@@ -1562,26 +1680,26 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1562
1680
  end
1563
1681
 
1564
1682
  context 'post conditions' do
1565
- assert_source <<-'RUBY'
1683
+ assert_source <<~'RUBY'
1566
1684
  x = (begin
1567
1685
  foo
1568
1686
  end while baz)
1569
1687
  RUBY
1570
1688
 
1571
- assert_source <<-'RUBY'
1689
+ assert_source <<~'RUBY'
1572
1690
  begin
1573
1691
  foo
1574
1692
  end while baz
1575
1693
  RUBY
1576
1694
 
1577
- assert_source <<-'RUBY'
1695
+ assert_source <<~'RUBY'
1578
1696
  begin
1579
1697
  foo
1580
1698
  bar
1581
1699
  end until baz
1582
1700
  RUBY
1583
1701
 
1584
- assert_source <<-'RUBY'
1702
+ assert_source <<~'RUBY'
1585
1703
  begin
1586
1704
  foo
1587
1705
  bar
@@ -1590,18 +1708,18 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1590
1708
  end
1591
1709
 
1592
1710
  context 'while' do
1593
- assert_source <<-'RUBY'
1711
+ assert_source <<~'RUBY'
1594
1712
  while false
1595
1713
  end
1596
1714
  RUBY
1597
1715
 
1598
- assert_source <<-'RUBY'
1716
+ assert_source <<~'RUBY'
1599
1717
  while false
1600
1718
  3
1601
1719
  end
1602
1720
  RUBY
1603
1721
 
1604
- assert_source <<-'RUBY'
1722
+ assert_source <<~'RUBY'
1605
1723
  while (foo do
1606
1724
  end)
1607
1725
  :body
@@ -1610,18 +1728,18 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1610
1728
  end
1611
1729
 
1612
1730
  context 'until' do
1613
- assert_source <<-'RUBY'
1731
+ assert_source <<~'RUBY'
1614
1732
  until false
1615
1733
  end
1616
1734
  RUBY
1617
1735
 
1618
- assert_source <<-'RUBY'
1736
+ assert_source <<~'RUBY'
1619
1737
  until false
1620
1738
  3
1621
1739
  end
1622
1740
  RUBY
1623
1741
 
1624
- assert_source <<-'RUBY'
1742
+ assert_source <<~'RUBY'
1625
1743
  until (foo do
1626
1744
  end)
1627
1745
  :body
@@ -1629,16 +1747,16 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1629
1747
  RUBY
1630
1748
  end
1631
1749
 
1632
- assert_source <<-'RUBY'
1750
+ assert_source <<~'RUBY'
1633
1751
  # comment before
1634
1752
  a_line_of_code
1635
1753
  RUBY
1636
1754
 
1637
- assert_source <<-'RUBY'
1755
+ assert_source <<~'RUBY'
1638
1756
  a_line_of_code # comment after
1639
1757
  RUBY
1640
1758
 
1641
- assert_source <<-'RUBY'
1759
+ assert_source <<~'RUBY'
1642
1760
  nested do # first
1643
1761
  # second
1644
1762
  something # comment
@@ -1647,7 +1765,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1647
1765
  # last
1648
1766
  RUBY
1649
1767
 
1650
- assert_generates <<-'RUBY', <<-'RUBY'
1768
+ assert_generates <<~'RUBY', <<~'RUBY'
1651
1769
  foo if bar
1652
1770
  # comment
1653
1771
  RUBY
@@ -1657,13 +1775,13 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1657
1775
  # comment
1658
1776
  RUBY
1659
1777
 
1660
- assert_source <<-'RUBY'
1778
+ assert_source <<~'RUBY'
1661
1779
  def noop
1662
1780
  # do nothing
1663
1781
  end
1664
1782
  RUBY
1665
1783
 
1666
- assert_source <<-'RUBY'
1784
+ assert_source <<~'RUBY'
1667
1785
  =begin
1668
1786
  block comment
1669
1787
  =end
@@ -1678,14 +1796,14 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1678
1796
  end
1679
1797
  RUBY
1680
1798
 
1681
- assert_generates(<<-'RUBY', <<-'RUBY')
1799
+ assert_generates(<<~'RUBY', <<~'RUBY')
1682
1800
  1 + # first
1683
1801
  2 # second
1684
1802
  RUBY
1685
1803
  1 + 2 # first # second
1686
1804
  RUBY
1687
1805
 
1688
- assert_generates(<<-'RUBY', <<-'RUBY')
1806
+ assert_generates(<<~'RUBY', <<~'RUBY')
1689
1807
  1 +
1690
1808
  # first
1691
1809
  2 # second
@@ -1693,7 +1811,7 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1693
1811
  1 + 2 # first # second
1694
1812
  RUBY
1695
1813
 
1696
- assert_generates(<<-'RUBY', <<-'RUBY')
1814
+ assert_generates(<<~'RUBY', <<~'RUBY')
1697
1815
  1 +
1698
1816
  =begin
1699
1817
  block comment
@@ -1705,6 +1823,5 @@ describe Unparser, mutant_expression: 'Unparser::Emitter*' do
1705
1823
  block comment
1706
1824
  =end
1707
1825
  RUBY
1708
-
1709
1826
  end
1710
1827
  end