]> code.delx.au - gnu-emacs/blob - test/indent/ruby.rb
522c3bd5465c81ae9463a9dea6d84f8ded9e8041
[gnu-emacs] / test / indent / ruby.rb
1 if something_wrong? # ruby-move-to-block-skips-heredoc
2 ActiveSupport::Deprecation.warn(<<-eowarn)
3 boo hoo
4 end
5 eowarn
6 foo
7
8 foo(<<~squiggly)
9 end
10 squiggly
11 end
12
13 def foo
14 %^bar^
15 end
16
17 # Percent literals.
18 b = %Q{This is a "string"}
19 c = %w!foo
20 bar
21 baz!
22 d = %(hello (nested) world)
23
24 # Don't propertize percent literals inside strings.
25 "(%s, %s)" % [123, 456]
26
27 "abc/#{def}ghi"
28 "abc\#{def}ghi"
29
30 # Or inside comments.
31 x = # "tot %q/to"; =
32 y = 2 / 3
33
34 # Regexp after whitelisted method.
35 "abc".sub /b/, 'd'
36
37 # Don't mis-match "sub" at the end of words.
38 a = asub / aslb + bsub / bslb;
39
40 # Highlight the regexp after "if".
41 x = toto / foo if /do bar/ =~ "dobar"
42
43 # Regexp options are highlighted.
44
45 /foo/xi != %r{bar}mo.tee
46
47 foo { /"tee/
48 bar { |qux| /'fee"/ } # bug#20026
49 }
50
51 bar(class: XXX) do # ruby-indent-keyword-label
52 foo
53 end
54 bar
55
56 foo = [1, # ruby-deep-indent
57 2]
58
59 foo = { # ruby-deep-indent-disabled
60 a: b
61 }
62
63 foo = { a: b,
64 a1: b1
65 }
66
67 foo({ # bug#16118
68 a: b,
69 c: d
70 })
71
72 bar = foo(
73 a, [
74 1,
75 ],
76 :qux => [
77 3
78 ])
79
80 foo(
81 [
82 {
83 a: b
84 },
85 ],
86 {
87 c: d
88 }
89 )
90
91 foo([{
92 a: 2
93 },
94 {
95 b: 3
96 },
97 4
98 ])
99
100 foo = [ # ruby-deep-indent-disabled
101 1
102 ]
103
104 foo( # ruby-deep-indent-disabled
105 a
106 )
107
108 # Multiline regexp.
109 /bars
110 tees # toots
111 nfoos/
112
113 def test1(arg)
114 puts "hello"
115 end
116
117 def test2 (arg)
118 a = "apple"
119
120 if a == 2
121 puts "hello"
122 else
123 puts "there"
124 end
125
126 if a == 2 then
127 puts "hello"
128 elsif a == 3
129 puts "hello3"
130 elsif a == 3 then
131 puts "hello3"
132 else
133 puts "there"
134 end
135
136 b = case a
137 when "a"
138 6
139 # Support for this syntax was removed in Ruby 1.9, so we
140 # probably don't need to handle it either.
141 # when "b" :
142 # 7
143 # when "c" : 2
144 when "d" then 4
145 else 5
146 end
147 end
148
149 # Some Cucumber code:
150 Given /toto/ do
151 print "hello"
152 end
153
154 # Bug#15208
155 if something == :==
156 do_something
157
158 return false unless method == :+
159 x = y + z # Bug#16609
160
161 a = 1 ? 2 :(
162 2 + 3
163 )
164 end
165
166 # Bug#17097
167 if x == :!=
168 something
169 end
170
171 qux :+,
172 bar,
173 :[]=,
174 bar,
175 :a
176
177 b = $:
178 c = ??
179
180 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
181 d = 4 + 5 + # no '\' needed
182 6 + 7
183
184 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
185 e = 8 + 9 \
186 + 10 # '\' needed
187
188 foo = obj.bar { |m| tee(m) } +
189 obj.qux { |m| hum(m) }
190
191 begin
192 foo
193 ensure
194 bar
195 end
196
197 # Bug#15369
198 MSG = 'Separate every 3 digits in the integer portion of a number' \
199 'with underscores(_).'
200
201 class C
202 def foo
203 self.end
204 D.new.class
205 end
206 end
207
208 a = foo(j, k) -
209 bar_tee
210
211 while a < b do # "do" is optional
212 foo
213 end
214
215 desc "foo foo" \
216 "bar bar"
217
218 foo.
219 bar
220
221 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
222 foo
223 .bar
224
225 z = {
226 foo: {
227 a: "aaa",
228 b: "bbb"
229 }
230 }
231
232 foo if
233 bar
234
235 fail "stuff" \
236 unless all_fine?
237
238 if foo?
239 bar
240 end
241
242 method arg1, # bug#15594
243 method2 arg2,
244 arg3
245
246 method? arg1,
247 arg2
248
249 method! arg1,
250 arg2
251
252 method !arg1,
253 arg2
254
255 method [],
256 arg2
257
258 method :foo,
259 :bar
260
261 method (a + b),
262 c, :d => :e,
263 f: g
264
265 desc "abc",
266 defg
267
268 it "is a method call with block" do |asd|
269 foo
270 end
271
272 it("is too!") {
273 bar
274 .qux
275 }
276
277 and_this_one(has) { |block, parameters|
278 tee
279 }
280
281 if foo &&
282 bar
283 end
284
285 foo +
286 bar
287
288 foo and
289 bar
290
291 foo > bar &&
292 tee < qux
293
294 zux do
295 foo == bar and
296 tee == qux
297 end
298
299 foo ^
300 bar
301
302 foo_bar_tee(1, 2, 3)
303 .qux.bar
304 .tee
305
306 foo do
307 bar
308 .tee
309 end
310
311 def bar
312 foo
313 .baz
314 end
315
316 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
317 tee = if foo
318 bar
319 else
320 tee
321 end
322
323 a = b {
324 c
325 }
326
327 aa = bb do
328 cc
329 end
330
331 foo :bar do
332 qux
333 end
334
335 foo do |*args|
336 tee
337 end
338
339 bar do |&block|
340 tee
341 end
342
343 foo = [1, 2, 3].map do |i|
344 i + 1
345 end
346
347 bar.foo do
348 bar
349 end
350
351 bar.foo(tee) do
352 bar
353 end
354
355 bar.foo(tee) {
356 bar
357 }
358
359 bar 1 do
360 foo 2 do
361 tee
362 end
363 end
364
365 foo |
366 bar
367
368 def qux
369 foo ||= begin
370 bar
371 tee
372 rescue
373 oomph
374 end
375 end
376
377 private def foo
378 bar
379 end
380
381 %^abc^
382 ddd
383
384 qux = foo.fee ?
385 bar :
386 tee
387
388 zoo.keep.bar!(
389 {x: y,
390 z: t})
391
392 zoo
393 .lose(
394 q, p)
395
396 a.records().map(&:b).zip(
397 foo)
398
399 # FIXME: This is not consistent with the example below it, but this
400 # offset only happens if the colon is at eol, which wouldn't be often.
401 # Tokenizing `bar:' as `:bar =>' would be better, but it's hard to
402 # distinguish from a variable reference inside a ternary operator.
403 foo(bar:
404 tee)
405
406 foo(:bar =>
407 tee)
408
409 {'a' => {
410 'b' => 'c',
411 'd' => %w(e f)
412 }
413 }
414
415 # Bug#17050
416
417 return render json: {
418 errors: { base: [message] },
419 copying: copying
420 },
421 status: 400
422
423 top test(
424 some,
425 top,
426 test)
427
428 foo bar, {
429 tee: qux
430 }