test_frontend_widget.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import unittest
  2. import pytest
  3. from qtpy import QtWidgets
  4. from qtconsole.frontend_widget import FrontendWidget
  5. from qtpy.QtTest import QTest
  6. from . import no_display
  7. @pytest.mark.skipif(no_display, reason="Doesn't work without a display")
  8. class TestFrontendWidget(unittest.TestCase):
  9. @classmethod
  10. def setUpClass(cls):
  11. """ Create the application for the test case.
  12. """
  13. cls._app = QtWidgets.QApplication.instance()
  14. if cls._app is None:
  15. cls._app = QtWidgets.QApplication([])
  16. cls._app.setQuitOnLastWindowClosed(False)
  17. @classmethod
  18. def tearDownClass(cls):
  19. """ Exit the application.
  20. """
  21. QtWidgets.QApplication.quit()
  22. def test_transform_classic_prompt(self):
  23. """ Test detecting classic prompts.
  24. """
  25. w = FrontendWidget(kind='rich')
  26. t = w._highlighter.transform_classic_prompt
  27. # Base case
  28. self.assertEqual(t('>>> test'), 'test')
  29. self.assertEqual(t(' >>> test'), 'test')
  30. self.assertEqual(t('\t >>> test'), 'test')
  31. # No prompt
  32. self.assertEqual(t(''), '')
  33. self.assertEqual(t('test'), 'test')
  34. # Continuation prompt
  35. self.assertEqual(t('... test'), 'test')
  36. self.assertEqual(t(' ... test'), 'test')
  37. self.assertEqual(t(' ... test'), 'test')
  38. self.assertEqual(t('\t ... test'), 'test')
  39. # Prompts that don't match the 'traditional' prompt
  40. self.assertEqual(t('>>>test'), '>>>test')
  41. self.assertEqual(t('>> test'), '>> test')
  42. self.assertEqual(t('...test'), '...test')
  43. self.assertEqual(t('.. test'), '.. test')
  44. # Prefix indicating input from other clients
  45. self.assertEqual(t('[remote] >>> test'), 'test')
  46. # Random other prefix
  47. self.assertEqual(t('[foo] >>> test'), '[foo] >>> test')
  48. def test_transform_ipy_prompt(self):
  49. """ Test detecting IPython prompts.
  50. """
  51. w = FrontendWidget(kind='rich')
  52. t = w._highlighter.transform_ipy_prompt
  53. # In prompt
  54. self.assertEqual(t('In [1]: test'), 'test')
  55. self.assertEqual(t('In [2]: test'), 'test')
  56. self.assertEqual(t('In [10]: test'), 'test')
  57. self.assertEqual(t(' In [1]: test'), 'test')
  58. self.assertEqual(t('\t In [1]: test'), 'test')
  59. # No prompt
  60. self.assertEqual(t(''), '')
  61. self.assertEqual(t('test'), 'test')
  62. # Continuation prompt
  63. self.assertEqual(t(' ...: test'), 'test')
  64. self.assertEqual(t(' ...: test'), 'test')
  65. self.assertEqual(t(' ...: test'), 'test')
  66. self.assertEqual(t('\t ...: test'), 'test')
  67. # Prompts that don't match the in-prompt
  68. self.assertEqual(t('In [1]:test'), 'In [1]:test')
  69. self.assertEqual(t('[1]: test'), '[1]: test')
  70. self.assertEqual(t('In: test'), 'In: test')
  71. self.assertEqual(t(': test'), ': test')
  72. self.assertEqual(t('...: test'), '...: test')
  73. # Prefix indicating input from other clients
  74. self.assertEqual(t('[remote] In [1]: test'), 'test')
  75. # Random other prefix
  76. self.assertEqual(t('[foo] In [1]: test'), '[foo] In [1]: test')