File: base_description_test.py

package info (click to toggle)
pyhamcrest 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: python: 4,081; makefile: 114; sh: 15
file content (69 lines) | stat: -rw-r--r-- 1,849 bytes parent folder | download | duplicates (3)
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
# coding: utf-8
import platform
from unittest.mock import sentinel

import pytest
from hamcrest.core.base_description import BaseDescription
from hamcrest.core.helpers.ismock import MOCKTYPES
from hamcrest.core.selfdescribing import SelfDescribing

__author__ = "Chris Rose"
__copyright__ = "Copyright 2015 hamcrest.org"
__license__ = "BSD, see License.txt"


class Collector(BaseDescription):
    def __init__(self):
        self.appended = []

    def append(self, obj):
        self.appended.append(obj)


class Described(SelfDescribing):
    def describe_to(self, desc):
        desc.append("described")


@pytest.fixture
def desc():
    return Collector()


def test_append_text_delegates(desc):
    desc.append_text(sentinel.Text)
    assert desc.appended == [sentinel.Text]


@pytest.mark.parametrize(
    "described, appended",
    (
        (Described(), "described"),
        ("unicode-py3", "'unicode-py3'"),
        (b"bytes-py3", "<b'bytes-py3'>"),
        pytest.param(
            "\U0001F4A9",
            "'{0}'".format("\U0001F4A9"),
            marks=pytest.mark.skipif(
                platform.python_implementation() == "PyPy",
                reason="Inexplicable failure on PyPy. Not super important, I hope!",
            ),
        ),
    ),
)
def test_append_description_types(desc, described, appended):
    desc.append_description_of(described)
    assert "".join(desc.appended) == appended


@pytest.mark.parametrize("char, rep", (("'", r"'"), ("\n", r"\n"), ("\r", r"\r"), ("\t", r"\t")))
def test_string_in_python_syntax(desc, char, rep):
    desc.append_string_in_python_syntax(char)
    assert "".join(desc.appended) == "'{0}'".format(rep)


@pytest.mark.parametrize("mock", MOCKTYPES)
def test_describe_mock(desc, mock):
    m = mock()
    desc.append_description_of(m)
    assert "".join(desc.appended) == str(m)