Completed
Pull Request — master (#70)
by
unknown
01:16
created

Collector   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A append() 0 2 1
A __init__() 0 2 1
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
from hamcrest.core.helpers.ismock import MOCKTYPES
9
10
__author__ = "Chris Rose"
11
__copyright__ = "Copyright 2015 hamcrest.org"
12
__license__ = "BSD, see License.txt"
13
14
15
class Collector(BaseDescription):
16
    def __init__(self):
17
        self.appended = []
18
19
    def append(self, obj):
20
        self.appended.append(obj)
21
22
class Described(SelfDescribing):
23
    def describe_to(self, desc):
24
        desc.append('described')
25
26
27
@pytest.fixture
28
def desc():
29
    return Collector()
30
31
def test_append_text_delegates(desc):
32
    desc.append_text(sentinel.Text)
33
    assert desc.appended == [sentinel.Text]
34
35
36
@pytest.mark.parametrize('described, appended', (
37
    (Described(), 'described'),
38
    pytest.mark.skipif(six.PY3, reason="py2 only")((six.u('unicode-py2'), "'unicode-py2'")),
39
    pytest.mark.skipif(six.PY3, reason="py2 only")((six.b('bytes-py2'), "'bytes-py2'")),
40
    pytest.mark.skipif(six.PY2, reason="py3 only")((six.u('unicode-py3'), "'unicode-py3'")),
41
    pytest.mark.skipif(six.PY2, reason="py3 only")((six.b('bytes-py3'), "<b'bytes-py3'>")),
42
    (six.u("\U0001F4A9"), six.u("'{0}'").format(six.u("\U0001F4A9"))),
43
))
44
def test_append_description_types(desc, described, appended):
45
    desc.append_description_of(described)
46
    assert ''.join(desc.appended) == appended
47
48
49
@pytest.mark.parametrize("char, rep", (
50
    ("'", r"'"),
51
    ("\n", r"\n"),
52
    ("\r", r"\r"),
53
    ("\t", r"\t"),
54
))
55
def test_string_in_python_syntax(desc, char, rep):
56
    desc.append_string_in_python_syntax(char)
57
    assert ''.join(desc.appended) == "'{0}'".format(rep)
58
59
60
@pytest.mark.parametrize("mock", MOCKTYPES)
61
def test_describe_mock(desc, mock):
62
    m = mock()
63
    desc.append_description_of(m)
64
    assert ''.join(desc.appended) == str(m)
65