88 Equals ,
99 Is ,
1010 Not ,
11- )
11+ )
1212
1313from extras import (
1414 try_import ,
1515 try_imports ,
16- )
16+ )
1717
18- def check_error_callback ( test , function , arg , expected_error_count ,
19- expect_result ):
18+
19+ def check_error_callback ( test , function , arg , expected_error_count , expect_result ):
2020 """General test template for error_callback argument.
2121
2222 :param test: Test case instance.
@@ -27,9 +27,11 @@ def check_error_callback(test, function, arg, expected_error_count,
2727 ultimately be returned or not.
2828 """
2929 cb_calls = []
30+
3031 def cb (e ):
3132 test .assertIsInstance (e , ImportError )
3233 cb_calls .append (e )
34+
3335 try :
3436 result = function (arg , error_callback = cb )
3537 except ImportError :
@@ -43,58 +45,60 @@ def cb(e):
4345
4446
4547class TestTryImport (TestCase ):
46-
4748 def test_doesnt_exist (self ):
4849 # try_import('thing', foo) returns foo if 'thing' doesn't exist.
4950 marker = object ()
50- result = try_import (' doesntexist' , marker )
51+ result = try_import (" doesntexist" , marker )
5152 self .assertThat (result , Is (marker ))
5253
5354 def test_None_is_default_alternative (self ):
5455 # try_import('thing') returns None if 'thing' doesn't exist.
55- result = try_import (' doesntexist' )
56+ result = try_import (" doesntexist" )
5657 self .assertThat (result , Is (None ))
5758
5859 def test_existing_module (self ):
5960 # try_import('thing', foo) imports 'thing' and returns it if it's a
6061 # module that exists.
61- result = try_import ('os' , object ())
62+ result = try_import ("os" , object ())
6263 import os
64+
6365 self .assertThat (result , Is (os ))
6466
6567 def test_existing_submodule (self ):
6668 # try_import('thing.another', foo) imports 'thing' and returns it if
6769 # it's a module that exists.
68- result = try_import (' os.path' , object ())
70+ result = try_import (" os.path" , object ())
6971 import os
72+
7073 self .assertThat (result , Is (os .path ))
7174
7275 def test_nonexistent_submodule (self ):
7376 # try_import('thing.another', foo) imports 'thing' and returns foo if
7477 # 'another' doesn't exist.
7578 marker = object ()
76- result = try_import (' os.doesntexist' , marker )
79+ result = try_import (" os.doesntexist" , marker )
7780 self .assertThat (result , Is (marker ))
7881
7982 def test_object_from_module (self ):
8083 # try_import('thing.object') imports 'thing' and returns
8184 # 'thing.object' if 'thing' is a module and 'object' is not.
82- result = try_import (' os.path.join' )
85+ result = try_import (" os.path.join" )
8386 import os
87+
8488 self .assertThat (result , Is (os .path .join ))
8589
8690 def test_error_callback (self ):
8791 # the error callback is called on failures.
88- check_error_callback (self , try_import , ' doesntexist' , 1 , False )
92+ check_error_callback (self , try_import , " doesntexist" , 1 , False )
8993
9094 def test_error_callback_missing_module_member (self ):
9195 # the error callback is called on failures to find an object
9296 # inside an existing module.
93- check_error_callback (self , try_import , ' os.nonexistent' , 1 , False )
97+ check_error_callback (self , try_import , " os.nonexistent" , 1 , False )
9498
9599 def test_error_callback_not_on_success (self ):
96100 # the error callback is not called on success.
97- check_error_callback (self , try_import , ' os.path' , 0 , True )
101+ check_error_callback (self , try_import , " os.path" , 0 , True )
98102
99103 def test_handle_partly_imported_name (self ):
100104 # try_import('thing.other') when thing.other is mid-import
@@ -114,62 +118,60 @@ def test_handle_partly_imported_name(self):
114118
115119
116120class TestTryImports (TestCase ):
117-
118121 def test_doesnt_exist (self ):
119122 # try_imports('thing', foo) returns foo if 'thing' doesn't exist.
120123 marker = object ()
121- result = try_imports ([' doesntexist' ], marker )
124+ result = try_imports ([" doesntexist" ], marker )
122125 self .assertThat (result , Is (marker ))
123126
124127 def test_fallback (self ):
125- result = try_imports ([' doesntexist' , 'os' ])
128+ result = try_imports ([" doesntexist" , "os" ])
126129 import os
130+
127131 self .assertThat (result , Is (os ))
128132
129133 def test_None_is_default_alternative (self ):
130134 # try_imports('thing') returns None if 'thing' doesn't exist.
131- e = self .assertRaises (
132- ImportError , try_imports , ['doesntexist' , 'noreally' ])
135+ e = self .assertRaises (ImportError , try_imports , ["doesntexist" , "noreally" ])
133136 self .assertThat (
134- str (e ),
135- Equals ( "Could not import any of: doesntexist, noreally" ) )
137+ str (e ), Equals ( "Could not import any of: doesntexist, noreally" )
138+ )
136139
137140 def test_existing_module (self ):
138141 # try_imports('thing', foo) imports 'thing' and returns it if it's a
139142 # module that exists.
140- result = try_imports (['os' ], object ())
143+ result = try_imports (["os" ], object ())
141144 import os
145+
142146 self .assertThat (result , Is (os ))
143147
144148 def test_existing_submodule (self ):
145149 # try_imports('thing.another', foo) imports 'thing' and returns it if
146150 # it's a module that exists.
147- result = try_imports ([' os.path' ], object ())
151+ result = try_imports ([" os.path" ], object ())
148152 import os
153+
149154 self .assertThat (result , Is (os .path ))
150155
151156 def test_nonexistent_submodule (self ):
152157 # try_imports('thing.another', foo) imports 'thing' and returns foo if
153158 # 'another' doesn't exist.
154159 marker = object ()
155- result = try_imports ([' os.doesntexist' ], marker )
160+ result = try_imports ([" os.doesntexist" ], marker )
156161 self .assertThat (result , Is (marker ))
157162
158163 def test_fallback_submodule (self ):
159- result = try_imports ([' os.doesntexist' , ' os.path' ])
164+ result = try_imports ([" os.doesntexist" , " os.path" ])
160165 import os
166+
161167 self .assertThat (result , Is (os .path ))
162168
163169 def test_error_callback (self ):
164170 # One error for every class that doesn't exist.
165- check_error_callback (self , try_imports ,
166- ['os.doesntexist' , 'os.notthiseither' ],
167- 2 , False )
168- check_error_callback (self , try_imports ,
169- ['os.doesntexist' , 'os.notthiseither' , 'os' ],
170- 2 , True )
171- check_error_callback (self , try_imports ,
172- ['os.path' ],
173- 0 , True )
174-
175-
171+ check_error_callback (
172+ self , try_imports , ["os.doesntexist" , "os.notthiseither" ], 2 , False
173+ )
174+ check_error_callback (
175+ self , try_imports , ["os.doesntexist" , "os.notthiseither" , "os" ], 2 , True
176+ )
177+ check_error_callback (self , try_imports , ["os.path" ], 0 , True )
0 commit comments