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

src.hamcrest.core.BaseDescription   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 67
Duplicated Lines 0 %
Metric Value
dl 0
loc 67
rs 10
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A BaseDescription.append_text() 0 3 1
A BaseDescription.append_value() 0 10 2
C BaseDescription.append_description_of() 0 18 10
A BaseDescription.append_value_list() 0 5 1
A BaseDescription.append() 0 3 1
A BaseDescription.append_list() 0 11 3
1
from __future__ import absolute_import
2
__author__ = "Jon Reid"
3
__copyright__ = "Copyright 2011 hamcrest.org"
4
__license__ = "BSD, see License.txt"
5
6
import warnings
7
import six
8
9
from hamcrest.core.description import Description
10
from hamcrest.core.selfdescribingvalue import SelfDescribingValue
11
from hamcrest.core.helpers.hasmethod import hasmethod
12
from hamcrest.core.helpers.ismock import ismock
13
14
class BaseDescription(Description):
15
    """Base class for all :py:class:`~hamcrest.core.description.Description`
16
    implementations.
17
18
    """
19
20
    def append_text(self, text):
21
        self.append(text)
22
        return self
23
24
    def append_description_of(self, value):
25
        if not ismock(value) and hasmethod(value, 'describe_to'):
26
            value.describe_to(self)
27
        elif six.PY3 and isinstance(value, six.text_type):
28
            self.append(repr(value))
29
        elif six.PY2 and isinstance(value, six.binary_type):
30
            self.append_string_in_python_syntax(value)
31
        elif isinstance(value, six.text_type):
32
            self.append_string_in_python_syntax(value)
33
        else:
34
            description = str(value)
35
            if description[:1] == '<' and description[-1:] == '>':
36
                self.append(description)
37
            else:
38
                self.append('<')
39
                self.append(description)
40
                self.append('>')
41
        return self
42
43
    def append_value(self, value):
44
        warnings.warn('Call append_description_of instead of append_value',
45
                      DeprecationWarning)
46
        if isinstance(value, str):
47
            self.append_string_in_python_syntax(value)
48
        else:
49
            self.append('<')
50
            self.append(str(value))
51
            self.append('>')
52
        return self
53
54
    def append_value_list(self, start, separator, end, list):
55
        warnings.warn('Call append_list instead of append_value_list',
56
                      DeprecationWarning)
57
        return self.append_list(start, separator, end,
58
                                map(SelfDescribingValue, list))
59
60
    def append_list(self, start, separator, end, list):
61
        separate = False
62
63
        self.append(start)
64
        for item in list:
65
            if separate:
66
                self.append(separator)
67
            self.append_description_of(item)
68
            separate = True
69
        self.append(end)
70
        return self
71
72
    def append(self, string):
73
        """Append the string to the description."""
74
        raise NotImplementedError('append')
75
76
    def append_string_in_python_syntax(self, string):
77
        self.append("'")
78
        for ch in string:
79
            self.append(character_in_python_syntax(ch))
80
        self.append("'")
81
82
83
def character_in_python_syntax(ch):
84
    if ch == "'":
85
        return "\'"
86
    elif ch == '\n':
87
        return '\\n'
88
    elif ch == '\r':
89
        return '\\r'
90
    elif ch == '\t':
91
        return '\\t'
92
    else:
93
        return ch
94