test_jupyter_widget.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import unittest
  2. import pytest
  3. from qtpy import QtWidgets
  4. from qtconsole.client import QtKernelClient
  5. from qtconsole.jupyter_widget import JupyterWidget
  6. from . import no_display
  7. from qtpy.QtTest import QTest
  8. @pytest.mark.skipif(no_display, reason="Doesn't work without a display")
  9. class TestJupyterWidget(unittest.TestCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. """ Create the application for the test case.
  13. """
  14. cls._app = QtWidgets.QApplication.instance()
  15. if cls._app is None:
  16. cls._app = QtWidgets.QApplication([])
  17. cls._app.setQuitOnLastWindowClosed(False)
  18. @classmethod
  19. def tearDownClass(cls):
  20. """ Exit the application.
  21. """
  22. QtWidgets.QApplication.quit()
  23. def test_stylesheet_changed(self):
  24. """ Test changing stylesheets.
  25. """
  26. w = JupyterWidget(kind='rich')
  27. # By default, the background is light. White text is rendered as black
  28. self.assertEqual(w._ansi_processor.get_color(15).name(), '#000000')
  29. # Change to a dark colorscheme. White text is rendered as white
  30. w.syntax_style = 'monokai'
  31. self.assertEqual(w._ansi_processor.get_color(15).name(), '#ffffff')
  32. def test_other_output(self):
  33. """ Test displaying output from other clients.
  34. """
  35. w = JupyterWidget(kind='rich')
  36. w._append_plain_text('Header\n')
  37. w._show_interpreter_prompt(1)
  38. w.other_output_prefix = '[other] '
  39. w.syntax_style = 'default'
  40. control = w._control
  41. document = control.document()
  42. msg = dict(
  43. execution_count=1,
  44. code='a = 1 + 1\nb = range(10)',
  45. )
  46. w._append_custom(w._insert_other_input, msg, before_prompt=True)
  47. self.assertEqual(document.blockCount(), 6)
  48. self.assertEqual(document.toPlainText(), (
  49. u'Header\n'
  50. u'\n'
  51. u'[other] In [1]: a = 1 + 1\n'
  52. u' ...: b = range(10)\n'
  53. u'\n'
  54. u'In [2]: '
  55. ))
  56. # Check proper syntax highlighting
  57. self.assertEqual(document.toHtml(), (
  58. u'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">\n'
  59. u'<html><head><meta name="qrichtext" content="1" /><style type="text/css">\n'
  60. u'p, li { white-space: pre-wrap; }\n'
  61. u'</style></head><body style=" font-family:\'Monospace\'; font-size:9pt; font-weight:400; font-style:normal;">\n'
  62. u'<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Header</p>\n'
  63. u'<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p>\n'
  64. u'<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" color:#000080;">[other] In [</span><span style=" font-weight:600; color:#000080;">1</span><span style=" color:#000080;">]:</span> a = 1 + 1</p>\n'
  65. u'<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" color:#000080;">\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0...:</span> b = range(10)</p>\n'
  66. u'<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p>\n'
  67. u'<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" color:#000080;">In [</span><span style=" font-weight:600; color:#000080;">2</span><span style=" color:#000080;">]:</span> </p></body></html>'
  68. ))