s4:dsdb/drepl: update the source_dsa_obj/invocation_id in repsFrom
[Samba/gebeck_regimport.git] / lib / testtools / testtools / tests / test_with_with.py
blobe06adeb1816fec362b6e446299afa15253266f37
1 # Copyright (c) 2011 testtools developers. See LICENSE for details.
3 from __future__ import with_statement
5 import sys
7 from testtools import (
8 ExpectedException,
9 TestCase,
11 from testtools.matchers import (
12 AfterPreprocessing,
13 Equals,
17 class TestExpectedException(TestCase):
18 """Test the ExpectedException context manager."""
20 def test_pass_on_raise(self):
21 with ExpectedException(ValueError, 'tes.'):
22 raise ValueError('test')
24 def test_pass_on_raise_matcher(self):
25 with ExpectedException(
26 ValueError, AfterPreprocessing(str, Equals('test'))):
27 raise ValueError('test')
29 def test_raise_on_text_mismatch(self):
30 try:
31 with ExpectedException(ValueError, 'tes.'):
32 raise ValueError('mismatch')
33 except AssertionError:
34 e = sys.exc_info()[1]
35 self.assertEqual("'mismatch' does not match /tes./", str(e))
36 else:
37 self.fail('AssertionError not raised.')
39 def test_raise_on_general_mismatch(self):
40 matcher = AfterPreprocessing(str, Equals('test'))
41 value_error = ValueError('mismatch')
42 try:
43 with ExpectedException(ValueError, matcher):
44 raise value_error
45 except AssertionError:
46 e = sys.exc_info()[1]
47 self.assertEqual(matcher.match(value_error).describe(), str(e))
48 else:
49 self.fail('AssertionError not raised.')
51 def test_raise_on_error_mismatch(self):
52 try:
53 with ExpectedException(TypeError, 'tes.'):
54 raise ValueError('mismatch')
55 except ValueError:
56 e = sys.exc_info()[1]
57 self.assertEqual('mismatch', str(e))
58 else:
59 self.fail('ValueError not raised.')
61 def test_raise_if_no_exception(self):
62 try:
63 with ExpectedException(TypeError, 'tes.'):
64 pass
65 except AssertionError:
66 e = sys.exc_info()[1]
67 self.assertEqual('TypeError not raised.', str(e))
68 else:
69 self.fail('AssertionError not raised.')
71 def test_pass_on_raise_any_message(self):
72 with ExpectedException(ValueError):
73 raise ValueError('whatever')