1"Test pathbrowser, coverage 95%." 2 3from idlelib import pathbrowser 4import unittest 5from test.support import requires 6from tkinter import Tk 7 8import os.path 9import pyclbr # for _modules 10import sys # for sys.path 11 12from idlelib.idle_test.mock_idle import Func 13import idlelib # for __file__ 14from idlelib import browser 15from idlelib.tree import TreeNode 16 17 18class PathBrowserTest(unittest.TestCase): 19 20 @classmethod 21 def setUpClass(cls): 22 requires('gui') 23 cls.root = Tk() 24 cls.root.withdraw() 25 cls.pb = pathbrowser.PathBrowser(cls.root, _utest=True) 26 27 @classmethod 28 def tearDownClass(cls): 29 cls.pb.close() 30 cls.root.update_idletasks() 31 cls.root.destroy() 32 del cls.root, cls.pb 33 34 def test_init(self): 35 pb = self.pb 36 eq = self.assertEqual 37 eq(pb.master, self.root) 38 eq(pyclbr._modules, {}) 39 self.assertIsInstance(pb.node, TreeNode) 40 self.assertIsNotNone(browser.file_open) 41 42 def test_settitle(self): 43 pb = self.pb 44 self.assertEqual(pb.top.title(), 'Path Browser') 45 self.assertEqual(pb.top.iconname(), 'Path Browser') 46 47 def test_rootnode(self): 48 pb = self.pb 49 rn = pb.rootnode() 50 self.assertIsInstance(rn, pathbrowser.PathBrowserTreeItem) 51 52 def test_close(self): 53 pb = self.pb 54 pb.top.destroy = Func() 55 pb.node.destroy = Func() 56 pb.close() 57 self.assertTrue(pb.top.destroy.called) 58 self.assertTrue(pb.node.destroy.called) 59 del pb.top.destroy, pb.node.destroy 60 61 62class DirBrowserTreeItemTest(unittest.TestCase): 63 64 def test_DirBrowserTreeItem(self): 65 # Issue16226 - make sure that getting a sublist works 66 d = pathbrowser.DirBrowserTreeItem('') 67 d.GetSubList() 68 self.assertEqual('', d.GetText()) 69 70 dir = os.path.split(os.path.abspath(idlelib.__file__))[0] 71 self.assertEqual(d.ispackagedir(dir), True) 72 self.assertEqual(d.ispackagedir(dir + '/Icons'), False) 73 74 75class PathBrowserTreeItemTest(unittest.TestCase): 76 77 def test_PathBrowserTreeItem(self): 78 p = pathbrowser.PathBrowserTreeItem() 79 self.assertEqual(p.GetText(), 'sys.path') 80 sub = p.GetSubList() 81 self.assertEqual(len(sub), len(sys.path)) 82 self.assertEqual(type(sub[0]), pathbrowser.DirBrowserTreeItem) 83 84 85if __name__ == '__main__': 86 unittest.main(verbosity=2, exit=False) 87