Remove bundled subunit.
[obnox/samba/samba-obnox.git] / lib / testtools / testtools / tests / matchers / test_doctest.py
1 # Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
2
3 import doctest
4
5 from testtools import TestCase
6 from testtools.compat import (
7     str_is_unicode,
8     _b,
9     _u,
10     )
11 from testtools.matchers._doctest import DocTestMatches
12 from testtools.tests.helpers import FullStackRunTest
13 from testtools.tests.matchers.helpers import TestMatchersInterface
14
15
16
17 class TestDocTestMatchesInterface(TestCase, TestMatchersInterface):
18
19     matches_matcher = DocTestMatches("Ran 1 test in ...s", doctest.ELLIPSIS)
20     matches_matches = ["Ran 1 test in 0.000s", "Ran 1 test in 1.234s"]
21     matches_mismatches = ["Ran 1 tests in 0.000s", "Ran 2 test in 0.000s"]
22
23     str_examples = [("DocTestMatches('Ran 1 test in ...s\\n')",
24         DocTestMatches("Ran 1 test in ...s")),
25         ("DocTestMatches('foo\\n', flags=8)", DocTestMatches("foo", flags=8)),
26         ]
27
28     describe_examples = [('Expected:\n    Ran 1 tests in ...s\nGot:\n'
29         '    Ran 1 test in 0.123s\n', "Ran 1 test in 0.123s",
30         DocTestMatches("Ran 1 tests in ...s", doctest.ELLIPSIS))]
31
32
33 class TestDocTestMatchesInterfaceUnicode(TestCase, TestMatchersInterface):
34
35     matches_matcher = DocTestMatches(_u("\xa7..."), doctest.ELLIPSIS)
36     matches_matches = [_u("\xa7"), _u("\xa7 more\n")]
37     matches_mismatches = ["\\xa7", _u("more \xa7"), _u("\n\xa7")]
38
39     str_examples = [("DocTestMatches(%r)" % (_u("\xa7\n"),),
40         DocTestMatches(_u("\xa7"))),
41         ]
42
43     describe_examples = [(
44         _u("Expected:\n    \xa7\nGot:\n    a\n"),
45         "a",
46         DocTestMatches(_u("\xa7"), doctest.ELLIPSIS))]
47
48
49 class TestDocTestMatchesSpecific(TestCase):
50
51     run_tests_with = FullStackRunTest
52
53     def test___init__simple(self):
54         matcher = DocTestMatches("foo")
55         self.assertEqual("foo\n", matcher.want)
56
57     def test___init__flags(self):
58         matcher = DocTestMatches("bar\n", doctest.ELLIPSIS)
59         self.assertEqual("bar\n", matcher.want)
60         self.assertEqual(doctest.ELLIPSIS, matcher.flags)
61
62     def test_describe_non_ascii_bytes(self):
63         """Even with bytestrings, the mismatch should be coercible to unicode
64
65         DocTestMatches is intended for text, but the Python 2 str type also
66         permits arbitrary binary inputs. This is a slightly bogus thing to do,
67         and under Python 3 using bytes objects will reasonably raise an error.
68         """
69         header = _b("\x89PNG\r\n\x1a\n...")
70         if str_is_unicode:
71             self.assertRaises(TypeError,
72                 DocTestMatches, header, doctest.ELLIPSIS)
73             return
74         matcher = DocTestMatches(header, doctest.ELLIPSIS)
75         mismatch = matcher.match(_b("GIF89a\1\0\1\0\0\0\0;"))
76         # Must be treatable as unicode text, the exact output matters less
77         self.assertTrue(unicode(mismatch.describe()))
78
79
80 def test_suite():
81     from unittest import TestLoader
82     return TestLoader().loadTestsFromName(__name__)