Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: chrome/common/extensions/docs/server2/future_test.py

Issue 417163004: Docserver: Update Future.Then() to be more Promise-like (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add tests Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved. 2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 import traceback 6 import traceback
7 import unittest 7 import unittest
8 8
9 9
10 from future import All, Future, Race 10 from future import All, Future, Race
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 race = Race((Future(callback=throws_error), 152 race = Race((Future(callback=throws_error),
153 Future(callback=throws_except_error)), 153 Future(callback=throws_except_error)),
154 except_pass=(NotImplementedError,)) 154 except_pass=(NotImplementedError,))
155 self.assertRaises(ValueError, race.Get) 155 self.assertRaises(ValueError, race.Get)
156 156
157 race = Race((Future(callback=throws_error), 157 race = Race((Future(callback=throws_error),
158 Future(callback=throws_error)), 158 Future(callback=throws_error)),
159 except_pass=(ValueError,)) 159 except_pass=(ValueError,))
160 self.assertRaises(ValueError, race.Get) 160 self.assertRaises(ValueError, race.Get)
161 161
162 def testThen(self):
163 def assertIs42(val):
164 self.assertEquals(val, 42)
165
166 then = Future(value=42).Then(assertIs42)
167 # Shouldn't raise an error.
168 then.Get()
169
170 # Test raising an error.
171 then = Future(value=41).Then(assertIs42)
172 self.assertRaises(AssertionError, then.Get)
173
174 # Test setting up an error handler.
175 def handle(error):
176 if isinstance(error, ValueError):
177 return 'Caught'
178 raise error
179
180 def raiseValueError():
181 raise ValueError
182
183 def raiseException():
184 raise Exception
185
186 then = Future(callback=raiseValueError).Then(assertIs42, handle)
187 self.assertEquals(then.Get(), 'Caught')
188 then = Future(callback=raiseException).Then(assertIs42, handle)
189 self.assertRaises(Exception, then.Get)
190
191 # Test chains of thens.
192 addOne = lambda val: val + 1
193 then = Future(value=40).Then(addOne).Then(addOne).Then(assertIs42)
194 # Shouldn't raise an error.
195 then.Get()
196
197 # Test error in chain.
198 then = Future(value=40).Then(addOne).Then(assertIs42).Then(addOne)
199 self.assertRaises(AssertionError, then.Get)
200
201 # Test handle error in chain.
202 def raiseValueErrorWithVal(val):
203 raise ValueError
204
205 then = Future(value=40).Then(addOne).Then(raiseValueErrorWithVal).Then(
206 addOne, handle).Then(lambda val: val + ' me')
207 self.assertEquals(then.Get(), 'Caught me')
208
209 # Test multiple handlers.
210 def myHandle(error):
211 if isinstance(error, AssertionError):
212 return 10
213 raise error
214
215 then = Future(value=40).Then(assertIs42).Then(addOne, handle).Then(addOne,
216 myHandle)
217 self.assertEquals(then.Get(), 10)
218
162 219
163 if __name__ == '__main__': 220 if __name__ == '__main__':
164 unittest.main() 221 unittest.main()
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/future.py ('k') | chrome/common/extensions/docs/server2/mock_file_system.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698