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