@@ -312,6 +312,14 @@ def test_cursor_position_after_wrap_and_move_up(self):
312
312
self .assertEqual (reader .pos , 10 )
313
313
self .assertEqual (reader .cxy , (1 , 1 ))
314
314
315
+
316
+ class TestPyReplAutoindent (TestCase ):
317
+ def prepare_reader (self , events ):
318
+ console = FakeConsole (events )
319
+ config = ReadlineConfig (readline_completer = None )
320
+ reader = ReadlineAlikeReader (console = console , config = config )
321
+ return reader
322
+
315
323
def test_auto_indent_default (self ):
316
324
# fmt: off
317
325
input_code = (
@@ -372,7 +380,6 @@ def test_auto_indent_prev_block(self):
372
380
),
373
381
)
374
382
375
-
376
383
output_code = (
377
384
"def g():\n "
378
385
" pass\n "
@@ -385,6 +392,78 @@ def test_auto_indent_prev_block(self):
385
392
output2 = multiline_input (reader )
386
393
self .assertEqual (output2 , output_code )
387
394
395
+ def test_auto_indent_multiline (self ):
396
+ # fmt: off
397
+ events = itertools .chain (
398
+ code_to_events (
399
+ "def f():\n "
400
+ "pass"
401
+ ),
402
+ [
403
+ # go to the end of the first line
404
+ Event (evt = "key" , data = "up" , raw = bytearray (b"\x1b OA" )),
405
+ Event (evt = "key" , data = "\x05 " , raw = bytearray (b"\x1b O5" )),
406
+ # new line should be autoindented
407
+ Event (evt = "key" , data = "\n " , raw = bytearray (b"\n " )),
408
+ ],
409
+ code_to_events (
410
+ "pass"
411
+ ),
412
+ [
413
+ # go to end of last line
414
+ Event (evt = "key" , data = "down" , raw = bytearray (b"\x1b OB" )),
415
+ Event (evt = "key" , data = "\x05 " , raw = bytearray (b"\x1b O5" )),
416
+ # double newline to terminate the block
417
+ Event (evt = "key" , data = "\n " , raw = bytearray (b"\n " )),
418
+ Event (evt = "key" , data = "\n " , raw = bytearray (b"\n " )),
419
+ ],
420
+ )
421
+
422
+ output_code = (
423
+ "def f():\n "
424
+ " pass\n "
425
+ " pass\n "
426
+ " "
427
+ )
428
+ # fmt: on
429
+
430
+ reader = self .prepare_reader (events )
431
+ output = multiline_input (reader )
432
+ self .assertEqual (output , output_code )
433
+
434
+ def test_auto_indent_with_comment (self ):
435
+ # fmt: off
436
+ events = code_to_events (
437
+ "def f(): # foo\n "
438
+ "pass\n \n "
439
+ )
440
+
441
+ output_code = (
442
+ "def f(): # foo\n "
443
+ " pass\n "
444
+ " "
445
+ )
446
+ # fmt: on
447
+
448
+ reader = self .prepare_reader (events )
449
+ output = multiline_input (reader )
450
+ self .assertEqual (output , output_code )
451
+
452
+ def test_auto_indent_ignore_comments (self ):
453
+ # fmt: off
454
+ events = code_to_events (
455
+ "pass #:\n "
456
+ )
457
+
458
+ output_code = (
459
+ "pass #:"
460
+ )
461
+ # fmt: on
462
+
463
+ reader = self .prepare_reader (events )
464
+ output = multiline_input (reader )
465
+ self .assertEqual (output , output_code )
466
+
388
467
389
468
class TestPyReplOutput (TestCase ):
390
469
def prepare_reader (self , events ):
0 commit comments