tests.hamcrest_unit_test.Collector.append()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
1
# coding: utf-8
2
import six
3
import pytest
4
from mock import sentinel, patch
5
6
from hamcrest.core.base_description import BaseDescription
7
from hamcrest.core.selfdescribing import SelfDescribing
8
9
__author__ = "Chris Rose"
10
__copyright__ = "Copyright 2015 hamcrest.org"
11
__license__ = "BSD, see License.txt"
12
13
14
class Collector(BaseDescription):
15
    def __init__(self):
16
        self.appended = []
17
18
    def append(self, obj):
19
        self.appended.append(obj)
20
21
class Described(SelfDescribing):
22
    def describe_to(self, desc):
23
        desc.append('described')
24
25
26
@pytest.fixture
27
def desc():
28
    return Collector()
29
30
def test_append_text_delegates(desc):
31
    desc.append_text(sentinel.Text)
32
    assert desc.appended == [sentinel.Text]
33
34
35
@pytest.mark.parametrize('described, appended', (
36
    (Described(), 'described'),
37
    pytest.mark.skipif(six.PY3, reason="py2 only")((six.u('unicode-py2'), "'unicode-py2'")),
38
    pytest.mark.skipif(six.PY3, reason="py2 only")((six.b('bytes-py2'), "'bytes-py2'")),
39
    pytest.mark.skipif(six.PY2, reason="py3 only")((six.u('unicode-py3'), "'unicode-py3'")),
40
    pytest.mark.skipif(six.PY2, reason="py3 only")((six.b('bytes-py3'), "<b'bytes-py3'>")),
41
    (six.u("\U0001F4A9"), six.u("'{0}'").format(six.u("\U0001F4A9"))),
42
))
43
def test_append_description_types(desc, described, appended):
44
    desc.append_description_of(described)
45
    assert ''.join(desc.appended) == appended
46
47
48
@pytest.mark.parametrize("char, rep", (
49
    ("'", r"'"),
50
    ("\n", r"\n"),
51
    ("\r", r"\r"),
52
    ("\t", r"\t"),
53
))
54
def test_string_in_python_syntax(desc, char, rep):
55
    desc.append_string_in_python_syntax(char)
56
    assert ''.join(desc.appended) == "'{0}'".format(rep)
57