2016-06-04 00:53:56 -03:00
|
|
|
'''Test idlelib.textView.
|
2014-06-06 18:43:19 -03:00
|
|
|
|
|
|
|
Since all methods and functions create (or destroy) a TextViewer, which
|
|
|
|
is a widget containing multiple widgets, all tests must be gui tests.
|
|
|
|
Using mock Text would not change this. Other mocks are used to retrieve
|
|
|
|
information about calls.
|
|
|
|
|
|
|
|
The coverage is essentially 100%.
|
|
|
|
'''
|
|
|
|
from test.support import requires
|
|
|
|
requires('gui')
|
2014-06-05 04:38:34 -03:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
import os
|
2014-07-01 22:33:31 -03:00
|
|
|
from tkinter import Tk
|
2014-06-05 04:38:34 -03:00
|
|
|
from idlelib import textView as tv
|
|
|
|
from idlelib.idle_test.mock_idle import Func
|
|
|
|
from idlelib.idle_test.mock_tk import Mbox
|
|
|
|
|
2014-06-06 18:43:19 -03:00
|
|
|
def setUpModule():
|
|
|
|
global root
|
|
|
|
root = Tk()
|
2016-08-31 22:03:16 -03:00
|
|
|
root.withdraw()
|
2014-06-05 04:38:34 -03:00
|
|
|
|
2014-06-06 18:43:19 -03:00
|
|
|
def tearDownModule():
|
2016-08-10 17:52:24 -03:00
|
|
|
global root
|
2016-06-04 00:53:56 -03:00
|
|
|
root.update_idletasks()
|
2016-08-31 22:03:16 -03:00
|
|
|
root.destroy() # Pyflakes falsely sees root as undefined.
|
2014-06-06 18:43:19 -03:00
|
|
|
del root
|
2014-06-05 04:38:34 -03:00
|
|
|
|
|
|
|
|
2016-08-31 22:03:16 -03:00
|
|
|
class TV(tv.TextViewer): # Used in TextViewTest.
|
2014-06-06 18:43:19 -03:00
|
|
|
transient = Func()
|
|
|
|
grab_set = Func()
|
|
|
|
wait_window = Func()
|
|
|
|
|
|
|
|
class TextViewTest(unittest.TestCase):
|
2014-06-05 04:38:34 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
TV.transient.__init__()
|
|
|
|
TV.grab_set.__init__()
|
|
|
|
TV.wait_window.__init__()
|
2014-06-05 04:54:02 -03:00
|
|
|
|
2014-06-05 04:38:34 -03:00
|
|
|
def test_init_modal(self):
|
2014-06-06 18:43:19 -03:00
|
|
|
view = TV(root, 'Title', 'test text')
|
2014-06-05 04:38:34 -03:00
|
|
|
self.assertTrue(TV.transient.called)
|
|
|
|
self.assertTrue(TV.grab_set.called)
|
|
|
|
self.assertTrue(TV.wait_window.called)
|
|
|
|
view.Ok()
|
|
|
|
|
|
|
|
def test_init_nonmodal(self):
|
2014-06-06 18:43:19 -03:00
|
|
|
view = TV(root, 'Title', 'test text', modal=False)
|
2014-06-05 04:38:34 -03:00
|
|
|
self.assertFalse(TV.transient.called)
|
|
|
|
self.assertFalse(TV.grab_set.called)
|
|
|
|
self.assertFalse(TV.wait_window.called)
|
|
|
|
view.Ok()
|
|
|
|
|
|
|
|
def test_ok(self):
|
2014-06-06 18:43:19 -03:00
|
|
|
view = TV(root, 'Title', 'test text', modal=False)
|
2014-06-05 04:38:34 -03:00
|
|
|
view.destroy = Func()
|
|
|
|
view.Ok()
|
|
|
|
self.assertTrue(view.destroy.called)
|
2016-08-31 22:03:16 -03:00
|
|
|
del view.destroy # Unmask real function.
|
|
|
|
view.destroy()
|
2014-06-05 04:38:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
class textviewTest(unittest.TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2014-06-06 18:43:19 -03:00
|
|
|
cls.orig_mbox = tv.tkMessageBox
|
2014-06-05 04:38:34 -03:00
|
|
|
tv.tkMessageBox = Mbox
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2014-06-06 18:43:19 -03:00
|
|
|
tv.tkMessageBox = cls.orig_mbox
|
|
|
|
del cls.orig_mbox
|
2014-06-05 04:38:34 -03:00
|
|
|
|
|
|
|
def test_view_text(self):
|
2016-08-31 22:03:16 -03:00
|
|
|
# If modal True, get tk error 'can't invoke "event" command'.
|
2014-06-06 18:43:19 -03:00
|
|
|
view = tv.view_text(root, 'Title', 'test text', modal=False)
|
2014-06-05 04:38:34 -03:00
|
|
|
self.assertIsInstance(view, tv.TextViewer)
|
2016-08-31 22:03:16 -03:00
|
|
|
view.Ok()
|
2014-06-05 04:38:34 -03:00
|
|
|
|
|
|
|
def test_view_file(self):
|
|
|
|
test_dir = os.path.dirname(__file__)
|
|
|
|
testfile = os.path.join(test_dir, 'test_textview.py')
|
2014-06-06 18:43:19 -03:00
|
|
|
view = tv.view_file(root, 'Title', testfile, modal=False)
|
2014-06-05 04:38:34 -03:00
|
|
|
self.assertIsInstance(view, tv.TextViewer)
|
|
|
|
self.assertIn('Test', view.textView.get('1.0', '1.end'))
|
|
|
|
view.Ok()
|
|
|
|
|
|
|
|
# Mock messagebox will be used and view_file will not return anything
|
|
|
|
testfile = os.path.join(test_dir, '../notthere.py')
|
2014-06-06 18:43:19 -03:00
|
|
|
view = tv.view_file(root, 'Title', testfile, modal=False)
|
2014-06-05 04:38:34 -03:00
|
|
|
self.assertIsNone(view)
|
|
|
|
|
2014-06-06 18:43:19 -03:00
|
|
|
|
2014-06-05 04:38:34 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=2)
|