|
| 1 | +'''Test idlelib.macosx.py |
| 2 | +''' |
| 3 | +from idlelib import macosx |
| 4 | +from test.support import requires |
| 5 | +import sys |
| 6 | +import tkinter as tk |
| 7 | +import unittest |
| 8 | +import unittest.mock as mock |
| 9 | + |
| 10 | +MAC = sys.platform == 'darwin' |
| 11 | +mactypes = {'carbon', 'cocoa', 'xquartz'} |
| 12 | +nontypes = {'other'} |
| 13 | +alltypes = mactypes | nontypes |
| 14 | + |
| 15 | + |
| 16 | +class InitTktypeTest(unittest.TestCase): |
| 17 | + "Test _init_tk_type." |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def setUpClass(cls): |
| 21 | + requires('gui') |
| 22 | + cls.root = tk.Tk() |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def tearDownClass(cls): |
| 26 | + cls.root.update_idletasks() |
| 27 | + cls.root.destroy() |
| 28 | + del cls.root |
| 29 | + |
| 30 | + def test_init_sets_tktype(self): |
| 31 | + "Test that _init_tk_type sets _tk_type according to platform." |
| 32 | + for root in (None, self.root): |
| 33 | + with self.subTest(root=root): |
| 34 | + macosx._tk_type == None |
| 35 | + macosx._init_tk_type(root) |
| 36 | + self.assertIn(macosx._tk_type, |
| 37 | + mactypes if MAC else nontypes) |
| 38 | + |
| 39 | + |
| 40 | +class IsTypeTkTest(unittest.TestCase): |
| 41 | + "Test each of the four isTypeTk predecates." |
| 42 | + isfuncs = ((macosx.isAquaTk, ('carbon', 'cocoa')), |
| 43 | + (macosx.isCarbonTk, ('carbon')), |
| 44 | + (macosx.isCocoaTk, ('cocoa')), |
| 45 | + (macosx.isXQuartz, ('xquartz')), |
| 46 | + ) |
| 47 | + |
| 48 | + @mock.patch('idlelib.macosx._init_tk_type') |
| 49 | + def test_is_calls_init(self, mockinit): |
| 50 | + "Test that each isTypeTk calls _init_tk_type when _tk_type is None." |
| 51 | + macosx._tk_type = None |
| 52 | + for func, whentrue in self.isfuncs: |
| 53 | + with self.subTest(func=func): |
| 54 | + func() |
| 55 | + self.assertTrue(mockinit.called) |
| 56 | + mockinit.reset_mock() |
| 57 | + |
| 58 | + def test_isfuncs(self): |
| 59 | + "Test that each isTypeTk return correct bool." |
| 60 | + for func, whentrue in self.isfuncs: |
| 61 | + for tktype in alltypes: |
| 62 | + with self.subTest(func=func, whentrue=whentrue, tktype=tktype): |
| 63 | + macosx._tk_type = tktype |
| 64 | + (self.assertTrue if tktype in whentrue else self.assertFalse)\ |
| 65 | + (func()) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + unittest.main(verbosity=2) |
0 commit comments