test_pathbrowser.py 2.4 KB

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