2016-06-26 23:05:10 -03:00
|
|
|
"""Test idlelib.query.
|
|
|
|
|
|
|
|
Coverage: 100%.
|
|
|
|
"""
|
|
|
|
from test.support import requires
|
|
|
|
from tkinter import Tk
|
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
from idlelib.idle_test.mock_tk import Var, Mbox_func
|
|
|
|
from idlelib import query
|
|
|
|
|
2016-07-03 20:11:13 -03:00
|
|
|
Query = query.Query
|
2016-06-26 23:05:10 -03:00
|
|
|
class Dummy_Query:
|
|
|
|
# Mock for testing the following methods Query
|
|
|
|
entry_ok = Query.entry_ok
|
|
|
|
ok = Query.ok
|
|
|
|
cancel = Query.cancel
|
|
|
|
# Attributes, constant or variable, needed for tests
|
|
|
|
entry = Var()
|
|
|
|
result = None
|
|
|
|
destroyed = False
|
|
|
|
def destroy(self):
|
|
|
|
self.destroyed = True
|
|
|
|
|
|
|
|
# entry_ok calls modal messagebox.showerror if entry is not ok.
|
2016-07-03 20:11:13 -03:00
|
|
|
# Mock showerrer so don't need to click to continue.
|
2016-06-26 23:05:10 -03:00
|
|
|
orig_showerror = query.showerror
|
|
|
|
showerror = Mbox_func() # Instance has __call__ method.
|
|
|
|
|
|
|
|
def setUpModule():
|
|
|
|
query.showerror = showerror
|
|
|
|
|
|
|
|
def tearDownModule():
|
|
|
|
query.showerror = orig_showerror
|
|
|
|
|
|
|
|
|
|
|
|
class QueryTest(unittest.TestCase):
|
|
|
|
dialog = Dummy_Query()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
showerror.title = None
|
|
|
|
self.dialog.result = None
|
|
|
|
self.dialog.destroyed = False
|
|
|
|
|
|
|
|
def test_blank_entry(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set(' ')
|
2016-07-03 20:11:13 -03:00
|
|
|
Equal(dialog.entry_ok(), None)
|
2016-06-26 23:05:10 -03:00
|
|
|
Equal((dialog.result, dialog.destroyed), (None, False))
|
|
|
|
Equal(showerror.title, 'Entry Error')
|
|
|
|
self.assertIn('Blank', showerror.message)
|
|
|
|
|
|
|
|
def test_good_entry(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set(' good ')
|
|
|
|
Equal(dialog.entry_ok(), 'good')
|
|
|
|
Equal((dialog.result, dialog.destroyed), (None, False))
|
|
|
|
Equal(showerror.title, None)
|
|
|
|
|
|
|
|
def test_ok(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set('good')
|
|
|
|
Equal(dialog.ok(), None)
|
|
|
|
Equal((dialog.result, dialog.destroyed), ('good', True))
|
|
|
|
|
|
|
|
def test_cancel(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
Equal(self.dialog.cancel(), None)
|
|
|
|
Equal((dialog.result, dialog.destroyed), (None, True))
|
|
|
|
|
|
|
|
|
|
|
|
class Dummy_SectionName:
|
2016-07-03 20:11:13 -03:00
|
|
|
entry_ok = query.SectionName.entry_ok # Test override.
|
2016-06-26 23:05:10 -03:00
|
|
|
used_names = ['used']
|
|
|
|
entry = Var()
|
|
|
|
|
|
|
|
class SectionNameTest(unittest.TestCase):
|
|
|
|
dialog = Dummy_SectionName()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
showerror.title = None
|
|
|
|
|
2016-07-03 20:11:13 -03:00
|
|
|
def test_blank_section_name(self):
|
2016-06-26 23:05:10 -03:00
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set(' ')
|
2016-07-03 20:11:13 -03:00
|
|
|
Equal(dialog.entry_ok(), None)
|
2016-06-26 23:05:10 -03:00
|
|
|
Equal(showerror.title, 'Name Error')
|
|
|
|
self.assertIn('No', showerror.message)
|
|
|
|
|
2016-07-03 20:11:13 -03:00
|
|
|
def test_used_section_name(self):
|
2016-06-26 23:05:10 -03:00
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set('used')
|
2016-07-03 20:11:13 -03:00
|
|
|
Equal(self.dialog.entry_ok(), None)
|
2016-06-26 23:05:10 -03:00
|
|
|
Equal(showerror.title, 'Name Error')
|
|
|
|
self.assertIn('use', showerror.message)
|
|
|
|
|
2016-07-03 20:11:13 -03:00
|
|
|
def test_long_section_name(self):
|
2016-06-26 23:05:10 -03:00
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set('good'*8)
|
2016-07-03 20:11:13 -03:00
|
|
|
Equal(self.dialog.entry_ok(), None)
|
2016-06-26 23:05:10 -03:00
|
|
|
Equal(showerror.title, 'Name Error')
|
|
|
|
self.assertIn('too long', showerror.message)
|
|
|
|
|
2016-07-03 20:11:13 -03:00
|
|
|
def test_good_section_name(self):
|
2016-06-26 23:05:10 -03:00
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set(' good ')
|
|
|
|
Equal(dialog.entry_ok(), 'good')
|
|
|
|
Equal(showerror.title, None)
|
|
|
|
|
|
|
|
|
2016-07-03 20:11:13 -03:00
|
|
|
class Dummy_ModuleName:
|
|
|
|
entry_ok = query.ModuleName.entry_ok # Test override
|
|
|
|
text0 = ''
|
|
|
|
entry = Var()
|
|
|
|
|
|
|
|
class ModuleNameTest(unittest.TestCase):
|
|
|
|
dialog = Dummy_ModuleName()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
showerror.title = None
|
|
|
|
|
|
|
|
def test_blank_module_name(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set(' ')
|
|
|
|
Equal(dialog.entry_ok(), None)
|
|
|
|
Equal(showerror.title, 'Name Error')
|
|
|
|
self.assertIn('No', showerror.message)
|
|
|
|
|
|
|
|
def test_bogus_module_name(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set('__name_xyz123_should_not_exist__')
|
|
|
|
Equal(self.dialog.entry_ok(), None)
|
|
|
|
Equal(showerror.title, 'Import Error')
|
|
|
|
self.assertIn('not found', showerror.message)
|
|
|
|
|
|
|
|
def test_c_source_name(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set('itertools')
|
|
|
|
Equal(self.dialog.entry_ok(), None)
|
|
|
|
Equal(showerror.title, 'Import Error')
|
|
|
|
self.assertIn('source-based', showerror.message)
|
|
|
|
|
|
|
|
def test_good_module_name(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
Equal = self.assertEqual
|
|
|
|
dialog.entry.set('idlelib')
|
|
|
|
self.assertTrue(dialog.entry_ok().endswith('__init__.py'))
|
|
|
|
Equal(showerror.title, None)
|
|
|
|
|
|
|
|
|
2016-06-26 23:05:10 -03:00
|
|
|
class QueryGuiTest(unittest.TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
requires('gui')
|
2016-07-03 20:11:13 -03:00
|
|
|
cls.root = root = Tk()
|
|
|
|
cls.dialog = Query(root, 'TEST', 'test', _utest=True)
|
2016-06-26 23:05:10 -03:00
|
|
|
cls.dialog.destroy = mock.Mock()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
del cls.dialog
|
|
|
|
cls.root.destroy()
|
|
|
|
del cls.root
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.dialog.entry.delete(0, 'end')
|
|
|
|
self.dialog.result = None
|
|
|
|
self.dialog.destroy.reset_mock()
|
|
|
|
|
|
|
|
def test_click_ok(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
dialog.entry.insert(0, 'abc')
|
|
|
|
dialog.button_ok.invoke()
|
|
|
|
self.assertEqual(dialog.result, 'abc')
|
|
|
|
self.assertTrue(dialog.destroy.called)
|
|
|
|
|
|
|
|
def test_click_blank(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
dialog.button_ok.invoke()
|
|
|
|
self.assertEqual(dialog.result, None)
|
|
|
|
self.assertFalse(dialog.destroy.called)
|
|
|
|
|
|
|
|
def test_click_cancel(self):
|
|
|
|
dialog = self.dialog
|
|
|
|
dialog.entry.insert(0, 'abc')
|
|
|
|
dialog.button_cancel.invoke()
|
|
|
|
self.assertEqual(dialog.result, None)
|
|
|
|
self.assertTrue(dialog.destroy.called)
|
|
|
|
|
|
|
|
|
2016-07-03 20:11:13 -03:00
|
|
|
class SectionnameGuiTest(unittest.TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
requires('gui')
|
|
|
|
|
|
|
|
def test_click_section_name(self):
|
|
|
|
root = Tk()
|
|
|
|
dialog = query.SectionName(root, 'T', 't', {'abc'}, _utest=True)
|
|
|
|
Equal = self.assertEqual
|
|
|
|
Equal(dialog.used_names, {'abc'})
|
|
|
|
dialog.entry.insert(0, 'okay')
|
|
|
|
dialog.button_ok.invoke()
|
|
|
|
Equal(dialog.result, 'okay')
|
|
|
|
del dialog
|
|
|
|
root.destroy()
|
|
|
|
del root
|
|
|
|
|
|
|
|
|
|
|
|
class ModulenameGuiTest(unittest.TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
requires('gui')
|
|
|
|
|
|
|
|
def test_click_module_name(self):
|
|
|
|
root = Tk()
|
|
|
|
dialog = query.ModuleName(root, 'T', 't', 'idlelib', _utest=True)
|
|
|
|
Equal = self.assertEqual
|
|
|
|
Equal(dialog.text0, 'idlelib')
|
|
|
|
Equal(dialog.entry.get(), 'idlelib')
|
|
|
|
dialog.button_ok.invoke()
|
|
|
|
self.assertTrue(dialog.result.endswith('__init__.py'))
|
|
|
|
del dialog
|
|
|
|
root.destroy()
|
|
|
|
del root
|
|
|
|
|
|
|
|
|
2016-06-26 23:05:10 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=2, exit=False)
|