@@ -15,7 +15,9 @@ import {
1515 moveCursorLeft ,
1616 moveCursorRight ,
1717 moveCursorUp ,
18- moveToPosition
18+ moveToPosition ,
19+ restorePosition ,
20+ savePosition
1921} from './terminal_test_utils' ;
2022
2123chaiConfig . truncateThreshold = 0 ;
@@ -970,4 +972,49 @@ describe('Test terminal model', function () {
970972 assert . deepEqual ( [ [ 0 , 1 , 2 ] , [ 2 ] ] , this . changedLines ) ;
971973 } ) ;
972974 } ) ;
975+
976+ describe ( 'Save/restore cursor position' , function ( ) {
977+ beforeEach ( function ( ) {
978+ sinon . stub ( console , 'log' ) . returns ( void 0 ) ;
979+ } ) ;
980+
981+ afterEach ( function ( ) {
982+ console . log . restore ( ) ;
983+ } ) ;
984+
985+ it ( 'Test restore in the first line' , function ( ) {
986+ this . model . write ( '1234' + savePosition ( ) + '5678' + restorePosition ( ) + 'abc' ) ;
987+
988+ assert . deepEqual ( [ '1234abc8' ] , this . model . lines ) ;
989+ } ) ;
990+
991+ it ( 'Test restore in the first line, when 3 lines' , function ( ) {
992+ this . model . write ( '123' + savePosition ( ) + '45\n678\n90' + restorePosition ( ) + 'abc' ) ;
993+
994+ assert . deepEqual ( [ '123abc' , '678' , '90' ] , this . model . lines ) ;
995+ } ) ;
996+
997+ it ( 'Test restore in the last line, when 3 lines' , function ( ) {
998+ this . model . write ( '123\n4567\n8' + savePosition ( ) + '90' + moveCursorUp ( 2 ) + 'X'
999+ + restorePosition ( ) + 'abc' ) ;
1000+
1001+ assert . deepEqual ( [ '123X' , '4567' , '8abc' ] , this . model . lines ) ;
1002+ } ) ;
1003+
1004+ it ( 'Test restore position without save' , function ( ) {
1005+ this . model . write ( '1234' + restorePosition ( ) + 'abc' ) ;
1006+
1007+ assert . deepEqual ( [ '1234abc' ] , this . model . lines ) ;
1008+ expect ( console . log . args [ 0 ] [ 0 ] ) . to . equal ( 'WARN! trying to restore cursor position, but nothing is saved' ) ;
1009+ } ) ;
1010+
1011+ it ( 'Test restore position after clear' , function ( ) {
1012+ this . model . write ( '1234' + savePosition ( ) ) ;
1013+ this . model . clear ( ) ;
1014+ this . model . write ( 'abc' + restorePosition ( ) + 'XYZ' ) ;
1015+
1016+ assert . deepEqual ( [ 'abcXYZ' ] , this . model . lines ) ;
1017+ expect ( console . log . args [ 0 ] [ 0 ] ) . to . equal ( 'WARN! trying to restore cursor position, but nothing is saved' ) ;
1018+ } ) ;
1019+ } )
9731020} ) ;
0 commit comments