unparser 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. metadata +3 -59
  4. data/.github/workflows/ci.yml +0 -90
  5. data/.gitignore +0 -37
  6. data/.rspec +0 -4
  7. data/.rubocop.yml +0 -126
  8. data/Changelog.md +0 -162
  9. data/Gemfile +0 -9
  10. data/Gemfile.lock +0 -111
  11. data/LICENSE +0 -20
  12. data/Rakefile +0 -22
  13. data/config/devtools.yml +0 -2
  14. data/config/flay.yml +0 -3
  15. data/config/flog.yml +0 -2
  16. data/config/mutant.yml +0 -6
  17. data/config/reek.yml +0 -98
  18. data/config/yardstick.yml +0 -2
  19. data/spec/integration/unparser/corpus_spec.rb +0 -125
  20. data/spec/integrations.yml +0 -92
  21. data/spec/spec_helper.rb +0 -42
  22. data/spec/unit/unparser/buffer/append_spec.rb +0 -24
  23. data/spec/unit/unparser/buffer/append_without_prefix_spec.rb +0 -23
  24. data/spec/unit/unparser/buffer/capture_content_spec.rb +0 -17
  25. data/spec/unit/unparser/buffer/content_spec.rb +0 -38
  26. data/spec/unit/unparser/buffer/fresh_line_spec.rb +0 -20
  27. data/spec/unit/unparser/buffer/indent_spec.rb +0 -20
  28. data/spec/unit/unparser/buffer/nl_spec.rb +0 -16
  29. data/spec/unit/unparser/buffer/unindent_spec.rb +0 -20
  30. data/spec/unit/unparser/color_spec.rb +0 -40
  31. data/spec/unit/unparser/comments/consume_spec.rb +0 -22
  32. data/spec/unit/unparser/comments/take_all_spec.rb +0 -19
  33. data/spec/unit/unparser/comments/take_before_spec.rb +0 -46
  34. data/spec/unit/unparser/comments/take_eol_comments_spec.rb +0 -32
  35. data/spec/unit/unparser/diff_spec.rb +0 -189
  36. data/spec/unit/unparser/emitter/class_methods/handle_spec.rb +0 -17
  37. data/spec/unit/unparser/validation_spec.rb +0 -327
  38. data/spec/unit/unparser_spec.rb +0 -1920
  39. data/unparser.gemspec +0 -35
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Unparser::Emitter, '.handle', mutant_expression: 'Unparser::Emitter*' do
4
- subject { class_under_test.class_eval { handle :foo } }
5
-
6
- let(:class_under_test) do
7
- Class.new(described_class)
8
- end
9
-
10
- before do
11
- stub_const('Unparser::Emitter::REGISTRY', {})
12
- end
13
-
14
- it 'should register emitter' do
15
- expect { subject }.to change { Unparser::Emitter::REGISTRY }.from({}).to(foo: class_under_test)
16
- end
17
- end
@@ -1,327 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Unparser::Validation do
4
- let(:object) do
5
- described_class.new(
6
- identification: identification,
7
- generated_node: generated_node,
8
- generated_source: generated_source,
9
- original_node: original_node,
10
- original_source: original_source
11
- )
12
- end
13
-
14
- def right(value)
15
- MPrelude::Either::Right.new(value)
16
- end
17
-
18
- def left(value)
19
- MPrelude::Either::Left.new(value)
20
- end
21
-
22
- let(:generated_node) { right(s(:send, s(:int, 1), :foo)) }
23
- let(:generated_source) { right('1.foo') }
24
- let(:identification) { 'example-identification' }
25
- let(:original_node) { right(s(:send, s(:int, 1), :foo)) }
26
- let(:original_source) { right('1.foo') }
27
-
28
- let(:exception) do
29
- left(
30
- instance_double(
31
- RuntimeError,
32
- message: 'foo',
33
- backtrace: Array.new(21, &'line-%02d'.method(:%))
34
- )
35
- )
36
- end
37
-
38
- let(:exception_report) do
39
- <<~'REPORT'.strip
40
- #<InstanceDouble(RuntimeError) (anonymous)>
41
- line-00
42
- line-01
43
- line-02
44
- line-03
45
- line-04
46
- line-05
47
- line-06
48
- line-07
49
- line-08
50
- line-09
51
- line-10
52
- line-11
53
- line-12
54
- line-13
55
- line-14
56
- line-15
57
- line-16
58
- line-17
59
- line-18
60
- line-19
61
- REPORT
62
- end
63
-
64
- def report
65
- object.report
66
- end
67
-
68
- shared_examples 'not successful' do
69
- it 'is not successful' do
70
- expect(object.success?).to be(false)
71
- end
72
- end
73
-
74
- context 'on success' do
75
- it 'is successful' do
76
- expect(object.success?).to be(true)
77
- end
78
-
79
- it 'returns expected report' do
80
- expect(report).to eql(<<~'REPORT'.strip)
81
- example-identification
82
- Original-Source:
83
- 1.foo
84
- Generated-Source:
85
- 1.foo
86
- Original-Node:
87
- (send
88
- (int 1) :foo)
89
- Generated-Node:
90
- (send
91
- (int 1) :foo)
92
- REPORT
93
- end
94
- end
95
-
96
- context 'on failing to generate original source with exception' do
97
- let(:original_source) { exception }
98
-
99
- include_examples 'not successful'
100
-
101
- it 'returns expected report' do
102
- expect(report).to eql(<<~REPORT.strip)
103
- example-identification
104
- Original-Source:
105
- #{exception_report}
106
- Generated-Source:
107
- 1.foo
108
- Original-Node:
109
- (send
110
- (int 1) :foo)
111
- Generated-Node:
112
- (send
113
- (int 1) :foo)
114
- REPORT
115
- end
116
- end
117
-
118
- context 'on failing to parse generated source due precondition error' do
119
- let(:generated_node) { left(nil) }
120
-
121
- include_examples 'not successful'
122
-
123
- it 'returns expected report' do
124
- expect(report).to eql(<<~REPORT.strip)
125
- example-identification
126
- Original-Source:
127
- 1.foo
128
- Generated-Source:
129
- 1.foo
130
- Original-Node:
131
- (send
132
- (int 1) :foo)
133
- Generated-Node:
134
- undefined
135
- REPORT
136
- end
137
- end
138
-
139
- context 'on failing to parse original source' do
140
- let(:original_node) { exception }
141
-
142
- include_examples 'not successful'
143
-
144
- it 'returns expected report' do
145
- expect(report).to eql(<<~REPORT.strip)
146
- example-identification
147
- Original-Source:
148
- 1.foo
149
- Generated-Source:
150
- 1.foo
151
- Original-Node:
152
- #{exception_report}
153
- Generated-Node:
154
- (send
155
- (int 1) :foo)
156
- REPORT
157
- end
158
- end
159
-
160
- context 'on failing to generate generated source' do
161
- let(:generated_source) { exception }
162
-
163
- include_examples 'not successful'
164
-
165
- it 'returns expected report' do
166
- expect(report).to eql(<<~REPORT.strip)
167
- example-identification
168
- Original-Source:
169
- 1.foo
170
- Generated-Source:
171
- #{exception_report}
172
- Original-Node:
173
- (send
174
- (int 1) :foo)
175
- Generated-Node:
176
- (send
177
- (int 1) :foo)
178
- REPORT
179
- end
180
- end
181
-
182
- context 'on failing to parse generated source' do
183
- let(:generated_node) { exception }
184
-
185
- include_examples 'not successful'
186
-
187
- it 'returns expected report' do
188
- expect(report).to eql(<<~REPORT.strip)
189
- example-identification
190
- Original-Source:
191
- 1.foo
192
- Generated-Source:
193
- 1.foo
194
- Original-Node:
195
- (send
196
- (int 1) :foo)
197
- Generated-Node:
198
- #{exception_report}
199
- REPORT
200
- end
201
- end
202
-
203
- context 'on generating different node' do
204
- let(:generated_node) { right(s(:send, s(:int, 1), :bar)) }
205
-
206
- include_examples 'not successful'
207
-
208
- it 'returns expected report' do
209
- diff = [
210
- Unparser::Color::NONE.format(" (send\n"),
211
- Unparser::Color::RED.format("- (int 1) :foo)\n"),
212
- Unparser::Color::GREEN.format("+ (int 1) :bar)\n")
213
- ]
214
-
215
- expect(report).to eql(<<~'REPORT' + diff.join)
216
- example-identification
217
- Original-Source:
218
- 1.foo
219
- Generated-Source:
220
- 1.foo
221
- Original-Node:
222
- (send
223
- (int 1) :foo)
224
- Generated-Node:
225
- (send
226
- (int 1) :bar)
227
- Node-Diff:
228
- @@ -1,3 +1,3 @@
229
- REPORT
230
- end
231
- end
232
-
233
- describe '.from_path' do
234
- def apply
235
- described_class.from_path(path)
236
- end
237
-
238
- let(:path) { instance_double(Pathname, read: source, to_s: '/some/file') }
239
- let(:source) { 'true' }
240
-
241
- it 'returns expected validator' do
242
- expect(apply).to eql(
243
- described_class.new(
244
- generated_node: right(s(:true)),
245
- generated_source: right(source),
246
- identification: '/some/file',
247
- original_node: right(s(:true)),
248
- original_source: right(source)
249
- )
250
- )
251
- end
252
- end
253
-
254
- describe '.from_string' do
255
- def apply
256
- described_class.from_string(source)
257
- end
258
-
259
- let(:attributes) do
260
- {
261
- generated_node: right(s(:true)),
262
- generated_source: right(source),
263
- identification: '(string)',
264
- original_node: right(s(:true)),
265
- original_source: right(source)
266
- }
267
- end
268
-
269
- context 'on valid original source' do
270
- let(:source) { 'true' }
271
-
272
- it 'returns expected validator' do
273
- expect(apply).to eql(described_class.new(attributes))
274
- end
275
-
276
- context 'with unparsing error' do
277
- let(:exception) { RuntimeError.new('example-error') }
278
-
279
- before do
280
- allow(Unparser).to receive(:unparse).and_raise(exception)
281
- end
282
-
283
- it 'returns expected validator' do
284
- validator = apply
285
-
286
- expect(validator.generated_node).to eql(left(nil))
287
- expect(validator.generated_source.from_left.class).to be(RuntimeError)
288
- expect(validator.original_source).to eql(right(source))
289
- expect(validator.original_node).to eql(right(s(:true)))
290
- end
291
- end
292
- end
293
-
294
- # These are actually specifying the wrong behavior as the normalization is a conceptual mistake
295
- # But for now we specify them to get this PR through.
296
- #
297
- # Removal in followup.
298
- context 'on denormalized valid original source' do
299
- let(:source) { '(true)' }
300
-
301
- it 'returns expected validator' do
302
- expect(apply).to eql(described_class.new(attributes.merge(generated_source: right('true'))))
303
- end
304
- end
305
-
306
- context 'on very denormalized valid original source' do
307
- let(:source) { '((true))' }
308
-
309
- it 'returns expected validator' do
310
- expect(apply).to eql(described_class.new(attributes.merge(generated_source: right('true'))))
311
- end
312
- end
313
-
314
- context 'on invalid original source' do
315
- let(:source) { '(' }
316
-
317
- it 'returns expected validator' do
318
- validator = apply
319
-
320
- expect(validator.generated_node).to eql(left(nil))
321
- expect(validator.generated_source).to eql(left(nil))
322
- expect(validator.original_source).to eql(right(source))
323
- expect(validator.original_node.from_left.class).to be(Parser::SyntaxError)
324
- end
325
- end
326
- end
327
- end
@@ -1,1920 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Unparser, mutant_expression: 'Unparser::Emitter*' do
4
- describe '.buffer' do
5
- let(:source) { 'a + b' }
6
-
7
- def apply
8
- described_class.buffer(source)
9
- end
10
-
11
- it 'returns parser buffer with expected name' do
12
- expect(apply.name).to eql('(string)')
13
- end
14
-
15
- it 'returns parser buffer with pre-filled source' do
16
- expect(apply.source).to eql(source)
17
- end
18
- end
19
-
20
- describe '.parser' do
21
- let(:invalid_source_buffer) { Unparser.buffer('a +') }
22
-
23
- def apply
24
- described_class.parser
25
- end
26
-
27
- context 'failure' do
28
- def apply
29
- super.tap do |parser|
30
- parser.diagnostics.consumer = ->(_) {}
31
- end
32
- end
33
-
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(source)
61
- end
62
-
63
- context 'on present source' do
64
- let(:source) { 'self[1]=2' }
65
-
66
- it 'returns expected AST' do
67
- expect(apply).to eql(s(:indexasgn, s(:self), s(:int, 1), s(:int, 2)))
68
- end
69
- end
70
-
71
- context 'on empty source' do
72
- let(:source) { '' }
73
-
74
- it 'returns ni' do
75
- expect(apply).to be(nil)
76
- end
77
- end
78
-
79
- context 'on syntax error' do
80
- let(:source) { '[' }
81
-
82
- it 'raises error' do
83
- expect { apply }.to raise_error(Parser::SyntaxError)
84
- end
85
- end
86
- end
87
-
88
- describe '.parse_either' do
89
- def apply
90
- described_class.parse_either(source)
91
- end
92
-
93
- context 'on present source' do
94
- let(:source) { 'self[1]=2' }
95
-
96
- it 'returns right value with expected AST' do
97
- expect(apply).to eql(MPrelude::Either::Right.new(s(:indexasgn, s(:self), s(:int, 1), s(:int, 2))))
98
- end
99
- end
100
-
101
- context 'on empty source' do
102
- let(:source) { '' }
103
-
104
- it 'returns right value with nil' do
105
- expect(apply).to eql(MPrelude::Either::Right.new(nil))
106
- end
107
- end
108
-
109
- context 'on syntax error' do
110
- let(:source) { '[' }
111
-
112
- it 'returns left value with syntax error' do
113
- result = apply
114
-
115
- # Syntax errors that compare nicely under #eql? are hard to construct
116
- expect(result).to be_instance_of(MPrelude::Either::Left)
117
- expect(result.from_left).to be_instance_of(Parser::SyntaxError)
118
- end
119
- end
120
- end
121
-
122
- describe '.unparse' do
123
- context 'on unknown node type' do
124
- def apply
125
- Unparser.unparse(node)
126
- end
127
-
128
- let(:node) { s(:example_node) }
129
-
130
- it 'raises UnknownNodeError' do
131
- expect { apply }.to raise_error(
132
- Unparser::UnknownNodeError,
133
- 'Unknown node type: :example_node'
134
- )
135
- end
136
- end
137
- end
138
-
139
- describe '.unparse' do
140
- let(:builder_options) { {} }
141
-
142
- def parser
143
- Unparser.parser.tap do |parser|
144
- builder_options.each do |name, value|
145
- parser.builder.public_send(:"#{name}=", value)
146
- end
147
- end
148
- end
149
-
150
- def buffer(input)
151
- Unparser.buffer(input)
152
- end
153
-
154
- def parse_with_comments(string)
155
- parser.parse_with_comments(buffer(string))
156
- end
157
-
158
- def self.with_builder_options(options, &block)
159
- context "with #{options}" do
160
- let(:builder_options) { options }
161
-
162
- class_eval(&block)
163
- end
164
- end
165
-
166
- def assert_round_trip(string, parser)
167
- ast, comments = parse_with_comments(string)
168
- generated = Unparser.unparse(ast, comments)
169
- expect(generated).to eql(string.chomp)
170
- generated_ast, _comments = parse_with_comments(generated)
171
- expect(ast == generated_ast).to be(true)
172
- end
173
-
174
- def assert_generates_from_string(parser, string, expected)
175
- ast_with_comments = parse_with_comments(string)
176
- assert_generates_from_ast(parser, ast_with_comments, expected.chomp)
177
- end
178
-
179
- def assert_generates_from_ast(parser, ast_with_comments, expected)
180
- generated = Unparser.unparse(*ast_with_comments)
181
- expect(generated).to eql(expected)
182
- ast, comments = parse_with_comments(generated)
183
- expect(Unparser.unparse(ast, comments)).to eql(expected)
184
- end
185
-
186
- def self.assert_unterminated(expression)
187
- assert_source(expression)
188
- assert_source("(#{expression}).foo")
189
- end
190
-
191
- def self.assert_terminated(expression)
192
- assert_source(expression)
193
- assert_source("foo(#{expression})")
194
- assert_source("#{expression}.foo")
195
- end
196
-
197
- def self.assert_generates(input, expected)
198
- it "should generate #{input} as #{expected}" do
199
- if input.is_a?(String)
200
- assert_generates_from_string(parser, input, expected)
201
- else
202
- assert_generates_from_ast(parser, [input, []], expected)
203
- end
204
- end
205
- end
206
-
207
- def self.assert_round_trip(input)
208
- it "should round trip #{input}" do
209
- assert_round_trip(input, parser)
210
- end
211
- end
212
-
213
- def self.assert_source(input)
214
- assert_round_trip(input)
215
- end
216
-
217
- context 'kwargs' do
218
- assert_source <<~RUBY
219
- def foo(bar:, baz:)
220
- end
221
- RUBY
222
-
223
- assert_source <<~RUBY
224
- foo(**bar)
225
- RUBY
226
-
227
- assert_source <<~RUBY
228
- def foo(bar:, baz: "value")
229
- end
230
- RUBY
231
- end
232
-
233
- context 'literal' do
234
- context 'int' do
235
- assert_generates '-0', '0'
236
- assert_source '++1'
237
- assert_terminated '1'
238
- assert_unterminated '-1'
239
- assert_generates '0x1', '1'
240
- assert_generates '1_000', '1000'
241
- assert_generates '1e10', '10000000000.0'
242
- assert_generates '10e10000000000', 'Float::INFINITY'
243
- assert_generates '-10e10000000000', '-Float::INFINITY'
244
- end
245
-
246
- context 'rational' do
247
- assert_terminated '1r'
248
- assert_generates '1.0r', '1r'
249
- assert_generates '-0r', '0r'
250
-
251
- assert_terminated '1.5r'
252
- assert_terminated '1.3r'
253
- end
254
-
255
- context 'complex' do
256
- %w(
257
- 5i
258
- -5i
259
- 0.6i
260
- -0.6i
261
- 1000000000000000000000000000000i
262
- 1ri
263
- ).each do |expression|
264
- assert_terminated(expression)
265
- end
266
- end
267
-
268
- context 'string' do
269
- assert_generates '?c', '"c"'
270
- assert_generates '"foo" "bar"', '"#{"foo"}#{"bar"}"'
271
- assert_generates '"foo" "bar #{baz}"', '"#{"foo"}#{"#{"bar "}#{baz}"}"'
272
- assert_generates '%Q(foo"#{@bar})', '"#{"foo\\""}#{@bar}"'
273
- assert_generates '"foo#{1}bar"', '"#{"foo"}#{1}#{"bar"}"'
274
- assert_generates '"\\\\#{}"', '"#{"\\\\"}#{}"'
275
- assert_generates '"#{}\#{}"', '"#{}#{"\#{}"}"'
276
- assert_generates '"\#{}#{}"', '"#{"\#{}"}#{}"'
277
- assert_terminated '"\""'
278
- assert_terminated '"foo bar"'
279
- assert_terminated '"foo\nbar"'
280
- # Within indentation
281
- assert_generates <<~'RUBY', <<~'RUBY'
282
- if foo
283
- "
284
- #{foo}
285
- "
286
- end
287
- RUBY
288
- if foo
289
- "#{"\n"}#{" "}#{foo}#{"\n"}#{" "}"
290
- end
291
- RUBY
292
- end
293
-
294
- context 'execute string' do
295
- assert_generates '`foo`', '`#{"foo"}`'
296
- assert_generates '`foo#{@bar}`', '`#{"foo"}#{@bar}`'
297
- assert_generates '%x(\))', '`#{")"}`'
298
- assert_generates '%x(`)', '`#{"`"}`'
299
- assert_generates '`"`', '`#{"\\""}`'
300
- end
301
-
302
- context 'symbol' do
303
- assert_generates s(:sym, :foo), ':foo'
304
- assert_generates s(:sym, :"A B"), ':"A B"'
305
- assert_terminated ':foo'
306
- assert_terminated ':"A B"'
307
- assert_terminated ':"A\"B"'
308
- assert_terminated ':""'
309
- end
310
-
311
- context 'regexp' do
312
- assert_terminated '/foo/'
313
- assert_terminated %q(/[^-+',.\/:@[:alnum:]\[\]]+/)
314
- assert_terminated '/foo#{@bar}/'
315
- assert_terminated '/foo#{@bar}/imx'
316
- assert_terminated '/#{"\u0000"}/'
317
- assert_terminated "/\n/"
318
- assert_terminated '/\n/'
319
- assert_terminated "/\n/x"
320
- # Within indentation
321
- assert_source <<~RUBY
322
- if foo
323
- /
324
- /
325
- end
326
- RUBY
327
- assert_generates '%r(/)', '/\//'
328
- assert_generates '%r(\))', '/\)/'
329
- assert_generates '%r(#{@bar}baz)', '/#{@bar}baz/'
330
- assert_terminated '/\/\//x'
331
- end
332
-
333
- context 'dynamic symbol' do
334
- assert_generates ':"foo#{bar}baz"', ':"#{"foo"}#{bar}#{"baz"}"'
335
- assert_source ':"#{"foo"}"'
336
- end
337
-
338
- context 'irange' do
339
- assert_unterminated '1..'
340
- assert_unterminated '1..2'
341
- assert_unterminated '(0.0 / 0.0)..1'
342
- assert_unterminated '1..(0.0 / 0.0)'
343
- assert_unterminated '(0.0 / 0.0)..100'
344
- end
345
-
346
- context 'erange' do
347
- assert_unterminated '1...'
348
- assert_unterminated '1...2'
349
- end
350
-
351
- context 'float' do
352
- assert_source '-0.1'
353
- assert_terminated '0.1'
354
- assert_terminated '0.1'
355
- assert_generates '10.2e10000000000', 'Float::INFINITY'
356
- assert_generates '-10.2e10000000000', '-Float::INFINITY'
357
- assert_generates s(:float, -0.1), '-0.1'
358
- assert_generates s(:float, 0.1), '0.1'
359
- end
360
-
361
- context 'array' do
362
- assert_terminated '[1, 2]'
363
- assert_terminated '[1, (), n2]'
364
- assert_terminated '[1]'
365
- assert_terminated '[]'
366
- assert_terminated '[1, *@foo]'
367
- assert_terminated '[*@foo, 1]'
368
- assert_terminated '[*@foo, *@baz]'
369
- assert_generates '%w(foo bar)', '["foo", "bar"]'
370
- end
371
-
372
- context 'hash' do
373
- assert_terminated '{}'
374
- assert_source '{ () => () }'
375
- assert_source '{ 1 => 2 }'
376
- assert_source '{ 1 => 2, 3 => 4 }'
377
-
378
- # special case for 2.1.3
379
- assert_source "{ foo: (if true\nend) }"
380
-
381
- context 'with symbol keys' do
382
- assert_source '{ a: (1 rescue foo), b: 2 }'
383
- assert_source '{ a: 1, b: 2 }'
384
- assert_source '{ a: :a }'
385
- assert_source '{ :"a b" => 1 }'
386
- assert_source '{ :-@ => 1 }'
387
- end
388
- end
389
- end
390
-
391
- context 'access' do
392
- %w(@a @@a $a $1 $` CONST SCOPED::CONST ::TOPLEVEL ::TOPLEVEL::CONST).each do |expression|
393
- assert_terminated(expression)
394
- end
395
- end
396
-
397
- context 'control keywords' do
398
- %w(retry redo).each do |expression|
399
- assert_terminated(expression)
400
- end
401
- end
402
-
403
- context 'singletons' do
404
- %w(self true false nil).each do |expression|
405
- assert_terminated(expression)
406
- end
407
- end
408
-
409
- context 'magic keywords' do
410
- assert_source '__ENCODING__'
411
-
412
- # These two assertions don't actually need to be wrapped in this block since `true` is the default,
413
- # but it is helpful to contrast with the assertions farther down.
414
- with_builder_options(emit_file_line_as_literals: true) do
415
- assert_generates '__FILE__', '"(string)"'
416
- assert_generates '__LINE__', '1'
417
- end
418
-
419
- with_builder_options(emit_file_line_as_literals: false) do
420
- assert_source '__FILE__'
421
- assert_source '__LINE__'
422
- end
423
- end
424
-
425
- context 'assignment' do
426
- context 'single' do
427
- assert_unterminated 'a = 1'
428
- assert_unterminated '@a = 1'
429
- assert_unterminated '@@a = 1'
430
- assert_unterminated '$a = 1'
431
- assert_unterminated 'CONST = 1'
432
- assert_unterminated 'Name::Spaced::CONST = 1'
433
- assert_unterminated '::Foo = ::Bar'
434
- end
435
-
436
- context 'lvar assigned from method with same name' do
437
- assert_unterminated 'foo = foo()'
438
- end
439
-
440
- context 'lvar introduction from condition' do
441
- assert_source 'foo = bar while foo'
442
- assert_source 'foo = bar until foo'
443
- assert_source <<~'RUBY'
444
- foo = exp
445
- while foo
446
- foo = bar
447
- end
448
- RUBY
449
-
450
- # Ugly I know. But its correct :D
451
- #
452
- # if foo { |pair| }
453
- # pair = :foo
454
- # foo
455
- # end
456
- assert_source <<~'RUBY'
457
- if foo do |pair|
458
- pair
459
- end
460
- pair = :foo
461
- foo
462
- end
463
- RUBY
464
-
465
- assert_source <<~'RUBY'
466
- while foo
467
- foo = bar
468
- end
469
- RUBY
470
-
471
- assert_source <<~'RUBY'
472
- each do |bar|
473
- while foo
474
- foo = bar
475
- end
476
- end
477
- RUBY
478
-
479
- assert_source <<~'RUBY'
480
- def foo
481
- foo = bar while foo != baz
482
- end
483
- RUBY
484
-
485
- assert_source <<~'RUBY'
486
- each do |baz|
487
- while foo
488
- foo = bar
489
- end
490
- end
491
- RUBY
492
-
493
- assert_source <<~'RUBY'
494
- each do |foo|
495
- while foo
496
- foo = bar
497
- end
498
- end
499
- RUBY
500
- end
501
-
502
- context 'multiple' do
503
- assert_source 'a, * = [1, 2]'
504
- assert_source 'a, *foo = [1, 2]'
505
- assert_source '*a = []'
506
- assert_source '*foo = [1, 2]'
507
- assert_source 'a, = foo'
508
- assert_unterminated 'a, b = [1, 2]'
509
- assert_unterminated '@a, @b = [1, 2]'
510
- assert_unterminated 'a.foo, a.bar = [1, 2]'
511
- assert_unterminated 'a[0], a[1] = [1, 2]'
512
- assert_unterminated 'a[*foo], a[1] = [1, 2]'
513
- assert_unterminated '@@a, @@b = [1, 2]'
514
- assert_unterminated '$a, $b = [1, 2]'
515
- assert_unterminated 'a, b = foo'
516
- assert_unterminated 'a, (b, c) = [1, [2, 3]]'
517
- assert_unterminated 'a = (b, c = 1)'
518
- assert_unterminated '(a,), b = 1'
519
- end
520
- end
521
-
522
- %w(next return break).each do |keyword|
523
-
524
- context keyword do
525
- assert_terminated keyword.to_s
526
- assert_unterminated "#{keyword} 1"
527
- assert_unterminated "#{keyword} 2, 3"
528
- assert_unterminated "#{keyword} *nil"
529
- assert_unterminated "#{keyword} *foo, bar"
530
-
531
- assert_generates <<~RUBY, <<~RUBY
532
- foo do |bar|
533
- bar =~ // || #{keyword}
534
- baz
535
- end
536
- RUBY
537
- foo do |bar|
538
- (bar =~ //) || #{keyword}
539
- baz
540
- end
541
- RUBY
542
-
543
- assert_generates <<~RUBY, <<~RUBY
544
- #{keyword}(a ? b : c)
545
- RUBY
546
- #{keyword} (if a
547
- b
548
- else
549
- c
550
- end)
551
- RUBY
552
- end
553
- end
554
-
555
- context 'conditional send (csend)' do
556
- assert_terminated 'a&.b'
557
- assert_terminated 'a&.b(c)'
558
- end
559
-
560
- context 'send' do
561
- assert_terminated 'foo'
562
- assert_terminated 'self.foo'
563
- assert_terminated 'a.foo'
564
- assert_terminated 'A.foo'
565
- assert_terminated 'foo[]'
566
- assert_terminated 'foo[1]'
567
- assert_terminated 'foo[*baz]'
568
- assert_terminated 'foo(1)'
569
- assert_terminated 'foo(bar)'
570
- assert_terminated 'foo(&block)'
571
- assert_terminated 'foo(&(foo || bar))'
572
- assert_terminated 'foo(*arguments)'
573
- assert_terminated 'foo(*arguments)'
574
-
575
- assert_source <<~'RUBY'
576
- foo do
577
- end
578
- RUBY
579
-
580
- assert_source <<~'RUBY'
581
- foo do |a|
582
- end
583
- RUBY
584
-
585
- assert_source <<~'RUBY'
586
- foo do |a, |
587
- end
588
- RUBY
589
-
590
- assert_source <<~'RUBY'
591
- foo do |a, b|
592
- end
593
- RUBY
594
-
595
- assert_source <<~'RUBY'
596
- foo(1) do
597
- nil
598
- end
599
- RUBY
600
-
601
- assert_source <<~'RUBY'
602
- foo do |a, *b|
603
- nil
604
- end
605
- RUBY
606
-
607
- assert_source <<~'RUBY'
608
- foo do |a, *|
609
- nil
610
- end
611
- RUBY
612
-
613
- assert_source <<~'RUBY'
614
- foo do
615
- bar
616
- end
617
- RUBY
618
-
619
- assert_source <<~'RUBY'
620
- foo.bar(*args)
621
- RUBY
622
-
623
- assert_source <<~'RUBY'
624
- foo.bar do |(a, b), c|
625
- d
626
- end
627
- RUBY
628
-
629
- assert_source <<~'RUBY'
630
- foo.bar do |*a; b|
631
- end
632
- RUBY
633
-
634
- assert_source <<~'RUBY'
635
- foo.bar do |a; b|
636
- end
637
- RUBY
638
-
639
- assert_source <<~'RUBY'
640
- foo.bar do |; a, b|
641
- end
642
- RUBY
643
-
644
- assert_source <<~'RUBY'
645
- foo.bar do |*|
646
- d
647
- end
648
- RUBY
649
-
650
- assert_source <<~'RUBY'
651
- foo.bar do |(*)|
652
- d
653
- end
654
- RUBY
655
-
656
- assert_source <<~'RUBY'
657
- foo.bar do |((*))|
658
- d
659
- end
660
- RUBY
661
-
662
- assert_source <<~'RUBY'
663
- foo.bar do |(a, (*))|
664
- d
665
- end
666
- RUBY
667
-
668
- assert_source <<~'RUBY'
669
- foo.bar do |(a, b)|
670
- d
671
- end
672
- RUBY
673
-
674
- assert_source <<~'RUBY'
675
- foo.bar do
676
- end.baz
677
- RUBY
678
-
679
- assert_source <<~'RUBY'
680
- FOO()
681
- RUBY
682
-
683
- assert_terminated '(1..2).max'
684
- assert_terminated '1..2.max'
685
- assert_unterminated 'a || return'
686
- assert_unterminated 'foo << (bar * baz)'
687
-
688
- assert_source <<~'RUBY'
689
- foo ||= (a, _ = b)
690
- RUBY
691
-
692
- assert_source <<~'RUBY'
693
- begin
694
- rescue
695
- end.bar
696
- RUBY
697
-
698
- assert_source <<~'RUBY'
699
- case (def foo
700
- end
701
- :bar)
702
- when bar
703
- end.baz
704
- RUBY
705
-
706
- assert_source <<~'RUBY'
707
- case foo
708
- when bar
709
- end.baz
710
- RUBY
711
-
712
- assert_source <<~'RUBY'
713
- class << self
714
- end.bar
715
- RUBY
716
-
717
- assert_source <<~'RUBY'
718
- def self.foo
719
- end.bar
720
- RUBY
721
-
722
- assert_source <<~'RUBY'
723
- def foo
724
- end.bar
725
- RUBY
726
-
727
- assert_source <<~'RUBY'
728
- until foo
729
- end.bar
730
- RUBY
731
-
732
- assert_source <<~'RUBY'
733
- while foo
734
- end.bar
735
- RUBY
736
-
737
- assert_source <<~'RUBY'
738
- loop do
739
- end.bar
740
- RUBY
741
-
742
- assert_source <<~'RUBY'
743
- class Foo
744
- end.bar
745
- RUBY
746
-
747
- assert_source <<~'RUBY'
748
- module Foo
749
- end.bar
750
- RUBY
751
-
752
- assert_source <<~'RUBY'
753
- if foo
754
- end.baz
755
- RUBY
756
-
757
- assert_source <<~'RUBY'
758
- local = 1
759
- local.bar
760
- RUBY
761
-
762
- assert_terminated 'foo.bar(*args)'
763
- assert_terminated 'foo.bar(*arga, foo, *argb)'
764
- assert_terminated 'foo.bar(*args, foo)'
765
- assert_terminated 'foo.bar(foo, *args)'
766
- assert_terminated 'foo.bar(foo, *args, &block)'
767
- assert_source <<~'RUBY'
768
- foo(bar, *args)
769
- RUBY
770
-
771
- assert_terminated 'foo(*args, &block)'
772
- assert_terminated 'foo.bar(&baz)'
773
- assert_terminated 'foo.bar(:baz, &baz)'
774
- assert_terminated 'foo.bar=:baz'
775
-
776
- assert_unterminated 'self.foo=:bar'
777
-
778
- assert_terminated 'foo.bar(baz: boz)'
779
- assert_terminated 'foo.bar(foo, "baz" => boz)'
780
- assert_terminated 'foo.bar({ foo: boz }, boz)'
781
- assert_terminated 'foo.bar(foo, {})'
782
- end
783
-
784
- context 'begin; end' do
785
- assert_generates s(:begin), ''
786
-
787
- assert_source <<~'RUBY'
788
- begin
789
- end
790
- RUBY
791
-
792
- assert_source <<~'RUBY'
793
- foo
794
- bar
795
- RUBY
796
-
797
- assert_source <<~'RUBY'
798
- begin
799
- foo
800
- bar
801
- end.blah
802
- RUBY
803
- end
804
-
805
- context 'begin / rescue / ensure' do
806
- assert_source <<~'RUBY'
807
- begin
808
- foo
809
- ensure
810
- bar
811
- baz
812
- end
813
- RUBY
814
-
815
- assert_source <<~'RUBY'
816
- begin
817
- foo
818
- rescue
819
- baz
820
- end
821
- RUBY
822
-
823
- assert_source <<~'RUBY'
824
- begin
825
- begin
826
- foo
827
- bar
828
- rescue
829
- end
830
- rescue
831
- baz
832
- bar
833
- end
834
- RUBY
835
-
836
- assert_source <<~'RUBY'
837
- begin
838
- raise(Exception) rescue foo = bar
839
- rescue Exception
840
- end
841
- RUBY
842
-
843
- assert_source <<~'RUBY'
844
- begin
845
- foo
846
- bar
847
- rescue
848
- baz
849
- bar
850
- end
851
- RUBY
852
-
853
- assert_source <<~'RUBY'
854
- begin
855
- foo
856
- rescue Exception
857
- bar
858
- end
859
- RUBY
860
-
861
- assert_source <<~'RUBY'
862
- begin
863
- foo
864
- rescue => bar
865
- bar
866
- end
867
- RUBY
868
-
869
- assert_source <<~'RUBY'
870
- begin
871
- foo
872
- rescue Exception, Other => bar
873
- bar
874
- end
875
- RUBY
876
-
877
- assert_source <<~'RUBY'
878
- class << self
879
- undef :bar rescue nil
880
- end
881
- RUBY
882
-
883
- assert_source <<~'RUBY'
884
- module Foo
885
- undef :bar rescue nil
886
- end
887
- RUBY
888
-
889
- assert_source <<~'RUBY'
890
- class Foo
891
- undef :bar rescue nil
892
- end
893
- RUBY
894
-
895
- assert_source <<~'RUBY'
896
- begin
897
- rescue Exception => e
898
- end
899
- RUBY
900
-
901
- assert_source <<~'RUBY'
902
- begin
903
- ensure
904
- end
905
- RUBY
906
-
907
- assert_source <<~'RUBY'
908
- begin
909
- rescue
910
- ensure
911
- end
912
- RUBY
913
-
914
- assert_source <<~'RUBY'
915
- begin
916
- foo
917
- rescue Exception => bar
918
- bar
919
- end
920
- RUBY
921
-
922
- assert_source <<~'RUBY'
923
- begin
924
- bar
925
- rescue SomeError, *bar
926
- baz
927
- end
928
- RUBY
929
-
930
- assert_source <<~'RUBY'
931
- begin
932
- bar
933
- rescue SomeError, *bar => exception
934
- baz
935
- end
936
- RUBY
937
-
938
- assert_source <<~'RUBY'
939
- begin
940
- bar
941
- rescue *bar
942
- baz
943
- end
944
- RUBY
945
-
946
- assert_source <<~'RUBY'
947
- begin
948
- bar
949
- rescue LoadError
950
- end
951
- RUBY
952
-
953
- assert_source <<~'RUBY'
954
- begin
955
- bar
956
- rescue
957
- else
958
- baz
959
- end
960
- RUBY
961
-
962
- assert_source <<~'RUBY'
963
- begin
964
- bar
965
- rescue *bar => exception
966
- baz
967
- end
968
- RUBY
969
-
970
- assert_source <<~'RUBY'
971
- m do
972
- rescue Exception => e
973
- end
974
- RUBY
975
-
976
- assert_source <<~'RUBY'
977
- m do
978
- ensure
979
- end
980
- RUBY
981
-
982
- assert_source <<~'RUBY'
983
- m do
984
- rescue
985
- ensure
986
- end
987
- RUBY
988
-
989
- assert_source <<~'RUBY'
990
- m do
991
- foo
992
- rescue Exception => bar
993
- bar
994
- end
995
- RUBY
996
-
997
- assert_source <<~'RUBY'
998
- m do
999
- bar
1000
- rescue SomeError, *bar
1001
- baz
1002
- end
1003
- RUBY
1004
-
1005
- assert_source <<~'RUBY'
1006
- m do
1007
- bar
1008
- rescue SomeError, *bar => exception
1009
- baz
1010
- end
1011
- RUBY
1012
-
1013
- assert_source <<~'RUBY'
1014
- m do
1015
- bar
1016
- rescue *bar
1017
- baz
1018
- end
1019
- RUBY
1020
-
1021
- assert_source <<~'RUBY'
1022
- m do
1023
- bar
1024
- rescue LoadError
1025
- end
1026
- RUBY
1027
-
1028
- assert_source <<~'RUBY'
1029
- m do
1030
- bar
1031
- rescue
1032
- else
1033
- baz
1034
- end
1035
- RUBY
1036
-
1037
- assert_source <<~'RUBY'
1038
- m do
1039
- bar
1040
- rescue *bar => exception
1041
- baz
1042
- end
1043
- RUBY
1044
-
1045
- assert_source 'foo rescue bar'
1046
- assert_source 'foo rescue return bar'
1047
- assert_source 'x = (foo rescue return bar)'
1048
-
1049
- %w(while until if).each do |keyword|
1050
- assert_source <<~RUBY
1051
- #{keyword} (
1052
- foo rescue false
1053
- )
1054
- end
1055
- RUBY
1056
-
1057
- assert_generates <<~RUBY, <<~GENERATED
1058
- foo rescue false #{keyword} true
1059
- RUBY
1060
- #{keyword} true
1061
- foo rescue false
1062
- end
1063
- GENERATED
1064
- end
1065
-
1066
- assert_generates <<~'RUBY', <<~GENERATED
1067
- case (foo rescue false)
1068
- when true
1069
- end
1070
- RUBY
1071
- case (
1072
- foo rescue false
1073
- )
1074
- when true
1075
- end
1076
- GENERATED
1077
- end
1078
-
1079
- context 'super' do
1080
- assert_source 'super'
1081
-
1082
- assert_source 'super()'
1083
- assert_source 'super(a)'
1084
- assert_source 'super(a, b)'
1085
- assert_source 'super(&block)'
1086
- assert_source 'super(a, &block)'
1087
-
1088
- assert_source <<~'RUBY'
1089
- super(a do
1090
- foo
1091
- end)
1092
- RUBY
1093
-
1094
- assert_source <<~'RUBY'
1095
- super do
1096
- foo
1097
- end
1098
- RUBY
1099
-
1100
- assert_source <<~'RUBY'
1101
- super(a) do
1102
- foo
1103
- end
1104
- RUBY
1105
-
1106
- assert_source <<~'RUBY'
1107
- super() do
1108
- foo
1109
- end
1110
- RUBY
1111
-
1112
- assert_source <<~'RUBY'
1113
- super(a, b) do
1114
- foo
1115
- end
1116
- RUBY
1117
-
1118
- end
1119
-
1120
- context 'undef' do
1121
- assert_source 'undef :foo'
1122
- assert_source 'undef :foo, :bar'
1123
- end
1124
-
1125
- context 'BEGIN' do
1126
- assert_source <<~'RUBY'
1127
- BEGIN {
1128
- foo
1129
- }
1130
- RUBY
1131
- end
1132
-
1133
- context 'END' do
1134
- assert_source <<~'RUBY'
1135
- END {
1136
- foo
1137
- }
1138
- RUBY
1139
- end
1140
-
1141
- context 'alias' do
1142
- assert_source <<~'RUBY'
1143
- alias $foo $bar
1144
- RUBY
1145
-
1146
- assert_source <<~'RUBY'
1147
- alias :foo :bar
1148
- RUBY
1149
- end
1150
-
1151
- context 'yield' do
1152
- context 'without arguments' do
1153
- assert_source 'yield'
1154
- end
1155
-
1156
- context 'with argument' do
1157
- assert_source 'yield(a)'
1158
- end
1159
-
1160
- context 'with arguments' do
1161
- assert_source 'yield(a, b)'
1162
- end
1163
- end
1164
-
1165
- context 'if statement' do
1166
- assert_source <<~'RUBY'
1167
- if /foo/
1168
- bar
1169
- end
1170
- RUBY
1171
-
1172
- assert_source <<~'RUBY'
1173
- if 3
1174
- 9
1175
- end
1176
- RUBY
1177
-
1178
- assert_source <<~'RUBY'
1179
- if 4
1180
- 5
1181
- else
1182
- 6
1183
- end
1184
- RUBY
1185
-
1186
- assert_source <<~'RUBY'
1187
- unless 3
1188
- nil
1189
- end
1190
- RUBY
1191
-
1192
- assert_source <<~'RUBY'
1193
- unless 3
1194
- 9
1195
- end
1196
- RUBY
1197
-
1198
- assert_source <<~'RUBY'
1199
- if foo
1200
- end
1201
- RUBY
1202
-
1203
- assert_source <<~'RUBY'
1204
- foo = bar if foo
1205
- RUBY
1206
-
1207
- assert_source <<~'RUBY'
1208
- foo = bar unless foo
1209
- RUBY
1210
-
1211
- assert_source <<~'RUBY'
1212
- def foo(*foo)
1213
- unless foo
1214
- foo = bar
1215
- end
1216
- end
1217
- RUBY
1218
-
1219
- assert_source <<~'RUBY'
1220
- each do |foo|
1221
- unless foo
1222
- foo = bar
1223
- end
1224
- end
1225
- RUBY
1226
- end
1227
-
1228
- context 'def' do
1229
- context 'on instance' do
1230
-
1231
- assert_source <<~'RUBY'
1232
- def foo
1233
- end
1234
- RUBY
1235
-
1236
- assert_source <<~'RUBY'
1237
- def foo
1238
- bar
1239
- end
1240
- RUBY
1241
-
1242
- assert_source <<~'RUBY'
1243
- def foo
1244
- foo
1245
- rescue
1246
- bar
1247
- ensure
1248
- baz
1249
- end
1250
- RUBY
1251
-
1252
- assert_source <<~'RUBY'
1253
- begin
1254
- foo
1255
- ensure
1256
- bar rescue nil
1257
- end
1258
- RUBY
1259
-
1260
- assert_source <<~'RUBY'
1261
- def foo
1262
- bar
1263
- ensure
1264
- baz
1265
- end
1266
- RUBY
1267
-
1268
- assert_source <<~'RUBY'
1269
- def self.foo
1270
- bar
1271
- rescue
1272
- baz
1273
- end
1274
- RUBY
1275
-
1276
- assert_source <<~'RUBY'
1277
- def foo
1278
- bar
1279
- rescue
1280
- baz
1281
- end
1282
- RUBY
1283
-
1284
- assert_source <<~'RUBY'
1285
- def foo(bar)
1286
- bar
1287
- end
1288
- RUBY
1289
-
1290
- assert_source <<~'RUBY'
1291
- def foo(bar, baz)
1292
- bar
1293
- end
1294
- RUBY
1295
-
1296
- assert_source <<~'RUBY'
1297
- def foo(bar = ())
1298
- bar
1299
- end
1300
- RUBY
1301
-
1302
- assert_source <<~'RUBY'
1303
- def foo(bar = (baz
1304
- nil))
1305
- end
1306
- RUBY
1307
-
1308
- assert_source <<~'RUBY'
1309
- def foo(bar = true)
1310
- bar
1311
- end
1312
- RUBY
1313
-
1314
- assert_source <<~'RUBY'
1315
- def foo(bar, baz = true)
1316
- bar
1317
- end
1318
- RUBY
1319
-
1320
- assert_source <<~'RUBY'
1321
- def foo(bar: 1)
1322
- end
1323
- RUBY
1324
-
1325
- assert_source <<~'RUBY'
1326
- def foo(bar: bar)
1327
- end
1328
- RUBY
1329
-
1330
- assert_source <<~'RUBY'
1331
- def foo(bar: bar())
1332
- end
1333
- RUBY
1334
-
1335
- assert_source <<~'RUBY'
1336
- def foo(*)
1337
- bar
1338
- end
1339
- RUBY
1340
-
1341
- assert_source <<~'RUBY'
1342
- def foo(*bar)
1343
- bar
1344
- end
1345
- RUBY
1346
-
1347
- assert_source <<~'RUBY'
1348
- def foo(bar, *baz)
1349
- bar
1350
- end
1351
- RUBY
1352
-
1353
- assert_source <<~'RUBY'
1354
- def foo(baz = true, *bor)
1355
- bar
1356
- end
1357
- RUBY
1358
-
1359
- assert_source <<~'RUBY'
1360
- def foo(baz = true, *bor, &block)
1361
- bar
1362
- end
1363
- RUBY
1364
-
1365
- assert_source <<~'RUBY'
1366
- def foo(bar, baz = true, *bor)
1367
- bar
1368
- end
1369
- RUBY
1370
-
1371
- assert_source <<~'RUBY'
1372
- def foo(&block)
1373
- bar
1374
- end
1375
- RUBY
1376
-
1377
- assert_source <<~'RUBY'
1378
- def foo(bar, &block)
1379
- bar
1380
- end
1381
- RUBY
1382
-
1383
- assert_source <<~'RUBY'
1384
- def foo
1385
- bar
1386
- baz
1387
- end
1388
- RUBY
1389
-
1390
- assert_source <<~'RUBY'
1391
- def (foo do |bar|
1392
- end).bar
1393
- bar
1394
- end
1395
- RUBY
1396
-
1397
- assert_source <<~'RUBY'
1398
- def (foo(1)).bar
1399
- bar
1400
- end
1401
- RUBY
1402
-
1403
- assert_source <<~'RUBY'
1404
- def (Foo::Bar.baz).bar
1405
- baz
1406
- end
1407
- RUBY
1408
-
1409
- assert_source <<~'RUBY'
1410
- def (Foo::Bar).bar
1411
- baz
1412
- end
1413
- RUBY
1414
-
1415
- assert_source <<~'RUBY'
1416
- def Foo.bar
1417
- baz
1418
- end
1419
- RUBY
1420
-
1421
- assert_source <<~'RUBY'
1422
- def foo.bar
1423
- baz
1424
- end
1425
- RUBY
1426
- end
1427
-
1428
- context 'on singleton' do
1429
- assert_source <<~'RUBY'
1430
- def self.foo
1431
- end
1432
- RUBY
1433
-
1434
- assert_source <<~'RUBY'
1435
- def self.foo
1436
- bar
1437
- end
1438
- RUBY
1439
-
1440
- assert_source <<~'RUBY'
1441
- def self.foo
1442
- bar
1443
- baz
1444
- end
1445
- RUBY
1446
-
1447
- assert_source <<~'RUBY'
1448
- def Foo.bar
1449
- bar
1450
- end
1451
- RUBY
1452
-
1453
- end
1454
-
1455
- context 'class' do
1456
- assert_source <<~'RUBY'
1457
- class TestClass
1458
- end
1459
- RUBY
1460
-
1461
- assert_source <<~'RUBY'
1462
- class << some_object
1463
- end
1464
- RUBY
1465
-
1466
- assert_source <<~'RUBY'
1467
- class << some_object
1468
- the_body
1469
- end
1470
- RUBY
1471
-
1472
- assert_source <<~'RUBY'
1473
- class SomeNameSpace::TestClass
1474
- end
1475
- RUBY
1476
-
1477
- assert_source <<~'RUBY'
1478
- class Some::Name::Space::TestClass
1479
- end
1480
- RUBY
1481
-
1482
- assert_source <<~'RUBY'
1483
- class TestClass < Object
1484
- end
1485
- RUBY
1486
-
1487
- assert_source <<~'RUBY'
1488
- class TestClass < SomeNameSpace::Object
1489
- end
1490
- RUBY
1491
-
1492
- assert_source <<~'RUBY'
1493
- class TestClass
1494
- def foo
1495
- :bar
1496
- end
1497
- end
1498
- RUBY
1499
-
1500
- assert_source <<~'RUBY'
1501
- class ::TestClass
1502
- end
1503
- RUBY
1504
- end
1505
-
1506
- context 'module' do
1507
-
1508
- assert_source <<~'RUBY'
1509
- module TestModule
1510
- end
1511
- RUBY
1512
-
1513
- assert_source <<~'RUBY'
1514
- module SomeNameSpace::TestModule
1515
- end
1516
- RUBY
1517
-
1518
- assert_source <<~'RUBY'
1519
- module Some::Name::Space::TestModule
1520
- end
1521
- RUBY
1522
-
1523
- assert_source <<~'RUBY'
1524
- module TestModule
1525
- def foo
1526
- :bar
1527
- end
1528
- end
1529
- RUBY
1530
-
1531
- end
1532
-
1533
- context 'op assign' do
1534
- %w(|= ||= &= &&= += -= *= /= **= %=).each do |op|
1535
- assert_source "self.foo #{op} bar"
1536
- assert_source "foo[key] #{op} bar"
1537
- assert_source "a #{op} (true\nfalse)"
1538
- end
1539
- end
1540
-
1541
- context 'element assignment' do
1542
- assert_source 'foo[index] = value'
1543
- assert_source 'foo[*index] = value'
1544
- assert_source 'foo[a, b] = value'
1545
- assert_source 'foo[1..2] = value'
1546
- assert_source 'foo.[]=()'
1547
- assert_source 'foo.[]=true'
1548
- assert_source 'foo.[]=(1, 2)'
1549
- assert_source 'foo[] = 1'
1550
- assert_unterminated 'foo[] = 1'
1551
-
1552
- %w(+ - * / % & | || &&).each do |operator|
1553
- context "with #{operator}" do
1554
- assert_source "foo[index] #{operator}= 2"
1555
- assert_source "foo[] #{operator}= 2"
1556
- end
1557
- end
1558
- end
1559
-
1560
- context 'defined?' do
1561
- assert_source <<~'RUBY'
1562
- defined?(@foo)
1563
- RUBY
1564
-
1565
- assert_source <<~'RUBY'
1566
- defined?(Foo)
1567
- RUBY
1568
-
1569
- assert_source <<~'RUBY'
1570
- defined?((a, b = [1, 2]))
1571
- RUBY
1572
- end
1573
- end
1574
-
1575
- context 'lambda' do
1576
- assert_source <<~'RUBY'
1577
- lambda do
1578
- end
1579
- RUBY
1580
-
1581
- assert_source <<~'RUBY'
1582
- lambda do |a, b|
1583
- a
1584
- end
1585
- RUBY
1586
-
1587
- assert_source <<~'RUBY'
1588
- ->() do
1589
- end
1590
- RUBY
1591
-
1592
- assert_source <<~'RUBY'
1593
- ->(a) do
1594
- end
1595
- RUBY
1596
-
1597
- assert_source <<~'RUBY'
1598
- ->(a, b) do
1599
- end
1600
- RUBY
1601
- end
1602
-
1603
- context 'match operators' do
1604
- assert_source '/bar/ =~ foo'
1605
- assert_source '/bar/ =~ :foo'
1606
- assert_source '(/bar/ =~ :foo).foo'
1607
- assert_source 'foo =~ /bar/'
1608
- assert_source 'foo(foo =~ /bar/)'
1609
- assert_source '(foo =~ /bar/).foo'
1610
- end
1611
-
1612
- context 'binary operator methods' do
1613
- %w(+ - * / & | << >> == === != <= < <=> > >= =~ !~ ^ **).each do |operator|
1614
- assert_source "(-1) #{operator} 2"
1615
- assert_source "(-1.2) #{operator} 2"
1616
- assert_source "left.#{operator}(*foo)"
1617
- assert_source "left.#{operator}(a, b)"
1618
- assert_source "self #{operator} b"
1619
- assert_source "a #{operator} b"
1620
- assert_source "(a #{operator} b).foo"
1621
- end
1622
-
1623
- assert_source 'left / right'
1624
- end
1625
-
1626
- context 'nested binary operators' do
1627
- assert_source '(a + b) / (c - d)'
1628
- assert_source '(a + b) / c.-(e, f)'
1629
- assert_source '(a + b) / c.-(*f)'
1630
- end
1631
-
1632
- context 'binary operator' do
1633
- assert_source 'a || (return foo)'
1634
- assert_source '(return foo) || a'
1635
- assert_source 'a || (break foo)'
1636
- assert_source '(break foo) || a'
1637
- assert_source '(a || b).foo'
1638
- assert_source 'a || (b || c)'
1639
- end
1640
-
1641
- { or: :'||', and: :'&&' }.each do |word, symbol|
1642
- assert_generates "a #{word} return foo", "a #{symbol} (return foo)"
1643
- assert_generates "a #{word} break foo", "a #{symbol} (break foo)"
1644
- assert_generates "a #{word} next foo", "a #{symbol} (next foo)"
1645
- end
1646
-
1647
- context 'expansion of shortcuts' do
1648
- assert_source 'a += 2'
1649
- assert_source 'a -= 2'
1650
- assert_source 'a **= 2'
1651
- assert_source 'a *= 2'
1652
- assert_source 'a /= 2'
1653
- end
1654
-
1655
- context 'shortcuts' do
1656
- assert_source 'a &&= b'
1657
- assert_source 'a ||= 2'
1658
- assert_source '(a ||= 2).bar'
1659
- assert_source '(h ||= {})[k] = v'
1660
- end
1661
-
1662
- context 'flip flops' do
1663
- context 'inclusive' do
1664
- assert_source <<~'RUBY'
1665
- if ((i == 4)..(i == 4))
1666
- foo
1667
- end
1668
- RUBY
1669
- end
1670
-
1671
- context 'exclusive' do
1672
- assert_source <<~'RUBY'
1673
- if ((i == 4)...(i == 4))
1674
- foo
1675
- end
1676
- RUBY
1677
- end
1678
- end
1679
-
1680
- context 'case statement' do
1681
- assert_source <<~'RUBY'
1682
- case
1683
- when bar
1684
- baz
1685
- when baz
1686
- bar
1687
- end
1688
- RUBY
1689
-
1690
- assert_source <<~'RUBY'
1691
- case foo
1692
- when bar
1693
- when baz
1694
- bar
1695
- end
1696
- RUBY
1697
-
1698
- assert_source <<~'RUBY'
1699
- case foo
1700
- when bar
1701
- baz
1702
- when baz
1703
- bar
1704
- end
1705
- RUBY
1706
-
1707
- assert_source <<~'RUBY'
1708
- case foo
1709
- when bar, baz
1710
- :other
1711
- end
1712
- RUBY
1713
-
1714
- assert_source <<~'RUBY'
1715
- case foo
1716
- when *bar
1717
- :value
1718
- end
1719
- RUBY
1720
-
1721
- assert_source <<~'RUBY'
1722
- case foo
1723
- when bar
1724
- baz
1725
- else
1726
- :foo
1727
- end
1728
- RUBY
1729
- end
1730
-
1731
- context 'for' do
1732
- assert_source <<~'RUBY'
1733
- bar(for a in bar do
1734
- baz
1735
- end)
1736
- RUBY
1737
- assert_source <<~'RUBY'
1738
- for a in bar do
1739
- baz
1740
- end
1741
- RUBY
1742
-
1743
- assert_source <<~'RUBY'
1744
- for a, *b in bar do
1745
- baz
1746
- end
1747
- RUBY
1748
-
1749
- assert_source <<~'RUBY'
1750
- for a, b in bar do
1751
- baz
1752
- end
1753
- RUBY
1754
- end
1755
-
1756
- context 'unary operators' do
1757
- assert_source '!1'
1758
- assert_source '!(!1)'
1759
- assert_source '!(!(foo || bar))'
1760
- assert_source '!(!1).baz'
1761
- assert_source '~a'
1762
- assert_source '-a'
1763
- assert_source '+a'
1764
- assert_source '-(-a).foo'
1765
- end
1766
-
1767
- context 'loop' do
1768
- assert_source <<~'RUBY'
1769
- loop do
1770
- foo
1771
- end
1772
- RUBY
1773
- end
1774
-
1775
- context 'post conditions' do
1776
- assert_source <<~'RUBY'
1777
- x = (begin
1778
- foo
1779
- end while baz)
1780
- RUBY
1781
-
1782
- assert_source <<~'RUBY'
1783
- begin
1784
- foo
1785
- end while baz
1786
- RUBY
1787
-
1788
- assert_source <<~'RUBY'
1789
- begin
1790
- foo
1791
- bar
1792
- end until baz
1793
- RUBY
1794
-
1795
- assert_source <<~'RUBY'
1796
- begin
1797
- foo
1798
- bar
1799
- end while baz
1800
- RUBY
1801
- end
1802
-
1803
- context 'while' do
1804
- assert_source <<~'RUBY'
1805
- while false
1806
- end
1807
- RUBY
1808
-
1809
- assert_source <<~'RUBY'
1810
- while false
1811
- 3
1812
- end
1813
- RUBY
1814
-
1815
- assert_source <<~'RUBY'
1816
- while (foo do
1817
- end)
1818
- :body
1819
- end
1820
- RUBY
1821
- end
1822
-
1823
- context 'until' do
1824
- assert_source <<~'RUBY'
1825
- until false
1826
- end
1827
- RUBY
1828
-
1829
- assert_source <<~'RUBY'
1830
- until false
1831
- 3
1832
- end
1833
- RUBY
1834
-
1835
- assert_source <<~'RUBY'
1836
- until (foo do
1837
- end)
1838
- :body
1839
- end
1840
- RUBY
1841
- end
1842
-
1843
- assert_source <<~'RUBY'
1844
- # comment before
1845
- a_line_of_code
1846
- RUBY
1847
-
1848
- assert_source <<~'RUBY'
1849
- a_line_of_code # comment after
1850
- RUBY
1851
-
1852
- assert_source <<~'RUBY'
1853
- nested do # first
1854
- # second
1855
- something # comment
1856
- # another
1857
- end
1858
- # last
1859
- RUBY
1860
-
1861
- assert_generates <<~'RUBY', <<~'RUBY'
1862
- foo if bar
1863
- # comment
1864
- RUBY
1865
- if bar
1866
- foo
1867
- end
1868
- # comment
1869
- RUBY
1870
-
1871
- assert_source <<~'RUBY'
1872
- def noop
1873
- # do nothing
1874
- end
1875
- RUBY
1876
-
1877
- assert_source <<~'RUBY'
1878
- =begin
1879
- block comment
1880
- =end
1881
- nested do
1882
- =begin
1883
- another block comment
1884
- =end
1885
- something
1886
- =begin
1887
- last block comment
1888
- =end
1889
- end
1890
- RUBY
1891
-
1892
- assert_generates(<<~'RUBY', <<~'RUBY')
1893
- 1 + # first
1894
- 2 # second
1895
- RUBY
1896
- 1 + 2 # first # second
1897
- RUBY
1898
-
1899
- assert_generates(<<~'RUBY', <<~'RUBY')
1900
- 1 +
1901
- # first
1902
- 2 # second
1903
- RUBY
1904
- 1 + 2 # first # second
1905
- RUBY
1906
-
1907
- assert_generates(<<~'RUBY', <<~'RUBY')
1908
- 1 +
1909
- =begin
1910
- block comment
1911
- =end
1912
- 2
1913
- RUBY
1914
- 1 + 2
1915
- =begin
1916
- block comment
1917
- =end
1918
- RUBY
1919
- end
1920
- end