testtools: Import new upstream snapshot.
[nivanova/samba-autobuild/.git] / lib / testtools / testtools / tests / test_helpers.py
1 # Copyright (c) 2010 Jonathan M. Lange. See LICENSE for details.
2
3 from testtools import TestCase
4 from testtools.helpers import (
5     try_import,
6     try_imports,
7     )
8 from testtools.matchers import (
9     Equals,
10     Is,
11     )
12
13
14 class TestTryImport(TestCase):
15
16     def test_doesnt_exist(self):
17         # try_import('thing', foo) returns foo if 'thing' doesn't exist.
18         marker = object()
19         result = try_import('doesntexist', marker)
20         self.assertThat(result, Is(marker))
21
22     def test_None_is_default_alternative(self):
23         # try_import('thing') returns None if 'thing' doesn't exist.
24         result = try_import('doesntexist')
25         self.assertThat(result, Is(None))
26
27     def test_existing_module(self):
28         # try_import('thing', foo) imports 'thing' and returns it if it's a
29         # module that exists.
30         result = try_import('os', object())
31         import os
32         self.assertThat(result, Is(os))
33
34     def test_existing_submodule(self):
35         # try_import('thing.another', foo) imports 'thing' and returns it if
36         # it's a module that exists.
37         result = try_import('os.path', object())
38         import os
39         self.assertThat(result, Is(os.path))
40
41     def test_nonexistent_submodule(self):
42         # try_import('thing.another', foo) imports 'thing' and returns foo if
43         # 'another' doesn't exist.
44         marker = object()
45         result = try_import('os.doesntexist', marker)
46         self.assertThat(result, Is(marker))
47
48     def test_object_from_module(self):
49         # try_import('thing.object') imports 'thing' and returns
50         # 'thing.object' if 'thing' is a module and 'object' is not.
51         result = try_import('os.path.join')
52         import os
53         self.assertThat(result, Is(os.path.join))
54
55
56 class TestTryImports(TestCase):
57
58     def test_doesnt_exist(self):
59         # try_imports('thing', foo) returns foo if 'thing' doesn't exist.
60         marker = object()
61         result = try_imports(['doesntexist'], marker)
62         self.assertThat(result, Is(marker))
63
64     def test_fallback(self):
65         result = try_imports(['doesntexist', 'os'])
66         import os
67         self.assertThat(result, Is(os))
68
69     def test_None_is_default_alternative(self):
70         # try_imports('thing') returns None if 'thing' doesn't exist.
71         e = self.assertRaises(
72             ImportError, try_imports, ['doesntexist', 'noreally'])
73         self.assertThat(
74             str(e),
75             Equals("Could not import any of: doesntexist, noreally"))
76
77     def test_existing_module(self):
78         # try_imports('thing', foo) imports 'thing' and returns it if it's a
79         # module that exists.
80         result = try_imports(['os'], object())
81         import os
82         self.assertThat(result, Is(os))
83
84     def test_existing_submodule(self):
85         # try_imports('thing.another', foo) imports 'thing' and returns it if
86         # it's a module that exists.
87         result = try_imports(['os.path'], object())
88         import os
89         self.assertThat(result, Is(os.path))
90
91     def test_nonexistent_submodule(self):
92         # try_imports('thing.another', foo) imports 'thing' and returns foo if
93         # 'another' doesn't exist.
94         marker = object()
95         result = try_imports(['os.doesntexist'], marker)
96         self.assertThat(result, Is(marker))
97
98     def test_fallback_submodule(self):
99         result = try_imports(['os.doesntexist', 'os.path'])
100         import os
101         self.assertThat(result, Is(os.path))
102
103
104 def test_suite():
105     from unittest import TestLoader
106     return TestLoader().loadTestsFromName(__name__)