testtools: Update to latest version.
[metze/samba/wip.git] / lib / testtools / testtools / tests / test_helpers.py
1 # Copyright (c) 2010-2012 testtools developers. 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     Not,
12     )
13 from testtools.tests.helpers import (
14     FullStackRunTest,
15     hide_testtools_stack,
16     is_stack_hidden,
17     safe_hasattr,
18     )
19
20
21 def check_error_callback(test, function, arg, expected_error_count,
22     expect_result):
23     """General test template for error_callback argument.
24
25     :param test: Test case instance.
26     :param function: Either try_import or try_imports.
27     :param arg: Name or names to import.
28     :param expected_error_count: Expected number of calls to the callback.
29     :param expect_result: Boolean for whether a module should
30         ultimately be returned or not.
31     """
32     cb_calls = []
33     def cb(e):
34         test.assertIsInstance(e, ImportError)
35         cb_calls.append(e)
36     try:
37         result = function(arg, error_callback=cb)
38     except ImportError:
39         test.assertFalse(expect_result)
40     else:
41         if expect_result:
42             test.assertThat(result, Not(Is(None)))
43         else:
44             test.assertThat(result, Is(None))
45     test.assertEquals(len(cb_calls), expected_error_count)
46
47
48 class TestSafeHasattr(TestCase):
49
50     def test_attribute_not_there(self):
51         class Foo(object):
52             pass
53         self.assertEqual(False, safe_hasattr(Foo(), 'anything'))
54
55     def test_attribute_there(self):
56         class Foo(object):
57             pass
58         foo = Foo()
59         foo.attribute = None
60         self.assertEqual(True, safe_hasattr(foo, 'attribute'))
61
62     def test_property_there(self):
63         class Foo(object):
64             @property
65             def attribute(self):
66                 return None
67         foo = Foo()
68         self.assertEqual(True, safe_hasattr(foo, 'attribute'))
69
70     def test_property_raises(self):
71         class Foo(object):
72             @property
73             def attribute(self):
74                 1/0
75         foo = Foo()
76         self.assertRaises(ZeroDivisionError, safe_hasattr, foo, 'attribute')
77
78
79 class TestTryImport(TestCase):
80
81     def test_doesnt_exist(self):
82         # try_import('thing', foo) returns foo if 'thing' doesn't exist.
83         marker = object()
84         result = try_import('doesntexist', marker)
85         self.assertThat(result, Is(marker))
86
87     def test_None_is_default_alternative(self):
88         # try_import('thing') returns None if 'thing' doesn't exist.
89         result = try_import('doesntexist')
90         self.assertThat(result, Is(None))
91
92     def test_existing_module(self):
93         # try_import('thing', foo) imports 'thing' and returns it if it's a
94         # module that exists.
95         result = try_import('os', object())
96         import os
97         self.assertThat(result, Is(os))
98
99     def test_existing_submodule(self):
100         # try_import('thing.another', foo) imports 'thing' and returns it if
101         # it's a module that exists.
102         result = try_import('os.path', object())
103         import os
104         self.assertThat(result, Is(os.path))
105
106     def test_nonexistent_submodule(self):
107         # try_import('thing.another', foo) imports 'thing' and returns foo if
108         # 'another' doesn't exist.
109         marker = object()
110         result = try_import('os.doesntexist', marker)
111         self.assertThat(result, Is(marker))
112
113     def test_object_from_module(self):
114         # try_import('thing.object') imports 'thing' and returns
115         # 'thing.object' if 'thing' is a module and 'object' is not.
116         result = try_import('os.path.join')
117         import os
118         self.assertThat(result, Is(os.path.join))
119
120     def test_error_callback(self):
121         # the error callback is called on failures.
122         check_error_callback(self, try_import, 'doesntexist', 1, False)
123
124     def test_error_callback_missing_module_member(self):
125         # the error callback is called on failures to find an object
126         # inside an existing module.
127         check_error_callback(self, try_import, 'os.nonexistent', 1, False)
128
129     def test_error_callback_not_on_success(self):
130         # the error callback is not called on success.
131         check_error_callback(self, try_import, 'os.path', 0, True)
132
133
134 class TestTryImports(TestCase):
135
136     def test_doesnt_exist(self):
137         # try_imports('thing', foo) returns foo if 'thing' doesn't exist.
138         marker = object()
139         result = try_imports(['doesntexist'], marker)
140         self.assertThat(result, Is(marker))
141
142     def test_fallback(self):
143         result = try_imports(['doesntexist', 'os'])
144         import os
145         self.assertThat(result, Is(os))
146
147     def test_None_is_default_alternative(self):
148         # try_imports('thing') returns None if 'thing' doesn't exist.
149         e = self.assertRaises(
150             ImportError, try_imports, ['doesntexist', 'noreally'])
151         self.assertThat(
152             str(e),
153             Equals("Could not import any of: doesntexist, noreally"))
154
155     def test_existing_module(self):
156         # try_imports('thing', foo) imports 'thing' and returns it if it's a
157         # module that exists.
158         result = try_imports(['os'], object())
159         import os
160         self.assertThat(result, Is(os))
161
162     def test_existing_submodule(self):
163         # try_imports('thing.another', foo) imports 'thing' and returns it if
164         # it's a module that exists.
165         result = try_imports(['os.path'], object())
166         import os
167         self.assertThat(result, Is(os.path))
168
169     def test_nonexistent_submodule(self):
170         # try_imports('thing.another', foo) imports 'thing' and returns foo if
171         # 'another' doesn't exist.
172         marker = object()
173         result = try_imports(['os.doesntexist'], marker)
174         self.assertThat(result, Is(marker))
175
176     def test_fallback_submodule(self):
177         result = try_imports(['os.doesntexist', 'os.path'])
178         import os
179         self.assertThat(result, Is(os.path))
180
181     def test_error_callback(self):
182         # One error for every class that doesn't exist.
183         check_error_callback(self, try_imports,
184             ['os.doesntexist', 'os.notthiseither'],
185             2, False)
186         check_error_callback(self, try_imports,
187             ['os.doesntexist', 'os.notthiseither', 'os'],
188             2, True)
189         check_error_callback(self, try_imports,
190             ['os.path'],
191             0, True)
192
193
194 class TestStackHiding(TestCase):
195
196     run_tests_with = FullStackRunTest
197
198     def setUp(self):
199         super(TestStackHiding, self).setUp()
200         self.addCleanup(hide_testtools_stack, is_stack_hidden())
201
202     def test_is_stack_hidden_consistent_true(self):
203         hide_testtools_stack(True)
204         self.assertEqual(True, is_stack_hidden())
205
206     def test_is_stack_hidden_consistent_false(self):
207         hide_testtools_stack(False)
208         self.assertEqual(False, is_stack_hidden())
209
210
211 def test_suite():
212     from unittest import TestLoader
213     return TestLoader().loadTestsFromName(__name__)