blob: 6e7e4558aad88b48cfea776bcf2fd45226407326 (
plain) (
blame)
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
|
from __future__ import absolute_import
import codecs
import six
from .base_description import BaseDescription
__author__ = "Jon Reid"
__copyright__ = "Copyright 2011 hamcrest.org"
__license__ = "BSD, see License.txt"
def tostring(selfdescribing):
"""Returns the description of a
:py:class:`~hamcrest.core.selfdescribing.SelfDescribing` object as a
string.
:param selfdescribing: The object to be described.
:returns: The description of the object.
"""
return str(StringDescription().append_description_of(selfdescribing))
class StringDescription(BaseDescription):
"""A :py:class:`~hamcrest.core.description.Description` that is stored as a
string.
"""
def __init__(self):
self.__out_list = []
def __str__(self):
"""Returns the description."""
return ''.join(map(six.ensure_str, self.__out_list))
def append(self, string):
if six.PY3:
self.out += str(string)
else:
if isinstance(string, unicode):
self.__out_list.append(string)
else:
self.__out_list.append(unicode(string, errors="ignore"))
|