Imported Upstream version 12.1.0
[contrib/python-twisted.git] / twisted / python / test / test_runtime.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for runtime checks.
6 """
7
8 import sys
9
10 from twisted.python.runtime import Platform
11 from twisted.trial.unittest import TestCase
12
13
14
15 class PlatformTests(TestCase):
16     """
17     Tests for the default L{Platform} initializer.
18     """
19
20     def test_isVistaConsistency(self):
21         """
22         Verify consistency of L{Platform.isVista}: it can only be C{True} if
23         L{Platform.isWinNT} and L{Platform.isWindows} are C{True}.
24         """
25         platform = Platform()
26         if platform.isVista():
27             self.assertTrue(platform.isWinNT())
28             self.assertTrue(platform.isWindows())
29             self.assertFalse(platform.isMacOSX())
30
31
32     def test_isMacOSXConsistency(self):
33         """
34         L{Platform.isMacOSX} can only return C{True} if L{Platform.getType}
35         returns C{'posix'}.
36         """
37         platform = Platform()
38         if platform.isMacOSX():
39             self.assertEqual(platform.getType(), 'posix')
40
41
42     def test_isLinuxConsistency(self):
43         """
44         L{Platform.isLinux} can only return C{True} if L{Platform.getType}
45         returns C{'posix'} and L{sys.platform} starts with C{"linux"}.
46         """
47         platform = Platform()
48         if platform.isLinux():
49             self.assertTrue(sys.platform.startswith("linux"))
50
51
52
53 class ForeignPlatformTests(TestCase):
54     """
55     Tests for L{Platform} based overridden initializer values.
56     """
57
58     def test_getType(self):
59         """
60         If an operating system name is supplied to L{Platform}'s initializer,
61         L{Platform.getType} returns the platform type which corresponds to that
62         name.
63         """
64         self.assertEqual(Platform('nt').getType(), 'win32')
65         self.assertEqual(Platform('ce').getType(), 'win32')
66         self.assertEqual(Platform('posix').getType(), 'posix')
67         self.assertEqual(Platform('java').getType(), 'java')
68
69
70     def test_isMacOSX(self):
71         """
72         If a system platform name is supplied to L{Platform}'s initializer, it
73         is used to determine the result of L{Platform.isMacOSX}, which returns
74         C{True} for C{"darwin"}, C{False} otherwise.
75         """
76         self.assertTrue(Platform(None, 'darwin').isMacOSX())
77         self.assertFalse(Platform(None, 'linux2').isMacOSX())
78         self.assertFalse(Platform(None, 'win32').isMacOSX())
79
80
81     def test_isLinux(self):
82         """
83         If a system platform name is supplied to L{Platform}'s initializer, it
84         is used to determine the result of L{Platform.isLinux}, which returns
85         C{True} for values beginning with C{"linux"}, C{False} otherwise.
86         """
87         self.assertFalse(Platform(None, 'darwin').isLinux())
88         self.assertTrue(Platform(None, 'linux').isLinux())
89         self.assertTrue(Platform(None, 'linux2').isLinux())
90         self.assertTrue(Platform(None, 'linux3').isLinux())
91         self.assertFalse(Platform(None, 'win32').isLinux())