File: test_doctest.py

package info (click to toggle)
python-testtools 2.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,244 kB
  • sloc: python: 15,086; makefile: 127; sh: 3
file content (85 lines) | stat: -rw-r--r-- 2,765 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright (c) 2008-2012 testtools developers. See LICENSE for details.

import doctest
from typing import ClassVar

from testtools import TestCase
from testtools.compat import (
    _b,
)
from testtools.matchers._doctest import DocTestMatches

from ..helpers import FullStackRunTest
from ..matchers.helpers import TestMatchersInterface


class TestDocTestMatchesInterface(TestCase, TestMatchersInterface):
    matches_matcher: ClassVar = DocTestMatches("Ran 1 test in ...s", doctest.ELLIPSIS)
    matches_matches: ClassVar = ["Ran 1 test in 0.000s", "Ran 1 test in 1.234s"]
    matches_mismatches: ClassVar = [
        "Ran 1 tests in 0.000s",
        "Ran 2 test in 0.000s",
    ]

    str_examples: ClassVar = [
        (
            "DocTestMatches('Ran 1 test in ...s\\n')",
            DocTestMatches("Ran 1 test in ...s"),
        ),
        ("DocTestMatches('foo\\n', flags=8)", DocTestMatches("foo", flags=8)),
    ]

    describe_examples: ClassVar = [
        (
            "Expected:\n    Ran 1 tests in ...s\nGot:\n    Ran 1 test in 0.123s\n",
            "Ran 1 test in 0.123s",
            DocTestMatches("Ran 1 tests in ...s", doctest.ELLIPSIS),
        )
    ]


class TestDocTestMatchesInterfaceUnicode(TestCase, TestMatchersInterface):
    matches_matcher: ClassVar = DocTestMatches("\xa7...", doctest.ELLIPSIS)
    matches_matches: ClassVar = ["\xa7", "\xa7 more\n"]
    matches_mismatches: ClassVar = ["\\xa7", "more \xa7", "\n\xa7"]

    str_examples: ClassVar = [
        ("DocTestMatches({!r})".format("\xa7\n"), DocTestMatches("\xa7")),
    ]

    describe_examples: ClassVar = [
        (
            "Expected:\n    \xa7\nGot:\n    a\n",
            "a",
            DocTestMatches("\xa7", doctest.ELLIPSIS),
        )
    ]


class TestDocTestMatchesSpecific(TestCase):
    run_tests_with = FullStackRunTest

    def test___init__simple(self):
        matcher = DocTestMatches("foo")
        self.assertEqual("foo\n", matcher.want)

    def test___init__flags(self):
        matcher = DocTestMatches("bar\n", doctest.ELLIPSIS)
        self.assertEqual("bar\n", matcher.want)
        self.assertEqual(doctest.ELLIPSIS, matcher.flags)

    def test_describe_non_ascii_bytes(self):
        """Even with bytestrings, the mismatch should be coercible to unicode

        DocTestMatches is intended for text, but the Python 2 str type also
        permits arbitrary binary inputs. This is a slightly bogus thing to do,
        and under Python 3 using bytes objects will reasonably raise an error.
        """
        header = _b("\x89PNG\r\n\x1a\n...")
        self.assertRaises(TypeError, DocTestMatches, header, doctest.ELLIPSIS)


def test_suite():
    from unittest import TestLoader

    return TestLoader().loadTestsFromName(__name__)