|
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
|
|
|
|
|
13
|
|
|
class BaseDescription(Description): |
|
14
|
|
|
"""Base class for all :py:class:`~hamcrest.core.description.Description` |
|
15
|
|
|
implementations. |
|
16
|
|
|
|
|
17
|
|
|
""" |
|
18
|
|
|
|
|
19
|
|
|
def append_text(self, text): |
|
20
|
|
|
self.append(text) |
|
21
|
|
|
return self |
|
22
|
|
|
|
|
23
|
|
|
def append_description_of(self, value): |
|
24
|
|
|
if hasmethod(value, 'describe_to'): |
|
25
|
|
|
value.describe_to(self) |
|
26
|
|
|
elif six.PY3 and isinstance(value, six.text_type): |
|
27
|
|
|
self.append(repr(value)) |
|
28
|
|
|
elif six.PY2 and isinstance(value, six.binary_type): |
|
29
|
|
|
self.append_string_in_python_syntax(value) |
|
30
|
|
|
elif isinstance(value, six.text_type): |
|
31
|
|
|
self.append_string_in_python_syntax(value) |
|
32
|
|
|
else: |
|
33
|
|
|
description = str(value) |
|
34
|
|
|
if description[:1] == '<' and description[-1:] == '>': |
|
35
|
|
|
self.append(description) |
|
36
|
|
|
else: |
|
37
|
|
|
self.append('<') |
|
38
|
|
|
self.append(description) |
|
39
|
|
|
self.append('>') |
|
40
|
|
|
return self |
|
41
|
|
|
|
|
42
|
|
|
def append_value(self, value): |
|
43
|
|
|
warnings.warn('Call append_description_of instead of append_value', |
|
44
|
|
|
DeprecationWarning) |
|
45
|
|
|
if isinstance(value, str): |
|
46
|
|
|
self.append_string_in_python_syntax(value) |
|
47
|
|
|
else: |
|
48
|
|
|
self.append('<') |
|
49
|
|
|
self.append(str(value)) |
|
50
|
|
|
self.append('>') |
|
51
|
|
|
return self |
|
52
|
|
|
|
|
53
|
|
|
def append_value_list(self, start, separator, end, list): |
|
54
|
|
|
warnings.warn('Call append_list instead of append_value_list', |
|
55
|
|
|
DeprecationWarning) |
|
56
|
|
|
return self.append_list(start, separator, end, |
|
57
|
|
|
map(SelfDescribingValue, list)) |
|
58
|
|
|
|
|
59
|
|
|
def append_list(self, start, separator, end, list): |
|
60
|
|
|
separate = False |
|
61
|
|
|
|
|
62
|
|
|
self.append(start) |
|
63
|
|
|
for item in list: |
|
64
|
|
|
if separate: |
|
65
|
|
|
self.append(separator) |
|
66
|
|
|
self.append_description_of(item) |
|
67
|
|
|
separate = True |
|
68
|
|
|
self.append(end) |
|
69
|
|
|
return self |
|
70
|
|
|
|
|
71
|
|
|
def append(self, string): |
|
72
|
|
|
"""Append the string to the description.""" |
|
73
|
|
|
raise NotImplementedError('append') |
|
74
|
|
|
|
|
75
|
|
|
def append_string_in_python_syntax(self, string): |
|
76
|
|
|
self.append("'") |
|
77
|
|
|
for ch in string: |
|
78
|
|
|
self.append(character_in_python_syntax(ch)) |
|
79
|
|
|
self.append("'") |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def character_in_python_syntax(ch): |
|
83
|
|
|
if ch == "'": |
|
84
|
|
|
return "\'" |
|
85
|
|
|
elif ch == '\n': |
|
86
|
|
|
return '\\n' |
|
87
|
|
|
elif ch == '\r': |
|
88
|
|
|
return '\\r' |
|
89
|
|
|
elif ch == '\t': |
|
90
|
|
|
return '\\t' |
|
91
|
|
|
else: |
|
92
|
|
|
return ch |
|
93
|
|
|
|