| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 
 | """
PC-BASIC test_console
Tests for console
(c) 2020--2023 Rob Hagemans
This file is released under the GNU GPL version 3 or later.
"""
from pcbasic import Session
from tests.unit.utils import TestCase, run_tests
_LIPSUM = (
    b'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt '
    b'ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco '
    b'laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in '
    b'voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat '
    b'cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
)
class ConsoleTest(TestCase):
    """Console tests."""
    tag = u'console'
    def test_control_keys(self):
        """Test special keys in console."""
        with Session() as s:
            s.execute(b'cls:print "%s"' % (_LIPSUM[:200],))
            # home
            s.press_keys(u'\0\x47')
            s.press_keys(u'1')
            # ctrl + right
            s.press_keys(u'\0\x74\0\x74')
            s.press_keys(u'2')
            # ctrl + left
            s.press_keys(u'\0\x73\0\x73')
            s.press_keys(u'3')
            # right
            s.press_keys(u'\0\x4d\0\x4d')
            s.press_keys(u'4')
            # left
            s.press_keys(u'\0\x4b\0\x4b')
            s.press_keys(u'5')
            # ctrl + right
            s.press_keys(u'\0\x74\0\x74')
            s.press_keys(u'6')
            # backspace
            s.press_keys(u'\b')
            s.press_keys(u'7')
            # ctrl + right
            s.press_keys(u'\0\x74\0\x74')
            s.press_keys(u'8')
            # ctrl + end
            s.press_keys(u'\0\x75')
            s.press_keys(u'9')
            # down
            s.press_keys(u'\0\x50\0\x50\0\x50')
            # up
            s.press_keys(u'\0\x48')
            s.press_keys(u'system\r')
            s.interact()
        assert self.get_text_stripped(s) == [
            b'1orem 3p54m 2olor 7t amet, 89',
            b'Ok\xff',
            b'                             system'
        ] + [b''] * 22
    def test_control_keys_2(self):
        """Test special keys in console."""
        with Session() as s:
            # bel
            s.press_keys(u'1\a2')
            # tab
            s.press_keys(u'3\t4')
            # lf
            s.press_keys(u'5\n6')
            # down, down
            s.press_keys(u'\0\x50\0\x50')
            # esc
            s.press_keys(u'7\x1b8')
            # down, system, enter
            s.press_keys(u'\0\x50system\r')
            s.interact()
        assert self.get_text_stripped(s) == [
            b'Ok\xff', b'123     45', b'6', b'', b'8', b' system',
        ] + [b''] * 19
    def test_control_keys_3(self):
        """Test special keys in console."""
        with Session() as s:
            s.execute(b'cls:print "%s"' % (_LIPSUM[:200],))
            # home
            s.press_keys(u'\0\x47')
            # del
            s.press_keys(u'\0\x53')
            # ins
            s.press_keys(u'\0\x52')
            s.press_keys(u'1')
            # down, system, enter
            s.press_keys(u'\0\x50\0\x50\0\x50\0\x50system\r')
            s.interact()
        assert self.get_text_stripped(s) == [
            b'1orem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i',
            b'ncididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostru',
            b'd exercitation ullamco laboris nisi ut a',
            b'Ok\xff',
            b' system',
        ] + [b''] * 20
    def test_end(self):
        """Test end key in console."""
        with Session() as s:
            s.execute(b'cls:print "%s"' % (_LIPSUM[:200],))
            # ctrl + home
            s.press_keys(u'\0\x77')
            s.press_keys(u'system\r')
            s.interact()
        assert self.get_text_stripped(s) == [b'system'] + [b''] * 24
    def test_control_home(self):
        """Test ctrl-home in console."""
        with Session() as s:
            s.execute(b'cls:print "%s"' % (_LIPSUM[:200],))
            # home
            s.press_keys(u'\0\x47')
            # end
            s.press_keys(u'\0\x4f')
            # down, esc, system, enter
            s.press_keys(u'\0\x50\x1bsystem\r')
            s.interact()
        assert self.get_text_stripped(s) == [
            b'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i',
            b'ncididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostru',
            b'd exercitation ullamco laboris nisi ut a',
            b'system'
        ] + [b''] * 21
    def test_control_printscreen(self):
        """Test ctrl+printscreen in console."""
        with Session(devices={'lpt1:': 'FILE:'+self.output_path(u'printscr.txt')}) as s:
            s.execute(b'cls:print "%s"' % (_LIPSUM[:200],))
            # ctrl + prtscr
            s.press_keys(u'\0\x72')
            # down, system, enter
            s.press_keys(u'\0\x50\0\x50\0\x50\0\x50system\r')
            s.interact()
        with open(self.output_path(u'printscr.txt'), 'rb') as f:
            assert f.read() == b'system\r\n'
    def test_control_c(self):
        """Test ctrl-home in console."""
        with Session() as s:
            # ctrl+c
            s.press_keys(u'\x03')
            s.press_keys(u'system\r')
            s.interact()
        assert self.get_text_stripped(s) == [b'Ok\xff', b'', b'system'] + [b'']*22
    def test_print_control(self):
        """Test printing control chars."""
        with Session() as s:
            s.execute(b'print chr$(7) "1" chr$(9) "2" chr$(&h1c) "3" chr$(&h1d) "4"')
            s.execute(b'print chr$(7)+"1"+chr$(9)+"2"+chr$(&h1c)+"3"+chr$(&h1d)+"4"')
        assert self.get_text_stripped(s) == [b'1       2 4', b'1       2 4'] + [b''] * 23
    def test_print_control_2(self):
        """Test printing control chars."""
        with Session() as s:
            s.execute(b'print "  1" chr$(&h1f) chr$(&h1f) "2" chr$(&h1e) "3" chr$(&h0b) "4"')
        assert self.get_text_stripped(s) == [b'4 1', b'    3', b'   2'] +[b''] * 22
    def test_print_control_3(self):
        """Test printing control chars."""
        with Session() as s:
            s.execute(b'print "  1" chr$(&h1f) chr$(&h1f) "2" chr$(&h1e) "3" chr$(&h0c) "4"')
        assert self.get_text_stripped(s) == [b'4'] + [b''] * 24
    def test_input_wrapping_line(self):
        """Test input on top of an existing long line."""
        with Session() as s:
            s.press_keys(u'1\r')
            s.execute(b'cls:print "%s"' % (_LIPSUM[:200],))
            s.execute(b'locate 1,1: input a$')
            assert s.get_variable('a$') == b'1'
            assert self.get_text_stripped(s)[0] == (
                b'? 1em ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i'
            )
    def test_close_stream(self):
        """Test input stream."""
        with open(self.output_path(u'input.txt'), 'wb') as f:
            f.write(b'?1\r')
        input_stream = open(self.output_path(u'input.txt'), 'rb')
        with Session(input_streams=input_stream) as s:
            s.interact()
        assert self.get_text_stripped(s) == [b'Ok\xff', b'?1', b' 1', b'Ok\xff'] + [b''] * 21
    # cursor position and overflow
    def test_cursor_move(self):
        """Test cursor movement after print."""
        with Session() as s:
            s.execute(b'locate 1,1: print"xy";: print csrlin; pos(0);:locate 10,1')
        # normal behaviour, cursor moves
        assert self.get_text_stripped(s) == [b'xy 1  6'] + [b'']*24, repr(s.get_text())
    def test_cursor_overflow(self):
        """Test cursor movement after print on last column."""
        with Session() as s:
            s.execute(b'locate 1,80: print"x";')
        # cursor has not moved to next row
        assert s._impl.text_screen.current_row == 1, s._impl.text_screen.current_row
        assert s._impl.text_screen.current_col == 80, s._impl.text_screen.current_col
    def test_cursor_overflow_cr(self):
        """Test cursor movement after print char and cr on last column."""
        with Session() as s:
            s.execute(b'locate 1,80: print"x";chr$(13);')
        # cursor has moved after CR
        assert s._impl.text_screen.current_row == 2, s._impl.text_screen.current_row
        assert s._impl.text_screen.current_col == 1, s._impl.text_screen.current_col
    def test_cursor_overflow_char(self):
        """Test cursor movement after print two chars on last column."""
        with Session() as s:
            s.execute(b'locate 1,80: print"x" "y";')
        # cursor has moved after printing char
        assert s._impl.text_screen.current_row == 2, s._impl.text_screen.current_row
        assert s._impl.text_screen.current_col == 2, s._impl.text_screen.current_col
        assert self.get_text_stripped(s) == [b' '*79 + b'x', b'y'] + [b''] * 23, repr(self.get_text_stripped(s))
    def test_cursor_overflow_word(self):
        """Test cursor movement after print a two-char word on last column."""
        with Session() as s:
            s.execute(b'locate 1,80: print"xy";')
        # cursor has moved after printing char
        assert s._impl.text_screen.current_row == 2, s._impl.text_screen.current_row
        assert s._impl.text_screen.current_col == 3, s._impl.text_screen.current_col
        assert self.get_text_stripped(s) == [b'', b'xy'] + [b''] * 23, repr(self.get_text_stripped(s))
    def test_cursor_overflow_cr_char(self):
        """Test cursor movement after print char, return, char on last column."""
        with Session() as s:
            s.execute(b'locate 1,80: print"x" chr$(13) "y";')
        # cursor has moved after printing char, but no extra line for CR
        assert s._impl.text_screen.current_row == 2, s._impl.text_screen.current_row
        assert s._impl.text_screen.current_col == 2, s._impl.text_screen.current_col
        assert self.get_text_stripped(s) == [b' '*79 + b'x', b'y'] + [b''] * 23, repr(self.get_text_stripped(s))
    def test_cursor_bottom(self):
        """Test cursor movement after print on last column, last row."""
        with Session() as s:
            # normal behaviour
            s.execute(b'locate 24,80: print"x";')
        # cursor has not moved to next row, no scroll
        assert s._impl.text_screen.current_row == 24, s._impl.text_screen.current_row
        assert s._impl.text_screen.current_col == 80, s._impl.text_screen.current_col
        assert self.get_text_stripped(s) == [b''] * 23 + [b' '*79 + b'x', b''], repr(self.get_text_stripped(s))
    def test_cursor_bottom_cr(self):
        """Test cursor movement after print two chars on last column, last row."""
        with Session() as s:
            s.execute(b'locate 24,80: print"x";chr$(13);')
        # cursor has moved after CR, screen has scrolled
        assert s._impl.text_screen.current_row == 24, s._impl.text_screen.current_row
        assert s._impl.text_screen.current_col == 1, s._impl.text_screen.current_col
        assert self.get_text_stripped(s) == [b''] * 22 + [b' '*79 + b'x', b'', b''], repr(self.get_text_stripped(s))
    def test_cursor_bottom_char(self):
        """Test cursor movement after print char and return on last column, last row."""
        with Session() as s:
            s.execute(b'locate 24,80: print"x" "y";')
        # cursor has moved after printing char, screen has scrolled
        assert s._impl.text_screen.current_row == 24, s._impl.text_screen.current_row
        assert s._impl.text_screen.current_col == 2, s._impl.text_screen.current_col
        assert self.get_text_stripped(s) == [b''] * 22 + [b' '*79 + b'x', b'y', b''], repr(self.get_text_stripped(s))
    def test_cursor_bottom_cr_char(self):
        """Test cursor movement after print char, return, char on last column, last row."""
        with Session() as s:
            s.execute(b'locate 24,80: print"x" chr$(13) "y";')
        # cursor has moved after printing char, but no extra line for CR
        assert s._impl.text_screen.current_row == 24, s._impl.text_screen.current_row
        assert s._impl.text_screen.current_col == 2, s._impl.text_screen.current_col
        assert self.get_text_stripped(s) == [b''] * 22 + [b' '*79 + b'x', b'y', b''], repr(self.get_text_stripped(s))
if __name__ == '__main__':
    run_tests()
 |