5
5
Using mock Text would not change this. Other mocks are used to retrieve
6
6
information about calls.
7
7
8
- Coverage: 94 %.
8
+ Coverage: 100 %.
9
9
'''
10
10
from idlelib import textview as tv
11
11
from test .support import requires
@@ -28,12 +28,18 @@ def tearDownModule():
28
28
root .destroy () # Pyflakes falsely sees root as undefined.
29
29
del root
30
30
31
+ # If we call TextViewer or wrapper functions with defaults
32
+ # modal=True, _utest=False, test hangs on call to wait_window.
33
+ # Have also gotten tk error 'can't invoke "event" command'.
34
+
31
35
32
36
class TV (tv .TextViewer ): # Used in TextViewTest.
33
37
transient = Func ()
34
38
grab_set = Func ()
35
39
wait_window = Func ()
36
40
41
+
42
+ # Call wrapper class with mock wait_window.
37
43
class TextViewTest (unittest .TestCase ):
38
44
39
45
def setUp (self ):
@@ -64,6 +70,7 @@ def test_ok(self):
64
70
view .destroy ()
65
71
66
72
73
+ # Call TextViewer with modal=False.
67
74
class ViewFunctionTest (unittest .TestCase ):
68
75
69
76
@classmethod
@@ -77,23 +84,71 @@ def tearDownClass(cls):
77
84
del cls .orig_error
78
85
79
86
def test_view_text (self ):
80
- # If modal True, get tk error 'can't invoke "event" command'.
81
87
view = tv .view_text (root , 'Title' , 'test text' , modal = False )
82
88
self .assertIsInstance (view , tv .TextViewer )
83
89
view .Ok ()
84
90
85
91
def test_view_file (self ):
86
- test_dir = os .path .dirname (__file__ )
87
- testfile = os .path .join (test_dir , 'test_textview.py' )
88
- view = tv .view_file (root , 'Title' , testfile , modal = False )
92
+ view = tv .view_file (root , 'Title' , __file__ , modal = False )
89
93
self .assertIsInstance (view , tv .TextViewer )
90
94
self .assertIn ('Test' , view .textView .get ('1.0' , '1.end' ))
91
95
view .Ok ()
92
96
97
+ def test_bad_file (self ):
93
98
# Mock showerror will be used; view_file will return None.
94
- testfile = os .path .join (test_dir , '../notthere.py' )
95
- view = tv .view_file (root , 'Title' , testfile , modal = False )
99
+ view = tv .view_file (root , 'Title' , 'abc.xyz' , modal = False )
96
100
self .assertIsNone (view )
101
+ self .assertEqual (tv .showerror .title , 'File Load Error' )
102
+
103
+ def test_bad_encoding (self ):
104
+ p = os .path
105
+ fn = p .abspath (p .join (p .dirname (__file__ ), '..' , 'CREDITS.txt' ))
106
+ tv .showerror .title = None
107
+ view = tv .view_file (root , 'Title' , fn , 'ascii' , modal = False )
108
+ self .assertIsNone (view )
109
+ self .assertEqual (tv .showerror .title , 'Unicode Decode Error' )
110
+
111
+
112
+
113
+ # Call TextViewer with _utest=True.
114
+ class ButtonClickTest (unittest .TestCase ):
115
+
116
+ def setUp (self ):
117
+ self .view = None
118
+ self .called = False
119
+
120
+ def tearDown (self ):
121
+ if self .view :
122
+ self .view .destroy ()
123
+
124
+ def test_view_text_bind_with_button (self ):
125
+ def _command ():
126
+ self .called = True
127
+ self .view = tv .view_text (root , 'TITLE_TEXT' , 'COMMAND' , _utest = True )
128
+ button = Button (root , text = 'BUTTON' , command = _command )
129
+ button .invoke ()
130
+ self .addCleanup (button .destroy )
131
+
132
+ self .assertEqual (self .called , True )
133
+ self .assertEqual (self .view .title (), 'TITLE_TEXT' )
134
+ self .assertEqual (self .view .textView .get ('1.0' , '1.end' ), 'COMMAND' )
135
+
136
+ def test_view_file_bind_with_button (self ):
137
+ def _command ():
138
+ self .called = True
139
+ self .view = tv .view_file (root , 'TITLE_FILE' , __file__ , _utest = True )
140
+ button = Button (root , text = 'BUTTON' , command = _command )
141
+ button .invoke ()
142
+ self .addCleanup (button .destroy )
143
+
144
+ self .assertEqual (self .called , True )
145
+ self .assertEqual (self .view .title (), 'TITLE_FILE' )
146
+ with open (__file__ ) as f :
147
+ self .assertEqual (self .view .textView .get ('1.0' , '1.end' ),
148
+ f .readline ().strip ())
149
+ f .readline ()
150
+ self .assertEqual (self .view .textView .get ('3.0' , '3.end' ),
151
+ f .readline ().strip ())
97
152
98
153
99
154
if __name__ == '__main__' :
0 commit comments