Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / test / test_glibbase.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for twisted.internet.glibbase.
6 """
7
8 import sys
9 from twisted.trial.unittest import TestCase
10 from twisted.internet._glibbase import ensureNotImported
11
12
13
14 class EnsureNotImportedTests(TestCase):
15     """
16     L{ensureNotImported} protects against unwanted past and future imports.
17     """
18
19     def test_ensureWhenNotImported(self):
20         """
21         If the specified modules have never been imported, and import
22         prevention is requested, L{ensureNotImported} makes sure they will not
23         be imported in the future.
24         """
25         modules = {}
26         self.patch(sys, "modules", modules)
27         ensureNotImported(["m1", "m2"], "A message.",
28                           preventImports=["m1", "m2", "m3"])
29         self.assertEquals(modules, {"m1": None, "m2": None, "m3": None})
30
31
32     def test_ensureWhenNotImportedDontPrevent(self):
33         """
34         If the specified modules have never been imported, and import
35         prevention is not requested, L{ensureNotImported} has no effect.
36         """
37         modules = {}
38         self.patch(sys, "modules", modules)
39         ensureNotImported(["m1", "m2"], "A message.")
40         self.assertEquals(modules, {})
41
42
43     def test_ensureWhenFailedToImport(self):
44         """
45         If the specified modules have been set to C{None} in C{sys.modules},
46         L{ensureNotImported} does not complain.
47         """
48         modules = {"m2": None}
49         self.patch(sys, "modules", modules)
50         ensureNotImported(["m1", "m2"], "A message.", preventImports=["m1", "m2"])
51         self.assertEquals(modules, {"m1": None, "m2": None})
52
53
54     def test_ensureFailsWhenImported(self):
55         """
56         If one of the specified modules has been previously imported,
57         L{ensureNotImported} raises an exception.
58         """
59         module = object()
60         modules = {"m2": module}
61         self.patch(sys, "modules", modules)
62         e = self.assertRaises(ImportError, ensureNotImported,
63                               ["m1", "m2"], "A message.",
64                               preventImports=["m1", "m2"])
65         self.assertEquals(modules, {"m2": module})
66         self.assertEquals(e.args, ("A message.",))