1# (C) Copyright 2005-2020 Enthought, Inc., Austin, TX
2# All rights reserved.
3#
4# This software is provided without warranty under the terms of the BSD
5# license included in LICENSE.txt and may be redistributed only under
6# the conditions described in the aforementioned license. The license
7# is also available online at http://www.enthought.com/licenses/BSD.txt
8#
9# Thanks for using Enthought open source!
10
11
12import os
13import sys
14import unittest
15
16from ..python_shell import PythonShell
17from ..toolkit import toolkit_object
18from ..window import Window
19
20GuiTestAssistant = toolkit_object("util.gui_test_assistant:GuiTestAssistant")
21no_gui_test_assistant = GuiTestAssistant.__name__ == "Unimplemented"
22
23PYTHON_SCRIPT = os.path.abspath(
24    os.path.join(os.path.dirname(__file__), "python_shell_script.py")
25)
26
27
28@unittest.skipIf(no_gui_test_assistant, "No GuiTestAssistant")
29class TestPythonShell(unittest.TestCase, GuiTestAssistant):
30    def setUp(self):
31        GuiTestAssistant.setUp(self)
32        self.window = Window()
33        self.window._create()
34
35    def tearDown(self):
36        if self.widget.control is not None:
37            with self.delete_widget(self.widget.control):
38                self.widget.destroy()
39        if self.window.control is not None:
40            with self.delete_widget(self.window.control):
41                self.window.destroy()
42        del self.widget
43        del self.window
44        GuiTestAssistant.tearDown(self)
45
46    def test_lifecycle(self):
47        # test that destroy works
48        with self.event_loop():
49            self.widget = PythonShell(self.window.control)
50        with self.event_loop():
51            self.widget.destroy()
52
53    def test_bind(self):
54        # test that binding a variable works
55        with self.event_loop():
56            self.widget = PythonShell(self.window.control)
57        with self.event_loop():
58            self.widget.bind("x", 1)
59
60        self.assertEqual(self.widget.interpreter().locals.get("x"), 1)
61
62        with self.event_loop():
63            self.widget.destroy()
64
65    def test_execute_command(self):
66        # test that executing a command works
67        with self.event_loop():
68            self.widget = PythonShell(self.window.control)
69
70        with self.assertTraitChanges(self.widget, "command_executed", count=1):
71            with self.event_loop():
72                self.widget.execute_command("x = 1")
73
74        self.assertEqual(self.widget.interpreter().locals.get("x"), 1)
75
76        with self.event_loop():
77            self.widget.destroy()
78
79    def test_execute_command_not_hidden(self):
80        # test that executing a non-hidden command works
81        with self.event_loop():
82            self.widget = PythonShell(self.window.control)
83
84        with self.assertTraitChanges(self.widget, "command_executed", count=1):
85            with self.event_loop():
86                self.widget.execute_command("x = 1", hidden=False)
87
88        self.assertEqual(self.widget.interpreter().locals.get("x"), 1)
89
90        with self.event_loop():
91            self.widget.destroy()
92
93    def test_execute_file(self):
94        # test that executing a file works
95        with self.event_loop():
96            self.widget = PythonShell(self.window.control)
97
98        # XXX inconsistent behaviour between backends
99        # with self.assertTraitChanges(self.widget, 'command_executed', count=1):
100        with self.event_loop():
101            self.widget.execute_file(PYTHON_SCRIPT)
102
103        self.assertEqual(self.widget.interpreter().locals.get("x"), 1)
104        self.assertEqual(self.widget.interpreter().locals.get("sys"), sys)
105
106        with self.event_loop():
107            self.widget.destroy()
108
109    def test_execute_file_not_hidden(self):
110        # test that executing a file works
111        with self.event_loop():
112            self.widget = PythonShell(self.window.control)
113
114        # XXX inconsistent behaviour between backends
115        # with self.assertTraitChanges(self.widget, 'command_executed', count=1):
116        with self.event_loop():
117            self.widget.execute_file(PYTHON_SCRIPT, hidden=False)
118
119        self.assertEqual(self.widget.interpreter().locals.get("x"), 1)
120        self.assertEqual(self.widget.interpreter().locals.get("sys"), sys)
121
122        with self.event_loop():
123            self.widget.destroy()
124
125    def test_get_history(self):
126        # test that executing a command works
127        with self.event_loop():
128            self.widget = PythonShell(self.window.control)
129
130        with self.event_loop():
131            self.widget.execute_command("x = 1", hidden=False)
132
133        history, history_index = self.widget.get_history()
134
135        self.assertEqual(history, ["x = 1"])
136        self.assertEqual(history_index, 1)
137
138        with self.event_loop():
139            self.widget.destroy()
140
141    def test_set_history(self):
142        # test that executing a command works
143        with self.event_loop():
144            self.widget = PythonShell(self.window.control)
145
146        with self.event_loop():
147            self.widget.set_history(["x = 1", "y = x + 1"], 1)
148
149        history, history_index = self.widget.get_history()
150        self.assertEqual(history, ["x = 1", "y = x + 1"])
151        self.assertEqual(history_index, 1)
152
153        with self.event_loop():
154            self.widget.destroy()
155