1 """Unit tests for idlelib.configSectionNameDialog""" 2 import unittest 3 from idlelib.idle_test.mock_tk import Var, Mbox 4 from idlelib import configSectionNameDialog as name_dialog_module 5 6 name_dialog = name_dialog_module.GetCfgSectionNameDialog 7 8 class Dummy_name_dialog(object): 9 # Mock for testing the following methods of name_dialog 10 name_ok = name_dialog.name_ok.im_func 11 Ok = name_dialog.Ok.im_func 12 Cancel = name_dialog.Cancel.im_func 13 # Attributes, constant or variable, needed for tests 14 used_names = ['used'] 15 name = Var() 16 result = None 17 destroyed = False 18 def destroy(self): 19 self.destroyed = True 20 21 # name_ok calls Mbox.showerror if name is not ok 22 orig_mbox = name_dialog_module.tkMessageBox 23 showerror = Mbox.showerror 24 25 class ConfigNameTest(unittest.TestCase): 26 dialog = Dummy_name_dialog() 27 28 @classmethod 29 def setUpClass(cls): 30 name_dialog_module.tkMessageBox = Mbox 31 32 @classmethod 33 def tearDownClass(cls): 34 name_dialog_module.tkMessageBox = orig_mbox 35 36 def test_blank_name(self): 37 self.dialog.name.set(' ') 38 self.assertEqual(self.dialog.name_ok(), '') 39 self.assertEqual(showerror.title, 'Name Error') 40 self.assertIn('No', showerror.message) 41 42 def test_used_name(self): 43 self.dialog.name.set('used') 44 self.assertEqual(self.dialog.name_ok(), '') 45 self.assertEqual(showerror.title, 'Name Error') 46 self.assertIn('use', showerror.message) 47 48 def test_long_name(self): 49 self.dialog.name.set('good'*8) 50 self.assertEqual(self.dialog.name_ok(), '') 51 self.assertEqual(showerror.title, 'Name Error') 52 self.assertIn('too long', showerror.message) 53 54 def test_good_name(self): 55 self.dialog.name.set(' good ') 56 showerror.title = 'No Error' # should not be called 57 self.assertEqual(self.dialog.name_ok(), 'good') 58 self.assertEqual(showerror.title, 'No Error') 59 60 def test_ok(self): 61 self.dialog.destroyed = False 62 self.dialog.name.set('good') 63 self.dialog.Ok() 64 self.assertEqual(self.dialog.result, 'good') 65 self.assertTrue(self.dialog.destroyed) 66 67 def test_cancel(self): 68 self.dialog.destroyed = False 69 self.dialog.Cancel() 70 self.assertEqual(self.dialog.result, '') 71 self.assertTrue(self.dialog.destroyed) 72 73 74 if __name__ == '__main__': 75 unittest.main(verbosity=2, exit=False) 76