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
4 # found in the LICENSE file.
9 from future
import Future
11 class FutureTest(unittest
.TestCase
):
12 def testNoValueOrDelegate(self
):
13 self
.assertRaises(ValueError, Future
)
16 future
= Future(value
=42)
17 self
.assertEqual(42, future
.Get())
18 self
.assertEqual(42, future
.Get())
20 def testDelegateValue(self
):
21 assertFalse
= self
.assertFalse
22 class delegate(object):
24 self
._get
_called
= False
26 assertFalse(self
._get
_called
)
27 self
._get
_called
= True
30 future
= Future(delegate
=delegate())
31 self
.assertEqual(42, future
.Get())
32 self
.assertEqual(42, future
.Get())
34 def testErrorThrowingDelegate(self
):
35 class FunkyException(Exception):
38 # Set up a chain of functions to test the stack trace.
40 raise FunkyException()
47 chain
= [foo
, bar
, baz
, qux
]
49 assertFalse
= self
.assertFalse
50 class delegate(object):
52 self
._get
_called
= False
54 assertFalse(self
._get
_called
)
55 self
._get
_called
= True
59 assertTrue
= self
.assertTrue
60 def assert_raises_full_stack(future
, err
):
63 fail('Did not raise %s' % err
)
64 except Exception as e
:
65 assertTrue(isinstance(e
, err
))
66 stack
= traceback
.format_exc()
67 assertTrue(all(stack
.find(fn
.__name
__) != -1 for fn
in chain
))
69 future
= Future(delegate
=delegate())
70 assert_raises_full_stack(future
, FunkyException
)
71 assert_raises_full_stack(future
, FunkyException
)
73 if __name__
== '__main__':