ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_textview.py

Python code coverage for Lib/idlelib/idle_test/test_textview.py

#countcontent
1n/a'''Test idlelib.textview.
2n/a
3n/aSince all methods and functions create (or destroy) a TextViewer, which
4n/ais a widget containing multiple widgets, all tests must be gui tests.
5n/aUsing mock Text would not change this. Other mocks are used to retrieve
6n/ainformation about calls.
7n/a
8n/aCoverage: 94%.
9n/a'''
10n/afrom idlelib import textview as tv
11n/afrom test.support import requires
12n/arequires('gui')
13n/a
14n/aimport unittest
15n/aimport os
16n/afrom tkinter import Tk
17n/afrom idlelib.idle_test.mock_idle import Func
18n/afrom idlelib.idle_test.mock_tk import Mbox_func
19n/a
20n/adef setUpModule():
21n/a global root
22n/a root = Tk()
23n/a root.withdraw()
24n/a
25n/adef tearDownModule():
26n/a global root
27n/a root.update_idletasks()
28n/a root.destroy() # Pyflakes falsely sees root as undefined.
29n/a del root
30n/a
31n/a
32n/aclass TV(tv.TextViewer): # Used in TextViewTest.
33n/a transient = Func()
34n/a grab_set = Func()
35n/a wait_window = Func()
36n/a
37n/aclass TextViewTest(unittest.TestCase):
38n/a
39n/a def setUp(self):
40n/a TV.transient.__init__()
41n/a TV.grab_set.__init__()
42n/a TV.wait_window.__init__()
43n/a
44n/a def test_init_modal(self):
45n/a view = TV(root, 'Title', 'test text')
46n/a self.assertTrue(TV.transient.called)
47n/a self.assertTrue(TV.grab_set.called)
48n/a self.assertTrue(TV.wait_window.called)
49n/a view.Ok()
50n/a
51n/a def test_init_nonmodal(self):
52n/a view = TV(root, 'Title', 'test text', modal=False)
53n/a self.assertFalse(TV.transient.called)
54n/a self.assertFalse(TV.grab_set.called)
55n/a self.assertFalse(TV.wait_window.called)
56n/a view.Ok()
57n/a
58n/a def test_ok(self):
59n/a view = TV(root, 'Title', 'test text', modal=False)
60n/a view.destroy = Func()
61n/a view.Ok()
62n/a self.assertTrue(view.destroy.called)
63n/a del view.destroy # Unmask real function.
64n/a view.destroy()
65n/a
66n/a
67n/aclass ViewFunctionTest(unittest.TestCase):
68n/a
69n/a @classmethod
70n/a def setUpClass(cls):
71n/a cls.orig_error = tv.showerror
72n/a tv.showerror = Mbox_func()
73n/a
74n/a @classmethod
75n/a def tearDownClass(cls):
76n/a tv.showerror = cls.orig_error
77n/a del cls.orig_error
78n/a
79n/a def test_view_text(self):
80n/a # If modal True, get tk error 'can't invoke "event" command'.
81n/a view = tv.view_text(root, 'Title', 'test text', modal=False)
82n/a self.assertIsInstance(view, tv.TextViewer)
83n/a view.Ok()
84n/a
85n/a def test_view_file(self):
86n/a test_dir = os.path.dirname(__file__)
87n/a testfile = os.path.join(test_dir, 'test_textview.py')
88n/a view = tv.view_file(root, 'Title', testfile, modal=False)
89n/a self.assertIsInstance(view, tv.TextViewer)
90n/a self.assertIn('Test', view.textView.get('1.0', '1.end'))
91n/a view.Ok()
92n/a
93n/a # Mock showerror will be used; view_file will return None.
94n/a testfile = os.path.join(test_dir, '../notthere.py')
95n/a view = tv.view_file(root, 'Title', testfile, modal=False)
96n/a self.assertIsNone(view)
97n/a
98n/a
99n/aif __name__ == '__main__':
100n/a unittest.main(verbosity=2)