Coverage for idle_test/test_macosx.py: 61%

77 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-11 13:22 -0700

1"Test macosx, coverage 45% on Windows." 

2 

3from idlelib import macosx 

4import unittest 

5from test.support import requires 

6import tkinter as tk 

7import unittest.mock as mock 

8from idlelib.filelist import FileList 

9 

10mactypes = {'carbon', 'cocoa', 'xquartz'} 

11nontypes = {'other'} 

12alltypes = mactypes | nontypes 

13 

14 

15def setUpModule(): 

16 global orig_tktype 

17 orig_tktype = macosx._tk_type 

18 

19 

20def tearDownModule(): 

21 macosx._tk_type = orig_tktype 

22 

23 

24class InitTktypeTest(unittest.TestCase): 

25 "Test _init_tk_type." 

26 

27 @classmethod 

28 def setUpClass(cls): 

29 requires('gui') 

30 cls.root = tk.Tk() 

31 cls.root.withdraw() 

32 cls.orig_platform = macosx.platform 

33 

34 @classmethod 

35 def tearDownClass(cls): 

36 cls.root.update_idletasks() 

37 cls.root.destroy() 

38 del cls.root 

39 macosx.platform = cls.orig_platform 

40 

41 def test_init_sets_tktype(self): 

42 "Test that _init_tk_type sets _tk_type according to platform." 

43 for platform, types in ('darwin', alltypes), ('other', nontypes): 

44 with self.subTest(platform=platform): 

45 macosx.platform = platform 

46 macosx._tk_type = None 

47 macosx._init_tk_type() 

48 self.assertIn(macosx._tk_type, types) 

49 

50 

51class IsTypeTkTest(unittest.TestCase): 

52 "Test each of the four isTypeTk predecates." 

53 isfuncs = ((macosx.isAquaTk, ('carbon', 'cocoa')), 

54 (macosx.isCarbonTk, ('carbon')), 

55 (macosx.isCocoaTk, ('cocoa')), 

56 (macosx.isXQuartz, ('xquartz')), 

57 ) 

58 

59 @mock.patch('idlelib.macosx._init_tk_type') 

60 def test_is_calls_init(self, mockinit): 

61 "Test that each isTypeTk calls _init_tk_type when _tk_type is None." 

62 macosx._tk_type = None 1b

63 for func, whentrue in self.isfuncs: 1b

64 with self.subTest(func=func): 1b

65 func() 1b

66 self.assertTrue(mockinit.called) 1b

67 mockinit.reset_mock() 1b

68 

69 def test_isfuncs(self): 

70 "Test that each isTypeTk return correct bool." 

71 for func, whentrue in self.isfuncs: 1c

72 for tktype in alltypes: 1c

73 with self.subTest(func=func, whentrue=whentrue, tktype=tktype): 1c

74 macosx._tk_type = tktype 1c

75 (self.assertTrue if tktype in whentrue else self.assertFalse)\ 1c

76 (func()) 

77 

78 

79class SetupTest(unittest.TestCase): 

80 "Test setupApp." 

81 

82 @classmethod 

83 def setUpClass(cls): 

84 requires('gui') 

85 cls.root = tk.Tk() 

86 cls.root.withdraw() 

87 def cmd(tkpath, func): 

88 assert isinstance(tkpath, str) 

89 assert isinstance(func, type(cmd)) 

90 cls.root.createcommand = cmd 

91 

92 @classmethod 

93 def tearDownClass(cls): 

94 cls.root.update_idletasks() 

95 cls.root.destroy() 

96 del cls.root 

97 

98 @mock.patch('idlelib.macosx.overrideRootMenu') #27312 

99 def test_setupapp(self, overrideRootMenu): 

100 "Call setupApp with each possible graphics type." 

101 root = self.root 

102 flist = FileList(root) 

103 for tktype in alltypes: 

104 with self.subTest(tktype=tktype): 

105 macosx._tk_type = tktype 

106 macosx.setupApp(root, flist) 

107 if tktype in ('carbon', 'cocoa'): 

108 self.assertTrue(overrideRootMenu.called) 

109 overrideRootMenu.reset_mock() 

110 

111 

112if __name__ == '__main__': 112 ↛ 113line 112 didn't jump to line 113, because the condition on line 112 was never true

113 unittest.main(verbosity=2)