def test_debug(self): """ Make sure the cursor works while debugging It might not because the console is "_executing" """ # Kernel client to test the responses of is_complete class TestIPyKernelClient(object): def is_complete(self, source): tm = TransformerManager() check_complete = tm.check_complete(source) responses.append(check_complete) # Initialize widget responses = [] w = ConsoleWidget() w._append_plain_text('Header\n') w._prompt = 'prompt>' w._show_prompt() w.kernel_client = TestIPyKernelClient() control = w._control # Execute incomplete statement inside a block code = "%debug range(1)\n" w._set_input_buffer(code) w.execute(interactive=True) # We should be able to move the cursor while debugging w._set_input_buffer("abd") QTest.keyClick(control, QtCore.Qt.Key_Left) QTest.keyClick(control, 'c') self.assertEqual(w._get_input_buffer(),"abcd")
def test_complete_python(self): """Test that is_complete is working correctly for Python.""" # Kernel client to test the responses of is_complete class TestIPyKernelClient(object): def is_complete(self, source): tm = TransformerManager() check_complete = tm.check_complete(source) responses.append(check_complete) # Initialize widget responses = [] w = ConsoleWidget() w._append_plain_text('Header\n') w._prompt = 'prompt>' w._show_prompt() w.kernel_client = TestIPyKernelClient() # Execute incomplete statement inside a block code = '\n'.join(["if True:", " a = 1"]) w._set_input_buffer(code) w.execute(interactive=True) assert responses == [('incomplete', 4)] # Execute complete statement inside a block responses = [] code = '\n'.join(["if True:", " a = 1\n\n"]) w._set_input_buffer(code) w.execute(interactive=True) assert responses == [('complete', None)]
def test_select_all(self): w = ConsoleWidget() w._append_plain_text('Header\n') w._prompt = 'prompt>' w._show_prompt() control = w._control app = QtWidgets.QApplication.instance() cursor = w._get_cursor() w._insert_plain_text_into_buffer(cursor, "if:\n pass") cursor.clearSelection() control.setTextCursor(cursor) # "select all" action selects cell first w.select_all_smart() QTest.keyClick(control, QtCore.Qt.Key_C, QtCore.Qt.ControlModifier) copied = app.clipboard().text() self.assertEqual(copied, 'if:\n> pass') # # "select all" action triggered a second time selects whole document w.select_all_smart() QTest.keyClick(control, QtCore.Qt.Key_C, QtCore.Qt.ControlModifier) copied = app.clipboard().text() self.assertEqual(copied, 'Header\nprompt>if:\n> pass')
def test_keypresses(self): """Test the event handling code for keypresses.""" w = ConsoleWidget() w._append_plain_text('Header\n') w._prompt = 'prompt>' w._show_prompt() control = w._control # Test setting the input buffer w._set_input_buffer('test input') self.assertEqual(w._get_input_buffer(), 'test input') # Ctrl+K kills input until EOL w._set_input_buffer('test input') c = control.textCursor() c.setPosition(c.position() - 3) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_K, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), 'test in') # Ctrl+V pastes w._set_input_buffer('test input ') QtGui.qApp.clipboard().setText('pasted text') QTest.keyClick(control, QtCore.Qt.Key_V, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), 'test input pasted text') self.assertEqual(control.document().blockCount(), 2) # Paste should strip indentation w._set_input_buffer('test input ') QtGui.qApp.clipboard().setText(' pasted text') QTest.keyClick(control, QtCore.Qt.Key_V, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), 'test input pasted text') self.assertEqual(control.document().blockCount(), 2) # Multiline paste, should also show continuation marks w._set_input_buffer('test input ') QtGui.qApp.clipboard().setText('line1\nline2\nline3') QTest.keyClick(control, QtCore.Qt.Key_V, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), 'test input line1\nline2\nline3') self.assertEqual(control.document().blockCount(), 4) self.assertEqual(control.document().findBlockByNumber(1).text(), 'prompt>test input line1') self.assertEqual(control.document().findBlockByNumber(2).text(), '> line2') self.assertEqual(control.document().findBlockByNumber(3).text(), '> line3')
def test_indent(self): """Test the event handling code for indent/dedent keypresses .""" w = ConsoleWidget() w._append_plain_text('Header\n') w._prompt = 'prompt>' w._show_prompt() control = w._control # TAB with multiline selection should block-indent w._set_input_buffer("") c = control.textCursor() pos=c.position() w._set_input_buffer("If 1:\n pass") c.setPosition(pos, QtGui.QTextCursor.KeepAnchor) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_Tab) self.assertEqual(w._get_input_buffer()," If 1:\n pass") # TAB with multiline selection, should block-indent to next multiple # of 4 spaces, if first line has 0 < indent < 4 w._set_input_buffer("") c = control.textCursor() pos=c.position() w._set_input_buffer(" If 2:\n pass") c.setPosition(pos, QtGui.QTextCursor.KeepAnchor) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_Tab) self.assertEqual(w._get_input_buffer()," If 2:\n pass") # Shift-TAB with multiline selection should block-dedent w._set_input_buffer("") c = control.textCursor() pos=c.position() w._set_input_buffer(" If 3:\n pass") c.setPosition(pos, QtGui.QTextCursor.KeepAnchor) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_Backtab) self.assertEqual(w._get_input_buffer(),"If 3:\n pass")
def test_indent(self): """Test the event handling code for indent/dedent keypresses .""" w = ConsoleWidget() w._append_plain_text('Header\n') w._prompt = 'prompt>' w._show_prompt() control = w._control # TAB with multiline selection should block-indent w._set_input_buffer("") c = control.textCursor() pos = c.position() w._set_input_buffer("If 1:\n pass") c.setPosition(pos, QtGui.QTextCursor.KeepAnchor) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_Tab) self.assertEqual(w._get_input_buffer(), " If 1:\n pass") # TAB with multiline selection, should block-indent to next multiple # of 4 spaces, if first line has 0 < indent < 4 w._set_input_buffer("") c = control.textCursor() pos = c.position() w._set_input_buffer(" If 2:\n pass") c.setPosition(pos, QtGui.QTextCursor.KeepAnchor) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_Tab) self.assertEqual(w._get_input_buffer(), " If 2:\n pass") # Shift-TAB with multiline selection should block-dedent w._set_input_buffer("") c = control.textCursor() pos = c.position() w._set_input_buffer(" If 3:\n pass") c.setPosition(pos, QtGui.QTextCursor.KeepAnchor) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_Backtab) self.assertEqual(w._get_input_buffer(), "If 3:\n pass")
def test_select_all(self): w = ConsoleWidget() w._append_plain_text('Header\n') w._prompt = 'prompt>' w._show_prompt() control = w._control cursor = w._get_cursor() w._insert_plain_text_into_buffer(cursor, "if:\n pass") cursor.clearSelection() control.setTextCursor(cursor) # "select all" action selects cell first w.select_all_smart() QTest.keyClick(control, QtCore.Qt.Key_C, QtCore.Qt.ControlModifier) copied = QtGui.qApp.clipboard().text() self.assertEqual(copied, 'if:\n> pass') # # "select all" action triggered a second time selects whole document w.select_all_smart() QTest.keyClick(control, QtCore.Qt.Key_C, QtCore.Qt.ControlModifier) copied = QtGui.qApp.clipboard().text() self.assertEqual(copied, 'Header\nprompt>if:\n> pass')
def test_prompt_cursors(self): """Test the cursors that keep track of where the prompt begins and ends""" w = ConsoleWidget() w._prompt = 'prompt>' doc = w._control.document() # Fill up the QTextEdit area with the maximum number of blocks doc.setMaximumBlockCount(10) for _ in range(9): w._append_plain_text('line\n') # Draw the prompt, this should cause the first lines to be deleted w._show_prompt() self.assertEqual(doc.blockCount(), 10) # _prompt_pos should be at the end of the document self.assertEqual(w._prompt_pos, w._get_end_pos()) # _append_before_prompt_pos should be at the beginning of the prompt self.assertEqual(w._append_before_prompt_pos, w._prompt_pos - len(w._prompt)) # insert some more text without drawing a new prompt w._append_plain_text('line\n') self.assertEqual(w._prompt_pos, w._get_end_pos() - len('line\n')) self.assertEqual(w._append_before_prompt_pos, w._prompt_pos - len(w._prompt)) # redraw the prompt w._show_prompt() self.assertEqual(w._prompt_pos, w._get_end_pos()) self.assertEqual(w._append_before_prompt_pos, w._prompt_pos - len(w._prompt)) # insert some text before the prompt w._append_plain_text('line', before_prompt=True) self.assertEqual(w._prompt_pos, w._get_end_pos()) self.assertEqual(w._append_before_prompt_pos, w._prompt_pos - len(w._prompt))
def test_keypresses(self): """Test the event handling code for keypresses.""" w = ConsoleWidget() w._append_plain_text('Header\n') w._prompt = 'prompt>' w._show_prompt() control = w._control # Test setting the input buffer w._set_input_buffer('test input') self.assertEqual(w._get_input_buffer(), 'test input') # Ctrl+K kills input until EOL w._set_input_buffer('test input') c = control.textCursor() c.setPosition(c.position() - 3) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_K, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), 'test in') # Ctrl+V pastes w._set_input_buffer('test input ') QtGui.qApp.clipboard().setText('pasted text') QTest.keyClick(control, QtCore.Qt.Key_V, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), 'test input pasted text') self.assertEqual(control.document().blockCount(), 2) # Paste should strip indentation w._set_input_buffer('test input ') QtGui.qApp.clipboard().setText(' pasted text') QTest.keyClick(control, QtCore.Qt.Key_V, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), 'test input pasted text') self.assertEqual(control.document().blockCount(), 2) # Multiline paste, should also show continuation marks w._set_input_buffer('test input ') QtGui.qApp.clipboard().setText('line1\nline2\nline3') QTest.keyClick(control, QtCore.Qt.Key_V, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), 'test input line1\nline2\nline3') self.assertEqual(control.document().blockCount(), 4) self.assertEqual(control.document().findBlockByNumber(1).text(), 'prompt>test input line1') self.assertEqual(control.document().findBlockByNumber(2).text(), '> line2') self.assertEqual(control.document().findBlockByNumber(3).text(), '> line3') # Multiline paste should strip indentation intelligently # in the case where pasted text has leading whitespace on first line # and we're pasting into indented position w._set_input_buffer(' ') QtGui.qApp.clipboard().setText(' If 1:\n pass') QTest.keyClick(control, QtCore.Qt.Key_V, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), ' If 1:\n pass') # Ctrl+Backspace should intelligently remove the last word w._set_input_buffer("foo = ['foo', 'foo', 'foo', \n" " 'bar', 'bar', 'bar']") QTest.keyClick(control, QtCore.Qt.Key_Backspace, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), ("foo = ['foo', 'foo', 'foo', \n" " 'bar', 'bar', '")) QTest.keyClick(control, QtCore.Qt.Key_Backspace, QtCore.Qt.ControlModifier) QTest.keyClick(control, QtCore.Qt.Key_Backspace, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), ("foo = ['foo', 'foo', 'foo', \n" " '")) QTest.keyClick(control, QtCore.Qt.Key_Backspace, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), ("foo = ['foo', 'foo', 'foo', \n" "")) QTest.keyClick(control, QtCore.Qt.Key_Backspace, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), "foo = ['foo', 'foo', 'foo',") # Ctrl+Delete should intelligently remove the next word w._set_input_buffer("foo = ['foo', 'foo', 'foo', \n" " 'bar', 'bar', 'bar']") c = control.textCursor() c.setPosition(35) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_Delete, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), ("foo = ['foo', 'foo', ', \n" " 'bar', 'bar', 'bar']")) QTest.keyClick(control, QtCore.Qt.Key_Delete, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), ("foo = ['foo', 'foo', \n" " 'bar', 'bar', 'bar']")) QTest.keyClick(control, QtCore.Qt.Key_Delete, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), "foo = ['foo', 'foo', 'bar', 'bar', 'bar']") w._set_input_buffer("foo = ['foo', 'foo', 'foo', \n" " 'bar', 'bar', 'bar']") c = control.textCursor() c.setPosition(48) control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_Delete, QtCore.Qt.ControlModifier) self.assertEqual(w._get_input_buffer(), ("foo = ['foo', 'foo', 'foo', \n" "'bar', 'bar', 'bar']")) # Left and right keys should respect the continuation prompt w._set_input_buffer("line 1\n" "line 2\n" "line 3") c = control.textCursor() c.setPosition(20) # End of line 1 control.setTextCursor(c) QTest.keyClick(control, QtCore.Qt.Key_Right) # Cursor should have moved after the continuation prompt self.assertEqual(control.textCursor().position(), 23) QTest.keyClick(control, QtCore.Qt.Key_Left) # Cursor should have moved to the end of the previous line self.assertEqual(control.textCursor().position(), 20)