-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_console.py
More file actions
134 lines (111 loc) · 4.57 KB
/
Copy pathtest_console.py
File metadata and controls
134 lines (111 loc) · 4.57 KB
1
2
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
# SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum hereon GmbH
#
# SPDX-License-Identifier: LGPL-3.0-only
# -*- coding: utf-8 -*-
"""Skript to test the InProcessShell that is used in the psyplot gui"""
import inspect
import re
import unittest
import _base_testing as bt
import psyplot.project as psy
import six
from psyplot_gui.compat.qtcompat import QTest, with_qt5
travis_qt_msg = "Does not work on Travis with Qt4"
class ConsoleTest(bt.PsyPlotGuiTestCase):
"""A testcase to test the InProcess IPython console of the psyplot GUI"""
def setUp(self):
import psyplot_gui.console
# XXX HACK: Set the _with_prompt attribute to False to tell the
# ConsoleWidget to use the _prompt_cursor
psyplot_gui.console._with_prompt = False
super(ConsoleTest, self).setUp()
def tearDown(self):
import psyplot_gui.console
# XXX HACK: Set the _with_prompt attribute to True again to tell the
# ConsoleWidget to use the _prompt_cursor
psyplot_gui.console._with_prompt = True
super(ConsoleTest, self).tearDown()
def insert_text(self, text):
"""Convenience method to insert a single line into the console"""
c = self.window.console
return c._insert_plain_text(c._get_prompt_cursor(), text)
def _test_object_docu(self, symbol):
"""Tests whether the documentation of :class:`object` can be displayed
Parameters
----------
symbol: {'?', '('}
The symbol to use for displaying the doc
See Also
--------
test_questionmark, test_bracketleft
"""
from psyplot_gui.help_explorer import signature
c = self.window.console
he = self.window.help_explorer
he.set_viewer("Plain text")
# we insert the text here otherwise using console _insert_plain_text
# method because apparently the text is not inserted when using
# QTest.keyClicks
self.insert_text("object")
QTest.keyClicks(c._control, symbol)
sig = (
""
if six.PY2
else re.sub(
r"^\(\s*self,\s*", "(", str(signature(object.__init__))
)
)
header = "object" + sig
bars = "=" * len(header)
self.assertEqual(
he.viewer.editor.toPlainText(),
"\n".join(
[
bars,
header,
bars + "\n\n",
inspect.getdoc(object),
"\n" + inspect.getdoc(object.__init__),
]
),
)
@unittest.skipIf(bt.on_travis and not with_qt5, travis_qt_msg)
def test_questionmark(self):
"""Test the connection to the help explorer by typing '?'"""
self._test_object_docu("?")
@unittest.skipIf(bt.on_travis and not with_qt5, travis_qt_msg)
def test_bracketleft(self):
"""Test the connection to the help explorer by typing '?'"""
self._test_object_docu("(")
@unittest.skipIf(bt.on_travis and not with_qt5, travis_qt_msg)
def test_current_object(self):
"""Test whether the current object is given correctly"""
c = self.window.console
self.insert_text("print(test.anything(object")
self.assertEqual(c.get_current_object(True), "object")
try: # qtconsole >4.3 uses the _prompt_cursor attribute
cursor = c._prompt_cursor
except AttributeError:
cursor = c._control.textCursor()
curr = cursor.position()
self.insert_text(") + 3")
cursor.setPosition(curr)
self.assertEqual(c.get_current_object(), "object")
def test_command(self):
self.window.console.run_command_in_shell("a = 4")
self.assertEqual(self.window.console.get_obj("a")[1], 4)
def test_mp_sp(self):
"""Test whether the mp and sp variables are set correctly"""
from xarray import DataArray
psy.Project.oncpchange.emit(psy.gcp(True))
psy.Project.oncpchange.emit(psy.gcp())
self.assertIs(self.window.console.get_obj("mp")[1], psy.gcp(True))
self.assertIs(self.window.console.get_obj("sp")[1], psy.gcp())
sp = psy.plot.lineplot(DataArray([1, 2, 3], name="test").to_dataset())
self.assertIs(self.window.console.get_obj("mp")[1], psy.gcp(True))
self.assertIs(self.window.console.get_obj("sp")[1], sp)
sp.close(True, True)
self.assertIs(self.window.console.get_obj("mp")[1], psy.gcp(True))
self.assertIs(self.window.console.get_obj("sp")[1], psy.gcp())
if __name__ == "__main__":
unittest.main()