1 """Test help_about, coverage 100%. 2 help_about.build_bits branches on sys.platform='darwin'. 3 '100% combines coverage on Mac and others. 4 """ 5 6 from idlelib import help_about 7 import unittest 8 from test.support import requires, findfile 9 from tkinter import Tk, TclError 10 from idlelib.idle_test.mock_idle import Func 11 from idlelib.idle_test.mock_tk import Mbox_func 12 from idlelib import textview 13 import os.path 14 from platform import python_version 15 16 About = help_about.AboutDialog 17 18 19 class LiveDialogTest(unittest.TestCase): 20 """Simulate user clicking buttons other than [Close]. 21 22 Test that invoked textview has text from source. 23 """ 24 @classmethod 25 def setUpClass(cls): 26 requires('gui') 27 cls.root = Tk() 28 cls.root.withdraw() 29 cls.dialog = About(cls.root, 'About IDLE', _utest=True) 30 31 @classmethod 32 def tearDownClass(cls): 33 del cls.dialog 34 cls.root.update_idletasks() 35 cls.root.destroy() 36 del cls.root 37 38 def test_build_bits(self): 39 self.assertIn(help_about.build_bits(), ('32', '64')) 40 41 def test_dialog_title(self): 42 """Test about dialog title""" 43 self.assertEqual(self.dialog.title(), 'About IDLE') 44 45 def test_dialog_logo(self): 46 """Test about dialog logo.""" 47 path, file = os.path.split(self.dialog.icon_image['file']) 48 fn, ext = os.path.splitext(file) 49 self.assertEqual(fn, 'idle_48') 50 51 def test_printer_buttons(self): 52 """Test buttons whose commands use printer function.""" 53 dialog = self.dialog 54 button_sources = [(dialog.py_license, license, 'license'), 55 (dialog.py_copyright, copyright, 'copyright'), 56 (dialog.py_credits, credits, 'credits')] 57 58 for button, printer, name in button_sources: 59 with self.subTest(name=name): 60 printer._Printer__setup() 61 button.invoke() 62 get = dialog._current_textview.viewframe.textframe.text.get 63 lines = printer._Printer__lines 64 if len(lines) < 2: 65 self.fail(name + ' full text was not found') 66 self.assertEqual(lines[0], get('1.0', '1.end')) 67 self.assertEqual(lines[1], get('2.0', '2.end')) 68 dialog._current_textview.destroy() 69 70 def test_file_buttons(self): 71 """Test buttons that display files.""" 72 dialog = self.dialog 73 button_sources = [(self.dialog.readme, 'README.txt', 'readme'), 74 (self.dialog.idle_news, 'NEWS.txt', 'news'), 75 (self.dialog.idle_credits, 'CREDITS.txt', 'credits')] 76 77 for button, filename, name in button_sources: 78 with self.subTest(name=name): 79 button.invoke() 80 fn = findfile(filename, subdir='idlelib') 81 get = dialog._current_textview.viewframe.textframe.text.get 82 with open(fn, encoding='utf-8') as f: 83 self.assertEqual(f.readline().strip(), get('1.0', '1.end')) 84 f.readline() 85 self.assertEqual(f.readline().strip(), get('3.0', '3.end')) 86 dialog._current_textview.destroy() 87 88 89 class DefaultTitleTest(unittest.TestCase): 90 "Test default title." 91 92 @classmethod 93 def setUpClass(cls): 94 requires('gui') 95 cls.root = Tk() 96 cls.root.withdraw() 97 cls.dialog = About(cls.root, _utest=True) 98 99 @classmethod 100 def tearDownClass(cls): 101 del cls.dialog 102 cls.root.update_idletasks() 103 cls.root.destroy() 104 del cls.root 105 106 def test_dialog_title(self): 107 """Test about dialog title""" 108 self.assertEqual(self.dialog.title(), 109 f'About IDLE {python_version()}' 110 f' ({help_about.build_bits()} bit)') 111 112 113 class CloseTest(unittest.TestCase): 114 """Simulate user clicking [Close] button""" 115 116 @classmethod 117 def setUpClass(cls): 118 requires('gui') 119 cls.root = Tk() 120 cls.root.withdraw() 121 cls.dialog = About(cls.root, 'About IDLE', _utest=True) 122 123 @classmethod 124 def tearDownClass(cls): 125 del cls.dialog 126 cls.root.update_idletasks() 127 cls.root.destroy() 128 del cls.root 129 130 def test_close(self): 131 self.assertEqual(self.dialog.winfo_class(), 'Toplevel') 132 self.dialog.button_ok.invoke() 133 with self.assertRaises(TclError): 134 self.dialog.winfo_class() 135 136 137 class Dummy_about_dialog(): 138 # Dummy class for testing file display functions. 139 idle_credits = About.show_idle_credits 140 idle_readme = About.show_readme 141 idle_news = About.show_idle_news 142 # Called by the above 143 display_file_text = About.display_file_text 144 _utest = True 145 146 147 class DisplayFileTest(unittest.TestCase): 148 """Test functions that display files. 149 150 While somewhat redundant with gui-based test_file_dialog, 151 these unit tests run on all buildbots, not just a few. 152 """ 153 dialog = Dummy_about_dialog() 154 155 @classmethod 156 def setUpClass(cls): 157 cls.orig_error = textview.showerror 158 cls.orig_view = textview.view_text 159 cls.error = Mbox_func() 160 cls.view = Func() 161 textview.showerror = cls.error 162 textview.view_text = cls.view 163 164 @classmethod 165 def tearDownClass(cls): 166 textview.showerror = cls.orig_error 167 textview.view_text = cls.orig_view 168 169 def test_file_display(self): 170 for handler in (self.dialog.idle_credits, 171 self.dialog.idle_readme, 172 self.dialog.idle_news): 173 self.error.message = '' 174 self.view.called = False 175 with self.subTest(handler=handler): 176 handler() 177 self.assertEqual(self.error.message, '') 178 self.assertEqual(self.view.called, True) 179 180 181 if __name__ == '__main__': 182 unittest.main(verbosity=2) 183