Imported Upstream version 1.1.2
[platform/upstream/python-nose.git] / unit_tests / test_utils.py
1 import os
2 import unittest
3 import nose
4 from nose import case
5 from nose.pyversion import unbound_method
6 # don't import * -- some util functions look testlike
7 from nose import util
8
9 np = os.path.normpath
10
11 class TestUtils(unittest.TestCase):
12     
13     def test_file_like(self):
14         file_like = util.file_like
15         assert file_like('a/file')
16         assert file_like('file.py')
17         assert file_like('/some/file.py')
18         assert not file_like('a.file')
19         assert not file_like('some.package')
20         assert file_like('a-file')
21         assert not file_like('test')
22         
23     def test_split_test_name(self):
24         split_test_name = util.split_test_name
25         assert split_test_name('a.package:Some.method') == \
26             (None, 'a.package', 'Some.method')
27         assert split_test_name('some.module') == \
28             (None, 'some.module', None)
29         assert split_test_name('this/file.py:func') == \
30             (np('this/file.py'), None, 'func')
31         assert split_test_name('some/file.py') == \
32             (np('some/file.py'), None, None)
33         assert split_test_name(':Baz') == \
34             (None, None, 'Baz')
35         assert split_test_name('foo:bar/baz.py') == \
36             (np('foo:bar/baz.py'), None, None)
37
38     def test_split_test_name_windows(self):
39         # convenience
40         stn = util.split_test_name
41         self.assertEqual(stn(r'c:\some\path.py:a_test'),
42                          (np(r'c:\some\path.py'), None, 'a_test'))
43         self.assertEqual(stn(r'c:\some\path.py'),
44                          (np(r'c:\some\path.py'), None, None))
45         self.assertEqual(stn(r'c:/some/other/path.py'),
46                          (np(r'c:/some/other/path.py'), None, None))
47         self.assertEqual(stn(r'c:/some/other/path.py:Class.test'),
48                          (np(r'c:/some/other/path.py'), None, 'Class.test'))
49         try:
50             stn('cat:dog:something')
51         except ValueError:
52             pass
53         else:
54             self.fail("Nonsense test name should throw ValueError")
55
56     def test_test_address(self):
57         # test addresses are specified as
58         #     package.module:class.method
59         #     /path/to/file.py:class.method
60         # converted into 3-tuples (file, module, callable)
61         # all terms optional
62         test_address = util.test_address
63         absfile = util.absfile
64         class Foo:
65             def bar(self):
66                 pass
67         def baz():
68             pass
69
70         f = Foo()
71
72         class FooTC(unittest.TestCase):
73             def test_one(self):
74                 pass
75             def test_two(self):
76                 pass
77
78         class CustomTestType(type):
79             pass
80         class CustomTC(unittest.TestCase):
81             __metaclass__ = CustomTestType
82             def test_one(self):
83                 pass
84             def test_two(self):
85                 pass
86
87         foo_funct = case.FunctionTestCase(baz)
88         foo_functu = unittest.FunctionTestCase(baz)
89
90         foo_mtc = case.MethodTestCase(unbound_method(Foo, Foo.bar))
91
92         me = util.src(absfile(__file__))
93         self.assertEqual(test_address(baz),
94                          (me, __name__, 'baz'))
95         assert test_address(Foo) == (me, __name__, 'Foo')
96         assert test_address(unbound_method(Foo, Foo.bar)) == (me, __name__,
97                                                               'Foo.bar')
98         assert test_address(f) == (me, __name__, 'Foo')
99         assert test_address(f.bar) == (me, __name__, 'Foo.bar')
100         assert test_address(nose) == (
101             util.src(absfile(nose.__file__)), 'nose', None)
102
103         # test passing the actual test callable, as the
104         # missed test plugin must do
105         self.assertEqual(test_address(FooTC('test_one')),
106                          (me, __name__, 'FooTC.test_one'))
107         self.assertEqual(test_address(CustomTC('test_one')),
108                          (me, __name__, 'CustomTC.test_one'))
109         self.assertEqual(test_address(foo_funct),
110                          (me, __name__, 'baz'))
111         self.assertEqual(test_address(foo_functu),
112                          (me, __name__, 'baz'))
113         self.assertEqual(test_address(foo_mtc),
114                          (me, __name__, 'Foo.bar'))
115
116     def test_isclass_detects_classes(self):
117         class TC(unittest.TestCase):
118             pass
119         class TC_Classic:
120             pass
121         class TC_object(object):
122             pass
123         # issue153 -- was not detecting custom typed classes...
124         class TCType(type):
125             pass
126         class TC_custom_type(object):
127             __metaclass__ = TCType
128         class TC_unittest_custom_type(unittest.TestCase):
129             __metaclass__ = TCType
130         
131         assert util.isclass(TC), "failed to detect %s as class" % TC
132         assert util.isclass(TC_Classic), "failed to detect %s as class" % TC_Classic
133         assert util.isclass(TC_object), "failed to detect %s as class" % TC_object
134         assert util.isclass(TC_custom_type), "failed to detect %s as class" % TC_custom_type
135         assert util.isclass(TC_unittest_custom_type), "failed to detect %s as class" % TC_unittest_custom_type
136         
137     def test_isclass_ignores_nonclass_things(self):
138         anint = 1
139         adict = {}
140         assert not util.isclass(anint), "should have ignored %s" % type(anint)
141         assert not util.isclass(adict), "should have ignored %s" % type(adict)
142
143     def test_tolist(self):
144         tolist = util.tolist
145         assert tolist('foo') == ['foo']
146         assert tolist(['foo', 'bar']) == ['foo', 'bar']
147         assert tolist('foo,bar') == ['foo', 'bar']
148         self.assertEqual(tolist('.*foo/.*,.1'), ['.*foo/.*', '.1'])
149
150     def test_try_run(self):
151         try_run = util.try_run
152         import imp
153
154         def bar():
155             pass
156
157         def bar_m(mod):
158             pass
159
160         class Bar:
161             def __call__(self):
162                 pass
163
164         class Bar_m:
165             def __call__(self, mod):
166                 pass
167         
168         foo = imp.new_module('foo')
169         foo.bar = bar
170         foo.bar_m = bar_m
171         foo.i_bar = Bar()
172         foo.i_bar_m = Bar_m()
173
174         try_run(foo, ('bar',))
175         try_run(foo, ('bar_m',))
176         try_run(foo, ('i_bar',))
177         try_run(foo, ('i_bar_m',))
178         
179 if __name__ == '__main__':
180     unittest.main()