2016-06-04 00:58:37 -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.
|
|
|
|
|
2016-06-22 05:24:27 -03:00
|
|
|
Coverage: 94%.
|
2014-06-06 18:43:19 -03:00
|
|
|
'''
|
2016-06-10 19:19:21 -03:00
|
|
|
from idlelib import textview as tv
|
2014-06-06 18:43:19 -03:00
|
|
|
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.idle_test.mock_idle import Func
|
2016-06-22 05:24:27 -03:00
|
|
|
from idlelib.idle_test.mock_tk import Mbox_func
|
2014-06-05 04:38:34 -03:00
|
|
|
|
2014-06-06 18:43:19 -03:00
|
|
|
def setUpModule():
|
|
|
|
global root
|
|
|
|
root = Tk()
|
2014-06-05 04:38:34 -03:00
|
|
|
|
2014-06-06 18:43:19 -03:00
|
|
|
def tearDownModule():
|
2016-08-10 16:15:25 -03:00
|
|
|
global root
|
2016-06-04 00:53:56 -03:00
|
|
|
root.update_idletasks()
|
2014-07-01 22:33:31 -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
|
|
|
|
|
|
|
|
2014-06-06 18:43:19 -03:00
|
|
|
class TV(tv.TextViewer): # used by TextViewTest
|
|
|
|
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)
|
|
|
|
del view.destroy # unmask real function
|
|
|
|
view.destroy
|
|
|
|
|
|
|
|
|
2016-06-22 05:24:27 -03:00
|
|
|
class ViewFunctionTest(unittest.TestCase):
|
2014-06-05 04:38:34 -03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2016-06-22 05:24:27 -03:00
|
|
|
cls.orig_error = tv.showerror
|
|
|
|
tv.showerror = Mbox_func()
|
2014-06-05 04:38:34 -03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2016-06-22 05:24:27 -03:00
|
|
|
tv.showerror = cls.orig_error
|
|
|
|
del cls.orig_error
|
2014-06-05 04:38:34 -03:00
|
|
|
|
|
|
|
def test_view_text(self):
|
|
|
|
# If modal True, tkinter will error with '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)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2016-06-22 05:24:27 -03:00
|
|
|
# Mock showerror will be used and view_file will return None
|
2014-06-05 04:38:34 -03:00
|
|
|
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)
|