1
|
|
|
import six |
2
|
|
|
from hamcrest.core.string_description import * |
3
|
|
|
|
4
|
|
|
from hamcrest.core.selfdescribing import SelfDescribing |
5
|
|
|
import re |
6
|
|
|
import pytest |
7
|
|
|
try: |
8
|
|
|
import unittest2 as unittest |
9
|
|
|
except ImportError: |
10
|
|
|
import unittest |
11
|
|
|
|
12
|
|
|
__author__ = "Jon Reid" |
13
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
14
|
|
|
__license__ = "BSD, see License.txt" |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class FakeSelfDescribing(SelfDescribing): |
18
|
|
|
|
19
|
|
|
def describe_to(self, description): |
20
|
|
|
description.append_text('DESCRIPTION') |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class StringDescriptionTest(unittest.TestCase): |
24
|
|
|
|
25
|
|
|
def setUp(self): |
26
|
|
|
self.description = StringDescription() |
27
|
|
|
|
28
|
|
|
def testLetsSelfDescribingObjectDescribeItself(self): |
29
|
|
|
self.description.append_description_of(FakeSelfDescribing()) |
30
|
|
|
self.assertEqual('DESCRIPTION', str(self.description)) |
31
|
|
|
|
32
|
|
|
def testDescribesStringInQuotes(self): |
33
|
|
|
self.description.append_description_of('FOO') |
34
|
|
|
self.assertEqual("'FOO'", str(self.description)) |
35
|
|
|
|
36
|
|
|
def testWrapsNonSelfDescribingObjectInAngleBrackets(self): |
37
|
|
|
self.description.append_description_of(42) |
38
|
|
|
self.assertEqual('<42>', str(self.description)) |
39
|
|
|
|
40
|
|
|
def testShouldNotAddAngleBracketsIfObjectDescriptionAlreadyHasThem(self): |
41
|
|
|
self.description.append_description_of(object()) |
42
|
|
|
expected = re.compile("<object object at 0x[0-9a-fA-F]+>") |
43
|
|
|
self.assertTrue(expected.match(str(self.description))) |
44
|
|
|
|
45
|
|
|
@unittest.skip("Describe unicode strings doesn't malform in Python 3. Six makes this go away anyway :/") |
46
|
|
|
def testDescribeUnicodeStringAsBytes(self): |
47
|
|
|
self.description.append_description_of(six.u('\u05d0')) |
48
|
|
|
self.assertEqual(six.u('\\u05d0'), str(self.description)) |
49
|
|
|
|
50
|
|
|
@unittest.skipUnless(six.PY3, "Describe unicode strings only malforms in Python 2") |
51
|
|
|
def testDescribeUnicodeStringAsUnicode(self): |
52
|
|
|
self.description.append_description_of(six.u('\u05d0')) |
53
|
|
|
self.assertEqual(six.u("'\u05d0'"), str(self.description)) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
# below is a set of things that should append without error to string |
57
|
|
|
# descriptions |
58
|
|
|
@pytest.mark.parametrize('valid_input', ('native', six.b('bytes'), six.u('unicode'))) |
59
|
|
|
def test_description_append_valid_input(valid_input): |
60
|
|
|
desc = StringDescription() |
61
|
|
|
desc.append(valid_input) |
62
|
|
|
str(desc) |
63
|
|
|
|
64
|
|
|
if __name__ == "__main__": |
65
|
|
|
unittest.main() |
66
|
|
|
|