1 # Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
5 from testtools import TestCase
6 from testtools.compat import (
11 from testtools.matchers._doctest import DocTestMatches
12 from testtools.tests.helpers import FullStackRunTest
13 from testtools.tests.matchers.helpers import TestMatchersInterface
17 class TestDocTestMatchesInterface(TestCase, TestMatchersInterface):
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"]
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)),
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))]
33 class TestDocTestMatchesInterfaceUnicode(TestCase, TestMatchersInterface):
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")]
39 str_examples = [("DocTestMatches(%r)" % (_u("\xa7\n"),),
40 DocTestMatches(_u("\xa7"))),
43 describe_examples = [(
44 _u("Expected:\n \xa7\nGot:\n a\n"),
46 DocTestMatches(_u("\xa7"), doctest.ELLIPSIS))]
49 class TestDocTestMatchesSpecific(TestCase):
51 run_tests_with = FullStackRunTest
53 def test___init__simple(self):
54 matcher = DocTestMatches("foo")
55 self.assertEqual("foo\n", matcher.want)
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)
62 def test_describe_non_ascii_bytes(self):
63 """Even with bytestrings, the mismatch should be coercible to unicode
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.
69 header = _b("\x89PNG\r\n\x1a\n...")
71 self.assertRaises(TypeError,
72 DocTestMatches, header, doctest.ELLIPSIS)
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()))
81 from unittest import TestLoader
82 return TestLoader().loadTestsFromName(__name__)